请输入您要查询的百科知识:

 

词条 Private class data pattern
释义

  1. Standard documentation

     Name and classification  Intent  Also known as  Motivation  Applicability  Structure  Participants  Collaboration  Consequences  Implementation  Sample code  Known uses  See also 

  2. References

{{Multiple issues|{{notability|date=October 2009}}{{Lead too short|date=May 2009}}{{Cleanup rewrite|date=May 2009}}{{More citations needed|date=January 2017}}
}}

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

Standard documentation

The following documentation categories for the private class data design pattern follows the design pattern documentation style precedent set by the Gang of Four.

Name and classification

Pattern Name
This pattern is known as the private class data design pattern.
Pattern Classification
This pattern is a structural pattern.

Intent

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.

Also known as

PIMPL (Private IMPLementation) or Opaque pointer

Motivation

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).

Applicability

This design pattern applies to any class in any object oriented language.

Structure

{{Empty section|date=January 2011}}

Participants

{{Empty section|date=January 2011}}

Collaboration

{{Empty section|date=January 2011}}

Consequences

The consequences of using this design pattern include the following:

  • Controlling write access to class attributes;
  • Separating of data from methods that use it;
  • Encapsulating class attribute (data) initialization; and
  • Providing new type of final: final after constructor.

Implementation

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.

  • The data class exposes each attribute (variable or property) through a getter.
  • The data class exposes each attribute that must change after construction through a setter.

Sample code

The following C# code illustrates an opportunity to use the private class data design pattern:

public class Circle

{
    private double _radius;    private Color _color;    private Point _origin;    public Circle(double radius, Color color, Point origin)    {        this._radius = radius;        this._color = color;        this._origin = origin;    }    public double Circumference    {        get { return 2 * Math.PI * this._radius; }    }    public double Diameter    {        get { return 2 * this._radius; }    }    public void Draw(Graphics graphics)    {        //...    }

}

The attributes radius, color, and origin above 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.

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:

public class CircleData

{
    private double _radius;    private Color _color;    private Point _origin;    public CircleData(double radius, Color color, Point origin)    {        this._radius = radius;        this._color = color;        this._origin = origin;    }    public double Radius    {        get { return this._radius; }    }    public Color Color    {        get { return this._color; }    }    public Point Origin    {        get { return this._origin; }    }

}

public class Circle

{
    private CircleData _circleData;    public Circle(double radius, Color color, Point origin)    {        this._circleData = new CircleData(radius, color, origin);    }    public double Circumference    {        get { return 2 * this._circleData.Radius * Math.PI; }    }    public double Diameter    {        get { return this._circleData.Radius * 2; }    }    public void Draw(Graphics graphics)    {        //...    }

}

The Circle class in the resulting code has an attribute of type CircleData to encapsulate the attributes previously exposed to all of the methods of the class Circle. That encapsulation prevents methods from changing the attributes after the Circle() constructor. Note, however, that any method of Circle can still retrieve the values of the encapsulated attributes.

Known uses

The Qt framework uses the private class data pattern in its shared libraries.[1] The classes that implement the pattern include a "d-pointer" to the data class. Methods are provided for manipulating member variables in the data class, allowing changes without breaking binary compatibility.

See also

  • Structural pattern for related patterns.

References

1. ^{{cite web|url=https://wiki.qt.io/D-Pointer|title=D-Pointer |accessdate=7 January 2017}}
  • Sourcemaking.com article
{{DEFAULTSORT:Private Class Data Pattern}}

2 : Software design patterns|Articles with example C Sharp code

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/20 16:53:37