01.01.2015 Views

UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

108<br />

Saturday Morning<br />

The answer to each question goes next to the class that describes the objects that the<br />

question is counting. The answer to “How many People . . .” goes on the end of the association<br />

near the Person class. The answer to “How many Cars . . .” goes on the end of the association<br />

next to the Car class.<br />

Multiplicity is expressed in a couple of ways. The most common is a range defining the<br />

minimum number of objects allowed and the maximum number of objects allowed in the<br />

format<br />

Minimum . . Maximum<br />

You must use integer values for the minimum and maximum. But, as you have probably<br />

found in your own applications, sometimes you don’t know the upper limit or there is no<br />

actual upper limit. The <strong>UML</strong> suggests the use of an asterisk to mean no upper limit. Used by<br />

itself, the asterisk can also mean the minimum is zero and there is no upper limit, or zero<br />

or more.<br />

You may also encounter a situation when a range is not appropriate. If you had to define<br />

how many cylinders are in the engines you service, you might want to say, “I only work on<br />

4–, 6–, and 8-cylinder engines.” For these situations, the <strong>UML</strong> suggests a comma-separated<br />

list (for example, 4,6,8).<br />

You can simplify the notation when the minimum and maximum values are the same by<br />

using a single value. So instead of writing 4..4, you can just write 4. This is a nice shortcut,<br />

but beware! The most common place to use this shortcut is when the multiplicity is 1..1.<br />

Unfortunately, the shortcut encourages people to overlook or ignore the possibility that the<br />

minimum is zero, that is, the relationship is optional.<br />

I’ll use the example of a car and an engine. That’s easy! Each car has one engine, right<br />

Well, what about cars on an assembly line During the first n stages of assembly, there is no<br />

engine in the car. In this case, the multiplicity should be 0..1 to allow the car to exist<br />

before it has an engine installed. Frankly, I have found very few instances when the minimum<br />

multiplicity should be 1. My rule of thumb is to set the minimum to 0 until I have<br />

positive proof that the one object cannot exist with the other object.<br />

Most software failures are because of small, difficult-to-find errors like the<br />

difference between 0 and 1. Most of those errors are caused by assumptions<br />

Tip or failures to think critically about the details. I once witnessed an explosion<br />

in a power plant caused by a one-character mistake in a program. Like<br />

they say, “The devil is in the details.”<br />

Here’s a summary list of the options for specifying multiplicity followed by some<br />

examples.<br />

Values separated by two periods (..) mean a range. For example, 1..3 means between<br />

1 and 3 inclusively; 5..10 means between 5 and 10 inclusively.<br />

Values separated by commas mean an enumerated list of possibilities. For example,<br />

4,6,8 means you may have 4 objects or 6 objects or 8 objects of this type in the<br />

association.<br />

Asterisk (*) when used alone means zero or more, no lower or upper limit.<br />

Asterisk (*) when used in a range (1..*) means no upper limit — you must have at<br />

least one but you can have as many more as you want.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!