Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
@ -2,37 +2,36 @@ package com.iluwatar.privateclassdata;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Private Class Data design pattern seeks to reduce exposure of attributes
|
||||
* by limiting their visibility. It reduces the number of class attributes by
|
||||
* encapsulating them in single data object. It allows the class designer to
|
||||
* remove write privilege of attributes that are intended to be set only during
|
||||
* construction, even from methods of the target class.
|
||||
* The Private Class Data design pattern seeks to reduce exposure of attributes by limiting their
|
||||
* visibility. It reduces the number of class attributes by encapsulating them in single data
|
||||
* object. It allows the class designer to remove write privilege of attributes that are intended to
|
||||
* be set only during construction, even from methods of the target class.
|
||||
* <p>
|
||||
* In the example we have normal {@link Stew} class with some ingredients given in
|
||||
* constructor. Then we have methods to enumerate the ingredients and to taste
|
||||
* the stew. The method for tasting the stew alters the private members of the
|
||||
* {@link Stew} class.
|
||||
* In the example we have normal {@link Stew} class with some ingredients given in constructor. Then
|
||||
* we have methods to enumerate the ingredients and to taste the stew. The method for tasting the
|
||||
* stew alters the private members of the {@link Stew} class.
|
||||
*
|
||||
* The problem is solved with the Private Class Data pattern. We introduce
|
||||
* {@link ImmutableStew} class that contains {@link StewData}. The private data members of
|
||||
* {@link Stew} are now in {@link StewData} and cannot be altered by {@link ImmutableStew} methods.
|
||||
* The problem is solved with the Private Class Data pattern. We introduce {@link ImmutableStew}
|
||||
* class that contains {@link StewData}. The private data members of {@link Stew} are now in
|
||||
* {@link StewData} and cannot be altered by {@link ImmutableStew} methods.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main( String[] args ) {
|
||||
// stew is mutable
|
||||
Stew stew = new Stew(1, 2, 3, 4);
|
||||
stew.mix();
|
||||
stew.taste();
|
||||
stew.mix();
|
||||
|
||||
// immutable stew protected with Private Class Data pattern
|
||||
ImmutableStew immutableStew = new ImmutableStew(2, 4, 3, 6);
|
||||
immutableStew.mix();
|
||||
}
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// stew is mutable
|
||||
Stew stew = new Stew(1, 2, 3, 4);
|
||||
stew.mix();
|
||||
stew.taste();
|
||||
stew.mix();
|
||||
|
||||
// immutable stew protected with Private Class Data pattern
|
||||
ImmutableStew immutableStew = new ImmutableStew(2, 4, 3, 6);
|
||||
immutableStew.mix();
|
||||
}
|
||||
}
|
||||
|
@ -7,14 +7,15 @@ package com.iluwatar.privateclassdata;
|
||||
*/
|
||||
public class ImmutableStew {
|
||||
|
||||
private StewData data;
|
||||
|
||||
public ImmutableStew(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
|
||||
data = new StewData(numPotatoes, numCarrots, numMeat, numPeppers);
|
||||
}
|
||||
|
||||
public void mix() {
|
||||
System.out.println(String.format("Mixing the immutable stew we find: %d potatoes, %d carrots, %d meat and %d peppers",
|
||||
data.getNumPotatoes(), data.getNumCarrots(), data.getNumMeat(), data.getNumPeppers()));
|
||||
}
|
||||
private StewData data;
|
||||
|
||||
public ImmutableStew(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
|
||||
data = new StewData(numPotatoes, numCarrots, numMeat, numPeppers);
|
||||
}
|
||||
|
||||
public void mix() {
|
||||
System.out.println(String.format(
|
||||
"Mixing the immutable stew we find: %d potatoes, %d carrots, %d meat and %d peppers",
|
||||
data.getNumPotatoes(), data.getNumCarrots(), data.getNumMeat(), data.getNumPeppers()));
|
||||
}
|
||||
}
|
||||
|
@ -6,37 +6,38 @@ package com.iluwatar.privateclassdata;
|
||||
*
|
||||
*/
|
||||
public class Stew {
|
||||
|
||||
private int numPotatoes;
|
||||
private int numCarrots;
|
||||
private int numMeat;
|
||||
private int numPeppers;
|
||||
|
||||
public Stew(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
|
||||
this.numPotatoes = numPotatoes;
|
||||
this.numCarrots = numCarrots;
|
||||
this.numMeat = numMeat;
|
||||
this.numPeppers = numPeppers;
|
||||
}
|
||||
|
||||
public void mix() {
|
||||
System.out.println(String.format("Mixing the stew we find: %d potatoes, %d carrots, %d meat and %d peppers",
|
||||
numPotatoes, numCarrots, numMeat, numPeppers));
|
||||
}
|
||||
|
||||
public void taste() {
|
||||
System.out.println("Tasting the stew");
|
||||
if (numPotatoes > 0) {
|
||||
numPotatoes--;
|
||||
}
|
||||
if (numCarrots > 0) {
|
||||
numCarrots--;
|
||||
}
|
||||
if (numMeat > 0) {
|
||||
numMeat--;
|
||||
}
|
||||
if (numPeppers > 0) {
|
||||
numPeppers--;
|
||||
}
|
||||
}
|
||||
|
||||
private int numPotatoes;
|
||||
private int numCarrots;
|
||||
private int numMeat;
|
||||
private int numPeppers;
|
||||
|
||||
public Stew(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
|
||||
this.numPotatoes = numPotatoes;
|
||||
this.numCarrots = numCarrots;
|
||||
this.numMeat = numMeat;
|
||||
this.numPeppers = numPeppers;
|
||||
}
|
||||
|
||||
public void mix() {
|
||||
System.out.println(String.format(
|
||||
"Mixing the stew we find: %d potatoes, %d carrots, %d meat and %d peppers", numPotatoes,
|
||||
numCarrots, numMeat, numPeppers));
|
||||
}
|
||||
|
||||
public void taste() {
|
||||
System.out.println("Tasting the stew");
|
||||
if (numPotatoes > 0) {
|
||||
numPotatoes--;
|
||||
}
|
||||
if (numCarrots > 0) {
|
||||
numCarrots--;
|
||||
}
|
||||
if (numMeat > 0) {
|
||||
numMeat--;
|
||||
}
|
||||
if (numPeppers > 0) {
|
||||
numPeppers--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,31 +7,31 @@ package com.iluwatar.privateclassdata;
|
||||
*/
|
||||
public class StewData {
|
||||
|
||||
private int numPotatoes;
|
||||
private int numCarrots;
|
||||
private int numMeat;
|
||||
private int numPeppers;
|
||||
|
||||
public StewData(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
|
||||
this.numPotatoes = numPotatoes;
|
||||
this.numCarrots = numCarrots;
|
||||
this.numMeat = numMeat;
|
||||
this.numPeppers = numPeppers;
|
||||
}
|
||||
private int numPotatoes;
|
||||
private int numCarrots;
|
||||
private int numMeat;
|
||||
private int numPeppers;
|
||||
|
||||
public int getNumPotatoes() {
|
||||
return numPotatoes;
|
||||
}
|
||||
public StewData(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
|
||||
this.numPotatoes = numPotatoes;
|
||||
this.numCarrots = numCarrots;
|
||||
this.numMeat = numMeat;
|
||||
this.numPeppers = numPeppers;
|
||||
}
|
||||
|
||||
public int getNumCarrots() {
|
||||
return numCarrots;
|
||||
}
|
||||
public int getNumPotatoes() {
|
||||
return numPotatoes;
|
||||
}
|
||||
|
||||
public int getNumMeat() {
|
||||
return numMeat;
|
||||
}
|
||||
public int getNumCarrots() {
|
||||
return numCarrots;
|
||||
}
|
||||
|
||||
public int getNumPeppers() {
|
||||
return numPeppers;
|
||||
}
|
||||
public int getNumMeat() {
|
||||
return numMeat;
|
||||
}
|
||||
|
||||
public int getNumPeppers() {
|
||||
return numPeppers;
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.privateclassdata.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user