Part of the ObjectBasedProgramming
pattern language. Discussion occurs on PolymorphicFunctionWithEmbeddedBehaviourDiscussion?
Original at http://www.geocities.com/SiliconValley/Foothills/5962/pfwitheb.htm
Provide a strategy to implement polymorphic functions, with the lowest level of architectural complexity.
pattern requires a single polymorphic function to implement a particular operation for all subclasses in the class family. This function would need to exhibit different behaviour for objects of different
subclasses if the operation is overridden by the subclasses. This pattern provides a strategy to implement polymorphic functions with the minimal architectural complexity.
Applicability should be decided on an operation by operation basis. Some operations in a class family may benefit from this pattern while other would not. The Function Pointer pattern is applicable to an operation if:
- The operation is not intended to be overridden by the subclasses
- OR The operation can be overridden by the subclass, but the number of subclasses is small and the class family is not likely to grow.
The polymorphic function would identify the class of the instance it is operating on, and exhibit the behaviour required for the particular class. The implementation is directly hard-coded to deal with each of the recognised sub-classes.
pattern has the following important consequences:
- Not easily extensible. When a subclass is added to the class family, the polymorphic functions implementing the operations need to be modified to recognise the new subclass. Maintainability is reduced.
- Less conceptual complexity. The implementation is conceptually easier to understand.
- ClassTag pattern must be applied. The ClassTag pattern is required to provide class identification information at run-time.
Consider the following class family.
The setState operation is supported by two subclass Leaf alarm and Simple condition. The polymorphic function ConditionFamilySetState?
implements the setState operation. A sample implementation of ConditionFamilySetState?
with this pattern applied is shown below. The ClassTag
pattern is also applied to provide class identification information.
void ConditionFamilySetState?( void *pvInstanceData, param1, ... )
/* Get the class type - class tag is stored at the beginning of the
instance data structure */
nClassTag = pvInstanceData->nClassTag;
/* Exhibit different behaviour for different subclasses */
switch ( nClassTag )
Do things to the leaf alarm instance
Do things to the simple condition instance
case CONDITION_TYPE_ALARM: /* Fallthrough */
case CONDITION_TYPE_CONDITION_ALARM: /* Fallthrough */
case CONDITION_TYPE_NON_LEAF_ALARM: /* Fallthrough */
Error handling - these classes do not support this operation
Error handling - this subclass is not supported
This section is to be filled in.
This pattern provides an implementation strategy to implement polymorphic functions described the PolymorphicFunction
pattern describes an contrasting strategy to implement polymorphic functions.
pattern is required as a pre-requisite of this pattern. The ClassTag
pattern provides the mechanism to identify the class of an object at run-time.