auto commit
This commit is contained in:
parent
95cfcc2a08
commit
69d14774ef
|
@ -20,7 +20,7 @@
|
|||
* [10. 访问者模式](#10-访问者模式)
|
||||
* [11. 空对象模式](#11-空对象模式)
|
||||
* [四、结构型](#四结构型)
|
||||
* [1. 适配器](#1-适配器)
|
||||
* [1. 适配器模式](#1-适配器模式)
|
||||
* [2. 桥接模式](#2-桥接模式)
|
||||
* [3. 组合模式](#3-组合模式)
|
||||
* [4. 装饰者模式](#4-装饰者模式)
|
||||
|
@ -50,7 +50,7 @@
|
|||
|
||||
私有构造函数保证了不能通过构造函数来创建对象实例,只能通过公有静态函数返回唯一的私有静态变量。
|
||||
|
||||
<div align="center"> <img src="../pics//db54db2f-82b2-4222-8d63-e49a8a7fc966.png"/> </div><br>
|
||||
<div align="center"> <img src="../pics//562f2844-d77c-40e0-887a-28a7128abd42.png"/> </div><br>
|
||||
|
||||
### 实现
|
||||
|
||||
|
@ -203,7 +203,7 @@ public class Singleton implements Serializable{
|
|||
|
||||
简单工厂不是设计模式,更像是一种编程习惯。它把实例化的操作单独放到一个类中,这个类就成为简单工厂类,让简单工厂类来决定应该用哪个子类来实例化。
|
||||
|
||||
<div align="center"> <img src="../pics//d7f6dec1-02b6-4969-b3ab-e01ee78659b9.png"/> </div><br>
|
||||
<div align="center"> <img src="../pics//c79da808-0f28-4a36-bc04-33ccc5b83c13.png"/> </div><br>
|
||||
|
||||
这样做能把客户类和具体子类的实现解耦,客户类不再需要知道有哪些子类以及应当实例化哪个子类。因为客户类往往有多个,如果不使用简单工厂,所有的客户类都要知道所有子类的细节。而且一旦子类发生改变,例如增加子类,那么所有的客户类都要进行修改。
|
||||
|
||||
|
@ -281,7 +281,7 @@ public class Client {
|
|||
|
||||
下图中,Factory 有一个 doSomethind() 方法,这个方法需要用到一组产品对象,这组产品对象由 factoryMethod() 方法创建。该方法是抽象的,需要由子类去实现。
|
||||
|
||||
<div align="center"> <img src="../pics//bf0ff9fc-467e-4a3f-8922-115ba2c55bde.png"/> </div><br>
|
||||
<div align="center"> <img src="../pics//1818e141-8700-4026-99f7-900a545875f5.png"/> </div><br>
|
||||
|
||||
### 实现
|
||||
|
||||
|
@ -337,7 +337,7 @@ public class ConcreteFactory2 extends Factory {
|
|||
|
||||
### 类图
|
||||
|
||||
<div align="center"> <img src="../pics//14cfe8d4-e31b-49e0-ac6a-6f4f7aa06ab6.png"/> </div><br>
|
||||
<div align="center"> <img src="../pics//e82e0454-4ece-435e-945f-90bc0d37bf44.png"/> </div><br>
|
||||
|
||||
抽象工厂模式创建的是对象家族,也就是很多对象而不是一个对象,并且这些对象是相关的,也就是说必须一起创建出来。而工厂模式只是用于创建一个对象,这和抽象工厂模式有很大不同。
|
||||
|
||||
|
@ -584,12 +584,70 @@ public class Client {
|
|||
|
||||
# 四、结构型
|
||||
|
||||
## 1. 适配器
|
||||
## 1. 适配器模式
|
||||
|
||||
### 意图
|
||||
|
||||
把一个类接口转换成另一个用户需要的接口。
|
||||
|
||||
<div align="center"> <img src="../pics//3d5b828e-5c4d-48d8-a440-281e4a8e1c92.png"/> </div><br>
|
||||
|
||||
### 类图
|
||||
|
||||
<div align="center"> <img src="../pics//0f754c1d-b5cb-48cd-90e0-4a86034290a1.png"/> </div><br>
|
||||
|
||||
### 实现
|
||||
|
||||
鸭子(Duck)和火鸡(Turkey)拥有不同的叫声,Duck 的叫声调用 quack() 方法,而 Turkey 调用 gobble() 方法。
|
||||
|
||||
要求将 Turkey 的 gobble() 方法适配成 Duck 的 quack() 方法,从而让火鸡冒充鸭子!
|
||||
|
||||
```java
|
||||
public interface Duck {
|
||||
void quack();
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public interface Turkey {
|
||||
void gobble();
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class WildTurkey implements Turkey {
|
||||
@Override
|
||||
public void gobble() {
|
||||
System.out.println("gobble!");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class TurkeyAdapter implements Duck {
|
||||
Turkey turkey;
|
||||
|
||||
public TurkeyAdapter(Turkey turkey) {
|
||||
this.turkey = turkey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void quack() {
|
||||
turkey.gobble();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class Client {
|
||||
public static void main(String[] args) {
|
||||
Turkey turkey = new WildTurkey();
|
||||
Duck duck = new TurkeyAdapter(turkey);
|
||||
duck.quack();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### JDK
|
||||
|
||||
- [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29)
|
||||
|
|
BIN
pics/0f754c1d-b5cb-48cd-90e0-4a86034290a1.png
Normal file
BIN
pics/0f754c1d-b5cb-48cd-90e0-4a86034290a1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
BIN
pics/1818e141-8700-4026-99f7-900a545875f5.png
Normal file
BIN
pics/1818e141-8700-4026-99f7-900a545875f5.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
BIN
pics/3d5b828e-5c4d-48d8-a440-281e4a8e1c92.png
Normal file
BIN
pics/3d5b828e-5c4d-48d8-a440-281e4a8e1c92.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
BIN
pics/562f2844-d77c-40e0-887a-28a7128abd42.png
Normal file
BIN
pics/562f2844-d77c-40e0-887a-28a7128abd42.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.0 KiB |
BIN
pics/c79da808-0f28-4a36-bc04-33ccc5b83c13.png
Normal file
BIN
pics/c79da808-0f28-4a36-bc04-33ccc5b83c13.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
BIN
pics/e82e0454-4ece-435e-945f-90bc0d37bf44.png
Normal file
BIN
pics/e82e0454-4ece-435e-945f-90bc0d37bf44.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 33 KiB |
Loading…
Reference in New Issue
Block a user