|
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 | +} |
0 commit comments