世界是普遍联系的,因此程序世界中的类,也不可能是孤立的。UML为我们定义了它们之间的关系,就是:依赖、关联、聚合、组合还有泛化。
泛化关系比较好理解,就是表示类之间的继承关系。容易混淆的是依赖、关联、聚合和组合的关系。这里做一些甄别:
1、依赖和关联的颠倒颠
在网上查找了一下依赖和关联的区别,有说“关联本身即是一种依赖”,亦有说“依赖是一种弱关联”,其实说来说去是一档子事。依赖和关联都是说一个类用到了另一个类。其区别在于一个是使用,一个是拥有。
依赖:具有某种偶然性。比如说我要过河,没有桥怎么办,我就去借来一条小船渡过去。我与小船的关系仅仅是使用(借用)的关系。表现在代码上,为依赖的类的某个方法以被依赖的类作为其参数。或者是class A 的某个方法创造了 class B 的实例抑或对class B的静态方法的调用。如果A依赖于B,那意味着B的变化可能要求A也发生变化;
这是uml图表示的依赖关系:
代码表现:
以上是转自,http://zjzkiss.cnblogs.com/
Highlighter1_19_116_Open_Image" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif">
Highlighter1_19_116_Closed_Image" style="display: none" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif">
public class Person {2
Highlighter1_29_37_Open_Image" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif">
Highlighter1_29_37_Closed_Image" style="display: none" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif">
/**划船*/3
Highlighter1_76_110_Open_Image" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif">
Highlighter1_76_110_Closed_Image" style="display: none" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif">
publicvoidoarage(Boatboat){4boat.oarage();
5}
6}
7
关联:有名的客户和订单的关系以及公司和员工的关系,都是关联关系。还有就是我和我的单车的例子,他们都是一种“拥有”的关系。表现在代码上,就是一个类包含另一个类的实例,通常表现为被关联类以类属性的形式出现在关联类的类定义中,也可以表现为关联类引用了一个类型为被关联类的全局变量。关联可以使单向的,也可以使双向的。
从网上找到的公司和员工的UML图和代码 :
公司和员工的关联关系
Highlighter1_20_326_Open_Image" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif">
Highlighter1_20_326_Closed_Image" style="display: none" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif">
public class Company {2privateEmployeeemployee;
3
Highlighter1_94_133_Open_Image" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif">
Highlighter1_94_133_Closed_Image" style="display: none" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif">
publicEmployeegetEmployee(){4returnemployee;
5}
6
Highlighter1_185_231_Open_Image" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif">
Highlighter1_185_231_Closed_Image" style="display: none" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif">
publicvoidsetEmployee(Employeeemployee){7this.employee=employee;
8}
9//公司运作
10
Highlighter1_273_320_Open_Image" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif">
Highlighter1_273_320_Closed_Image" style="display: none" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif">
publicvoidrun(){11employee.startWorking();
12}
13}
14
可见依赖于与关联亦有动静之别,关联的类“静态”地引用了被关联类的实例变量,而依赖的偶然性也正说明了它的动态性。
2、聚合与组合同出而异体
聚合与组合其实都是关联的特例,都是整体和部分的关系。他们的区别在于聚合的两个对象之间是可分离的,他们具有各自的生命周期。而组合往往表现为一种唇齿相依的关系。
聚合:一种容纳或曰包含的关系,如同机场和飞机,汽车和轮胎的关系。其实仔细想想,前面的公司和员工的关系也有聚合的味道在里面。
组合:也可称之为强聚合,整体和部分是不可分的,整体的生命周期结束时也就是部分的生命周期到头时。很有名的就是桌子和桌子腿的关系。
聚合的UML图:
组合的UML图:
然而,聚合与组合的代码表现形式是一样的,都可以表现为以下的形式,它们仅仅具有语义上的区别。
Highlighter1_21_285_Open_Image" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif">
Highlighter1_21_285_Closed_Image" style="display: none" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif">
public class Computer {2privatecpucpu;
3
Highlighter1_75_109_Open_Image" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif">
Highlighter1_75_109_Closed_Image" style="display: none" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif">
publiccpugetcpu(){4returncpu;
5}
6
Highlighter1_146_182_Open_Image" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif">
Highlighter1_146_182_Closed_Image" style="display: none" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif">
publicvoidsetcpu(cpucpu){7this.cpu=cpu;
8}
9//开启电脑
10
Highlighter1_226_279_Open_Image" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif">
Highlighter1_226_279_Closed_Image" style="display: none" alt="" src="http://img.jb51.cc/vcimg/static/loading.png" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif">
publicvoidstart(){11//cpu运作
12cpu.run();
13}
14}
15