Design Pattern----13.Structural.Private.Pattern (CSharp Sample)
Intent
- Control write access to class attributes
- Separate data from methods that use it
- Encapsulate class data initialization
- Providing new type of
final
- final after constructor
Problem
A class may expose its attributes (class variables) to manipulation when manipulation is no longer desirable, e.g. after construction. Using the private class data design pattern prevents that undesirable manipulation.
A class may have one-time mutable attributes that cannot be declared final. Using this design pattern allows one-time setting of those class attributes.
The motivation for this design pattern comes from the design goal of protecting class state by minimizing the visibility of its attributes (data).
Discussion
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.
Structure
The private class data design pattern solves the problems above by extracting a data class for the target class and giving the target class instance an instance of the extracted data class.
Check list
- Create data class. Move to data class all attributes that need hiding.
- Create in main class instance of data class.
- Main class must initialize data class through the data class's constructor.
- Expose each attribute (variable or property) of data class through a getter.
- Expose each attribute that will change in further through a setter.
Before
The attributes radius, color, and origin below should not change after the Circle() constructor. Note that the visibility is already limited by scoping them as private, but doing methods of class Circle can still modify them.
Although marking attributes of classes as const (or final or ReadOnly in other programming languages) restricts their manipulation, the attributes above are set in the constructor and hence cannot be marked as such.
1: public class Circle {2: private double radius;3: private Color color;
4: private Point origin;
5: public Circle(double radius, Color color, Point origin) {6: this.radius = radius;
7: this.color = color;
8: this.origin = origin;
9: }10: public double Circumference {11: get { return 2 * Math.PI * this.radius; }12: }13: public double Diameter {14: get { return 2 * this.radius; }15: }16: public void Draw(Graphics graphics) {17: //...
18: }19: }20:
After
The excess exposure of the attributes creates a type of (undesirable) coupling between methods that access those attributes. To reduce the visibility of the attributes and thus reduce the coupling, implement the private class data design pattern, as follows:
1: public class CircleData {2: private double radius;3: private Color color;
4: private Point origin;
5: public CircleData(double radius, Color color, Point origin) {6: this.radius = radius;
7: this.color = color;
8: this.origin = origin;
9: }10: public double Radius {11: get { return this.radius; }12: }13: public Color Color {
14: get { return this.color; }15: }16: public Point Origin {
17: get { return this.origin; }18: }19: }20: public class Circle {21: private CircleData circleData;
22: public Circle(double radius, Color color, Point origin) {23: this.circleData = new CircleData(radius, color, origin);24: }25: public double Circumference {26: get { return this.circleData.Radius * Math.PI; }27: }28: public double Diameter {29: get { return this.circleData.Radius * 2; }30: }31: public void Draw(Graphics graphics) {32: //...
33: }34: }