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

Questions & Answers

A golfer on a fairway is 70 m away from the green, which sits below the level of the fairway by 20 m. If the golfer hits the ball at an angle of 40° with an initial speed of 20 m/s, how close to the green does she come?
Aislinn Reply
cm
tijani
what is titration
John Reply
what is physics
Siyaka Reply
A mouse of mass 200 g falls 100 m down a vertical mine shaft and lands at the bottom with a speed of 8.0 m/s. During its fall, how much work is done on the mouse by air resistance
Jude Reply
Can you compute that for me. Ty
Jude
what is the dimension formula of energy?
David Reply
what is viscosity?
David
what is inorganic
emma Reply
what is chemistry
Youesf Reply
what is inorganic
emma
Chemistry is a branch of science that deals with the study of matter,it composition,it structure and the changes it undergoes
Adjei
please, I'm a physics student and I need help in physics
Adjanou
chemistry could also be understood like the sexual attraction/repulsion of the male and female elements. the reaction varies depending on the energy differences of each given gender. + masculine -female.
Pedro
A ball is thrown straight up.it passes a 2.0m high window 7.50 m off the ground on it path up and takes 1.30 s to go past the window.what was the ball initial velocity
Krampah Reply
2. A sled plus passenger with total mass 50 kg is pulled 20 m across the snow (0.20) at constant velocity by a force directed 25° above the horizontal. Calculate (a) the work of the applied force, (b) the work of friction, and (c) the total work.
Sahid Reply
you have been hired as an espert witness in a court case involving an automobile accident. the accident involved car A of mass 1500kg which crashed into stationary car B of mass 1100kg. the driver of car A applied his brakes 15 m before he skidded and crashed into car B. after the collision, car A s
Samuel Reply
can someone explain to me, an ignorant high school student, why the trend of the graph doesn't follow the fact that the higher frequency a sound wave is, the more power it is, hence, making me think the phons output would follow this general trend?
Joseph Reply
Nevermind i just realied that the graph is the phons output for a person with normal hearing and not just the phons output of the sound waves power, I should read the entire thing next time
Joseph
Follow up question, does anyone know where I can find a graph that accuretly depicts the actual relative "power" output of sound over its frequency instead of just humans hearing
Joseph
"Generation of electrical energy from sound energy | IEEE Conference Publication | IEEE Xplore" ***ieeexplore.ieee.org/document/7150687?reload=true
Ryan
what's motion
Maurice Reply
what are the types of wave
Maurice
answer
Magreth
progressive wave
Magreth
hello friend how are you
Muhammad Reply
fine, how about you?
Mohammed
hi
Mujahid
A string is 3.00 m long with a mass of 5.00 g. The string is held taut with a tension of 500.00 N applied to the string. A pulse is sent down the string. How long does it take the pulse to travel the 3.00 m of the string?
yasuo Reply
Who can show me the full solution in this problem?
Reofrir Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply
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