OOPortal OOPortal


Logical View  «Prev  Next»
Lesson 11 Delegation
Objective Define the notation for specifying delegation, an alternative to inheritance.

Specifying UML Delegation as an Alternative to Inheritance

Generalization tends to be overused, much like aggregation. Remember that generalization does generate different code, so be sure that you are using it properly. Some design pattern texts use alternatives to generalization that provide more control.

Delegation

The delegation technique makes one class a part of another class, using aggregation. When the containing class requires assistance it simply forwards, or delegates, the request to the component class. When the component class is finished with the delegated task it returns control to the requestor.

Classes Circle, Rectangle, and Polygon inherit from class Shape.

Delegation is a common technique for allowing one class to get help from another class without using inheritance. To give one class access to another, follow these steps:
  1. Identify the class that needs help. A TextBox needs to be drawn in a variety of shapes.
  2. Identify the class with the desired behaviors; a Shape superclass with sub-classes like Ellipse and Polygon, and a predefined shape called Cloud.
  3. To use these shapes, tell the TextBox about them using aggregation. Remember to assign the multiplicity at each end
  4. Implement the aggregation association using a new attribute in TextBox called boxshape. The data type of boxshape is Shape, the class that is associated with TextBox.
  5. Implement the draw() operation in the TextBox by delegating the operation to the Shape object in the boxshape attribute.


UML Delegation Textbox
For another application of the delegation technique, look at the State design pattern.
This pattern can be very helpful in an application that requires complex state behavior.
The next lesson concludes this module.

Uml Delegation - Exercise

Click the Exercise link below to apply what you have learned in this module to the course project.
Uml Delegation - Exercise