auto commit
This commit is contained in:
parent
9164742ff0
commit
c18c85d51d
195
notes/面向对象思想.md
195
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)
|
||||
* [其他常见原则](#其他常见原则)
|
||||
* [参考资料](#参考资料)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
@ -114,7 +114,148 @@ public class Music {
|
||||
}
|
||||
```
|
||||
|
||||
# 二、设计原则
|
||||
# 二、类图
|
||||
|
||||
以下类图使用 [PlantUML](https://www.planttext.com/) 绘制,更多语言及使用请参考:http://plantuml.com/
|
||||
|
||||
## 泛化关系 (Generalization)
|
||||
|
||||
用来描述继承关系,在 Java 中使用 extends 关键字。
|
||||
|
||||
<div align="center"> <img src="../pics//SoWkIImgAStDuU8goIp9ILLmJyrBBKh.png"/> </div><br>
|
||||
|
||||
```text
|
||||
@startuml
|
||||
|
||||
title Generalization
|
||||
|
||||
class Vihical
|
||||
class Car
|
||||
class Trunck
|
||||
|
||||
Vihical <|-- Car
|
||||
Vihical <|-- Trunck
|
||||
|
||||
@enduml
|
||||
```
|
||||
|
||||
## 实现关系 (Realization)
|
||||
|
||||
用来实现一个接口,在 Java 中使用 implement 关键字。
|
||||
|
||||
<div align="center"> <img src="../pics//SoWkIImgAStDuU8goIp9ILK8IatCoQn.png"/> </div><br>
|
||||
|
||||
```text
|
||||
@startuml
|
||||
|
||||
title Realization
|
||||
|
||||
interface MoveBehavior
|
||||
class Fly
|
||||
class Run
|
||||
|
||||
MoveBehavior <|.. Fly
|
||||
MoveBehavior <|.. Run
|
||||
|
||||
@enduml
|
||||
```
|
||||
|
||||
## 聚合关系 (Aggregation)
|
||||
|
||||
表示整体由部分组成,但是整体和部分不是强依赖的,整体不存在了部分还是会存在。
|
||||
|
||||
<div align="center"> <img src="../pics//SoWkIImgAStDuU8goIp9ILLmJ4ylIar.png"/> </div><br>
|
||||
|
||||
```text
|
||||
@startuml
|
||||
|
||||
title Aggregation
|
||||
|
||||
class Computer
|
||||
class Keyboard
|
||||
class Mouse
|
||||
class Screen
|
||||
|
||||
Computer o-- Keyboard
|
||||
Computer o-- Mouse
|
||||
Computer o-- Screen
|
||||
|
||||
@enduml
|
||||
```
|
||||
|
||||
## 组合关系 (Composition)
|
||||
|
||||
和聚合不同,组合中整体和部分是强依赖的,整体不存在了部分也不存在了。比如公司和部门,公司没了部门就不存在了。但是公司和员工就属于聚合关系了,因为公司没了员工还在。
|
||||
|
||||
<div align="center"> <img src="../pics//SoWkIImgAStDuU8goIp9ILLmpiyjo2_.png"/> </div><br>
|
||||
|
||||
```text
|
||||
@startuml
|
||||
|
||||
title Composition
|
||||
|
||||
class Company
|
||||
class DepartmentA
|
||||
class DepartmentB
|
||||
|
||||
Company *-- DepartmentA
|
||||
Company *-- DepartmentB
|
||||
|
||||
@enduml
|
||||
```
|
||||
|
||||
## 关联关系 (Association)
|
||||
|
||||
表示不同类对象之间有关联,这是一种静态关系,与运行过程的状态无关,在最开始就可以确定。因此也可以用 1 对 1、多对 1、多对多这种关联关系来表示。比如学生和学校就是一种关联关系,一个学校可以有很多学生,但是一个学生只属于一个学校,因此这是一种多对一的关系,在运行开始之前就可以确定。
|
||||
|
||||
<div align="center"> <img src="../pics//SoWkIImgAStDuU8goIp9ILLmB2xEJyv.png"/> </div><br>
|
||||
|
||||
```text
|
||||
@startuml
|
||||
|
||||
title Association
|
||||
|
||||
class School
|
||||
class Student
|
||||
|
||||
School "1" - "n" Student
|
||||
|
||||
@enduml
|
||||
```
|
||||
|
||||
## 依赖关系 (Dependency)
|
||||
|
||||
和关联关系不同的是,依赖关系是在运行过程中起作用的。A 类和 B 类是依赖关系主要有三种形式:
|
||||
|
||||
- A 类是 B 类中的(某中方法的)局部变量;
|
||||
- A 类是 B 类方法当中的一个参数;
|
||||
- A 类向 B 类发送消息,从而影响 B 类发生变化;
|
||||
|
||||
<div align="center"> <img src="../pics//LOun2W9134NxVugmbJPp15d4LalxC4O.png"/> </div><br>
|
||||
|
||||
```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 关键字。
|
||||
|
||||
<div align="center"> <img src="../pics//5341d726-ffde-4d2a-a000-46597bcc9c5a.png"/> </div><br>
|
||||
|
||||
## 实现关系 (Realization)
|
||||
|
||||
用来实现一个接口,在 Java 中使用 implement 关键字。
|
||||
|
||||
<div align="center"> <img src="../pics//123bdf81-1ef5-48a9-a08c-2db97057b4d2.png"/> </div><br>
|
||||
|
||||
## 聚合关系 (Aggregation)
|
||||
|
||||
表示整体由部分组成,但是整体和部分不是强依赖的,整体不存在了部分还是会存在。
|
||||
|
||||
<div align="center"> <img src="../pics//1be8b4b0-cc7a-44d7-9c77-85be37b76f7d.png"/> </div><br>
|
||||
|
||||
## 组合关系 (Composition)
|
||||
|
||||
和聚合不同,组合中整体和部分是强依赖的,整体不存在了部分也不存在了。比如公司和部门,公司没了部门就不存在了。但是公司和员工就属于聚合关系了,因为公司没了员工还在。
|
||||
|
||||
<div align="center"> <img src="../pics//eb4a7007-d437-4740-865d-672973effe25.png"/> </div><br>
|
||||
|
||||
## 关联关系 (Association)
|
||||
|
||||
表示不同类对象之间有关联,这是一种静态关系,与运行过程的状态无关,在最开始就可以确定。因此也可以用 1 对 1、多对 1、多对多这种关联关系来表示。比如学生和学校就是一种关联关系,一个学校可以有很多学生,但是一个学生只属于一个学校,因此这是一种多对一的关系,在运行开始之前就可以确定。
|
||||
|
||||
<div align="center"> <img src="../pics//518f16f2-a9f7-499a-98e1-f1dbb37b5a9a.png"/> </div><br>
|
||||
|
||||
## 依赖关系 (Dependency)
|
||||
|
||||
和关联关系不同的是,依赖关系是在运行过程中起作用的。A 类和 B 类是依赖关系主要有三种形式:
|
||||
|
||||
- A 类是 B 类中的(某中方法的)局部变量;
|
||||
- A 类是 B 类方法当中的一个参数;
|
||||
- A 类向 B 类发送消息,从而影响 B 类发生变化;
|
||||
|
||||
<div align="center"> <img src="../pics//c7d4956c-9988-4a10-a704-28fdae7f3d28.png"/> </div><br>
|
||||
|
||||
# 参考资料
|
||||
|
||||
- Java 编程思想
|
||||
|
BIN
pics/LOun2W9134NxVugmbJPp15d4LalxC4O.png
Normal file
BIN
pics/LOun2W9134NxVugmbJPp15d4LalxC4O.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 10 KiB |
BIN
pics/SoWkIImgAStDuU8goIp9ILK8IatCoQn.png
Normal file
BIN
pics/SoWkIImgAStDuU8goIp9ILK8IatCoQn.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.0 KiB |
BIN
pics/SoWkIImgAStDuU8goIp9ILLmB2xEJyv.png
Normal file
BIN
pics/SoWkIImgAStDuU8goIp9ILLmB2xEJyv.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.0 KiB |
BIN
pics/SoWkIImgAStDuU8goIp9ILLmJ4ylIar.png
Normal file
BIN
pics/SoWkIImgAStDuU8goIp9ILLmJ4ylIar.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.0 KiB |
BIN
pics/SoWkIImgAStDuU8goIp9ILLmJyrBBKh.png
Normal file
BIN
pics/SoWkIImgAStDuU8goIp9ILLmJyrBBKh.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.0 KiB |
BIN
pics/SoWkIImgAStDuU8goIp9ILLmpiyjo2_.png
Normal file
BIN
pics/SoWkIImgAStDuU8goIp9ILLmpiyjo2_.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.0 KiB |
Loading…
x
Reference in New Issue
Block a user