<< Chapter < Page | Chapter >> Page > |
In general, the UML class diagram for the union design pattern looks like such:
The characteristics of the union pattern are
Corollary : Another way of thinking about the union pattern is to consider that the abstract superclass represents any of the subclasses. That is, anywhere the superclass is referenced, an instance of any of subclasses could be used. This is the essence of polymorphism .
Consider the following examples:
One of the most important things that the union pattern does for an OO design is to define layers of abstraction. The abstract superclass represents a higher level of abstraction than the concrete subclasses. The union pattern thus defines two distinct abstraction levels. At any given moment, a program will that uses the classes involved in the union pattern will be running at either the lower, more concrete abstraction level or the higher, more abstract level.
Good OO design pays careful attention to maintaining a consistent abstraction level in any given section of code. Changing abstraction levels in the middle of a process, particularly the decreasing of abstraction, effectively nullifies the power and flexibility of having made the abstraction in the first place.
WARNING!
Simply having an abstract superclass does not automatically imply the existence of the union pattern!
Sometimes abstract superclasses are used to encapsulate and/or centralize shared or common code used in the subclasses. This gathering of code from the subclasses and relocating it to a superclass, often refered to as " hoisting ", does not guarantee that the superclass is an abstraction of the subclasses. For instance, it would be convenient to put a field in "fruit" that tells us how many sections it has, because so many fruit, such as oranges, grapefruit, etc, form in sections. But the very fact that some fruit, such as mangos are not describable in terms of sections, means that to include such a field in "fruit" would compromise it as an abstraction of all fruit. When performing hoisting, one must be very careful to understand the difference between "universalizing" (trying to do everything) and "abstracting" (capturing the essence of the problem).
One of the most fundamental OO design decisions is where exactly to put the dividing line between the invariant , unchanging aspects of the problem and the variant , changing aspects. The union design pattern provides a clear representation of one type of variant-invariant separation.
The abstract superclass is a representation of the essence that common to all possible concrete subclasses. It thus represents the invariant aspects of all elements in the union.
The concrete subclasses, while all equivalent at a higher, more abstract level, are different from each other in some manner. They thus represent the variant aspects of the problem.
Code that works at the abstraction level of the superclass is thus invariant code. It is capable of working with any instance of the concrete subclasses because it only deals with the invariant behavior of the superclass. The actual total behavior of the system is the invariant behavior of the abstract superclass plus the variant behaviors provided polymorphically by the instance of the concrete subclass being used.
Notification Switch
Would you like to follow the 'Design patterns' conversation and receive update notifications?