Friday, August 17, 2007

Overview UML

Class Diagram
A class diagram is a diagram that shows classes, interfaces, and their relationships. The most basic element of a class diagram is a class. Figure 2.1 provides an example of a class, showing many of the features that a class can have within a class diagram.


Figure 2.1: Basic class diagram
Classes are drawn as rectangles. The rectangles can be divided into two or three compartments. The class rectangle shown in Figure 2.1 has three compartments: the top compartment contains the name of the class; the middle compartment lists the class’s variables; and the bottom compartment lists the class’s methods.

The symbols that precede each variable and method are called visibility indicators. The possible visibility indicators and their meanings are shown in Table 2.1

Table 2.1: Visibility Indicators Visibility Indicators
Meaning

+
Public

#
Protected

–
Private


The variables in the middle compartment are shown as

visibilityIndicator name : type
Therefore, the two variables shown in the class are private variables. The name of the first variable is instance and its type is AudioClipManager. The name of the second variable is prevClip and its type is AudioClip.

Though not shown in Figure 2.1, an initial value can be indicated for a variable by following the variable’s type with an equal (=) sign and the value like this:

shutDown:boolean = false
Notice that the first variable shown in the class is underlined. An underlined variable denotes a static variable. The same applies to methods, too; underlined methods denote static methods.

The methods in the bottom compartment are shown as

visibilityIndicator name ( formalParameters ) : returnType
The getInstance method shown in the class above returns an AudioClipManager object.

UML indicates a void method by leaving out the “: returnType" from a method to indicate that it does not return anything. Therefore, the stop method shown in the class in Figure 2.1 does not return any result.

A method’s formal parameters consist of a name and a type like this:

setLength(length:int)
If a method has multiple parameters, commas separate them like this:

setPosition(x:int, y:int)
Two of the methods in the class in Figure 2.1 are preceded by a word in guillemets, like this:

«constructor»
In a UML drawing, a word in guillemets is called a stereotype. A stereotype is used like an adjective to modify what comes after it. The constructor stereotype indicates that the methods following it are constructors. The misc stereotype indicates that the methods following it are regular methods. Additional uses for stereotypes are described later in this chapter.

One last element that appears in the class in Figure 2.1 is an ellipsis (...). If an ellipsis appears in the bottom compartment of a class, it means that the class has additional methods that the diagram does not show. If an ellipsis appears in the middle compartment of a class, it means that the class has additional variables that the diagram does not show.

Often, it is neither necessary nor helpful to show as many details of a class as were shown in the class in Figure 2.1. As shown in Figure 2.2, a class may be drawn with only two compartments: a top compartment, which contains the class’s name, and a bottom compartment, which shows the class’s methods. A two-compartment class means only that its variables are not shown; it does not mean that the class lacks variables.


Figure 2.2: Two-compartment class.
The visibility indicators may be omitted from methods and variables. A method or variable shown without a visibility indicator means that there is no indication of the method’s or variable’s visibility. It does not imply that the method or variable is public, protected, or private.

A method’s parameters may be omitted if their return values are also omitted. For example, the visibility indicators and method parameters are omitted from the class shown in Figure 2.3.


Figure 2.3: Simplified class diagram.
The simplest form of a class has just one compartment that contains the class name, as shown in Figure 2.4. A one-compartment representation of a class merely identifies the class; it provides no indication about what variables or methods the class has.


Figure 2.4: One-compartment class
Interfaces are drawn in a manner similar to classes. The difference is that the name in the top compartment is preceded by an interface stereotype. Figure 2.5 is an example of an interface.


Figure 2.5: Interface.
Classes and interfaces are important elements of class diagrams. Other elements of a class diagram show relationships between classes and interfaces. Figure 2.6 is a typical class diagram.


Figure 2.6: Class diagram.
The lines in Figure 2.6 indicate the relationships between the classes and an interface. A solid line with a closed hollow head, such as the one in Figure 2.7, indicates the relationship of a subclass that inherits from a superclass.


Figure 2.7: Subclass inherits from superclass.
The class diagram in Figure 2.6 shows the abstract class Product as the superclass of the ConcreteProduct class. You can tell that it is abstract because its name is italicized. You can tell that its methods are abstract because they, too, are italicized.

A similar sort of line is used to indicate that a class implements an interface. It is a dotted or dashed line with a closed head like the one in Figure 2.8.


Figure 2.8: Class implements an Interface.
The class diagram in Figure 2.6 shows that the Factory class implements the FactoryIF interface.

The other lines show other types of relationships between the classes and interface. UML calls these other types of relationships associations. With associations can appear a number of things that provide information about the nature of an association. Although the following items are optional, this book consistently uses them wherever it makes sense to do so.

Association Name. Somewhere around the middle of an association there may be an association name. The name of an association is always capitalized. At one end of the association name there may be a triangle to indicate the direction in which you should read the association.

Looking at Figure 2.6, you will see that the association between the Factory and ConcreteProduct classes has the name Creates.

Navigation Arrows. Arrowheads that may appear at the ends of an association are called navigation arrows. Navigation arrows indicate the direction in which you may navigate an association.

Looking at the association named Creates in Figure 2.6, you will see a navigation arrow pointing from the Factory class to the ConcreteProduct class. Because of the nature of creation, it seems clear that it means the Factory class is responsible for creating instances of the ConcreteProduct class.

The nature of some associations is less obvious. To make the nature of such associations clear, it may be necessary to supply additional information about the association. One common way to clarify the nature of an association is to name the role that each class plays in the association.

Role Name. To clarify the nature of an association, the name of the role each class plays in the association can appear at each end of an association, next to the corresponding class. Role names are always lowercase. That makes them easier to distinguish from association names, which are always capitalized.

