Class Tag

Part of the ObjectBasedProgramming pattern language. Discussion occurs on ClassTagDiscussion?.

Original at http://www.geocities.com/SiliconValley/Foothills/5962/classtag.htm


Intent

Provide run-time object class identification.

Motivation

Some other patterns described in this pattern language require the ability to determine the class of an object at run-time. A procedural language often has no built-in mechanism to determine the type of a data structure, hence it is impossible without help to uniquely distinguish the type of instance data. The ability to determine the class of an object must be provided externally.

Applicability

Use the ClassTag pattern when

Solution

Type information is added to the instance data so that the class of the object can be identified in run-time. The class information can be stored in any format as long as the type can be uniquely determined. Typical formats include:

The type information must be stored in a fixed location in the instance data, so the polymorphic functions can access the type information without first knowing the structure of the instance data. A typical location is offset 0.

It is important to note that it is not possible to access the class tag without knowledge of the instance data. As a result, unless the principal of encapsulation is broken, it is not possible for an external entity to directly determine the class of an object, even if this pattern is applied. If the class of an object is to be made externally available, the class family should be designed to support a corresponding operation.

Consequences

The ClassTag pattern has the one important consequence:

  1. Higher memory consumption. The class tag is encoded in the instance data for each instance.

Sample Code

Consider the following class family:

[1]

For the sake of demonstration, we assume neither the FamilySplit pattern nor the FamilyFusion pattern have been applied. Each subclass has its own instance data structure.

The ClassTag pattern is applied. The location of the class tag is decided to be at offset 0 of the instance data.

The definition of the instance data structure would be as follows:

typedef struct { 
int nShapeClass, /* class tag */ 
} tsSHAPE; 

typedef struct { int nShapeClass, /* class tag */ int nWidth, int nLength } tsRECTANGLE;

typedef struct { int nShapeClass, /* class tag */ int nRadius } tsCIRCLE;

Known Uses

This section is to be filled out.

Related Patterns

The ClassTag pattern is a pre-requisite of the PolymorphicFunctionWithEmbeddedBehaviour pattern and the ClassDescriptor pattern.


Category?


EditText of this page (last edited October 18, 2014) or FindPage with title or text search