Skip to content

Commit 14729be

Browse files
committed
Commented the example.
1 parent 2eaf939 commit 14729be

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

private-class-data/src/main/java/com/iluwatar/App.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* The Private Class Data design pattern seeks to reduce exposure of attributes
6+
* by limiting their visibility. It reduces the number of class attributes by
7+
* encapsulating them in single data object. It allows the class designer to
8+
* remove write privilege of attributes that are intended to be set only during
9+
* construction, even from methods of the target class.
10+
*
11+
* In the example we have normal Stew class with some ingredients given in
12+
* constructor. Then we have methods to enumerate the ingredients and to taste
13+
* the stew. The method for tasting the stew alters the private members of the
14+
* stew class.
15+
*
16+
* The problem is solved with the Private Class Data pattern. We introduce
17+
* ImmutableStew class that contains StewData. The private data members of
18+
* Stew are now in StewData and cannot be altered by ImmutableStew methods.
19+
*
20+
*/
321
public class App {
422

523
public static void main( String[] args ) {
24+
// stew is mutable
625
Stew stew = new Stew(1, 2, 3, 4);
726
stew.mix();
827
stew.taste();
928
stew.mix();
1029

30+
// immutable stew protected with Private Class Data pattern
1131
ImmutableStew immutableStew = new ImmutableStew(2, 4, 3, 6);
1232
immutableStew.mix();
1333
}

private-class-data/src/main/java/com/iluwatar/ImmutableStew.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Immutable stew class, protected with Private Class Data pattern
6+
*
7+
*/
38
public class ImmutableStew {
49

510
private StewData data;

private-class-data/src/main/java/com/iluwatar/Stew.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Mutable stew class
6+
*
7+
*/
38
public class Stew {
49

510
private int numPotatoes;

private-class-data/src/main/java/com/iluwatar/StewData.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Stew ingredients
6+
*
7+
*/
38
public class StewData {
49

510
private int numPotatoes;

0 commit comments

Comments
 (0)