- Mastering Microservices with Java 9(Second Edition)
- Sourabh Sharma
- 394字
- 2021-07-02 21:54:49
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.
- Boost.Asio C++ Network Programming(Second Edition)
- FuelPHP Application Development Blueprints
- 自然語(yǔ)言處理實(shí)戰(zhàn):預(yù)訓(xùn)練模型應(yīng)用及其產(chǎn)品化
- SQL Server 2012數(shù)據(jù)庫(kù)技術(shù)及應(yīng)用(微課版·第5版)
- PaaS程序設(shè)計(jì)
- 樂學(xué)Web編程:網(wǎng)站制作不神秘
- 數(shù)據(jù)結(jié)構(gòu)習(xí)題精解(C語(yǔ)言實(shí)現(xiàn)+微課視頻)
- Java程序設(shè)計(jì)與計(jì)算思維
- Full-Stack Vue.js 2 and Laravel 5
- Java應(yīng)用開發(fā)技術(shù)實(shí)例教程
- Python:Master the Art of Design Patterns
- 零基礎(chǔ)Java學(xué)習(xí)筆記
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)教程(Windows 7+Office 2010)
- Python入門很輕松(微課超值版)
- 代碼閱讀