Update 设计模式 - 空对象.md

小节标题改为中文,重新绘制类图,补充参考资料。
This commit is contained in:
CyC2018
2026-10-28 03:20:00 +08:00
parent 89cbdfb3f1
commit 8155102dd8
2 changed files with 9 additions and 7 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 135 KiB

+9 -7
View File
@@ -1,17 +1,14 @@
## 空对象(Null)
### Intent
### 意图
使用什么都不做的空对象来代替 NULL。
一个方法返回 NULL,意味着方法的调用端需要去检查返回值是否是 NULL,这么做会导致非常多的冗余的检查代码。并且如果某一个调用端忘记了做这个检查返回值,而直接使用返回的对象,那么就有可能抛出空指针异常。
### Class Diagram
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/22870bbe-898f-4c17-a31a-d7c5ee5d1c10.png"/> </div><br>
### Implementation
### 类图
<div align="center"> <img src="pics/dp-null-object.png" width="760px"/> </div><br>
### 实现
```java
public abstract class AbstractOperation {
abstract void request();
@@ -51,3 +48,8 @@ public class Client {
}
}
```
## 参考资料
- 弗里曼. Head First 设计模式 [M]. 中国电力出版社, 2007.
- Gamma E. 设计模式: 可复用面向对象软件的基础 [M]. 机械工业出版社, 2007.