Part of the ObjectBasedProgramming pattern language. Discussion occurs on ClassDescriptorDiscussion?.
Original at http://www.geocities.com/SiliconValley/Foothills/5962/classdescriptor.htm
Intent
Provide a centralised storage to store information common to a particular class, but extrinsic to an instance of that class.
Motivation
The two motivations are:
- If the instance data contains any information common to a class and hence extrinsic to the instance, duplication will result, contributing to higher memory usage, higher coupling and reduced maintainability. It is desirable to eliminate the duplication.
- When an operation exhibits similar behaviour for different subclasses, its implementation can sometimes be generalised to a single parameterized algorithm with different parameters for different subclasses. The parameters are intrinsic to the class but extrinsic to a particular instance. Neither the instance data nor the polymorphic function are the ideal place to store the information.
Applicability
Use the ClassDescriptor pattern when
- There is information extrinsic to an instance, but pertain to an entire class
- AND there is a significant number of instances.
Solution
Information pertaining to an entire class is moved out of the instance data structure, and moved into a class descriptor.
Consequences
The ClassDescriptor has three important consequences:
- Reduced memory usage. Information pertaining to a class is separated from the information intrinsic to an object. The extrinsic information is removed from the instance data. The elimination of duplication can greatly reduce memory usage, especially if there is a great number of instances.
- Higher conceptual complexity. The use of a class descriptor introduces another layer of indirection, and consequent conceptual complexity.
- Class Tag pattern must be applied. To access the correct class descriptor, there must be a mechanism to determine the class of an object at run-time.
Sample Code
This section is to be filled out.
Known Uses
This section is to be filled out.
Related Patterns
The ClassDescriptor pattern is an implementation-level application of the FlyweightPattern.
The ClassTag pattern is a pre-requisite of this pattern.
The FunctionPointer pattern is often used with this pattern. The function pointers are information extrinsic to an object instance, and are a perfect candidate to move into the class descriptor.