UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory UML Weekend Crash Course™ - To Parent Directory

crnarupa.singidunum.ac.rs
from crnarupa.singidunum.ac.rs More from this publisher
01.01.2015 Views

Session 11—The Class Diagram: Aggregation and Generalization 123 A subclass is a class that contains some combination of attributes, operations, and associations that are unique to a type of object that is partially defined by a superclass. In Figure 11-5, Apple, Watermelon, Orange, RedDelicious and GrannySmith are all examples of subclasses. Note that a class may be both a superclass and a subclass. The term subclass reflects the concept of subset. The subset, or subclass, contains a unique set of properties for only certain objects within the set. The GrannySmith class in Figure 11-5 would only contain the properties that are unique to GrannySmith apples. A GrannySmith object would get the rest of the information about the properties it shares with all apples from the Apple superclass, and all the properties it has in common with all fruits from the Fruit superclass. In other words, it actually takes three classes to generate a single object representing a GrannySmith apple. An abstract class is a class that cannot create objects (cannot be instantiated). Any superclass that defines at least one operation that does not have a method is said to be abstract, or lacking a complete definition. Only a superclass can be abstract. A concrete class is a class that has a method for every operation, so it can create objects. The methods may be defined in the class or inherited from a superclass. All classes at the bottom of a generalization hierarchy must be concrete. Any superclass may be concrete. A discriminator is an attribute or rule that describes how I choose to identify the set of subclasses for a superclass. If I wanted to organize the information about types of cars, I could discriminate based on price range, manufacturer, engine size, fuel type, usage, or any number of other criteria. The discriminator I choose depends on the problem I am trying to solve. In Figure 11-6, I use a very common practice. I identify a set of predefined types (that is, type of fruit and varieties within types). Other times I might use a property of the objects themselves like size, price range, capacity, or age. Fruit Discriminator Fruit Type Apple Watermelon Orange Discriminator RedDelicious Fruit Variety GrannySmith Figure 11-6 Modeling generalization with discriminators The composition of a class reveals the possible discriminating properties. Classes define properties of objects such as attributes, operations, and association. These are the first three possible discriminating properties. If objects of the class share the same attributes,

124 Saturday Afternoon like age and address, they might be in the same subgroup. However, objects might have the same attribute (like age), but the values allowed for age in some of the objects are different from those allowed in others. For example, every Person has an age value assigned. However, minors would have age values less than 21 (in some states) and adults would have ages greater than 20. The same concept applies to operations. Objects might have the same operation, that is, the same interface, like “accelerate.” But different objects might implement that interface in very different ways. A car accelerates very differently from a rocket. Even different cars accelerate using different combinations of parts and fuels. In summary, there are at least five objective criteria I can use to discriminate between objects within the same class (superclass): Attribute type Attribute values allowed Operation (interface) Method (implementation) Associations Tip Proper use of the discriminator facilitates the use of design patterns such as abstract factory and state. There are actually two ways to draw a generalization. Figure 11-5 showed separate lines from each subclass to its superclass. Figure 11-6 merged the lines from all the subclasses of a superclass. In either form, draw a triangle at the superclass end of the generalization line (pointing to the superclass). (Note that you draw a triangle, not an arrow.) Connect the other end of the generalization to the subclass. Add the discriminator on the generalization line in simple text. An illustration: How to model generalization Building a generalization hierarchy can work in two directions, from the most general class down to the most specialized classes (specialization), and from the most specialized classes to the most generalized class (generalization). The following example builds from general concept down to specifics, using the class Dog. I need to keep track of various breeds in a dog obedience school in order to understand and anticipate their unique training requirements. Step 1: Draw the starting superclass, as in Figure 11-7. It is a class like any other class. The only thing that makes it a superclass is the fact that I plan to break it down.

Session 11—The Class Diagram: Aggregation and Generalization 123<br />

A subclass is a class that contains some combination of attributes, operations, and associations<br />

that are unique to a type of object that is partially defined by a superclass. In<br />

Figure 11-5, Apple, Watermelon, Orange, RedDelicious and GrannySmith are all examples<br />

of subclasses. Note that a class may be both a superclass and a subclass.<br />

The term subclass reflects the concept of subset. The subset, or subclass, contains a<br />

unique set of properties for only certain objects within the set. The GrannySmith class in<br />

Figure 11-5 would only contain the properties that are unique to GrannySmith apples. A<br />

GrannySmith object would get the rest of the information about the properties it shares<br />

with all apples from the Apple superclass, and all the properties it has in common with all<br />

fruits from the Fruit superclass. In other words, it actually takes three classes to generate a<br />

single object representing a GrannySmith apple.<br />

An abstract class is a class that cannot create objects (cannot be instantiated). Any<br />

superclass that defines at least one operation that does not have a method is said to be<br />

abstract, or lacking a complete definition. Only a superclass can be abstract.<br />

A concrete class is a class that has a method for every operation, so it can create objects.<br />

The methods may be defined in the class or inherited from a superclass. All classes at the<br />

bottom of a generalization hierarchy must be concrete. Any superclass may be concrete.<br />

A discriminator is an attribute or rule that describes how I choose to identify the set of<br />

subclasses for a superclass. If I wanted to organize the information about types of cars, I<br />

could discriminate based on price range, manufacturer, engine size, fuel type, usage, or any<br />

number of other criteria. The discriminator I choose depends on the problem I am trying to<br />

solve. In Figure 11-6, I use a very common practice. I identify a set of predefined types<br />

(that is, type of fruit and varieties within types). Other times I might use a property of<br />

the objects themselves like size, price range, capacity, or age.<br />

Fruit<br />

Discriminator<br />

Fruit Type<br />

Apple Watermelon Orange<br />

Discriminator<br />

RedDelicious<br />

Fruit Variety<br />

GrannySmith<br />

Figure 11-6 Modeling generalization with discriminators<br />

The composition of a class reveals the possible discriminating properties. Classes define<br />

properties of objects such as attributes, operations, and association. These are the first<br />

three possible discriminating properties. If objects of the class share the same attributes,

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

Saved successfully!

Ooh no, something went wrong!