UML中M之间的关系

  • 最先看个人结论:
  • Association 联合
  • Aggregation 聚合
  • Composition 组合
  • Relationship
    • Association (has-a)
    • Directed Association
    • Aggregation (has-a + whole-part)
    • Composition (has-a + whole-part + ownership)
    • Dependency (references)
    • Generalization
    • Interface Realization

最先看个人结论:

Name 表现特点 形象举例
Association A 与 B 两者一直有关系 桌子与放在桌子上的电脑,一直有关系,但又没啥关系
Aggregation 在上层关系基础上,是整体与部分之间的关系,
即:A 是由 B 组成的
电脑与内存/CUP/主板等,缺一不可,电脑包括全部。
但与桌子不是这种关系,因为桌子不是由电脑组成的。
(非要拿一堆电脑摞成桌子也没办法 --_-- ! )
Composition 在上层关系基础上,是完全不可分割的关系,
即:A 不存在 B 也不能存在
程序窗口与窗口里的图片,
窗口销毁,图片自然也没有了
(这个只能拿程序举例,现实生活中的多数都不符合这个设定)
But !!!
巩义馍夹串该算~~
烧饼与串组成馍夹串,馍夹串被吃完了,不存在了,串与烧饼都没了
~~oh yeah ~~


以下内容转自 https://www.jianshu.com/p/1feee1c813d2

Association 联合

表示两个类之间存在一种交互关系,而它们本身并不存在谁包含谁的关系。

class Table {public: void support(Cup &cup) {}
};
class Cup {public: void decorate(Table &table) {}
};

Aggregation 聚合

表示类B是类A的一部分,或者说类A中含有类B,但与组合不同的是,类B的声明周期并不依赖与类A。类A被销毁了类B 仍然可以继续存在。
例如房间里的桌椅板凳在房间被拆了仍然可以继续存在。

class Bed {};
class Table {};
class Chair {};
class Room {public:Room(Bed *bed = nullptr,Chair *chairs = nullptr,Table *table = nullptr):bed_(bed),chairs_(chairs),table_(table) {}private:Bed *bed_;Chair *chairs_;Table *table_;
};

Composition 组合

组合表示类B是类A的一部分,并且类B的生命周期依赖与类A,如果类A被销毁了,那么类B也将随之被销毁。
例如房间是一栋房子的一部分,房子如果没有了,房间也就没有了。

class Room {};
class House {private:Room room;
};



这篇文章 讲的很好,比我的好,可以看!如果看不了,可以看我抄下来的文章《UML基础系列:类图》

Relationship

先上盗图:
1
1
Another example of Composition is Car and it’s part e.g. engines, wheels etc. Individual parts of the car can not function when a car is destroyed. While in the case of Aggregation, including object can exists without being part of the main object e.g. a Player which is part of a Team, can exist without a team and can become part of other teams as well.

Another example of Aggregation is Student in School class, when School closed, Student still exist and then can join another School or so. In UML notation, a composition is denoted by a filled diamond, while aggregation is denoted by an empty diamond, which shows their obvious difference in terms of strength of the relationship.

The composition is stronger than Aggregation. In Short, a relationship between two objects is referred as an association, and an association is known as composition when one object owns other while an association is known as aggregation when one object uses another object.2
大意是说:
Composition 的关系就像是:车,发动机,轮子等,车坏了,每一部都不能独立工作,但是Aggregation关系就像一个乐队与乐手,队没了,每个乐手离开后能加到其他队里,继续工作。
(比喻太过牵强,车的部件拆出来不能拼成一个新车?但在代下面的码里能体现要表达的意思)

另一个Aggregation的例子是学校里的学生,学校关门,学生仍然存在,还可以去其他辅导班。

类与类关系都可以简称:association。
不同的是:
在 composition 的关系中,一个类是拥有(owns)另一个类的。
在 Aggregation 的关系中,一个类是使用(uses) 另一个类的。

个人总结:如图中所示,Aggregation 关系不强,类之间独立的生命周期,但在Composition中,组成的大类没有了,各部件也就没了,具体例子如下单词解释与详细的代码说明

Association (has-a)

英文释义:

  1. an offical group of people who have joined together for a particular purpose
    synonym organization

  2. association(with somebody/something) a connection or relationship between peoople or organizations

  3. an idea or memory that is suggested by somebody/something; a mental connection between ideas

  4. a connection between things where one is caused by the other

    Example :
    I have a relationship with an object. Foo uses Bar

    Cpp Code :

    public class Foo { void Baz(Bar bar) {}
    };
    

Directed Association

Aggregation (has-a + whole-part)

英文释义:

  1. bula

    Example :
    I have an object which I’ve borrowed from someone else. When Foo dies, Bar may live on.
    Class Diagram :

    Cpp Code:

    public class Foo { private Bar* ptr_bar; Foo(Bar* p) { ptr_bar = p; }
    }
    

Composition (has-a + whole-part + ownership)

