Part of the ObjectBasedProgramming pattern language. Discussion occurs on PolymorphicFunctionDiscussion.
Original at http://www.geocities.com/SiliconValley/Foothills/5962/polymorphicfunction.htm
Intent
Implement polymorphic operation without exposing the internal implementation.
Motivation
True polymorphism enhances the potential of reuse, extensibility and maintainability. Polymorphism makes it possible to interact with an object without knowledge of its concrete class.
The implementation of the object-oriented concepts in a procedural language often requires additional implementation scaffolding. The underlying mechanisms providing the support should be completely hidden from the outside world. This implementation abstraction is necessary to provide the flexibility to modify the underlying mechanism in the future.
Applicability
Apply the Polymorphic Function Pattern when
Each operation in the superclass and all subclasses map to a polymorphic function. This polymorphic function can operate on all subclasses in the class family that implemented the said operation. Its behaviour will differ to match the class of the object. This polymorphic function accepts an instance data structure as a parameter. The instance data structure is passed as an opaque type (In C it will be implemented as a *void).
Consequences
The Polymorphic Function Pattern has four important consequences:
Consider the following class family:
[1]
The object diagram does not show the parameters and the return value type of the operations.
The operations getDescendentChangeCount():int and getChangeCount():int would be respectively implemented by functions with the following interfaces:
int ConditionFamilyGetDescendentChangeCount( void *pvInstance ); int ConditionFamilyGetChangeCount( void *pvInstance );The operation reevaluateCondition(): is a private operation and therefore it does not have a public interface.
The subclasses Leaf alarm and Simple conditions independently extend the Condition class to provide the operation setState(bState: Boolean):. The operation would be implemented by a function with the following interface:
void ConditionFamilySetState( void *pvInstance, BOOL bState );Known Uses
The Polymorphic Function pattern is fundamental to the application of object-based programming style described in this pattern language. This pattern has been extensively applied in the Network Management Unit Software (NUS) developed by MAS Technology. The Management Procedure Lock, the Condition Manager and the object handlers are implemented with the application of this pattern.
Related Patterns
The interface of the polymorphic functions is described by this pattern. However this pattern does not address how to actually implement these functions. The FunctionPointer pattern, and the PolymorphicFunctionWithEmbeddedBehaviour pattern describe the techniques to implement an polymorphic function that can be performed on any instance in a class hierarchy.
Under some restricted conditions, operations need not be implemented as a function. An operation can be factored out and becomes a direct access to the instance data. This is described by the OperationVapourware pattern.
Polymorphic functions take instance data as inputs, which is described by the InstanceData pattern.