UML 类与类之间的关系

关联
Association represents the ability of one instance to send a message to another instance. This is typically implemented with a pointer or reference instance variable, although it might also be implemented as a method argument, or the creation of a local variable.

[Example:]

|A|----------->|B|

class A
{
private:B* itsB;
};

聚合
Aggregation [...] is the typical whole/part relationship. This is exactly the same as an association with the exception that instances cannot have cyclic aggregation relationships (i.e. a part cannot contain its whole).
[Example:]

|Node|<>-------->|Node|

class Node
{
private:vector<Node*> itsNodes;
};
The fact that this is aggregation means that the instances of Node cannot form a cycle. Thus, this is a Tree of Nodes not a graph of Nodes.

Composition [...] is exactly like Aggregation except that the lifetime of the 'part' is controlled by the 'whole'. This control may be direct or transitive. That is, the 'whole' may take direct responsibility for creating or destroying the 'part', or it may accept an already created part, and later pass it on to some other whole that assumes responsibility for it.

[Example:]

|Car|<#>-------->|Carburetor|
|汽车|<#>------->|汽化器|
class Car
{
public:virtual ~Car() {delete itsCarb;}
private:Carburetor* itsCarb
};

 

聚集是关联中的一种,聚集对象由部分对象组成;
组合又是一种特殊的聚集。

在一个组合对象中,部分对象只能作为组成对象的一部分与组合对象同时存在。即是说,组合是“当聚集对象和它的组成对象之间是具有强关联的一种特殊聚集”,组合对象的关键特征是部分对象只能存在于组合对象之中,并且部分体的寿命可能比组合体短,但组合体消亡,部分体也必然消亡。

我们举例来说明:聚集电脑可以由显示器、CPU、主板、硬盘、键盘、鼠标等聚集而成。在这种关系里面,各个组成部分是可以分拆开独立存在的。

组合衬衣是由主体、衣领、袖口、衣袖、钮扣等组合而成。在这种关系里面,衣袖或者衣领等如果拆分开来并不能算是一个独立的主体,不具有价值了。

树是由树干、树根、树枝、树叶等组合而成的。这里面树叶可以先于树消亡,但如果树被砍掉,那么树叶也没有存在价值了。

多数参考资料上都是上面叙述的那样解释的,不过我觉得理解起来还是有些难度:电脑我把他砸了,电脑坏了,那你说各个组成部分到底是一起消亡了还是没消亡呢?也许应该说至少没有被砸坏的部分还是可以独立使用的;可若这样说的话,那我衬衣破了个洞,我要把衬衣裁减为各个部分,那其他部分是不是也可以拿去拼装为

我的理解:“把衬衣裁减为各个部分”,这各个部分便是没有定义的部分,不像电脑里的主板硬盘是已经定义的对象,拆了可以作为对象。(如果衬衣裁了成有定义的部分,便可以看作聚合?这样理解对否?)


Keeping it Simple,
Aggregation or Composition depends up on life time of the child object.
聚合和组合决定于它的子对象的生命周期?

If the child object cannot exist beyond the lifetime of its parent then the relationship is composite(Strong relationship)
强关系,组合:如果在父生命周期下子对象不能存在,这种关系是组合。

If the child object can exist beyond the lifetime of its parent, then the relationship is aggregtion.

However, at a particular moment the child object can be controlled only by a single parent(ie. Unshared), and it is the responsibility of the parent object to transfer the control of the child object to some other instance while its destruction.

 

又一论述:
Associations indicate that the two classes have a relationship as discussed above.

Composition and aggregation indicate something more about the relationship:

Composition indicates that one class belongs to the other.  A polygon is made up of several points.  If the polygon is destroyed, so are the points.

Aggregation is similar to composition, but is a less rigorous way of grouping things.  An order is made up of several products, but a product continues to exist even if the order is destroyed.
UML 类与类之间的关系

类与类之间的关系对于理解面向对象具有很重要的作用,以前在面试的时候也经常被问到这个问题,在这里我就介绍一下。

类与类之间存在以下关系:

     (1)泛化(Generalization)
     (2)关联(Association)
     (3)依赖(Dependency)
     (4)聚合(Aggregation)

