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

Value objects

Value objects (VOs) simplify a design. Entities have traits such as an 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. 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 toward keeping 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 for this are explained in the next few paragraphs.

Applications don't always need to be traceable 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. This makes things simple and improves 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 immutability of value objects. Value objects should be designed and coded as immutable. Once they have been created, they should never be modified during their life cycle. If you need a different value for 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.

FAQs

The following are the most commonly asked questions:

  • Can a value object contain another value object?
    Yes, it can.
  • Can a value object refer to another value object or entity?
    Yes, it can.
  • Can I create a value object using the attributes of different value objects or entities?
    Yes, you can.

主站蜘蛛池模板: 兴国县| 高邑县| 阳信县| 东至县| 介休市| 石台县| 若尔盖县| 潼关县| 东台市| 龙川县| 杭州市| 五台县| 南丰县| 盱眙县| 永福县| 抚松县| 轮台县| 冷水江市| 团风县| 理塘县| 前郭尔| 鹤山市| 安国市| 阳东县| 旅游| 忻州市| 当阳市| 亚东县| 北京市| 平潭县| 灌阳县| 西乌| 绥芬河市| 瑞丽市| 曲松县| 海淀区| 奉节县| 海口市| 于都县| 安徽省| 河源市|