I have a question about the value object.

Asked 1 years ago, Updated 1 years ago, 93 views

While reading the DDD book, the objects are divided into Value Object and Entity Object and understood as follows
Yes,
·Entity Object
   ·There is something that can be identified (such as ID).
   ·Other attributes can be changed
·ValueObject
   ·Values can only be set when generating and cannot be changed
   ·You can generate the same object.
   
In this case, for example, if the EntityObject attribute has a ValueObject
I don't think it's possible to change the ValueObject, but it can also be replaced
What should I do if I can't?Or is the usage wrong?
+++++++++++++++++++++++++++++++++
I wrote the following as an idea, so I don't know if there are many grammatical errors, but I wrote the source code.

public class ValueObject
{
     private intb;
     ValueObject (inta)
     {
          b = a;
     }
     ValueObject multi(inta)
     {
         return new ValueObject(a*b);
     }
     ValueObject copy()
     {
         return new ValueObject(b);
     }
}
public class EntityObject
{
      private final intid;
      private ValueObject vo;
      EntityObject(intid, int value)
      {
         this.id=id;
         vo = new ValueObject(30);


      }
      voidsetValue(inta)
      {
           // I don't know how to do this
      }
      ValueObject getValue()
      {
          return vo.copy();
      }
}

domain-driven-design

2022-09-30 17:21

1 Answers

The image looks like the one below.

public class EntityObject
    {
          private final intid;
          private ValueObject vo;
          EntityObject(intid, ValueObject value)
          {
             this.id=id;
             vo=value;
          }
          voidsetValue(inta)
          {
             vo = new ValueObject(a);
          }
          ValueObject getValue()
          {
              return vo.copy();
          }
    }

Unable to modify ValueObject means that the value of the ValueObject object attribute b cannot be changed (implemented as such).

The code in the question statement appears to be intended not to change the reference of the ValueObject type attribute, but it is not.

·Entity Object
  ·There is something that can be identified (such as ID).
  ·Other attributes can be changed

You can change it as it says.

Below is a little off the question, but I think it would be natural to use the following form of setter/getter.

void setValue(ValueObject value)
          {
             vo=value;
          }
          ValueObject getValue()
          {
              return vo;
          }
  • Set type and get type are aligned
  • Value objects are immutable and do not necessarily need to generate new instances

That's why


2022-09-30 17:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.