You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: value-object/README.md
+54-24Lines changed: 54 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -3,40 +3,45 @@ title: Value Object
3
3
category: Creational
4
4
language: en
5
5
tag:
6
-
- Instantiation
6
+
- Data binding
7
+
- Domain
8
+
- Encapsulation
9
+
- Immutable
7
10
---
8
11
12
+
## Also known as
13
+
14
+
* Immutable Object
15
+
9
16
## Intent
10
17
11
-
Provide objects which follow value semantics rather than reference semantics.
12
-
This means value objects' equality is not based on identity. Two value objects are
13
-
equal when they have the same value, not necessarily being the same object.
18
+
To create immutable objects that represent a descriptive aspect of the domain with no conceptual identity.
14
19
15
20
## Explanation
16
21
17
22
Real-world example
18
23
19
-
> There is a class for hero statistics in a role-playing game. The statistics contain attributes
20
-
>such as strength, intelligence, and luck. The statistics of different heroes should be equal
21
-
> when all the attributes are equal.
24
+
> Consider the case of a business card. In the real world, a business card contains information such as the person's name, job title, phone number, and email address. This information represents a specific and complete set of attributes describing the contact details of an individual but doesn't have an identity itself beyond this information.
25
+
>
26
+
> In a software system, you can create a `BusinessCard` class as a Value Object. This class would be immutable, meaning once a `BusinessCard` object is created with a person's details, those details cannot change. If you need a different business card, you create a new instance rather than modifying the existing one. The equality of two `BusinessCard` objects would be based on their contained data rather than their memory addresses, ensuring that two business cards with the same details are considered equal. This mirrors how business cards in real life are used and compared based on their content, not on the physical card itself.
22
27
23
28
In plain words
24
29
25
-
> Value objects are equal when their attributes have the same value
30
+
> Value objects are equal when their attributes have the same value.
26
31
27
32
Wikipedia says
28
33
29
-
> In computer science, a value object is a small object that represents a simple entity whose
30
-
> equality is not based on identity: i.e. two value objects are equal when they have the same
31
-
> value, not necessarily being the same object.
34
+
> In computer science, a value object is a small object that represents a simple entity whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object.
32
35
33
36
**Programmatic Example**
34
37
35
-
Here is the `HeroStat` class that is the value object. Notice the use of
There is a class for hero statistics in a role-playing game. The statistics contain attributes such as strength, intelligence, and luck. The statistics of different heroes should be equal when all the attributes are equal.
39
+
40
+
Here is the `HeroStat` class that is the value object. Notice the use of [Lombok's `@Value`](https://projectlombok.org/features/Value) annotation.
37
41
38
42
```java
39
43
@Value(staticConstructor="valueOf")
44
+
@ToString
40
45
classHeroStat {
41
46
42
47
int strength;
@@ -52,12 +57,12 @@ var statA = HeroStat.valueOf(10, 5, 0);
52
57
var statB =HeroStat.valueOf(10, 5, 0);
53
58
var statC =HeroStat.valueOf(5, 1, 8);
54
59
55
-
LOGGER.info(statA.toString());
56
-
LOGGER.info(statB.toString());
57
-
LOGGER.info(statC.toString());
60
+
LOGGER.info("statA: {}", statA);
61
+
LOGGER.info("statB: {}", statB);
62
+
LOGGER.info("statC: {}", statC);
58
63
59
-
LOGGER.info("Is statA and statB equal : {}", statA.equals(statB));
60
-
LOGGER.info("Is statA and statC equal : {}", statA.equals(statC));
64
+
LOGGER.info("Are statA and statB equal? {}", statA.equals(statB));
65
+
LOGGER.info("Are statA and statC equal? {}", statA.equals(statC));
* Thread-safe as the object's state cannot change after creation.
104
+
* Easier to reason about and maintain.
105
+
106
+
Trade-offs:
107
+
108
+
* Creating a new object for every change can be less efficient for complex objects.
109
+
* Increased memory usage due to the creation of multiple objects representing different states.
110
+
111
+
## Related Patterns
112
+
113
+
*[Factory Method](https://java-design-patterns.com/patterns/factory-method/): Often used to create instances of value objects.
114
+
*[Builder](https://java-design-patterns.com/patterns/builder/): Can be used to construct complex value objects step by step.
115
+
*[Prototype](https://java-design-patterns.com/patterns/prototype/): Can be used to clone existing value objects, though cloning is less common with immutable objects.
116
+
89
117
## Credits
90
118
91
-
*[Patterns of Enterprise Application Architecture](http://www.martinfowler.com/books/eaa.html)
0 commit comments