Private class data pattern

Private class data pattern

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

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

tructure

Participants

Collaboration

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 targed 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".

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

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.

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

Related patterns

See Structural pattern for related patterns.

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Structural pattern — In Software Engineering, Structural Design Patterns are Design Patterns that ease the design by identifying a simple way to realize relationships between entities.Examples of Structural Patterns include:* Adapter pattern: adapts one interface for …   Wikipedia

  • Class (computer programming) — In object oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable …   Wikipedia

  • Class (computer science) — In object oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint includes attributes and methods that the created objects all share.More technically, a class is a cohesive… …   Wikipedia

  • Pattern Recognition (novel) — infobox Book | name = Pattern Recognition image caption = Original 1st edition cover author = William Gibson cover artist = country = United States language = English series = genre = Science fiction novel publisher = G. P. Putnam s Sons release… …   Wikipedia

  • Class invariant — This article is about class invariants in computer programming, for use of the term in mathematics, see equivalence class and invariant. In computer programming, specifically object oriented programming, a class invariant is an invariant used to… …   Wikipedia

  • Class diagram — UML diagrams Structural UML diagrams Class diagram Component diagram Composite structure diagram Deployment diagram Object diagram …   Wikipedia

  • Adapter pattern — In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. An adapter allows classes to work together that …   Wikipedia

  • Social class in New Zealand — Class in New Zealand is a product of both Māori and Western social structures. New Zealand was traditionally supposed to be a classless society but this claim is problematic in a number of ways, and has been clearly untrue since at least the… …   Wikipedia

  • Template method pattern — [ LePUS3 ( [http://lepus.org.uk/ref/legend/legend.xml legend] ) ] In software engineering, the template method pattern is a design pattern.It is a so called behavioral pattern, and is unrelated to C++ templates.IntroductionIn a template pattern,… …   Wikipedia

  • Chain-of-responsibility pattern — In Object Oriented Design, the chain of responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains a set of logic that describes the types of command… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”