- 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.
- Learn to Create WordPress Themes by Building 5 Projects
- Visual FoxPro程序設計教程
- 樂高機器人設計技巧:EV3結構設計與編程指導
- EPLAN實戰設計
- Drupal 8 Module Development
- Working with Odoo
- Nagios Core Administration Cookbook(Second Edition)
- Xamarin Blueprints
- 大規模語言模型開發基礎與實踐
- Hands-On Dependency Injection in Go
- 交互設計師成長手冊:從零開始學交互
- Android技術內幕(系統卷)
- 嵌入式C編程實戰
- 深度學習:基于Python語言和TensorFlow平臺(視頻講解版)
- Web前端開發實戰教程(HTML5+CSS3+JavaScript)(微課版)