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

最先看个人结论:

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

这篇文章 讲的很好,比我的好,可以看!如果看不了,可以看我抄下来的文章《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 initialized
        private 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 ↩︎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章