When you build abstractions, you'll discover that very few of your classes stand alone. Instead, most of them collaborate with others in a number of ways. Therefore, when you model a system, not only must you identify the things that form the vocabulary of your system, you must also model how these things stand in relation to one another. 抽象和类不是单独存在的, 是为了与其他类相互协作, 相互关联。
In object-oriented modeling, there are three kinds of relationships that are especially important: dependencies, which represent using relationships among classes (including refinement, trace, and bind relationships); generalizations, which link generalized classes to their specializations; and associations, which represent structural relationships among objects. Each of these relationships provides a different way of combining your abstractions. 三个重要关系,依赖,关联和泛化
-
Dependencies are using relationships. For example, pipes depend on the water heater to heat the water they carry. 依赖是是一种使用的关系
-
Associations are structural relationships among instances. For example, rooms consist of walls and other things; walls themselves may have embedded doors and windows; pipes may pass through walls. 关联是一种在实例中结构性的, 组成的关系
-
Generalizations connect generalized classes to more-specialized ones in what is known as subclass/superclass or child/parent relationships. For example, a picture window is a kind of window with very large, fixed panes; a patio window is a kind of window with panes that open side to side. 泛化是把通用的类联系到更专有的类中, 就是我们所知道的父子关系
Dependencies
A dependency is a relationship that states that one thing (for example, class Window) uses the information and services of another thing (for example, class Event), but not necessarily the reverse. Graphically, a dependency is rendered as a dashed directed line, directed to the thing being depended on. Choose dependencies when you want to show one thing using another. 依赖是声明一样事物使用另外一个事物的信息或者服务的一种关系, 但是这种关系不能反转, 用虚点指向被依赖的对象。
Most often, you will use dependencies between classes to show that one class uses operations from another class or it uses variables or arguments typed by the other class; see Figure 5-2. This is very much a using relationshipif the used class changes, the operation of the other class may be affected as well, because the used class may now present a different interface or behavior. In the UML you can also create dependencies among many other things, especially notes and packages. 可以这样想一个方法的输入输出参数,或者方法实现内部使用到其他类, 则可以认为这是依赖关系, 代码表现形式看 :o
Generalizations
A generalization is a relationship between a general kind of thing (called the superclass or parent) and a more specific kind of thing (called the subclass or child). Generalization is sometimes called an "is-a-kind-of" relationship: one thing (like the class BayWindow) is-a-kind-of a more general thing (for example, the class Window). An objects of the child class may be used for a variable or parameter typed by the parent, but not the reverse. In other words, generalization means that the child is substitutable for a declaration of the parent. A child inherits the properties of its parents, especially their attributes and operations. Oftenbut not alwaysthe child has attributes and operations in addition to those found in its parents. An implementation of an operation in a child overrides an implementation of the same operation of the parent; this is known as polymorphism. To be the same, two operations must have the same signature (same name and parameters). Graphically, generalization is rendered as a solid directed line with a large unfilled triangular arrowhead, pointing to the parent, as shown in Figure 5-3. Use generalizations when you want to show parent/child relationships. 泛化是基于一般事物和指定的事物的关系, 有时我们称其为"is-a-kind-of" 继承关系, 泛化意味着子类可以替换父类声明,子类继承了父类的属性,特殊是他们的属性和操作,子类中一个操作的实现可重写父类中同一个操作, 这个就是多态, 为了一致,两个操作要有相同的名字和参数,使用带有没有填充的三角箭头的实线指向父类即是泛化。
Associations
An association is a structural relationship that specifies that objects of one thing are connected to objects of another. Given an association connecting two classes, you can relate objects of one class to objects of the other class. It's quite legal to have both ends of an association circle back to the same class. This means that, given an object of the class, you can link to other objects of the same class. An association that connects exactly two classes is called a binary association. Although it's not as common, you can have associations that connect more than two classes; these are called n-ary associations. Graphically, an association is rendered as a solid line connecting the same or different classes. Use associations when you want to show structural relationships. 关联是一种结构性的关系, 它声明一些对象连接到另外一些对象的关系。 我粗略认为一个类包含另外一个类作为属性。。
Name
An association can have a name, and you use that name to describe the nature of the relationship. So that there is no ambiguity about its meaning, you can give a direction to the name by providing a direction triangle that points in the direction you intend to read the name, as shown in Figure 5-4.
Star UML里面使用directed association表示。
Role
When a class participates in an association, it has a specific role that it plays in that relationship; a role is just the face the class at the far end of the association presents to the class at the near end of the association. You can explicitly name the role a class plays in an association. The role played by an end of an association is called an end name (in UML1, it was called a role name). In Figure 5-5, the class Person playing the role of employee is associated with the class Company playing the role of employer.
Star UML原来也可以画:0。
Multiplicity
An association represents a structural relationship among objects. In many modeling situations, it's important for you to state how many objects may be connected across an instance of an association. This "how many" is called the multiplicity of an association's role. It represents a range of integers specifying the possible size of the set of related objects. It is written as an expression with a minimum and maximum value, which may be the same; two dots are used to separate the minimum and maximum values. When you state a multiplicity at the far end of an association, you are specifying that, for each object of the class at the near end, how many objects at the near end may exist. The number of objects must be in the given range. You can show a multiplicity of exactly one (1), zero or one (0..1), many (0..*), or one or more (1..*). You can give an integer range (such as 2..5). You can even state an exact number (for example, 3, which is equivalent to 3..3).
Aggregation
A plain association between two classes represents a structural relationship between peers, meaning that both classes are conceptually at the same level, no one more important than the other. Sometimes you will want to model a "whole/part" relationship, in which one class represents a larger thing (the "whole"), which consists of smaller things (the "parts"). This kind of relationship is called aggregation, which represents a "has-a" relationship, meaning that an object of the whole has objects of the part. Aggregation is really just a special kind of association and is specified by adorning a plain association with an unfilled diamond at the whole end, as shown in Figure 5-7. 聚合是一种强关联,强调的是整体和部分的关系,has-a 的关系。
Hints and Tips
When you model relationships in the UML,
-
Use dependencies only when the relationship you are modeling is not structural.只有在你的模型是非结构的才使用依赖关系。
-
Use generalization only when you have an "is-a-kind-of" relationship; multiple inheritance can often be replaced with aggregation.当关系是is a kind of时才使用泛化, 多重继承经常可以用聚合代替(难道就是多用组合少用继承的pattern?)
-
Beware of introducing cyclical generalization relationships. 小心别引进循环的泛化关系, java里面写 A extends B{} B extends A {} 提示错误是cycle detected: a cycle exists in the type hierarchy between B and A.
-
Keep your generalization relationships generally balanced; inheritance lattices should not be too deep (more than five levels or so should be questioned) nor too wide (instead, look for the possibility of intermediate abstract classes). 保持泛化的均衡,继承不能太深,超过5级就要考虑一下是否有问题啦,too wide? (是不是说不要太多重继承?)
-
Use associations primarily where there are structural relationships among objects. Do not use them to show transient relationships such as parameters or local variables of procedures. 在表明透明的关系例如程序中的参数,局部变量就不要使用关联。
When you draw a relationship in the UML,
-
Use either rectilinear or oblique lines consistently. Rectilinear lines give a visual cue that emphasizes the connections among related things all pointing to one common thing. Oblique lines are often more space-efficient in complex diagrams. Using both kinds of lines in one diagram is useful for drawing attention to different groups of relationships. 直线,斜线。。
-
Avoid lines that cross unless absolutely necessary. 线不要交叉
-
Show only those relationships that are necessary to understand a particular grouping of things. Superfluous relationships (especially redundant associations) should be avoided. 过多关联是要避免的