Skip to content

Commit 04e3368

Browse files
authored
Merge pull request iluwatar#1482 from vINCENT8888801/private-class-data-explanation
Private class data explanation
2 parents 1e90d0d + 700f5c6 commit 04e3368

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

private-class-data/README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,109 @@ Private Class Data design pattern seeks to reduce exposure of
1313
attributes by limiting their visibility. It reduces the number of class
1414
attributes by encapsulating them in single Data object.
1515

16+
## Explanation
17+
18+
Real world example
19+
20+
> Imagine you are cooking a stew for your family for dinner. You want to prevent your family members from consuming the stew by tasting it while you are cooking, otherwise there will be no more stew for dinner later.
21+
22+
In plain words
23+
24+
> Private class data pattern prevent manipulation of data that is meant to be immutable by separating the data from methods that use it into a class that maintains the data state.
25+
26+
Wikipedia says
27+
28+
> Private class data is a design pattern in computer programming used to encapsulate class attributes and their manipulation.
29+
30+
**Programmatic Example**
31+
32+
Taking our stew example from above. First we have a `Stew` class where its data is not protected by private class data, making the stew's ingredient mutable to class methods.
33+
34+
```
35+
public class Stew {
36+
private static final Logger LOGGER = LoggerFactory.getLogger(Stew.class);
37+
private int numPotatoes;
38+
private int numCarrots;
39+
private int numMeat;
40+
private int numPeppers;
41+
public Stew(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
42+
this.numPotatoes = numPotatoes;
43+
this.numCarrots = numCarrots;
44+
this.numMeat = numMeat;
45+
this.numPeppers = numPeppers;
46+
}
47+
public void mix() {
48+
LOGGER.info("Mixing the stew we find: {} potatoes, {} carrots, {} meat and {} peppers",
49+
numPotatoes, numCarrots, numMeat, numPeppers);
50+
}
51+
public void taste() {
52+
LOGGER.info("Tasting the stew");
53+
if (numPotatoes > 0) {
54+
numPotatoes--;
55+
}
56+
if (numCarrots > 0) {
57+
numCarrots--;
58+
}
59+
if (numMeat > 0) {
60+
numMeat--;
61+
}
62+
if (numPeppers > 0) {
63+
numPeppers--;
64+
}
65+
}
66+
}
67+
```
68+
69+
Now, we have `ImmutableStew` class, where its data is protected by `StewData` class. Now, the methods in are unable to manipulate the data of the `ImmutableStew` class.
70+
```
71+
public class StewData {
72+
private final int numPotatoes;
73+
private final int numCarrots;
74+
private final int numMeat;
75+
private final int numPeppers;
76+
public StewData(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
77+
this.numPotatoes = numPotatoes;
78+
this.numCarrots = numCarrots;
79+
this.numMeat = numMeat;
80+
this.numPeppers = numPeppers;
81+
}
82+
public int getNumPotatoes() {
83+
return numPotatoes;
84+
}
85+
public int getNumCarrots() {
86+
return numCarrots;
87+
}
88+
public int getNumMeat() {
89+
return numMeat;
90+
}
91+
public int getNumPeppers() {
92+
return numPeppers;
93+
}
94+
}
95+
public class ImmutableStew {
96+
private static final Logger LOGGER = LoggerFactory.getLogger(ImmutableStew.class);
97+
private final StewData data;
98+
public ImmutableStew(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
99+
data = new StewData(numPotatoes, numCarrots, numMeat, numPeppers);
100+
}
101+
public void mix() {
102+
LOGGER
103+
.info("Mixing the immutable stew we find: {} potatoes, {} carrots, {} meat and {} peppers",
104+
data.getNumPotatoes(), data.getNumCarrots(), data.getNumMeat(), data.getNumPeppers());
105+
}
106+
}
107+
```
108+
109+
Let's try creating some instance of each class and calling their methods
110+
```
111+
var stew = new Stew(1, 2, 3, 4);
112+
stew.mix(); // Mixing the stew we find: 1 potatoes, 2 carrots, 3 meat and 4 peppers
113+
stew.taste(); // Tasting the stew
114+
stew.mix(); // Mixing the stew we find: 0 potatoes, 1 carrots, 2 meat and 3 peppers
115+
var immutableStew = new ImmutableStew(2, 4, 3, 6);
116+
immutableStew.mix(); // Mixing the immutable stew we find: 2 potatoes, 4 carrots, 3 meat and 6 peppers
117+
```
118+
16119
## Class diagram
17120
![alt text](./etc/private-class-data.png "Private Class Data")
18121

0 commit comments

Comments
 (0)