diff --git a/notes/面向对象思想.md b/notes/面向对象思想.md index 381a812a..63f3b609 100644 --- a/notes/面向对象思想.md +++ b/notes/面向对象思想.md @@ -3,16 +3,16 @@ * [封装](#封装) * [继承](#继承) * [多态](#多态) -* [二、设计原则](#二设计原则) - * [S.O.L.I.D](#solid) - * [其他常见原则](#其他常见原则) -* [三、类图](#三类图) +* [二、类图](#二类图) * [泛化关系 (Generalization)](#泛化关系-generalization) * [实现关系 (Realization)](#实现关系-realization) * [聚合关系 (Aggregation)](#聚合关系-aggregation) * [组合关系 (Composition)](#组合关系-composition) * [关联关系 (Association)](#关联关系-association) * [依赖关系 (Dependency)](#依赖关系-dependency) +* [三、设计原则](#三设计原则) + * [S.O.L.I.D](#solid) + * [其他常见原则](#其他常见原则) * [参考资料](#参考资料) @@ -114,7 +114,148 @@ public class Music { } ``` -# 二、设计原则 +# 二、类图 + +以下类图使用 [PlantUML](https://www.planttext.com/) 绘制,更多语言及使用请参考:http://plantuml.com/ + +## 泛化关系 (Generalization) + +用来描述继承关系,在 Java 中使用 extends 关键字。 + +

+ +```text +@startuml + +title Generalization + +class Vihical +class Car +class Trunck + +Vihical <|-- Car +Vihical <|-- Trunck + +@enduml +``` + +## 实现关系 (Realization) + +用来实现一个接口,在 Java 中使用 implement 关键字。 + +

+ +```text +@startuml + +title Realization + +interface MoveBehavior +class Fly +class Run + +MoveBehavior <|.. Fly +MoveBehavior <|.. Run + +@enduml +``` + +## 聚合关系 (Aggregation) + +表示整体由部分组成,但是整体和部分不是强依赖的,整体不存在了部分还是会存在。 + +

+ +```text +@startuml + +title Aggregation + +class Computer +class Keyboard +class Mouse +class Screen + +Computer o-- Keyboard +Computer o-- Mouse +Computer o-- Screen + +@enduml +``` + +## 组合关系 (Composition) + +和聚合不同,组合中整体和部分是强依赖的,整体不存在了部分也不存在了。比如公司和部门,公司没了部门就不存在了。但是公司和员工就属于聚合关系了,因为公司没了员工还在。 + +

+ +```text +@startuml + +title Composition + +class Company +class DepartmentA +class DepartmentB + +Company *-- DepartmentA +Company *-- DepartmentB + +@enduml +``` + +## 关联关系 (Association) + +表示不同类对象之间有关联,这是一种静态关系,与运行过程的状态无关,在最开始就可以确定。因此也可以用 1 对 1、多对 1、多对多这种关联关系来表示。比如学生和学校就是一种关联关系,一个学校可以有很多学生,但是一个学生只属于一个学校,因此这是一种多对一的关系,在运行开始之前就可以确定。 + +

+ +```text +@startuml + +title Association + +class School +class Student + +School "1" - "n" Student + +@enduml +``` + +## 依赖关系 (Dependency) + +和关联关系不同的是,依赖关系是在运行过程中起作用的。A 类和 B 类是依赖关系主要有三种形式: + +- A 类是 B 类中的(某中方法的)局部变量; +- A 类是 B 类方法当中的一个参数; +- A 类向 B 类发送消息,从而影响 B 类发生变化; + +

+ +```text +@startuml + +title Dependency + +class Vihicle { + move(MoveBehavior) +} + +interface MoveBehavior { + move() +} + +note "MoveBehavior.move()" as N + +Vihicle ..> MoveBehavior + +Vihicle .. N + +@enduml +``` + +# 三、设计原则 ## S.O.L.I.D @@ -200,50 +341,6 @@ public class Music { 包之间的依赖关系都应该是稳定方向依赖的,包要依赖的包要比自己更具有稳定性。 - - -# 三、类图 - -## 泛化关系 (Generalization) - -用来描述继承关系,在 Java 中使用 extends 关键字。 - -

- -## 实现关系 (Realization) - -用来实现一个接口,在 Java 中使用 implement 关键字。 - -

- -## 聚合关系 (Aggregation) - -表示整体由部分组成,但是整体和部分不是强依赖的,整体不存在了部分还是会存在。 - -

- -## 组合关系 (Composition) - -和聚合不同,组合中整体和部分是强依赖的,整体不存在了部分也不存在了。比如公司和部门,公司没了部门就不存在了。但是公司和员工就属于聚合关系了,因为公司没了员工还在。 - -

- -## 关联关系 (Association) - -表示不同类对象之间有关联,这是一种静态关系,与运行过程的状态无关,在最开始就可以确定。因此也可以用 1 对 1、多对 1、多对多这种关联关系来表示。比如学生和学校就是一种关联关系,一个学校可以有很多学生,但是一个学生只属于一个学校,因此这是一种多对一的关系,在运行开始之前就可以确定。 - -

- -## 依赖关系 (Dependency) - -和关联关系不同的是,依赖关系是在运行过程中起作用的。A 类和 B 类是依赖关系主要有三种形式: - -- A 类是 B 类中的(某中方法的)局部变量; -- A 类是 B 类方法当中的一个参数; -- A 类向 B 类发送消息,从而影响 B 类发生变化; - -

- # 参考资料 - Java 编程思想 diff --git a/pics/LOun2W9134NxVugmbJPp15d4LalxC4O.png b/pics/LOun2W9134NxVugmbJPp15d4LalxC4O.png new file mode 100644 index 00000000..188460a5 Binary files /dev/null and b/pics/LOun2W9134NxVugmbJPp15d4LalxC4O.png differ diff --git a/pics/SoWkIImgAStDuU8goIp9ILK8IatCoQn.png b/pics/SoWkIImgAStDuU8goIp9ILK8IatCoQn.png new file mode 100644 index 00000000..da66da14 Binary files /dev/null and b/pics/SoWkIImgAStDuU8goIp9ILK8IatCoQn.png differ diff --git a/pics/SoWkIImgAStDuU8goIp9ILLmB2xEJyv.png b/pics/SoWkIImgAStDuU8goIp9ILLmB2xEJyv.png new file mode 100644 index 00000000..e5ae5234 Binary files /dev/null and b/pics/SoWkIImgAStDuU8goIp9ILLmB2xEJyv.png differ diff --git a/pics/SoWkIImgAStDuU8goIp9ILLmJ4ylIar.png b/pics/SoWkIImgAStDuU8goIp9ILLmJ4ylIar.png new file mode 100644 index 00000000..e31398b6 Binary files /dev/null and b/pics/SoWkIImgAStDuU8goIp9ILLmJ4ylIar.png differ diff --git a/pics/SoWkIImgAStDuU8goIp9ILLmJyrBBKh.png b/pics/SoWkIImgAStDuU8goIp9ILLmJyrBBKh.png new file mode 100644 index 00000000..055d995e Binary files /dev/null and b/pics/SoWkIImgAStDuU8goIp9ILLmJyrBBKh.png differ diff --git a/pics/SoWkIImgAStDuU8goIp9ILLmpiyjo2_.png b/pics/SoWkIImgAStDuU8goIp9ILLmpiyjo2_.png new file mode 100644 index 00000000..37f32c4c Binary files /dev/null and b/pics/SoWkIImgAStDuU8goIp9ILLmpiyjo2_.png differ