You have a reference object that is small and immutable and difficult to manage.Convert it into a value object.
Motivation: it is sometimes not easy to select between the referenced object and the value object. After making a choice, you often need to go back,
If the referenced object becomes difficult to use, you should change it to a value object. The referenced object must be controlled in some way, and you must always request an appropriate referenced object to the Controller. They may cause an intricate association between memory areas. In distributed and concurrent systems, immutable value objects are especially useful because you don't need to consider their synchronization issues.
Value objects have a very important feature: they should be immutable. At any time, only you call the same query function of the same object should get the same result. If this is ensured, you can safely express the same thing with multiple objects. If the value object is variable, you must ensure that the modification of an object will automatically update other objects that "represent the same thing. Instead, it is better to turn it into a reference object.
To clarify the meaning of immutable: if you use the money class to represent the concept of money, there are two pieces of information, "currency" and "amount. The money object is usually an unchangeable value object. This does not mean that your salary cannot be changed, but means: If you want to change your salary, you need to use another money object to replace the existing money object, instead of modifying the existing money object. The relationship between you and the money object can be changed, but the money object itself cannot be changed.
Practice: 1. Check whether the reconstruction target is not an object, or whether it can be modified to an immutable object. If the object is not immutable, use remove setting method (to remove the setting function) until it becomes immutable. If the object cannot be modified to an immutable value, you can discard this item.
2Create equals () and hashcode ().
3, Compilation, and testing.
4Whether the factory function can be deleted and the constructor is declared as public.