mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-31 19:13:42 +00:00
Update 设计模式 - 简单工厂.md
调整使用简单工厂的场景表述,补充工厂方法模式对比以及与策略模式结合的用法,并对参考资料进行文字校对。
This commit is contained in:
@@ -31,7 +31,7 @@ public class ConcreteProduct2 implements Product {
|
||||
}
|
||||
```
|
||||
|
||||
以下的 Client 类包含了实例化的代码,这是一种错误的实现。如果在客户类中存在这种实例化代码,就需要考虑将代码放到简单工厂中。
|
||||
以下的 Client 类包含了实例化的代码。如果多个客户类中都有这种选择子类的代码,就需要考虑将代码放到简单工厂中。
|
||||
|
||||
```java
|
||||
public class Client {
|
||||
@@ -78,7 +78,11 @@ public class Client {
|
||||
}
|
||||
```
|
||||
|
||||
需要注意的是,新增产品时仍要修改 createProduct() 中的判断,[工厂方法](设计模式%20-%20工厂方法.md)把创建交给子类来解决这个问题。
|
||||
|
||||
简单工厂常和策略一起使用:工厂决定取得哪一个对象,策略决定执行哪一种算法,见[与简单工厂和 Spring 容器结合](设计模式%20-%20策略.md#2-与简单工厂和-spring-容器结合)。
|
||||
|
||||
## 参考资料
|
||||
|
||||
- 弗里曼. Head First 设计模式 [M]. 中国电力出版社, 2007.
|
||||
- Gamma E. 设计模式: 可复用面向对象软件的基础 [M]. 机械工业出版社, 2007.
|
||||
- Gamma E. 设计模式:可复用面向对象软件的基础 [M]. 机械工业出版社, 2007.
|
||||
|
||||
Reference in New Issue
Block a user