In Figure 2.6, the CreationRequester class and the FactoryIF interface participate in an association named Requests-Creation. The CreationRequester class participates in that association in a role called requester. The FactoryIF interface participates in that association in a role called creator.

Multiplicity Indicator. Another helpful detail of an association is how many instances of each class participate in an occurrence of the association. A multiplicity indicator may appear at each end of an association to provide that information. A multiplicity indicator can be a simple number, like 0 or 1, or a range of numbers indicated like this:

0..2
An asterisk as the high value of a range means an unlimited number of occurrences. The multiplicity indicator 1..* means at least one instance; 0..* means any number of instances. A simple * is equivalent to 0..*. Looking at the multiplicity indicators in Figure 2.6, you see that each one of the associations in the drawing is a one-to-many relationship.

Figure 2.9 is a class diagram that shows a class with multiple subclasses. Though the drawing in Figure 2.9 is perfectly valid, UML allows a more aesthetically pleasing way to draw a class with multiple subclasses. You can combine the arrowheads as is shown in Figure 2.10. The diagram in Figure 2.10 is identical in meaning to the diagram in Figure 2.9.


Figure 2.9: Multiple inheritance arrows.

Figure 2.10: Single inheritance arrow.
Sometimes there is a need to convey more structure than is implied by a simple one-to-many relationship. The type of one-to-many relationship where one object contains a collection of other objects is called an aggregation. A hollow diamond at the end of the association indicates aggregation. The hollow diamond appears at the end of the association attached to the class that contains instances of the other class. The class diagram in Figure 2.11 shows an aggregation.


Figure 2.11: Aggregation.
The class diagram in Figure 2.11 shows a class named Message Manager. Each of its instances contains zero or more instances of a class named MIMEMsg.

UML has another notation to indicate a stronger relationship than aggregation. That relationship is called composite aggregation. For an aggregation to be composite, two conditions must be satisfied:

Aggregated instances must belong to only one composite at a time.

Some operations must propagate from the composite to its aggregated instances. For example, when a composite object is cloned, its clone method will typically clone the aggregated instances so that the cloned composite will own clones of the original aggregated instances.

Figure 2.12 is a class diagram that contains a composite aggregation.


Figure 2.12: Composite aggregation.
The class diagram in Figure 2.12 shows a Document class. Document objects can contain Paragraph objects; Paragraph objects can contain DocChar objects. Because of the composite aggregation, you know that Paragraph objects do not share DocChar objects and Document objects do not share Paragraph objects.

Some associations are indirect. Instead of classes being directly associated with each other, they are associated indirectly through a third class. Consider the class diagram in Figure 2.13.


Figure 2.13: Association class.
The association in Figure 2.13 shows that instances of the Cache class refer to instances of the Object class through an instance of the ObjectID class.

There is another use for ellipsis in a class diagram. Some class diagrams need to show that a class has a large or open-ended set of subclasses, while only showing a few subclasses as examples of the sort of subclasses that the class has. The class diagram in Figure 2.14 shows how ellipsis can be used to show just that.


Figure 2.14: Open-ended subclasses.
The class diagram in Figure 2.14 shows a class named DataQuery. The DataQuery class has subclasses named JDBCQuery, OracleQuery, Sybase Query, and an indefinite number of other classes that are indicated by the ellipsis.

An association between classes or interfaces implies a dependency that involves an object reference connecting two objects. Other types of dependencies are possible. A dashed line is used to indicate a dependency in the more general sense. Figure 2.15 shows an example of such a dependency.


Figure 2.15: Dependency.
The classes in a class diagram can be organized into packages. A package is drawn as a large rectangle above which lies a small rectangle containing the package name. The small and large rectangles are arranged to have an overall shape similar to that of a manila folder. The class diagram in Figure 2.16 contains a package.


Figure 2.16: Package.
Figure 2.16 shows a package named ServicePackage. A visibility indicator can precede the name of classes and interfaces that appear within a package. Public classes are accessible to classes outside of the package; private classes are not.

Sometimes there are aspects of a design that you cannot make sufficiently clear without a comment in a diagram. Comments in UML are drawn as a rectangle with its upper right corner turned down. Comments are attached to the diagram element to which they relate via a dashed line. The class diagram in Figure 2.17 contains a comment.


Figure 2.17: Private static classes.
Figure 2.17 shows the static class MilestoneMemento, which is a private member of the GameModel class. There is no standard way in UML to represent a static private member class. The diagram uses a stereotype as an extension to UML to indicate that the MilestoneMemento class is static. It uses an association to indicate that the MilestoneMemento is a private member of the GameModel class. To make the relationship even more clear, Figure 2.17 contains a comment about it.Class diagrams can include objects. Most of the objects in the class diagrams in this book are drawn as shown in Figure 2.18.


Figure 2.18: Object.
The object shown in Figure 2.18 is an instance of a class named Area. The underline tells you that it is an object. A name may appear to the left of the colon (:). The only significance of the name is that you can use it to identify the individual object.

Some diagrams indicate an object as just an empty rectangle. Obviously, blank objects cannot be used to identify any particular kind of object. However, they can be used in a diagram that shows a structure in which objects of unspecified types are connected. The class diagram in Figure 2.19 shows such a structure.


Figure 2.19: Blank objects.
The lines that connect two objects are not associations but links. Whereas links are connections between objects, associations are relationships between classes. A link is an occurrence of an association, just as an object is an instance of a class. Links can have association names, navigation arrows, and most of the other embellishments that associations can have. Because a link is a connection between two objects, it may not have multiplicity indicators or aggregation diamonds.

Some diagrams consist of just objects and links. Such diagrams are considered a kind of class diagram. However, there is a special name for this kind of diagram: object diagram. Figure 2.20 is an example of an object diagram.


Figure 2.20: Object diagram.

No comments: