World Library  
Flag as Inappropriate
Email this Article

Private class data pattern

Article Id: WHEBN0001265481
Reproduction Date:

Title: Private class data pattern  
Author: World Heritage Encyclopedia
Language: English
Subject: Articles with example C Sharp code, Opaque pointer, Software design patterns
Collection:
Publisher: World Heritage Encyclopedia
Publication
Date:
 

Private class data pattern

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

Contents

  • Standard documentation 1
    • Name and classification 1.1
    • Intent 1.2
    • Also known as 1.3
    • Motivation 1.4
    • Applicability 1.5
    • Structure 1.6
    • Participants 1.7
    • Collaboration 1.8
    • Consequences 1.9
    • Implementation 1.10
    • Sample code 1.11
    • Known uses 1.12
    • Related patterns 1.13
  • References 2

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

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

Related patterns

See Structural pattern for related patterns.

References

  • Sourcemaking.com article
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and USA.gov, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for USA.gov and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
 
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
 
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.
 


Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.