mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-29 15:13:44 +00:00
Update 面向对象思想.md
重新绘制 UML 类图,更新示例中的类名。
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 131 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 104 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 107 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 109 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 91 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 112 KiB |
+16
-16
@@ -137,25 +137,25 @@ Percussion is playing...
|
||||
|
||||
## 二、类图
|
||||
|
||||
以下类图使用 [PlantUML](https://www.planttext.com/) 绘制,更多语法及使用请参考:http://plantuml.com/ 。
|
||||
每张类图后附有对应的 [PlantUML](https://www.planttext.com/) 代码,更多语法及使用请参考:http://plantuml.com/ 。
|
||||
|
||||
### 泛化关系 (Generalization)
|
||||
|
||||
用来描述继承关系,在 Java 中使用 extends 关键字。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/c0874e0a-dba3-467e-9c86-dd9313e0843e.jpg" width="180px"> </div><br>
|
||||
<div align="center"> <img src="pics/uml-generalization.png" width="640px"/> </div><br>
|
||||
|
||||
```text
|
||||
@startuml
|
||||
|
||||
title Generalization
|
||||
|
||||
class Vihical
|
||||
class Vehicle
|
||||
class Car
|
||||
class Trunck
|
||||
class Truck
|
||||
|
||||
Vihical <|-- Car
|
||||
Vihical <|-- Trunck
|
||||
Vehicle <|-- Car
|
||||
Vehicle <|-- Truck
|
||||
|
||||
@enduml
|
||||
```
|
||||
@@ -164,7 +164,7 @@ Vihical <|-- Trunck
|
||||
|
||||
用来实现一个接口,在 Java 中使用 implements 关键字。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/83d466bd-946b-4430-854a-cf7b0696d4c8.jpg" width="170px"> </div><br>
|
||||
<div align="center"> <img src="pics/uml-realization.png" width="640px"/> </div><br>
|
||||
|
||||
```text
|
||||
@startuml
|
||||
@@ -185,7 +185,7 @@ MoveBehavior <|.. Run
|
||||
|
||||
表示整体由部分组成,但是整体和部分不是强依赖的,整体不存在了部分还是会存在。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/a0ce43b7-afa8-4397-a96e-5c12a070f2ae.jpg" width="300px"> </div><br>
|
||||
<div align="center"> <img src="pics/uml-aggregation.png" width="640px"/> </div><br>
|
||||
|
||||
```text
|
||||
@startuml
|
||||
@@ -208,7 +208,7 @@ Computer o-- Screen
|
||||
|
||||
和聚合不同,组合中整体和部分是强依赖的,整体不存在了部分也不存在了。比如公司和部门,公司没了部门就不存在了。但是公司和员工就属于聚合关系了,因为公司没了员工还在。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/6a88a398-c494-41f5-bb62-9f7fb811df7c.jpg" width="280px"> </div><br>
|
||||
<div align="center"> <img src="pics/uml-composition.png" width="640px"/> </div><br>
|
||||
|
||||
```text
|
||||
@startuml
|
||||
@@ -229,7 +229,7 @@ Company *-- DepartmentB
|
||||
|
||||
表示不同类对象之间有关联,这是一种静态关系,与运行过程的状态无关,在最开始就可以确定。因此也可以用 1 对 1、多对 1、多对多这种关联关系来表示。比如学生和学校就是一种关联关系,一个学校可以有很多学生,但是一个学生只属于一个学校,因此这是一种多对一的关系,在运行开始之前就可以确定。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/a3e4dc62-0da5-4d22-94f2-140078281812.jpg" width="200px"> </div><br>
|
||||
<div align="center"> <img src="pics/uml-association.png" width="640px"/> </div><br>
|
||||
|
||||
```text
|
||||
@startuml
|
||||
@@ -239,7 +239,7 @@ title Association
|
||||
class School
|
||||
class Student
|
||||
|
||||
School "1" - "n" Student
|
||||
School "1" - "*" Student
|
||||
|
||||
@enduml
|
||||
```
|
||||
@@ -252,14 +252,14 @@ School "1" - "n" Student
|
||||
- A 类是 B 类方法的参数;
|
||||
- A 类向 B 类发送消息,从而影响 B 类发生变化。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/379444c9-f1d1-45cd-b7aa-b0c18427d388.jpg" width="330px"> </div><br>
|
||||
<div align="center"> <img src="pics/uml-dependency.png" width="640px"/> </div><br>
|
||||
|
||||
```text
|
||||
@startuml
|
||||
|
||||
title Dependency
|
||||
|
||||
class Vihicle {
|
||||
class Vehicle {
|
||||
move(MoveBehavior)
|
||||
}
|
||||
|
||||
@@ -269,9 +269,9 @@ interface MoveBehavior {
|
||||
|
||||
note "MoveBehavior.move()" as N
|
||||
|
||||
Vihicle ..> MoveBehavior
|
||||
Vehicle ..> MoveBehavior
|
||||
|
||||
Vihicle .. N
|
||||
Vehicle .. N
|
||||
|
||||
@enduml
|
||||
```
|
||||
@@ -352,7 +352,7 @@ Vihicle .. N
|
||||
|
||||
#### 3. 共同封闭原则
|
||||
|
||||
一起修改的类,应该组合在一起(同一个包里)。如果必须修改应用程序里的代码,我们希望所有的修改都发生在一个包里(修改关闭),而不是遍布在很多包里。
|
||||
一起修改的类,应该组合在一起(同一个包里)。如果必须修改应用程序里的代码,希望所有的修改都发生在一个包里(修改关闭),而不是遍布在很多包里。
|
||||
|
||||
#### 4. 稳定抽象原则
|
||||
|
||||
|
||||
Reference in New Issue
Block a user