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. Linux下的samba服务配置详解
  2. 最短路径生成树计数+最短路径生成树
  3. anaconda 安装pytorch_conda上安装PyTorch
  4. Spring开发--Bean配置实例讲解
  5. python制作桌面小程序_微信小程序在线制作:快速生成一个餐饮小程序
  6. FreeChart柱状图中如何取消柱子的倒影
  7. 全能视频播放器:OmniPlayer for Mac(1.4.6)
  8. 图像取证:源识别和伪造检测(Image Forensics: source identification and tampering detection)
  9. STM32数据手册中的ADC12、ADC123
  10. 数据分析--统计分析模型
  11. Excel如何制作下拉列表
  12. airpodspro窃听模式_AirPodsPro怎么切换模式
  13. 浅谈char类型范围
  14. 「产品读书」俞军产品方法论
  15. Mac 禁止 GlobalProtect运行
  16. 快速应用计算机制作pop,9计算机练习题
  17. linux 触控板 不识别,linux – 未检测到触控板
  18. android 定点定时任务,Android定时任务
  19. DOO-SABIN 细分正方体(1)利用邻接矩阵表示
  20. 网易云音乐 推荐算法

热门文章

  1. 非世而恶利,自讬於无为,此非士之情也。
  2. python模拟点击手机_Python Appium 滑动、点击等操作
  3. 用js写一个能自动回复消息的机器人
  4. 怎么用matlab编写quad8算法,quad8 是 matlab 中调用( )公式的命令
  5. 禾穗漫读 | 与自己和解:治愈你内心的内在小孩
  6. c语言中含负数的随机数,c-打印随机数将返回负数. (/ dev / urandom)
  7. WPF 制作侧边栏菜单之MenuItem
  8. 【记录】Android禁止多窗口模式
  9. 原生微信小程序引入公共方法(js文件)@令狐张豪
  10. OSChina 周六乱弹 —— 有人要提亲吗?