Skip to content

Commit c32246e

Browse files
committed
iluwatar#107 Improve Private Class Data example JavaDoc
1 parent 2bf00c3 commit c32246e

File tree

2 files changed

+57
-48
lines changed
  • private-class-data/src

2 files changed

+57
-48
lines changed
Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
1-
package com.iluwatar.privateclassdata;
2-
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-
*/
21-
public class App {
22-
23-
public static void main( String[] args ) {
24-
// stew is mutable
25-
Stew stew = new Stew(1, 2, 3, 4);
26-
stew.mix();
27-
stew.taste();
28-
stew.mix();
29-
30-
// immutable stew protected with Private Class Data pattern
31-
ImmutableStew immutableStew = new ImmutableStew(2, 4, 3, 6);
32-
immutableStew.mix();
33-
}
34-
}
1+
package com.iluwatar.privateclassdata;
2+
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+
* <p>
11+
* In the example we have normal {@link 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+
* {@link Stew} class.
15+
*
16+
* The problem is solved with the Private Class Data pattern. We introduce
17+
* {@link ImmutableStew} class that contains {@link StewData}. The private data members of
18+
* {@link Stew} are now in {@link StewData} and cannot be altered by {@link ImmutableStew} methods.
19+
*
20+
*/
21+
public class App {
22+
23+
/**
24+
* Program entry point
25+
* @param args command line args
26+
*/
27+
public static void main( String[] args ) {
28+
// stew is mutable
29+
Stew stew = new Stew(1, 2, 3, 4);
30+
stew.mix();
31+
stew.taste();
32+
stew.mix();
33+
34+
// immutable stew protected with Private Class Data pattern
35+
ImmutableStew immutableStew = new ImmutableStew(2, 4, 3, 6);
36+
immutableStew.mix();
37+
}
38+
}
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
package com.iluwatar.privateclassdata;
2-
3-
import org.junit.Test;
4-
5-
import com.iluwatar.privateclassdata.App;
6-
7-
public class AppTest {
8-
9-
@Test
10-
public void test() {
11-
String[] args = {};
12-
App.main(args);
13-
}
14-
}
1+
package com.iluwatar.privateclassdata;
2+
3+
import org.junit.Test;
4+
5+
import com.iluwatar.privateclassdata.App;
6+
7+
/**
8+
*
9+
* Application test
10+
*
11+
*/
12+
public class AppTest {
13+
14+
@Test
15+
public void test() {
16+
String[] args = {};
17+
App.main(args);
18+
}
19+
}

0 commit comments

Comments
 (0)