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 ↩︎

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