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.

Scheme of Private Class Data

Check list

  1. Create data class. Move to data class all attributes that need hiding.
  2. Create in main class instance of data class.
  3. Main class must initialize data class through the data class's constructor.
  4. Expose each attribute (variable or property) of data class through a getter.
  5. 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: }

转载于:https://www.cnblogs.com/xiuyusoft/archive/2011/07/01/2095341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值