UML图与应用代码例子:

1.泛化(Generalization)

[泛化]

表示类与类之间的继承关系,接口与接口之间的继承关系,或类对接口的实现关系。一般化的关系是从子类指向父类的,与继承或实现的方法相反。

[具体表现]

父类 父类实例=new 子类()

[UML图](图1.1)

图1.1 Animal类与Tiger类,Dog类的泛化关系

[代码表现]

  1. class Animal{}   
  2. class Tiger extends Animal{}   
  3. public class Test   
  4. {   
  5.     public void test()   
  6.      {   
  7.          Animal a=new Tiger();   
  8.      }   
  9. }

2.依赖(Dependency)

[依赖]

对于两个相对独立的对象,当一个对象负责构造另一个对象的实例,或者依赖另一个对象的服务时,这两个对象之间主要体现为依赖关系。

[具体表现]

依赖关系表现在局部变量,方法的参数,以及对静态方法的调用

[现实例子]

比如说你要去拧螺丝,你是不是要借助(也就是依赖)螺丝刀(Screwdriver)来帮助你完成拧螺丝(screw)的工作

[UML表现](图1.2)

图1.2 Person类与Screwdriver类的依赖关系

[代码表现]

  1. public class Person{   
  2.     /** 拧螺丝 */  
  3.     public void screw(Screwdriver screwdriver){   
  4.          screwdriver.screw();   
  5.      }   
  6. }  

3.关联(Association)

[关联]

对于两个相对独立的对象,当一个对象的实例与另一个对象的一些特定实例存在固定的对应关系时,这两个对象之间为关联关系。

[具体表现]

关联关系是使用实例变量来实现

[现实例子]

比如客户和订单,每个订单对应特定的客户,每个客户对应一些特定的订单;再例如公司和员工,每个公司对应一些特定的员工,每个员工对应一特定的公司

[UML图] (图1.3)

图1.3 公司和员工的关联关系

[代码表现]

  1. public class Company{   
  2.     private Employee employee;   
  3.     public Employee getEmployee(){   
  4.         return employee;   
  5.      }   
  6.     public void setEmployee(Employee employee){   
  7.         this.employee=employee;   
  8.      }   
  9.     //公司运作   
  10.     public void run(){   
  11.          employee.startWorking();   
  12.      }   
  13. }  

(4)聚合(Aggregation)

[聚合]

当对象A被加入到对象B中,成为对象B的组成部分时,对象B和对象A之间为聚集关系。聚合是关联关系的一种,是较强的关联关系,强调的是整体与部分之间的关系。

[具体表现]

与关联关系一样,聚合关系也是通过实例变量来实现这样关系的。关联关系和聚合关系来语法上是没办法区分的,从语义上才能更好的区分两者的区别。

[关联与聚合的区别]

(1)关联关系所涉及的两个对象是处在同一个层次上的。比如人和自行车就是一种关联关系,而不是聚合关系,因为人不是由自行车组成的。

聚合关系涉及的两个对象处于不平等的层次上,一个代表整体,一个代表部分。比如电脑和它的显示器、键盘、主板以及内存就是聚集关系,因为主板是电脑的组成部分。

(2) 对于具有聚集关系(尤其是强聚集关系)的两个对象,整体对象会制约它的组成对象的生命周期。部分类的对象不能单独存在,它的生命周期依赖于整体类的对象的 生命周期,当整体消失,部分也就随之消失。比如张三的电脑被偷了,那么电脑的所有组件也不存在了,除非张三事先把一些电脑的组件(比如硬盘和内存)拆了下 来。

[UML图](图1.4)

图1.3 电脑和组件的聚合关系

[代码表现]

  1. public class Computer{   
  2.     private CPU cpu;   
  3.     public CPU getCPU(){   
  4.         return cpu;   
  5.      }   
  6.     public void setCPU(CPU cpu){   
  7.         this.cpu=cpu;   
  8.      }   
  9.     //开启电脑   
  10.     public void start(){   
  11.         //cpu运作   
  12.          cpu.run();   
  13.      }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值