<< Chapter < Page Chapter >> Page >
The Union Design Pattern represents the abstraction of two or more (more) concrete representations. This representation of an abstraction is a fundamental concept in object-oriented design. The "union" refers to the abstraction representing the union of all the concrete subclasses.

Uml diagram and characteristics

In general, the UML class diagram for the union design pattern looks like such:

Union design pattern

UML diagram of the union design pattern

The characteristics of the union pattern are

  • 1 abstract superclass that represents the abstraction of all the subclasses.   (Note that the superclass could be an interface.)   The superclass can never instantiated because it does not represent any particular concrete entity. The methods of the superclass could be abstract (variant behaviors defined in the subclasses) or concrete (pure invariant behavior)--See below .
  • 2 or more subclasses (It is arguable whether or not a single subclass would qualify--if one considers that it might be possible that other subclasses could exist and are simply not coded yet, then one could argue that there is actually more than one subclass.)   A subclass could be abstract, in which case, it would represent another abstraction layer.

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     .

Examples

Consider the following examples:

  • Oranges, apples, pears, peaches, and mangos can all be abstractly represented as "fruit".   Note that there is no such thing as a fruit that is not of some concrete sort, e.g. plum or strawberry.    The concept of "fruit" is an abstraction that represents the union of oranges, apples, plums, etc.   The notion of a fruit contains only the essence of what is common to all the concrete instances, such as containing seeds, having a sweet juicy flesh, etc.    On the other hand, the notion of a fruit does not include specific information that pertains to only oranges--the number of orange sections it has, for instance--or to only strawberries -- the nature of its vine, for instance-- or to only any other particular kind of fruit.   To talk about a fruit is to talk about all fruit at once -- the union of all types of fruit.

    Union of different fruits

    Fruit represents the union or abstraction of many types of specific things
    In the above UML class diagram,  we see that oranges, apples, mangos and other concrete fruit classes all inherit from an abstract fruit class.
  • Keyboards, mice, microphones, network ports and speakers are all input/output ("I/O") devices.  An I/O device cannot exist without being a specific, concrete type of device, such as a keyboard or a speaker.     Thus the concept of an  "I/O device" is an abstraction of the union of all keyboards, printer ports, microphones, etc.   The notion of an I/O device contains high-level issues about moving data into and out of a computer, but not any low-level concrete information about how it is done such as via a parallel or serial bit stream or how the data is formatted.    "I/O device" refers to all possible types of input/output device. 

    Union of different i/o devices

    IODevice represents the union of many different types of specific devices
    As before, the UML class diagram shows us that the keyboard, mouse, network port and other concrete I/O devices inherit from an abstract I/O device class.

 

Relationship to abstraction and variant/invariant principles 

Abstraction layers

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).

Variant vs. invariant

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.

Practice Key Terms 4

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Design patterns. OpenStax CNX. Jun 04, 2009 Download for free at http://cnx.org/content/col10678/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Design patterns' conversation and receive update notifications?

Ask