首页 > 代码库 > UML基本架构建模--关联的术语和概念(二)

UML基本架构建模--关联的术语和概念(二)


 

Associations

联合

Anassociationis 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. Its 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 link to other objects of the same class. An association that connects exactly two classes is called a binary association. Although its not as common, you can have associations that connect more than two classes; these are calledn-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. Beyond this basic form, there are four adornments that apply to associations.

联合是一个结构关系,它描述一个事物的对象连接另一个事物的对象.给定一个关联连接两个类,你能建立一个类的对象和另一个类的对象之间的联系.一个联合的一端从一个类出发而另一端又回到这个类的关联是符合规则的.连接两个类的联合被称为二元联合.尽管不是很常见,但是可以有连接超过两个类的联合;这被称为n级联合.图形显示上,联合表现为一条连接相同或不同的类的实线.在你想显示结构关系时使用联合.除了基本形式,还有四个修饰适用于联合.

 

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

技术分享

名称:一个联合可以有名称.使用这个名称以描述关系的特征,以便对它的含义没有歧义.你能通过带有方向的三角形给名称提供方向,以指出从哪边开始读名称,如图5-4显示的.

 

Note:Although an association may have a name, you typically dont need to include one if you explicitly provide end names for the association. If you have more than one association connecting the same classes, it is necessary to use either association names or association end names to distinguish them. If an association has more than one end on the same class, it is necessary to use association end names to distinguish the ends. If there is only one association between a pair of classes, some modelers omit the names, but it is better to provide them to make the purpose of the association clear.

备注:尽管联合可能有名称,但如果你明确地为联合两端提供名称,那么通常就不需要再包含一个.如果有超过一个联合关联连接同一个类,有必要使用联合名称和联合端名称以示区分,如果一对类之间只有一个联合,有些模型会省略名称,但最好别那么做,以便清晰联合的意图.

 

RoleWhen 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 anend name (in UML 1, it was called arole name). In Figure 5-5, the classPerson playing the role ofemployee is associated with the classCompanyplaying the role ofemployer.

角色:当一个类加入到一个联合中,应该有一个特定的角色以使用这个关系;一个角色面对的是联合关联中远端的那个类,代表的是联合关联中近端的这个类.在一个联合关联里,你可以明确地为类的使用者--角色命名.由联合关联的一端扮演的角色被称为端名(在UML1.0中,它被称之为角色名).在图5-5中,这个类扮演着职员角色联合到公司这个类所扮演的雇主角色中.

技术分享

Note:The same class can play the same or different roles in other associations.

备注:同样的类能在其它的联合中扮演同样的或不同的角色.

 

Note:An attribute may be regarded as a one-way association owned by a class. The attribute name corresponds to the end name on the association end away from the class.

备注:属性可以被视为一个类所拥有的单向联合.属性名称与这个联合的端名(离这个类更远的那个端点)一致.

 

MultiplicityAn association represents a structural relationship among objects. In many modeling situations, its important for you to state how many objects may be connected across an instance of an association. Thishow many is called the multiplicity of an associations 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).

For example, in Figure 5-6, each company object has as employee one or more person objects (multiplicity 1..*); each person object has as employer zero or more company objects (multiplicity *, which is equivalent to 0..*)

多态:一个联合表示对象之间的结构关系.在大多数的建模情况下,重要的是你要声明多少个对象可以连接一个联合的一个实例.这里的多少被称为一个联合角色的多态.它表现为一个整数范围以指定相关对象集的可能大小.被书写为最小值和最大值的表达式,两个值可能一样;用两个小数点将最小值和最大值分开.当你为一个联合的远端那个类表述一个多态性时,你就为近端这个类的每一个对象指定在近端存在多少个对象.对象的数量必须在给定的范围之内.你可以用(1)表示只有一个多态,用(0..1)表示有0个或是一个多态,用(0..*)表示许多,或是(1..*)表示一个或更多.你还可以给定一个整数范围(如2..5),甚至是表达为一个精确的数字(如,3,这等价于3..3).

 举个例子,在图5-6中显示的,每个公司对象有一个或更多个人对象做为雇员(多态性1..*);每个人对象有0个或多个公司对象做为雇主(多态性 *,等价于0..*).

技术分享

 

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

聚合:两个类之间的等价联合表现的是对等的结构关系,也就是两个类在概念上属于同一层次,没有谁比谁更重要.有时你想构建整体/部分关系模型,其中一个类表现更大的事物(整体),它由更小的事物组成(部分).这一类的关系被称之为聚合,它表现的是拥有的关系,意思是一个整体的对象拥有部分对象.聚合是一种特殊的联合,它被描述为在等价联合的基础上,在表示整体的那端附加一个空心的菱形,如图5-7所示.

技术分享

 

Note:The meaning of this simple form of aggregation is entirely conceptual. The open diamond distinguishes thewhole from the part, no more, no less. This means that simple aggregation does not change the meaning of navigation across the association between the whole and its parts, nor does it link the lifetimes of the whole and its parts. See the section on composition in Chapter10 for a tighter form of aggregation.

备注:这个聚合的简单形式的意思是完全概念上的.明显的菱形正好将部分整体区分开来.这意味着简单的聚合不能改变在整体与部分之间的联合所具有的导航含义,也没有和整体与部分的生命周期相联系.第10章节部分有更严密的聚合形式介绍.

 

UML基本架构建模--关联的术语和概念(二)