Refactoring Change Reference to Value
You have a reference object that is too small and infrequently changed to justify managing its life cycle.
Turn it into a value object.
Inspiration to switch from a reference to a value may come from the inconvenience of working with the reference. References require management on your part:
- They always require requesting the necessary object from storage.
- References in memory may be inconvenient to work with.
- Working with references is particularly difficult, compared to values, on distributed and parallel systems.
Values are especially useful if you would rather have unchangeable objects than objects whose state may change during their lifetime.
- One important property of objects is that they should be unchangeable. The same result should be received for each query that returns an object value. If this is true, no problems arise if there are many objects representing the same thing.
- Values are much easier to implement.
- If a value is changeable, make sure if any object changes that the values in all the other objects representing the same entity are updated. This is so burdensome that it is easier to create a reference for this purpose.
How to Refactor
- Make the object unchangeable. The object should not have any setters or other methods that change its state and data (Remove Setting Method may help here). The only place where data should be assigned to the fields of a value object is a constructor.
- Create a comparison method to be able to compare two values.
- Check whether you can delete the factory method and make the object constructor public.
Tired of reading?
No wonder, there are 7 hours worth of the text on this website.
Try our interactive course on refactoring. It offers less boring approach to learning new stuff.Let's see...