# UML --- ![](https://i.imgur.com/4REv1pz.png) --- ![image alt](https://remnote-user-data.s3.amazonaws.com/9xsloX-YS7JduGKavoOvOtb45hnwHiDof--4kHKpmImCJdNPyEKRD2OIrnbOsWQu0GRB3xcRZxI6ypIixF-597qYj8j1XEi1y3cZvXeHN4DoMLQbsygQISh_LjhGUttN.png) ## 依賴: Dependency 如果**修改一個 class 可能会造成另一個 class 的變化**, 那么這兩個 class 間就存在依賴關係。 --- 當你在程式裡使用**具體 class 的名稱**時,通常代表有依賴關係。 透過讓 code 依賴 interface 或抽象 class(而不是具体类), 可以降低其依賴程度。 --- 一般來說,UML 圖不会展示所有依賴——数量太多了。為了不讓這些依賴關係破壞 UML 圖,通常只會畫出那些對於表達想法來說重要的依賴關係。 --- ## 關連: Association 一個 object 使用另一個 object 或與另一個 object 進行互動的關係。 --- 在 UML 圖中, 關連會指向這個 object (Class) 所使用的 object (Class) 。 有可能存在雙向關聯 ---- ## 依賴 v.s 關聯 關聯可以視為一種特殊的依賴,這種依賴中,一個 object 可以總是可以access 箭頭所指 object,這樣的依賴關係不會發生在簡單的依賴關係中。 --- ``` php class Professor is field Student student //... method teach(Course c) is // ... this.student.remember(c.getKnowledge()) ``` 依賴(這裡的 `teach` 和 `course` 的關係) 在這裡的 `teach` method會接收一个来 自 `Course` 這個 Class 作爲参数。如果有人修改了 `Course` 的 `getKnowledge` 方法,像是修改方法名或加了非 optional 的參數時, 這段 code 就會崩潰。这就是依賴關係。 --- ``` php class Professor is field Student student //... method teach(Course c) is // ... this.student.remember(c.getKnowledge()) ``` 關聯(這裡的 teach 和 Student 的關係) 首先,如果 student 裡面的 `remember` method 被修改,那麼 Professor 的 code 也會被影響。在這裡我們確認 Professor 依賴 student。 ---- ``` php class Professor is field Student student //... method teach(Course c) is // ... this.student.remember(c.getKnowledge()) ``` 接著,我們看 Student 被使用的方式是 `field Student student` ,也就是說,所有屬於 ` Professor ` 的 method (像是這裡的 teach ),都可以 access Student,這時候 ` Professor ` 和 ` Student ` 的關係就不只是依賴,而是關聯了。 ---- ## 聚合: Aggregation 一種的特殊的關聯,專門指多個對象之間「一對多」、「多對多」或者「整體對部分」的關連關係。 --- 一般的關聯通常只適用 1v1 的一組 object,然而在聚合中,一個 object 通常會「擁有」一組其他对象,並且扮演**容器**或者**集合**的角色。 --- ## 組合: Composition 一種特殊類型的聚合,其中一個 object 由一個或者多個其他對象的 instance 構成。 組合和其他關聯的區別在於組合只能做為容器的一部分存在。 --- ## 實現: Implementation 由 Interface B 聲明方法,並由 A Class 定義的方法實踐。A 的 object 可以被以對待 B 的 object 的方式對待(一定有一樣的 method)。 Class A 依賴 Class B(interface)。 --- ## 繼承: Inheritance Class A (子類)繼承 Class B (父類)的 Interface 和 Implementation (實現),但是可以進行擴展。子類 (Class A)的 object 可以被以對待 父類 (Class B)的 object 的方式對待(一定有一樣的 method)。子類 (Class A) 依賴父類 (Class B)。 ---- - Summary - Dependency: Class А can be affected by changes in class B. - Association: Object А knows about object B. Class A depends on B. --- - Aggregation: Object А knows about object B, and consists of B. Class A depends on B - Composition: Object А knows about object B, consists of B, and manages B’s life cycle. Class A depends on B. --- - Implementation: Class А defines methods declared in interface B. Objects A can be treated as B. Class A depends on B. - Inheritance: Class А inherits interface and implementation of class B but can extend it. Objects A can be treated as B. Class A depends on B.
{"metaMigratedAt":"2023-06-15T18:13:42.460Z","metaMigratedFrom":"Content","title":"UML","breaks":true,"contributors":"[{\"id\":\"55656006-7069-4b3c-b452-8f557a1d2ca8\",\"add\":3056,\"del\":156}]"}
    440 views
   Owned this note