英文释义:

  1. the different parts which something is make of ; the way in which the different parts are organized

  2. a pice of music or art, or a poem

  3. the act of composing something

  4. the art of wwriting music

  5. a short text that is written as a school exercise; a short essay

  6. the arrangement of people or objects in a painting or photograph

    Example1 :
    I own an object and I am responsible for its lifetime, when Foo dies, so does Bar
    Class Diagram :

    Cpp Code :

    public class Foo {private Bar bar = new Bar();
    }
    

    Example2 :
    Class Diagram :

    Java Code :

     public class car{public class Car {//final will make sure engine is initializedprivate final Engine engine;  public Car(){engine  = new Engine();}
    }
    class Engine {private String type;
    }
    

Dependency (references)

英文释义:

  1. dependency (on/upon somebody/something) the state of relying on sb/sth for sth., especially when this is not normal or necessary

  2. a country, an area, etc. that is controlled by another country

    Example :

    Class Diagram :
    Code:

Generalization

英文释义:

  1. a general statement that is based on only a few facts or examples; the act of marking such statements

继承,不解释。
Example :

Class Diagram :
Code:

Interface Realization

英文释义:

  1. bula

接口实现,C++里没有,不解释。

<b>Example : </b>Class Diagram :
Code:


  1. 出自: https://stackoverflow.com/questions/885937/what-is-the-difference-between-association-aggregation-and-composition ↩︎ ↩︎

  2. 出自: https://javarevisited.blogspot.com/2014/02/ifference-between-association-vs-composition-vs-aggregation.html#ixzz5WzJ9jL3L ↩︎

UML ——从不懂到装懂之:类图关系 梳理相关推荐

  1. UML类图关系大全 and 报表的基本用法

    UML类图关系大全 1.关联 双向关联: C1-C2:指双方都知道对方的存在,都可以调用对方的公共属性和方法. 在 GOF的设计模式书上是这样描述的:虽然在分析阶段这种关系是适用的,但我们觉得它对于描 ...

  2. 【UML】UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)

    http://www.cnblogs.com/olvo/archive/2012/05/03/2481014.html 继承.实现.依赖.关联.聚合.组合的联系与区别 分别介绍这几种关系: 继承 指的 ...

  3. 【转】 UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合

    目录(?)[-] 继承 实现 依赖 关联 聚合 组合 类图及类图中的关系 继承.实现.依赖.关联.聚合.组合的联系与区别 分别介绍这几种关系: 继承 指的是一个类(称为子类.子接口)继承另外的一个类( ...

  4. UML类图关系表示方法

    本文转载: http://blog.csdn.net/fengsh998/article/details/8105631 分类: UML2012-10-24 10:18 1175人阅读 评论(0) 收 ...

  5. UML类图关系全面剖析[转]

    UML类图关系全面剖析      本文随便转载,请保留出处:http://blog.csdn.net/dylgsy/ UML的类图关系分为: 关联.聚合/组合.依赖.泛化(继承).而其中关联又分为双向 ...

  6. 【重学UML】UML类图关系及其对应代码

    文章目录 一.UML类图关系总览 二.依赖关系 1.关系描述 2.举例 三.关联关系 1.关系描述 2.举例 3.思考 三.聚合.组合关系--关联关系的两种形式 1.关系描述 2.两者关系对比 3.关 ...

  7. UML 之 C++类图关系全面剖析

    UML的类图关系分为: 关联.聚合/组合.依赖.泛化(继承).而其中关联又分为双向关联.单向关联.自身关联:下面就让我们一起来看看这些关系究竟是什么,以及它们的区别在哪里. 1.关联 双向关联: C1 ...

  8. 统一建模语言UML(四):领域模型和类图

    统一建模语言UML(四):领域模型和类图 领域模型 领域模型是对领域内概念类或现实世界中对象的可视化表示,也称为概念模型.是更为完整的业务模型的一个特例.从UML的表示法角度,领域模型被描述为一组没有 ...

  9. mysql中unl是什么健_UNL类图关系全面剖析

    UML的类图关系分为: 关联.聚合/组合.依赖.泛化(继承).而其中关联又分为双向关联.单向关联.自身关联:下面就让我们一起来看看这些关系究竟是什么,以及它们的区别在哪里. 1.关联 双向关联: C1 ...

最新文章

  1. 最早的神经元从何而来?海绵基因图谱揭示神经系统起源
  2. 计算机高二期末考试试题,高二年级计算机专业期末试题
  3. 安装配置JDK和Eclipse的步骤
  4. 【创业公司的机遇与挑战】如何在1年内从产品助理到产品高管?
  5. YTU 2586: 填空题B-字画鉴别
  6. php获取curl头_php中CURL请求头和响应头获取方法
  7. ros重置后地址_从零开始丨INDEMIND双目惯性模组ROS平台下实时ORB-SLAM记录教程
  8. chown –r mysql:mysql,mysql部署,操作及异常处理
  9. 事件对象使用方法大全(源码解析)
  10. oracle外部表迁移,海量数据迁移之外部表并行抽取
  11. 【学习笔记】深入理解Linux内核第三版 ——第二章 内存寻址
  12. mingw gcc mysql_Windows平台mingw编译器 mysql
  13. Android全局设置APP为黑白模式的两种方案
  14. 【日语】日文假名输入与键盘对应
  15. python登录注册程序编写_python写注册程序
  16. Windows NTSTATUS Values 进程终止消息标识符
  17. html 在线测试 鱼缸,新做的草缸教程,看1遍你也会做,鱼缸造景其实很简单
  18. 我要悄悄学习,做一个浪漫的程序员
  19. latex中自动生成参考文献
  20. 计算机二级考试主要学什么,计算机二级考试需要学习什么内容

热门文章

  1. 武汉新时标文化传媒有限公司橱窗和抖音小店的差别非常大
  2. 我的第一次实质性开源贡献——Apache IoTDB
  3. mindspore-使用modelzoo中的yolov4,在运行时报错
  4. 酷派手机权限开启方法
  5. 校园介绍网页10多个页面
  6. Qt插件开发总结5--主界面嵌入插件UI
  7. LSI MegaRAID SAS 9260-8i数据安全测试记录
  8. URI和URL的世纪争论
  9. WPF-单选MenuItem
  10. 宣传新农合医药政策,普及健康生活方式,了解最新医药资讯