@@ -10,19 +10,80 @@ tags:
10
10
---
11
11
12
12
## Intent
13
+
13
14
Provide objects which follow value semantics rather than reference semantics.
14
- This means value objects' equality are not based on identity. Two value objects are
15
+ This means value objects' equality is not based on identity. Two value objects are
15
16
equal when they have the same value, not necessarily being the same object.
16
17
18
+ ## Explanation
19
+
20
+ Real-world example
21
+
22
+ > There is a class for hero statistics in a role-playing game. The statistics contain attributes
23
+ > such as strength, intelligence, and luck. The statistics of different heroes should be equal
24
+ > when all the attributes are equal.
25
+
26
+ In plain words
27
+
28
+ > Value objects are equal when their attributes have the same value
29
+
30
+ Wikipedia says
31
+
32
+ > In computer science, a value object is a small object that represents a simple entity whose
33
+ > equality is not based on identity: i.e. two value objects are equal when they have the same
34
+ > value, not necessarily being the same object.
35
+
36
+ ** Programmatic Example**
37
+
38
+ Here is the ` HeroStat ` class that is the value object. Notice the use of
39
+ [ Lombok's ` @Value ` ] ( https://projectlombok.org/features/Value ) annotation.
40
+
41
+ ``` java
42
+ @Value (staticConstructor = " valueOf" )
43
+ class HeroStat {
44
+
45
+ int strength;
46
+ int intelligence;
47
+ int luck;
48
+ }
49
+ ```
50
+
51
+ The example creates three different ` HeroStat ` s and compares their equality.
52
+
53
+ ``` java
54
+ var statA = HeroStat . valueOf(10 , 5 , 0 );
55
+ var statB = HeroStat . valueOf(10 , 5 , 0 );
56
+ var statC = HeroStat . valueOf(5 , 1 , 8 );
57
+
58
+ LOGGER . info(statA. toString());
59
+ LOGGER . info(statB. toString());
60
+ LOGGER . info(statC. toString());
61
+
62
+ LOGGER . info(" Is statA and statB equal : {}" , statA. equals(statB));
63
+ LOGGER . info(" Is statA and statC equal : {}" , statA. equals(statC));
64
+ ```
65
+
66
+ Here's the console output.
67
+
68
+ ```
69
+ 20:11:12.199 [main] INFO com.iluwatar.value.object.App - HeroStat(strength=10, intelligence=5, luck=0)
70
+ 20:11:12.202 [main] INFO com.iluwatar.value.object.App - HeroStat(strength=10, intelligence=5, luck=0)
71
+ 20:11:12.202 [main] INFO com.iluwatar.value.object.App - HeroStat(strength=5, intelligence=1, luck=8)
72
+ 20:11:12.202 [main] INFO com.iluwatar.value.object.App - Is statA and statB equal : true
73
+ 20:11:12.203 [main] INFO com.iluwatar.value.object.App - Is statA and statC equal : false
74
+ ```
75
+
17
76
## Class diagram
77
+
18
78
![ alt text] ( ./etc/value-object.png " Value Object ")
19
79
20
80
## Applicability
81
+
21
82
Use the Value Object when
22
83
23
- * You need to measure the objects' equality based on the objects' value
84
+ * The object's equality needs to be based on the object's value
24
85
25
- ## Real world examples
86
+ ## Known uses
26
87
27
88
* [ java.util.Optional] ( https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html )
28
89
* [ java.time.LocalDate] ( https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html )
@@ -31,6 +92,7 @@ Use the Value Object when
31
92
## Credits
32
93
33
94
* [ Patterns of Enterprise Application Architecture] ( http://www.martinfowler.com/books/eaa.html )
95
+ * [ ValueObject] ( https://martinfowler.com/bliki/ValueObject.html )
34
96
* [ VALJOs - Value Java Objects : Stephen Colebourne's blog] ( http://blog.joda.org/2014/03/valjos-value-java-objects.html )
35
97
* [ Value Object : Wikipedia] ( https://en.wikipedia.org/wiki/Value_object )
36
98
* [ J2EE Design Patterns] ( https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=f27d2644fbe5026ea448791a8ad09c94 )
0 commit comments