TOMATOFAN 发表于 2011-12-22 08:53

UML 类图中的几种关系

<span class="Apple-style-span" style="color: rgb(51, 51, 51); font-size: 15px; line-height: 26px; "><p style="font-family: Ubuntu, UbuntuBeta !important; ">分 UML 类图中的几种关系&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; "><br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; UML 类图中的 ”关联关系(association) “、”聚合关系(aggregation) “、”合成关系 (compostion)“ 和”依赖关系 (dependency)“ 不是很容易区分清楚,《UML distilled》 对这几个关系也没有解释的特别清楚。近日翻阅《Java 与模式》,发现其中对这些关系有较为清晰的描述,特摘录如下:</p><p style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;关联关系 (association):</p><p style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; (1)关联关系是类与类之间的联结,它使一个类知道另一个类的属性和方法。<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp; &nbsp;(2)关联可以是双向的,也可以是单向的。双向的关联可以有两个箭头或者没有箭头,单向的关联有一个箭头。<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp; &nbsp;(3)在 Java 或 c++ 中,关联关系是通过使用成员变量来实现的。 class 徒弟&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp; &nbsp;{ };&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp; &nbsp;class 唐僧&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp; &nbsp; {&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp; &nbsp; protected:&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp; &nbsp;list&lt;徒弟&gt; tdlist;&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp; &nbsp; };&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/images/cppblog_com/api/association.jpg" border="0" alt="" width="293" height="153" style="font-family: Ubuntu, UbuntuBeta !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></p><p style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;聚合关系 (aggregation):</p><p style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp; &nbsp;1、聚合关系是关联关系的一种,是强的关联关系。<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; 2、聚合是整体和部分之间的关系,例如汽车由引擎、轮胎以及其它零件组成。<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; 3、聚合关系也是通过成员变量来实现的。但是,关联关系所涉及的两个类处在同一个层次上,而聚合关系中,两个类处于不同的层次上,一个代表整体,一个代表 部分。<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; 4、关联与聚合仅仅从 Java 或 C++ 语法上是无法分辨的,必须考察所涉及的类之间的逻辑关系。<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; class 引擎&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; {&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; };&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; class 轮胎&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; {&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; };&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; class 汽车&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; {&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; protected:&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; 引擎 engine;&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; 轮胎 tyre;&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; };&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/images/cppblog_com/api/aggregation.jpg" border="0" alt="" width="305" height="225" style="font-family: Ubuntu, UbuntuBeta !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></p><p style="font-family: Ubuntu, UbuntuBeta !important; ">合成关系 (composition):</p><p style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; 1、合成关系是关联关系的一种,是比聚合关系还要强的关系。<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; 2、它要求普通的聚合关系中代表整体的对象负责代表部分的对象的生命周期。<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; 1 class 肢&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; {&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; };&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; class 人&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; {&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; protected:&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; 肢 limb;&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; };</p><p style="font-family: Ubuntu, UbuntuBeta !important; "><img src="http://www.cppblog.com/images/cppblog_com/api/composition.jpg" border="0" alt="" width="299" height="157" style="font-family: Ubuntu, UbuntuBeta !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></p><p style="font-family: Ubuntu, UbuntuBeta !important; ">依赖关系 (dependency):</p><p style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp; &nbsp;1、依赖关系也是类与类之间的联结<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; 2、依赖总是单向的。<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp; &nbsp;3、依赖关系在 Java 或 C++ 语言中体现为局部变量、方法的参数或者对静态方法的调用。</p><p style="font-family: Ubuntu, UbuntuBeta !important; ">显示代码打印</p><p style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; public class Person<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; {&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; public void buy(Car car)&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp;&nbsp; { ... }&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; ">&nbsp; &nbsp;}&nbsp;<br style="font-family: Ubuntu, UbuntuBeta !important; "><br style="font-family: Ubuntu, UbuntuBeta !important; "><img src="http://www.cppblog.com/images/cppblog_com/api/dependency.jpg" border="0" alt="" width="304" height="145" style="font-family: Ubuntu, UbuntuBeta !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></p></span>
页: [1]
查看完整版本: UML 类图中的几种关系