官术网_书友最值得收藏!

Value objects

Value objects (VOs) simplify the design. Entities have traits such as identity, a thread of continuity, and attributes that do not define their identity. In contrast to entities, value objects have only attributes and no conceptual identity. A best practice is to keep value objects as immutable objects. If possible, you should even keep entity objects immutable too.

Entity concepts may bias you to keep all objects as entities, as a uniquely identifiable object in the memory or database with life cycle continuity, but there has to be one instance for each object. Now, let's say you are creating customers as entity objects. Each customer object would represent the restaurant guest, and this cannot be used for booking orders for other guests. This may create millions of customer entity objects in the memory if millions of customers are using the system. Not only are there millions of uniquely identifiable objects that exist in the system, but each object is being tracked. Tracking as well as creating an identity is complex. A highly credible system is required to create and track these objects, which is not only very complex, but also resource heavy. It may result in system performance degradation. Therefore, it is important to use value objects instead of using entities. The reasons are explained in the next few paragraphs.

Applications don't always need to have to be trackable and have an identifiable customer object. There are cases when you just need to have some or all attributes of the domain element. These are the cases when value objects can be used by the application. It makes things simple and improves the performance.

Value objects can easily be created and destroyed, owing to the absence of identity. This simplifies the design—it makes value objects available for garbage collection if no other object has referenced them.

Let's discuss the value object's immutability. Value objects should be designed and coded as immutable. Once they are created, they should never be modified during their life-cycle. If you need a different value of the VO, or any of its objects, then simply create a new value object, but don't modify the original value object. Here, immutability carries all the significance from object-oriented programming (OOP). A value object can be shared and used without impacting on its integrity if, and only if, it is immutable.

主站蜘蛛池模板: 洛浦县| 沁阳市| 天水市| 遂溪县| 田林县| 枝江市| 财经| 河津市| 巴中市| 都匀市| 婺源县| 凤城市| 延庆县| 镶黄旗| 天长市| 开阳县| 陇川县| 淮阳县| 商丘市| 宁津县| 西乌珠穆沁旗| 曲阜市| 宜宾县| 阜阳市| 措勤县| 建平县| 皮山县| 淄博市| 黑山县| 乐平市| 平南县| 渭源县| 镇江市| 临潭县| 游戏| 涡阳县| 德阳市| 阜康市| 延寿县| 临江市| 峨眉山市|