Defer identification of state variables pattern

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m
Line 24: Line 24:
 
* [[Intelligent children pattern]]
 
* [[Intelligent children pattern]]
 
* [[Define classes by behavior, not state pattern]]
 
* [[Define classes by behavior, not state pattern]]
 +
* [[Ken Auer 1995]]

Revision as of 02:16, 3 August 2009

Fig.1 A basic abstract shape class
Fig.2 Defer identification of state variables pattern hard at work

Also known as the Intelligent children pattern the process of deferring identification of state variables is used so that the creation of variables can be postponed until the creation of subclasses further down the hierarchy. This helps make the definition of abstraction more adaptive to change and allows the creation of a class hierarchy that is less constrained by the consequences of design decisions. Every implementation detail that is put into concrete form forces subclasses to accept a design decision and its corresponding space/efficiency tradeoffs.

The deferring is achieved by creating abstract accessors instead of defining data fields. This allows the creation of the data fields to happen in a classes further down the hierarchy where there is more information available to make an informed decision. For example think of a class for shape which could be defined as shown in fig.1.


Now imagine that your system has need for a shape that is stored in cartesian coordinates, a shape stored in polar coordinates and a space efficient shape that has its coordinates packed into one integer. If we use the basic abstract shape class then there in no benefit to using inheritance the PolarCircle will have two data fields for position, one not being used. The same goes for the SpaceEfficientShape making it far from efficient. If deferring is used properly then a working inheritance structure might look like the one illustrated in Fig.2.


This pattern is useful in many situations. One such situation is the Parallel hierarchies problem that sometimes appears in OO designs. The OO design below shows an example of the Parallel Inheritance Hierarchies problem. This design shows the abstract class Vehicle holding an Operator as one of it's fields. One problem with this is that a Car object might be able to have a Pilot as its Operator instead of a Driver. For the solution see the Parallel hierarchies problem.


Deferred state variables 1.jpg

See also

This concept was originally introduced by Ken Auer in his paper Reusability through self-encapsulation.

Personal tools