Browser docs

Private Class Data

Intent

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.

Explanation

Real world example

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.

In plain words

Private class data pattern prevents manipulation of data that is meant to be immutable by separating the data from the methods that use it into a class that maintains the data state.

Wikipedia says

Private class data is a design pattern in computer programming used to encapsulate class attributes and their manipulation.

Programmatic Example

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.

 1@Slf4j
 2public class Stew {
 3  private int numPotatoes;
 4  private int numCarrots;
 5  private int numMeat;
 6  private int numPeppers;
 7  public Stew(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
 8    this.numPotatoes = numPotatoes;
 9    this.numCarrots = numCarrots;
10    this.numMeat = numMeat;
11    this.numPeppers = numPeppers;
12  }
13  public void mix() {
14    LOGGER.info("Mixing the stew we find: {} potatoes, {} carrots, {} meat and {} peppers",
15        numPotatoes, numCarrots, numMeat, numPeppers);
16  }
17  public void taste() {
18    LOGGER.info("Tasting the stew");
19    if (numPotatoes > 0) {
20      numPotatoes--;
21    }
22    if (numCarrots > 0) {
23      numCarrots--;
24    }
25    if (numMeat > 0) {
26      numMeat--;
27    }
28    if (numPeppers > 0) {
29      numPeppers--;
30    }
31  }
32}

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.

 1public class StewData {
 2  private final int numPotatoes;
 3  private final int numCarrots;
 4  private final int numMeat;
 5  private final int numPeppers;
 6  public StewData(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
 7    this.numPotatoes = numPotatoes;
 8    this.numCarrots = numCarrots;
 9    this.numMeat = numMeat;
10    this.numPeppers = numPeppers;
11  }
12  public int getNumPotatoes() {
13    return numPotatoes;
14  }
15  public int getNumCarrots() {
16    return numCarrots;
17  }
18  public int getNumMeat() {
19    return numMeat;
20  }
21  public int getNumPeppers() {
22    return numPeppers;
23  }
24}
25@Slf4j
26public class ImmutableStew {
27  private final StewData data;
28  public ImmutableStew(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
29    data = new StewData(numPotatoes, numCarrots, numMeat, numPeppers);
30  }
31  public void mix() {
32    LOGGER
33        .info("Mixing the immutable stew we find: {} potatoes, {} carrots, {} meat and {} peppers",
34            data.getNumPotatoes(), data.getNumCarrots(), data.getNumMeat(), data.getNumPeppers());
35  }
36}

Let’s try creating an instance of each class and call their methods:

1var stew = new Stew(1, 2, 3, 4);
2stew.mix();   // Mixing the stew we find: 1 potatoes, 2 carrots, 3 meat and 4 peppers
3stew.taste(); // Tasting the stew
4stew.mix();   // Mixing the stew we find: 0 potatoes, 1 carrots, 2 meat and 3 peppers
5var immutableStew = new ImmutableStew(2, 4, 3, 6);
6immutableStew.mix();  // Mixing the immutable stew we find: 2 potatoes, 4 carrots, 3 meat and 6 peppers

Class diagram

alt text

Applicability

Use the Private Class Data pattern when

  • You want to prevent write access to class data members.