CS-Notes/docs/notes/设计模式.md

3072 lines
80 KiB
Java
Raw Normal View History

2019-04-21 10:36:08 +08:00
<!-- GFM-TOC -->
2019-03-27 20:57:37 +08:00
* [概述](#一概述)
* [创建型](#二创建型)
* [1. 单例Singleton](#1-单例singleton)
* [2. 简单工厂Simple Factory](#2-简单工厂simple-factory)
* [3. 工厂方法Factory Method](#3-工厂方法factory-method)
* [4. 抽象工厂Abstract Factory](#4-抽象工厂abstract-factory)
* [5. 生成器Builder](#5-生成器builder)
* [6. 原型模式Prototype](#6-原型模式prototype)
* [行为型](#三行为型)
* [1. 责任链Chain Of Responsibility](#1-责任链chain-of-responsibility)
* [2. 命令Command](#2-命令command)
* [3. 解释器Interpreter](#3-解释器interpreter)
* [4. 迭代器Iterator](#4-迭代器iterator)
* [5. 中介者Mediator](#5-中介者mediator)
* [6. 备忘录Memento](#6-备忘录memento)
* [7. 观察者Observer](#7-观察者observer)
* [8. 状态State](#8-状态state)
* [9. 策略Strategy](#9-策略strategy)
* [10. 模板方法Template Method](#10-模板方法template-method)
* [11. 访问者Visitor](#11-访问者visitor)
* [12. 空对象Null](#12-空对象null)
* [结构型](#四结构型)
* [1. 适配器Adapter](#1-适配器adapter)
* [2. 桥接Bridge](#2-桥接bridge)
* [3. 组合Composite](#3-组合composite)
* [4. 装饰Decorator](#4-装饰decorator)
* [5. 外观Facade](#5-外观facade)
* [6. 享元Flyweight](#6-享元flyweight)
* [7. 代理Proxy](#7-代理proxy)
* [参考资料](#参考资料)
2019-04-21 10:36:08 +08:00
<!-- GFM-TOC -->
2019-03-27 20:57:37 +08:00
# 概述
2018-04-06 22:46:59 +08:00
2018-06-20 10:21:08 +08:00
设计模式是解决问题的方案学习现有的设计模式可以做到经验复用
2018-04-06 22:46:59 +08:00
拥有设计模式词汇在沟通时就能用更少的词汇来讨论并且不需要了解底层细节
2019-03-27 20:57:37 +08:00
# 创建型
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
## 1. 单例Singleton
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-04-06 22:46:59 +08:00
2018-06-04 11:48:12 +08:00
确保一个类只有一个实例并提供该实例的全局访问点
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-04-06 22:46:59 +08:00
使用一个私有构造函数一个私有静态变量以及一个公有静态函数来实现
私有构造函数保证了不能通过构造函数来创建对象实例只能通过公有静态函数返回唯一的私有静态变量
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/eca1f422-8381-409b-ad04-98ef39ae38ba.png"/> </div><br>
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
#### 懒汉式-线程不安全
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
以下实现中私有静态变量 uniqueInstance 被延迟实例化这样做的好处是如果没有用到该类那么就不会实例化 uniqueInstance从而节约资源
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
这个实现在多线程环境下是不安全的如果多个线程能够同时进入 `if (uniqueInstance == null)` 并且此时 uniqueInstance null那么会有多个线程执行 `uniqueInstance = new Singleton();` 语句这将导致实例化多次 uniqueInstance
2018-04-06 22:46:59 +08:00
```java
2019-03-27 20:57:37 +08:00
public class Singleton {
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
private static Singleton uniqueInstance;
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
private Singleton() {
}
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
public static Singleton getUniqueInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
2018-04-06 22:46:59 +08:00
}
```
2019-03-27 20:57:37 +08:00
#### 饿汉式-线程安全
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
线程不安全问题主要是由于 uniqueInstance 被实例化多次采取直接实例化 uniqueInstance 的方式就不会产生线程不安全问题
2018-04-06 22:46:59 +08:00
2018-08-25 23:12:21 +08:00
但是直接实例化的方式也丢失了延迟实例化带来的节约资源的好处
2018-04-06 22:46:59 +08:00
```java
2019-03-27 20:57:37 +08:00
private static Singleton uniqueInstance = new Singleton();
2018-04-06 22:46:59 +08:00
```
2019-03-27 20:57:37 +08:00
#### 懒汉式-线程安全
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
只需要对 getUniqueInstance() 方法加锁那么在一个时间点只能有一个线程能够进入该方法从而避免了实例化多次 uniqueInstance
2018-08-12 22:53:28 +08:00
2019-03-27 20:57:37 +08:00
但是当一个线程进入该方法之后其它试图进入该方法的线程都必须等待即使 uniqueInstance 已经被实例化了这会让线程阻塞时间过长因此该方法有性能问题不推荐使用
2018-04-06 22:46:59 +08:00
```java
2019-03-27 20:57:37 +08:00
public static synchronized Singleton getUniqueInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
2018-08-25 23:12:21 +08:00
}
2018-04-06 22:46:59 +08:00
```
2019-03-27 20:57:37 +08:00
#### 双重校验锁-线程安全
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
uniqueInstance 只需要被实例化一次之后就可以直接使用了加锁操作只需要对实例化那部分的代码进行只有当 uniqueInstance 没有被实例化时才需要进行加锁
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
双重校验锁先判断 uniqueInstance 是否已经被实例化如果没有被实例化那么才对实例化语句进行加锁
2018-04-06 22:46:59 +08:00
```java
2019-03-27 20:57:37 +08:00
public class Singleton {
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
private volatile static Singleton uniqueInstance;
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
private Singleton() {
}
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
public static Singleton getUniqueInstance() {
if (uniqueInstance == null) {
synchronized (Singleton.class) {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
}
}
return uniqueInstance;
}
2018-04-06 22:46:59 +08:00
}
```
2019-06-09 17:04:10 +08:00
考虑下面的实现也就是只使用了一个 if 语句 uniqueInstance == null 的情况下如果两个线程都执行了 if 语句那么两个线程都会进入 if 语句块内虽然在 if 语句块内有加锁操作但是两个线程都会执行 `uniqueInstance = new Singleton();` 这条语句只是先后的问题那么就会进行两次实例化因此必须使用双重校验锁也就是需要使用两个 if 语句第一个 if 语句用来避免 uniqueInstance 已经被实例化之后的加锁操作而第二个 if 语句进行了加锁所以只能有一个线程进入就不会出现 uniqueInstance == null 时两个线程同时进行实例化操作
2018-04-06 22:46:59 +08:00
```java
2019-03-27 20:57:37 +08:00
if (uniqueInstance == null) {
synchronized (Singleton.class) {
uniqueInstance = new Singleton();
}
2018-04-06 22:46:59 +08:00
}
```
2019-03-27 20:57:37 +08:00
uniqueInstance 采用 volatile 关键字修饰也是很有必要的 `uniqueInstance = new Singleton();` 这段代码其实是分为三步执行
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
1. uniqueInstance 分配内存空间
2. 初始化 uniqueInstance
3. uniqueInstance 指向分配的内存地址
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
但是由于 JVM 具有指令重排的特性执行顺序有可能变成 1>3>2指令重排在单线程环境下不会出现问题但是在多线程环境下会导致一个线程获得还没有初始化的实例例如线程 T<sub>1</sub> 执行了 1 3此时 T<sub>2</sub> 调用 getUniqueInstance() 后发现 uniqueInstance 不为空因此返回 uniqueInstance但此时 uniqueInstance 还未被初始化
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
使用 volatile 可以禁止 JVM 的指令重排保证在多线程环境下也能正常运行
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
#### 静态内部类实现
2019-06-09 17:04:10 +08:00
Singleton 类被加载时静态内部类 SingletonHolder 没有被加载进内存只有当调用 `getUniqueInstance()` 方法从而触发 `SingletonHolder.INSTANCE` SingletonHolder 才会被加载此时初始化 INSTANCE 实例并且 JVM 能确保 INSTANCE 只被实例化一次
2019-03-27 20:57:37 +08:00
这种方式不仅具有延迟初始化的好处而且由 JVM 提供了对线程安全的支持
2018-07-11 23:26:24 +08:00
2018-08-12 22:53:28 +08:00
```java
2019-03-27 20:57:37 +08:00
public class Singleton {
2019-03-27 20:57:37 +08:00
private Singleton() {
}
2019-03-27 20:57:37 +08:00
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
2019-03-27 20:57:37 +08:00
public static Singleton getUniqueInstance() {
return SingletonHolder.INSTANCE;
}
}
```
2019-03-27 20:57:37 +08:00
#### 枚举实现
2018-09-05 16:59:23 +08:00
2018-09-18 09:31:50 +08:00
```java
2019-03-27 20:57:37 +08:00
public enum Singleton {
2018-09-05 16:59:23 +08:00
2019-03-27 20:57:37 +08:00
INSTANCE;
2018-09-05 16:59:23 +08:00
2019-03-27 20:57:37 +08:00
private String objName;
2018-09-05 16:59:23 +08:00
2018-09-06 18:38:45 +08:00
2019-03-27 20:57:37 +08:00
public String getObjName() {
return objName;
}
2018-09-05 16:59:23 +08:00
2018-09-06 18:38:45 +08:00
2019-03-27 20:57:37 +08:00
public void setObjName(String objName) {
this.objName = objName;
}
2018-09-05 16:59:23 +08:00
2018-09-06 18:38:45 +08:00
2019-03-27 20:57:37 +08:00
public static void main(String[] args) {
2018-09-06 18:38:45 +08:00
2019-03-27 20:57:37 +08:00
// 单例测试
Singleton firstSingleton = Singleton.INSTANCE;
firstSingleton.setObjName("firstName");
System.out.println(firstSingleton.getObjName());
Singleton secondSingleton = Singleton.INSTANCE;
secondSingleton.setObjName("secondName");
System.out.println(firstSingleton.getObjName());
System.out.println(secondSingleton.getObjName());
2018-09-05 16:59:23 +08:00
2019-03-27 20:57:37 +08:00
// 反射获取实例测试
try {
Singleton[] enumConstants = Singleton.class.getEnumConstants();
for (Singleton enumConstant : enumConstants) {
System.out.println(enumConstant.getObjName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
2018-09-06 18:38:45 +08:00
}
2018-09-18 09:33:33 +08:00
```
2018-09-05 16:59:23 +08:00
2018-09-29 22:34:48 +08:00
```html
firstName
secondName
secondName
secondName
```
2019-03-27 20:57:37 +08:00
该实现可以防止反射攻击在其它实现中通过 setAccessible() 方法可以将私有构造函数的访问级别设置为 public然后调用构造函数从而实例化对象如果要防止这种攻击需要在构造函数中添加防止多次实例化的代码该实现是由 JVM 保证只会实例化一次因此不会出现上述的反射攻击
2018-09-05 16:59:23 +08:00
2019-06-09 17:04:10 +08:00
该实现在多次序列化和序列化之后不会得到多个实例而其它实现需要使用 transient 修饰所有字段并且实现序列化和反序列化的方法
2019-03-27 20:57:37 +08:00
### Examples
2018-04-08 22:37:24 +08:00
2019-03-27 20:57:37 +08:00
- Logger Classes
- Configuration Classes
- Accesing resources in shared mode
- Factories implemented as Singletons
2018-04-08 22:37:24 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-04-08 22:37:24 +08:00
2019-03-27 20:57:37 +08:00
- [java.lang.Runtime#getRuntime()](http://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#getRuntime%28%29)
- [java.awt.Desktop#getDesktop()](http://docs.oracle.com/javase/8/docs/api/java/awt/Desktop.html#getDesktop--)
- [java.lang.System#getSecurityManager()](http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getSecurityManager--)
2018-04-08 22:37:24 +08:00
2019-03-27 20:57:37 +08:00
## 2. 简单工厂Simple Factory
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-04-06 22:46:59 +08:00
2018-06-04 11:48:12 +08:00
在创建一个对象时不向客户暴露内部细节并提供一个创建对象的通用接口
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-04-06 22:46:59 +08:00
2018-09-29 22:42:04 +08:00
简单工厂把实例化的操作单独放到一个类中这个类就成为简单工厂类让简单工厂类来决定应该用哪个具体子类来实例化
2018-04-06 22:46:59 +08:00
2018-09-01 15:07:50 +08:00
这样做能把客户类和具体子类的实现解耦客户类不再需要知道有哪些子类以及应当实例化哪个子类客户类往往有多个如果不使用简单工厂那么所有的客户类都要知道所有子类的细节而且一旦子类发生改变例如增加子类那么所有的客户类都要进行修改
2018-04-06 22:46:59 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/40c0c17e-bba6-4493-9857-147c0044a018.png"/> </div><br>
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-04-06 22:46:59 +08:00
```java
2019-03-27 20:57:37 +08:00
public interface Product {
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteProduct implements Product {
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteProduct1 implements Product {
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteProduct2 implements Product {
2018-04-06 22:46:59 +08:00
}
```
2019-03-27 20:57:37 +08:00
以下的 Client 类包含了实例化的代码这是一种错误的实现如果在客户类中存在这种实例化代码就需要考虑将代码放到简单工厂中
2018-08-25 23:12:21 +08:00
```java
2019-03-27 20:57:37 +08:00
public class Client {
2018-09-18 09:42:24 +08:00
2019-03-27 20:57:37 +08:00
public static void main(String[] args) {
int type = 1;
Product product;
if (type == 1) {
product = new ConcreteProduct1();
} else if (type == 2) {
product = new ConcreteProduct2();
} else {
product = new ConcreteProduct();
}
// do something with the product
}
2018-08-25 23:12:21 +08:00
}
```
2019-03-27 20:57:37 +08:00
以下的 SimpleFactory 是简单工厂实现它被所有需要进行实例化的客户类调用
2018-08-25 23:12:21 +08:00
2018-04-06 22:46:59 +08:00
```java
2019-03-27 20:57:37 +08:00
public class SimpleFactory {
2018-09-18 09:42:24 +08:00
2019-03-27 20:57:37 +08:00
public Product createProduct(int type) {
if (type == 1) {
return new ConcreteProduct1();
} else if (type == 2) {
return new ConcreteProduct2();
}
return new ConcreteProduct();
}
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
2018-09-18 09:42:24 +08:00
2019-03-27 20:57:37 +08:00
public static void main(String[] args) {
SimpleFactory simpleFactory = new SimpleFactory();
Product product = simpleFactory.createProduct(1);
// do something with the product
}
2018-04-06 22:46:59 +08:00
}
```
2019-03-27 20:57:37 +08:00
## 3. 工厂方法Factory Method
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-04-06 22:46:59 +08:00
2018-08-12 22:53:28 +08:00
定义了一个创建对象的接口但由子类决定要实例化哪个类工厂方法把实例化操作推迟到子类
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-04-06 22:46:59 +08:00
在简单工厂中创建对象的是另一个类而在工厂方法中是由子类来创建对象
2019-03-27 20:57:37 +08:00
下图中Factory 有一个 doSomething() 方法这个方法需要用到一个产品对象这个产品对象由 factoryMethod() 方法创建该方法是抽象的需要由子类去实现
2018-04-06 22:46:59 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/f4d0afd0-8e78-4914-9e60-4366eaf065b5.png"/> </div><br>
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-04-06 22:46:59 +08:00
```java
2019-03-27 20:57:37 +08:00
public abstract class Factory {
abstract public Product factoryMethod();
public void doSomething() {
Product product = factoryMethod();
// do something with the product
}
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteFactory extends Factory {
public Product factoryMethod() {
return new ConcreteProduct();
}
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteFactory1 extends Factory {
public Product factoryMethod() {
return new ConcreteProduct1();
}
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteFactory2 extends Factory {
public Product factoryMethod() {
return new ConcreteProduct2();
}
2018-04-06 22:46:59 +08:00
}
```
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- [java.util.Calendar](http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance--)
- [java.util.ResourceBundle](http://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String-)
- [java.text.NumberFormat](http://docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html#getInstance--)
- [java.nio.charset.Charset](http://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html#forName-java.lang.String-)
- [java.net.URLStreamHandlerFactory](http://docs.oracle.com/javase/8/docs/api/java/net/URLStreamHandlerFactory.html#createURLStreamHandler-java.lang.String-)
- [java.util.EnumSet](https://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html#of-E-)
- [javax.xml.bind.JAXBContext](https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/JAXBContext.html#createMarshaller--)
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
## 4. 抽象工厂Abstract Factory
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
提供一个接口用于创建 **相关的对象家族**
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-04-06 22:46:59 +08:00
2018-06-21 17:32:32 +08:00
抽象工厂模式创建的是对象家族也就是很多对象而不是一个对象并且这些对象是相关的也就是说必须一起创建出来而工厂方法模式只是用于创建一个对象这和抽象工厂模式有很大不同
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
抽象工厂模式用到了工厂方法模式来创建单一对象AbstractFactory 中的 createProductA() createProductB() 方法都是让子类来实现这两个方法单独来看就是在创建一个对象这符合工厂方法模式的定义
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
至于创建对象的家族这一概念是在 Client 体现Client 要通过 AbstractFactory 同时调用两个方法来创建出两个对象在这里这两个对象就有很大的相关性Client 需要同时创建出这两个对象
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
从高层次来看抽象工厂使用了组合 Cilent 组合了 AbstractFactory而工厂方法模式使用了继承
2018-06-21 17:32:32 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/e2190c36-8b27-4690-bde5-9911020a1294.png"/> </div><br>
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-04-06 22:46:59 +08:00
```java
2019-03-27 20:57:37 +08:00
public class AbstractProductA {
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class AbstractProductB {
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ProductA1 extends AbstractProductA {
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ProductA2 extends AbstractProductA {
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ProductB1 extends AbstractProductB {
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ProductB2 extends AbstractProductB {
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public abstract class AbstractFactory {
abstract AbstractProductA createProductA();
abstract AbstractProductB createProductB();
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteFactory1 extends AbstractFactory {
AbstractProductA createProductA() {
return new ProductA1();
}
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
AbstractProductB createProductB() {
return new ProductB1();
}
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteFactory2 extends AbstractFactory {
AbstractProductA createProductA() {
return new ProductA2();
}
2018-04-06 22:46:59 +08:00
2019-03-27 20:57:37 +08:00
AbstractProductB createProductB() {
return new ProductB2();
}
2018-04-06 22:46:59 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
public static void main(String[] args) {
AbstractFactory abstractFactory = new ConcreteFactory1();
AbstractProductA productA = abstractFactory.createProductA();
AbstractProductB productB = abstractFactory.createProductB();
// do something with productA and productB
}
2018-04-06 22:46:59 +08:00
}
```
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- [javax.xml.parsers.DocumentBuilderFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html)
- [javax.xml.transform.TransformerFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/transform/TransformerFactory.html#newInstance--)
- [javax.xml.xpath.XPathFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/xpath/XPathFactory.html#newInstance--)
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 5. 生成器Builder
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
2018-06-05 09:26:23 +08:00
封装一个对象的构造过程并允许按步骤构造
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-05 09:26:23 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/db5e376d-0b3e-490e-a43a-3231914b6668.png"/> </div><br>
2018-06-05 09:26:23 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-05 09:26:23 +08:00
2019-03-27 20:57:37 +08:00
以下是一个简易的 StringBuilder 实现参考了 JDK 1.8 源码
2018-06-05 09:26:23 +08:00
```java
2019-03-27 20:57:37 +08:00
public class AbstractStringBuilder {
protected char[] value;
2018-06-05 09:26:23 +08:00
2019-03-27 20:57:37 +08:00
protected int count;
2018-06-05 09:26:23 +08:00
2019-03-27 20:57:37 +08:00
public AbstractStringBuilder(int capacity) {
count = 0;
value = new char[capacity];
}
2018-06-05 09:26:23 +08:00
2019-03-27 20:57:37 +08:00
public AbstractStringBuilder append(char c) {
ensureCapacityInternal(count + 1);
value[count++] = c;
return this;
}
2018-06-05 09:26:23 +08:00
2019-03-27 20:57:37 +08:00
private void ensureCapacityInternal(int minimumCapacity) {
// overflow-conscious code
if (minimumCapacity - value.length > 0)
expandCapacity(minimumCapacity);
}
2018-06-05 09:26:23 +08:00
2019-03-27 20:57:37 +08:00
void expandCapacity(int minimumCapacity) {
int newCapacity = value.length * 2 + 2;
if (newCapacity - minimumCapacity < 0)
newCapacity = minimumCapacity;
if (newCapacity < 0) {
if (minimumCapacity < 0) // overflow
throw new OutOfMemoryError();
newCapacity = Integer.MAX_VALUE;
}
value = Arrays.copyOf(value, newCapacity);
}
2018-06-05 09:26:23 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class StringBuilder extends AbstractStringBuilder {
public StringBuilder() {
super(16);
}
2018-06-05 09:26:23 +08:00
2019-03-27 20:57:37 +08:00
@Override
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}
2018-06-05 09:26:23 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
final int count = 26;
for (int i = 0; i < count; i++) {
sb.append((char) ('a' + i));
}
System.out.println(sb.toString());
}
2018-06-05 09:26:23 +08:00
}
```
```html
abcdefghijklmnopqrstuvwxyz
```
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- [java.lang.StringBuilder](http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html)
- [java.nio.ByteBuffer](http://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#put-byte-)
- [java.lang.StringBuffer](http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html#append-boolean-)
- [java.lang.Appendable](http://docs.oracle.com/javase/8/docs/api/java/lang/Appendable.html)
- [Apache Camel builders](https://github.com/apache/camel/tree/0e195428ee04531be27a0b659005e3aa8d159d23/camel-core/src/main/java/org/apache/camel/builder)
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 6. 原型模式Prototype
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
2018-06-05 10:12:43 +08:00
使用原型实例指定要创建对象的类型通过复制这个原型来创建新对象
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-05 10:12:43 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/b8922f8c-95e6-4187-be85-572a509afb71.png"/> </div><br>
2018-06-05 10:12:43 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-05 10:12:43 +08:00
```java
2019-03-27 20:57:37 +08:00
public abstract class Prototype {
abstract Prototype myClone();
2018-06-05 10:12:43 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcretePrototype extends Prototype {
2018-06-05 10:12:43 +08:00
2019-03-27 20:57:37 +08:00
private String filed;
2018-06-05 10:12:43 +08:00
2019-03-27 20:57:37 +08:00
public ConcretePrototype(String filed) {
this.filed = filed;
}
2018-06-05 10:12:43 +08:00
2019-03-27 20:57:37 +08:00
@Override
Prototype myClone() {
return new ConcretePrototype(filed);
}
2018-06-05 10:12:43 +08:00
2019-03-27 20:57:37 +08:00
@Override
public String toString() {
return filed;
}
2018-06-05 10:12:43 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
public static void main(String[] args) {
Prototype prototype = new ConcretePrototype("abc");
Prototype clone = prototype.myClone();
System.out.println(clone.toString());
}
2018-06-05 10:12:43 +08:00
}
```
```html
abc
```
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- [java.lang.Object#clone()](http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone%28%29)
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
# 行为型
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 1. 责任链Chain Of Responsibility
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
2018-06-21 17:32:32 +08:00
使多个对象都有机会处理请求从而避免请求的发送者和接收者之间的耦合关系将这些对象连成一条链并沿着这条链发送该请求直到有一个对象处理它为止
2018-06-05 10:40:38 +08:00
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-05 10:40:38 +08:00
2019-03-27 20:57:37 +08:00
- Handler定义处理请求的接口并且实现后继链successor
2018-06-05 10:40:38 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/ca9f23bf-55a4-47b2-9534-a28e35397988.png"/> </div><br>
2018-06-05 10:40:38 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-05 10:40:38 +08:00
```java
2019-03-27 20:57:37 +08:00
public abstract class Handler {
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
protected Handler successor;
2018-06-05 10:40:38 +08:00
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
public Handler(Handler successor) {
this.successor = successor;
}
2018-06-05 10:40:38 +08:00
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
protected abstract void handleRequest(Request request);
2018-06-05 10:40:38 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteHandler1 extends Handler {
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
public ConcreteHandler1(Handler successor) {
super(successor);
}
2018-06-05 10:40:38 +08:00
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
@Override
protected void handleRequest(Request request) {
if (request.getType() == RequestType.TYPE1) {
System.out.println(request.getName() + " is handle by ConcreteHandler1");
return;
}
if (successor != null) {
successor.handleRequest(request);
}
}
2018-06-05 10:40:38 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteHandler2 extends Handler {
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
public ConcreteHandler2(Handler successor) {
super(successor);
}
2018-06-05 10:40:38 +08:00
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
@Override
protected void handleRequest(Request request) {
if (request.getType() == RequestType.TYPE2) {
System.out.println(request.getName() + " is handle by ConcreteHandler2");
return;
}
if (successor != null) {
successor.handleRequest(request);
}
}
2018-06-05 10:40:38 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Request {
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
private RequestType type;
private String name;
2018-06-05 10:40:38 +08:00
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
public Request(RequestType type, String name) {
this.type = type;
this.name = name;
}
2018-06-05 10:40:38 +08:00
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
public RequestType getType() {
return type;
}
2018-06-05 10:40:38 +08:00
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
public String getName() {
return name;
}
2018-06-05 10:40:38 +08:00
}
2018-09-07 11:16:10 +08:00
2018-06-05 10:40:38 +08:00
```
```java
2019-03-27 20:57:37 +08:00
public enum RequestType {
TYPE1, TYPE2
2018-06-05 10:40:38 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
public static void main(String[] args) {
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
Handler handler1 = new ConcreteHandler1(null);
Handler handler2 = new ConcreteHandler2(handler1);
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
Request request1 = new Request(RequestType.TYPE1, "request1");
handler2.handleRequest(request1);
2018-09-07 11:16:10 +08:00
2019-03-27 20:57:37 +08:00
Request request2 = new Request(RequestType.TYPE2, "request2");
handler2.handleRequest(request2);
}
2018-06-05 10:40:38 +08:00
}
```
```html
2019-03-27 20:57:37 +08:00
request1 is handle by ConcreteHandler1
request2 is handle by ConcreteHandler2
2018-06-05 10:40:38 +08:00
```
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- [java.util.logging.Logger#log()](http://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html#log%28java.util.logging.Level,%20java.lang.String%29)
- [Apache Commons Chain](https://commons.apache.org/proper/commons-chain/index.html)
- [javax.servlet.Filter#doFilter()](http://docs.oracle.com/javaee/7/api/javax/servlet/Filter.html#doFilter-javax.servlet.ServletRequest-javax.servlet.ServletResponse-javax.servlet.FilterChain-)
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 2. 命令Command
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-09-01 01:22:24 +08:00
将命令封装成对象中具有以下作用
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- 使用命令来参数化其它对象
- 将命令放入队列中进行排队
- 将命令的操作记录到日志中
- 支持可撤销的操作
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
- Command命令
- Receiver命令接收者也就是命令真正的执行者
- Invoker通过它来调用命令
- Client可以设置命令与命令的接收者
2018-06-04 22:15:58 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/c44a0342-f405-4f17-b750-e27cf4aadde2.png"/> </div><br>
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-04 22:15:58 +08:00
2018-06-04 22:39:02 +08:00
设计一个遥控器可以控制电灯开关
2018-06-04 22:15:58 +08:00
2019-04-25 18:43:33 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/e6bded8e-41a0-489a-88a6-638e88ab7666.jpg"/> </div><br>
2018-06-04 22:15:58 +08:00
```java
2019-03-27 20:57:37 +08:00
public interface Command {
void execute();
2018-06-04 22:15:58 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class LightOnCommand implements Command {
Light light;
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
public LightOnCommand(Light light) {
this.light = light;
}
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void execute() {
light.on();
}
2018-06-04 22:15:58 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class LightOffCommand implements Command {
Light light;
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
public LightOffCommand(Light light) {
this.light = light;
}
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void execute() {
light.off();
}
2018-06-04 22:15:58 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Light {
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
public void on() {
System.out.println("Light is on!");
}
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
public void off() {
System.out.println("Light is off!");
}
2018-06-04 22:15:58 +08:00
}
```
```java
/**
2019-03-27 20:57:37 +08:00
* 遥控器
*/
public class Invoker {
private Command[] onCommands;
private Command[] offCommands;
private final int slotNum = 7;
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
public Invoker() {
this.onCommands = new Command[slotNum];
this.offCommands = new Command[slotNum];
}
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
public void setOnCommand(Command command, int slot) {
onCommands[slot] = command;
}
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
public void setOffCommand(Command command, int slot) {
offCommands[slot] = command;
}
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
public void onButtonWasPushed(int slot) {
onCommands[slot].execute();
}
2018-06-04 22:15:58 +08:00
2019-03-27 20:57:37 +08:00
public void offButtonWasPushed(int slot) {
offCommands[slot].execute();
}
2018-06-04 22:15:58 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
public static void main(String[] args) {
Invoker invoker = new Invoker();
Light light = new Light();
Command lightOnCommand = new LightOnCommand(light);
Command lightOffCommand = new LightOffCommand(light);
invoker.setOnCommand(lightOnCommand, 0);
invoker.setOffCommand(lightOffCommand, 0);
invoker.onButtonWasPushed(0);
invoker.offButtonWasPushed(0);
}
2018-06-04 22:15:58 +08:00
}
```
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- [java.lang.Runnable](http://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html)
- [Netflix Hystrix](https://github.com/Netflix/Hystrix/wiki)
- [javax.swing.Action](http://docs.oracle.com/javase/8/docs/api/javax/swing/Action.html)
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 3. 解释器Interpreter
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
为语言创建解释器通常由语言的语法和语法分析来定义
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
- TerminalExpression终结符表达式每个终结符都需要一个 TerminalExpression
- Context上下文包含解释器之外的一些全局信息
2018-06-05 11:21:16 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/2b125bcd-1b36-43be-9b78-d90b076be549.png"/> </div><br>
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
以下是一个规则检验器实现具有 and or 规则通过规则可以构建一颗解析树用来检验一个文本是否满足解析树定义的规则
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
例如一颗解析树为 D And (A Or (B C))文本 "D A" 满足该解析树定义的规则
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
这里的 Context 指的是 String
2018-06-05 11:21:16 +08:00
```java
2019-03-27 20:57:37 +08:00
public abstract class Expression {
public abstract boolean interpret(String str);
2018-06-05 11:21:16 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class TerminalExpression extends Expression {
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
private String literal = null;
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
public TerminalExpression(String str) {
literal = str;
}
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
public boolean interpret(String str) {
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens()) {
String test = st.nextToken();
if (test.equals(literal)) {
return true;
}
}
return false;
}
2018-06-05 11:21:16 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class AndExpression extends Expression {
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
private Expression expression1 = null;
private Expression expression2 = null;
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
public AndExpression(Expression expression1, Expression expression2) {
this.expression1 = expression1;
this.expression2 = expression2;
}
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
public boolean interpret(String str) {
return expression1.interpret(str) && expression2.interpret(str);
}
2018-06-05 11:21:16 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class OrExpression extends Expression {
private Expression expression1 = null;
private Expression expression2 = null;
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
public OrExpression(Expression expression1, Expression expression2) {
this.expression1 = expression1;
this.expression2 = expression2;
}
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
public boolean interpret(String str) {
return expression1.interpret(str) || expression2.interpret(str);
}
2018-06-05 11:21:16 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
/**
* 构建解析树
*/
public static Expression buildInterpreterTree() {
// Literal
Expression terminal1 = new TerminalExpression("A");
Expression terminal2 = new TerminalExpression("B");
Expression terminal3 = new TerminalExpression("C");
Expression terminal4 = new TerminalExpression("D");
// B C
Expression alternation1 = new OrExpression(terminal2, terminal3);
// A Or (B C)
Expression alternation2 = new OrExpression(terminal1, alternation1);
// D And (A Or (B C))
return new AndExpression(terminal4, alternation2);
}
2018-06-05 11:21:16 +08:00
2019-03-27 20:57:37 +08:00
public static void main(String[] args) {
Expression define = buildInterpreterTree();
String context1 = "D A";
String context2 = "A B";
System.out.println(define.interpret(context1));
System.out.println(define.interpret(context2));
}
2018-06-05 11:21:16 +08:00
}
```
```html
true
false
```
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- [java.util.Pattern](http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html)
- [java.text.Normalizer](http://docs.oracle.com/javase/8/docs/api/java/text/Normalizer.html)
- All subclasses of [java.text.Format](http://docs.oracle.com/javase/8/docs/api/java/text/Format.html)
- [javax.el.ELResolver](http://docs.oracle.com/javaee/7/api/javax/el/ELResolver.html)
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 4. 迭代器Iterator
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
2018-06-04 14:53:39 +08:00
提供一种顺序访问聚合对象元素的方法并且不暴露聚合对象的内部表示
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-04 14:53:39 +08:00
2019-03-27 20:57:37 +08:00
- Aggregate 是聚合类其中 createIterator() 方法可以产生一个 Iterator
- Iterator 主要定义了 hasNext() next() 方法
- Client 组合了 Aggregate为了迭代遍历 Aggregate也需要组合 Iterator
2018-06-04 16:04:08 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/89292ae1-5f13-44dc-b508-3f035e80bf89.png"/> </div><br>
2018-06-04 14:53:39 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-04 14:53:39 +08:00
```java
2019-03-27 20:57:37 +08:00
public interface Aggregate {
Iterator createIterator();
2018-06-04 14:53:39 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteAggregate implements Aggregate {
2018-06-04 14:53:39 +08:00
2019-03-27 20:57:37 +08:00
private Integer[] items;
2018-06-04 14:53:39 +08:00
2019-03-27 20:57:37 +08:00
public ConcreteAggregate() {
items = new Integer[10];
for (int i = 0; i < items.length; i++) {
items[i] = i;
}
}
2018-06-04 14:53:39 +08:00
2019-03-27 20:57:37 +08:00
@Override
public Iterator createIterator() {
return new ConcreteIterator<Integer>(items);
}
2018-06-04 14:53:39 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public interface Iterator<Item> {
2018-09-01 15:07:50 +08:00
2019-03-27 20:57:37 +08:00
Item next();
2018-06-04 14:53:39 +08:00
2019-03-27 20:57:37 +08:00
boolean hasNext();
2018-06-04 14:53:39 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteIterator<Item> implements Iterator {
2018-06-04 14:53:39 +08:00
2019-03-27 20:57:37 +08:00
private Item[] items;
private int position = 0;
2018-06-04 14:53:39 +08:00
2019-03-27 20:57:37 +08:00
public ConcreteIterator(Item[] items) {
this.items = items;
}
2018-06-04 14:53:39 +08:00
2019-03-27 20:57:37 +08:00
@Override
public Object next() {
return items[position++];
}
2018-06-04 14:53:39 +08:00
2019-03-27 20:57:37 +08:00
@Override
public boolean hasNext() {
return position < items.length;
}
2018-06-04 14:53:39 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
2018-09-01 15:07:50 +08:00
2019-03-27 20:57:37 +08:00
public static void main(String[] args) {
Aggregate aggregate = new ConcreteAggregate();
Iterator<Integer> iterator = aggregate.createIterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
2018-06-04 14:53:39 +08:00
}
```
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- [java.util.Iterator](http://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html)
- [java.util.Enumeration](http://docs.oracle.com/javase/8/docs/api/java/util/Enumeration.html)
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 5. 中介者Mediator
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
2018-06-05 14:38:36 +08:00
集中相关对象之间复杂的沟通和控制方式
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
- Mediator中介者定义一个接口用于与各同事Colleague对象通信
- Colleague同事相关对象
2018-06-05 14:38:36 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/30d6e95c-2e3c-4d32-bf4f-68128a70bc05.png"/> </div><br>
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-05 14:38:36 +08:00
Alarm闹钟CoffeePot咖啡壶Calendar日历Sprinkler喷头是一组相关的对象在某个对象的事件产生时需要去操作其它对象形成了下面这种依赖结构
2019-04-25 18:43:33 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/82cfda3b-b53b-4c89-9fdb-26dd2db0cd02.jpg"/> </div><br>
2018-06-05 14:38:36 +08:00
使用中介者模式可以将复杂的依赖结构变成星形结构
2019-04-25 18:43:33 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/5359cbf5-5a79-4874-9b17-f23c53c2cb80.jpg"/> </div><br>
2018-06-05 14:38:36 +08:00
```java
2019-03-27 20:57:37 +08:00
public abstract class Colleague {
public abstract void onEvent(Mediator mediator);
2018-06-05 14:38:36 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Alarm extends Colleague {
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void onEvent(Mediator mediator) {
mediator.doEvent("alarm");
}
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
public void doAlarm() {
System.out.println("doAlarm()");
}
2018-06-05 14:38:36 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class CoffeePot extends Colleague {
@Override
public void onEvent(Mediator mediator) {
mediator.doEvent("coffeePot");
}
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
public void doCoffeePot() {
System.out.println("doCoffeePot()");
}
2018-06-05 14:38:36 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Calender extends Colleague {
@Override
public void onEvent(Mediator mediator) {
mediator.doEvent("calender");
}
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
public void doCalender() {
System.out.println("doCalender()");
}
2018-06-05 14:38:36 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Sprinkler extends Colleague {
@Override
public void onEvent(Mediator mediator) {
mediator.doEvent("sprinkler");
}
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
public void doSprinkler() {
System.out.println("doSprinkler()");
}
2018-06-05 14:38:36 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public abstract class Mediator {
public abstract void doEvent(String eventType);
2018-06-05 14:38:36 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteMediator extends Mediator {
private Alarm alarm;
private CoffeePot coffeePot;
private Calender calender;
private Sprinkler sprinkler;
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
public ConcreteMediator(Alarm alarm, CoffeePot coffeePot, Calender calender, Sprinkler sprinkler) {
this.alarm = alarm;
this.coffeePot = coffeePot;
this.calender = calender;
this.sprinkler = sprinkler;
}
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void doEvent(String eventType) {
switch (eventType) {
case "alarm":
doAlarmEvent();
break;
case "coffeePot":
doCoffeePotEvent();
break;
case "calender":
doCalenderEvent();
break;
default:
doSprinklerEvent();
}
}
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
public void doAlarmEvent() {
alarm.doAlarm();
coffeePot.doCoffeePot();
calender.doCalender();
sprinkler.doSprinkler();
}
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
public void doCoffeePotEvent() {
// ...
}
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
public void doCalenderEvent() {
// ...
}
2018-06-05 14:38:36 +08:00
2019-03-27 20:57:37 +08:00
public void doSprinklerEvent() {
// ...
}
2018-06-05 14:38:36 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
public static void main(String[] args) {
Alarm alarm = new Alarm();
CoffeePot coffeePot = new CoffeePot();
Calender calender = new Calender();
Sprinkler sprinkler = new Sprinkler();
Mediator mediator = new ConcreteMediator(alarm, coffeePot, calender, sprinkler);
// 闹钟事件到达,调用中介者就可以操作相关对象
alarm.onEvent(mediator);
}
2018-06-05 14:38:36 +08:00
}
```
```java
doAlarm()
doCoffeePot()
doCalender()
doSprinkler()
```
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- All scheduleXXX() methods of [java.util.Timer](http://docs.oracle.com/javase/8/docs/api/java/util/Timer.html)
- [java.util.concurrent.Executor#execute()](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html#execute-java.lang.Runnable-)
- submit() and invokeXXX() methods of [java.util.concurrent.ExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html)
- scheduleXXX() methods of [java.util.concurrent.ScheduledExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledExecutorService.html)
- [java.lang.reflect.Method#invoke()](http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#invoke-java.lang.Object-java.lang.Object...-)
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 6. 备忘录Memento
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
在不违反封装的情况下获得对象的内部状态从而在需要时可以将对象恢复到最初状态
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
- Originator原始对象
- Caretaker负责保存好备忘录
- Menento备忘录存储原始对象的的状态备忘录实际上有两个接口一个是提供给 Caretaker 的窄接口它只能将备忘录传递给其它对象一个是提供给 Originator 的宽接口允许它访问到先前状态所需的所有数据理想情况是只允许 Originator 访问本备忘录的内部状态
2018-06-05 15:28:04 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/50678f34-694f-45a4-91c6-34d985c83fee.png"/> </div><br>
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-05 15:28:04 +08:00
以下实现了一个简单计算器程序可以输入两个值然后计算这两个值的和备忘录模式允许将这两个值存储起来然后在某个时刻用存储的状态进行恢复
2019-03-27 20:57:37 +08:00
实现参考[Memento Pattern - Calculator Example - Java Sourcecode](https://www.oodesign.com/memento-pattern-calculator-example-java-sourcecode.html)
2018-06-05 15:28:04 +08:00
```java
/**
2019-03-27 20:57:37 +08:00
* Originator Interface
*/
public interface Calculator {
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
// Create Memento
PreviousCalculationToCareTaker backupLastCalculation();
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
// setMemento
void restorePreviousCalculation(PreviousCalculationToCareTaker memento);
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
int getCalculationResult();
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
void setFirstNumber(int firstNumber);
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
void setSecondNumber(int secondNumber);
2018-06-05 15:28:04 +08:00
}
```
```java
/**
2019-03-27 20:57:37 +08:00
* Originator Implementation
*/
public class CalculatorImp implements Calculator {
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
private int firstNumber;
private int secondNumber;
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
@Override
public PreviousCalculationToCareTaker backupLastCalculation() {
// create a memento object used for restoring two numbers
return new PreviousCalculationImp(firstNumber, secondNumber);
}
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void restorePreviousCalculation(PreviousCalculationToCareTaker memento) {
this.firstNumber = ((PreviousCalculationToOriginator) memento).getFirstNumber();
this.secondNumber = ((PreviousCalculationToOriginator) memento).getSecondNumber();
}
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
@Override
public int getCalculationResult() {
// result is adding two numbers
return firstNumber + secondNumber;
}
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void setFirstNumber(int firstNumber) {
this.firstNumber = firstNumber;
}
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void setSecondNumber(int secondNumber) {
this.secondNumber = secondNumber;
}
2018-06-05 15:28:04 +08:00
}
```
```java
/**
2019-03-27 20:57:37 +08:00
* Memento Interface to Originator
*
* This interface allows the originator to restore its state
*/
public interface PreviousCalculationToOriginator {
int getFirstNumber();
int getSecondNumber();
2018-06-05 15:28:04 +08:00
}
```
```java
/**
2019-03-27 20:57:37 +08:00
* Memento interface to CalculatorOperator (Caretaker)
*/
public interface PreviousCalculationToCareTaker {
// no operations permitted for the caretaker
2018-06-05 15:28:04 +08:00
}
```
```java
/**
2019-03-27 20:57:37 +08:00
* Memento Object Implementation
* <p>
* Note that this object implements both interfaces to Originator and CareTaker
*/
public class PreviousCalculationImp implements PreviousCalculationToCareTaker,
PreviousCalculationToOriginator {
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
private int firstNumber;
private int secondNumber;
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
public PreviousCalculationImp(int firstNumber, int secondNumber) {
this.firstNumber = firstNumber;
this.secondNumber = secondNumber;
}
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
@Override
public int getFirstNumber() {
return firstNumber;
}
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
@Override
public int getSecondNumber() {
return secondNumber;
}
2018-06-05 15:28:04 +08:00
}
```
```java
/**
2019-03-27 20:57:37 +08:00
* CareTaker object
*/
public class Client {
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
public static void main(String[] args) {
// program starts
Calculator calculator = new CalculatorImp();
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
// assume user enters two numbers
calculator.setFirstNumber(10);
calculator.setSecondNumber(100);
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
// find result
System.out.println(calculator.getCalculationResult());
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
// Store result of this calculation in case of error
PreviousCalculationToCareTaker memento = calculator.backupLastCalculation();
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
// user enters a number
calculator.setFirstNumber(17);
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
// user enters a wrong second number and calculates result
calculator.setSecondNumber(-290);
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
// calculate result
System.out.println(calculator.getCalculationResult());
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
// user hits CTRL + Z to undo last operation and see last result
calculator.restorePreviousCalculation(memento);
2018-06-05 15:28:04 +08:00
2019-03-27 20:57:37 +08:00
// result restored
System.out.println(calculator.getCalculationResult());
}
2018-06-05 15:28:04 +08:00
}
```
```html
110
-273
110
```
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- java.io.Serializable
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 7. 观察者Observer
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
定义对象之间的一对多依赖当一个对象状态改变时它的所有依赖都会收到通知并且自动更新状态
2018-06-04 19:59:03 +08:00
主题Subject是被观察的对象而其所有依赖者Observer称为观察者
2019-04-25 18:43:33 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/7a3c6a30-c735-4edb-8115-337288a4f0f2.jpg" width="600"/> </div><br>
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-04 19:59:03 +08:00
2018-06-30 13:07:02 +08:00
主题Subject具有注册和移除观察者并通知所有观察者的功能主题是通过维护一张观察者列表来实现这些操作的
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
观察者Observer的注册功能需要调用主题的 registerObserver() 方法
2018-06-04 19:59:03 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/a8c8f894-a712-447c-9906-5caef6a016e3.png"/> </div><br>
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-04 19:59:03 +08:00
天气数据布告板会在天气信息发生改变时更新其内容布告板有多个并且在将来会继续增加
2019-04-25 18:43:33 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/b1df9732-86ce-4d69-9f06-fba1db7b3b5a.jpg"/> </div><br>
2018-06-04 19:59:03 +08:00
```java
2019-03-27 20:57:37 +08:00
public interface Subject {
void registerObserver(Observer o);
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
void removeObserver(Observer o);
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
void notifyObserver();
2018-06-04 19:59:03 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class WeatherData implements Subject {
private List<Observer> observers;
private float temperature;
private float humidity;
private float pressure;
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
public WeatherData() {
observers = new ArrayList<>();
}
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
public void setMeasurements(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
notifyObserver();
}
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void registerObserver(Observer o) {
observers.add(o);
}
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void removeObserver(Observer o) {
int i = observers.indexOf(o);
if (i >= 0) {
observers.remove(i);
}
}
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void notifyObserver() {
for (Observer o : observers) {
o.update(temperature, humidity, pressure);
}
}
2018-06-04 19:59:03 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public interface Observer {
void update(float temp, float humidity, float pressure);
2018-06-04 19:59:03 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class StatisticsDisplay implements Observer {
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
public StatisticsDisplay(Subject weatherData) {
weatherData.reisterObserver(this);
}
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void update(float temp, float humidity, float pressure) {
System.out.println("StatisticsDisplay.update: " + temp + " " + humidity + " " + pressure);
}
2018-06-04 19:59:03 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class CurrentConditionsDisplay implements Observer {
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
public CurrentConditionsDisplay(Subject weatherData) {
weatherData.registerObserver(this);
}
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void update(float temp, float humidity, float pressure) {
System.out.println("CurrentConditionsDisplay.update: " + temp + " " + humidity + " " + pressure);
}
2018-06-04 19:59:03 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class WeatherStation {
public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
2018-06-04 19:59:03 +08:00
2019-03-27 20:57:37 +08:00
weatherData.setMeasurements(0, 0, 0);
weatherData.setMeasurements(1, 1, 1);
}
2018-06-04 19:59:03 +08:00
}
```
```html
2019-03-27 20:57:37 +08:00
CurrentConditionsDisplay.update: 0.0 0.0 0.0
StatisticsDisplay.update: 0.0 0.0 0.0
CurrentConditionsDisplay.update: 1.0 1.0 1.0
StatisticsDisplay.update: 1.0 1.0 1.0
2018-06-04 19:59:03 +08:00
```
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- [java.util.Observer](http://docs.oracle.com/javase/8/docs/api/java/util/Observer.html)
- [java.util.EventListener](http://docs.oracle.com/javase/8/docs/api/java/util/EventListener.html)
- [javax.servlet.http.HttpSessionBindingListener](http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpSessionBindingListener.html)
- [RxJava](https://github.com/ReactiveX/RxJava)
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 8. 状态State
2018-06-04 20:31:33 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 22:39:02 +08:00
允许对象在内部状态改变时改变它的行为对象看起来好像修改了它所属的类
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-04 22:39:02 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/79df886f-fdc3-4020-a07f-c991bb58e0d8.png"/> </div><br>
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-04 22:39:02 +08:00
糖果销售机有多种状态每种状态下销售机有不同的行为状态可以发生转移使得销售机的行为也发生改变
2019-04-25 18:43:33 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/396be981-3f2c-4fd9-8101-dbf9c841504b.jpg" width="600"/> </div><br>
2018-06-04 22:39:02 +08:00
```java
2019-03-27 20:57:37 +08:00
public interface State {
/**
* 投入 25 分钱
*/
void insertQuarter();
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
/**
* 退回 25 分钱
*/
void ejectQuarter();
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
/**
* 转动曲柄
*/
void turnCrank();
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
/**
* 发放糖果
*/
void dispense();
2018-06-04 22:39:02 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class HasQuarterState implements State {
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
private GumballMachine gumballMachine;
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public HasQuarterState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void insertQuarter() {
System.out.println("You can't insert another quarter");
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void ejectQuarter() {
System.out.println("Quarter returned");
gumballMachine.setState(gumballMachine.getNoQuarterState());
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void turnCrank() {
System.out.println("You turned...");
gumballMachine.setState(gumballMachine.getSoldState());
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void dispense() {
System.out.println("No gumball dispensed");
}
2018-06-04 22:39:02 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class NoQuarterState implements State {
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
GumballMachine gumballMachine;
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public NoQuarterState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void insertQuarter() {
System.out.println("You insert a quarter");
gumballMachine.setState(gumballMachine.getHasQuarterState());
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void ejectQuarter() {
System.out.println("You haven't insert a quarter");
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void turnCrank() {
System.out.println("You turned, but there's no quarter");
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void dispense() {
System.out.println("You need to pay first");
}
2018-06-04 22:39:02 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class SoldOutState implements State {
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
GumballMachine gumballMachine;
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public SoldOutState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void insertQuarter() {
System.out.println("You can't insert a quarter, the machine is sold out");
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void ejectQuarter() {
System.out.println("You can't eject, you haven't inserted a quarter yet");
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void turnCrank() {
System.out.println("You turned, but there are no gumballs");
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void dispense() {
System.out.println("No gumball dispensed");
}
2018-06-04 22:39:02 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class SoldState implements State {
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
GumballMachine gumballMachine;
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public SoldState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void insertQuarter() {
System.out.println("Please wait, we're already giving you a gumball");
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void ejectQuarter() {
System.out.println("Sorry, you already turned the crank");
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void turnCrank() {
System.out.println("Turning twice doesn't get you another gumball!");
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void dispense() {
gumballMachine.releaseBall();
if (gumballMachine.getCount() > 0) {
gumballMachine.setState(gumballMachine.getNoQuarterState());
} else {
System.out.println("Oops, out of gumballs");
gumballMachine.setState(gumballMachine.getSoldOutState());
}
}
2018-06-04 22:39:02 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class GumballMachine {
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
private State soldOutState;
private State noQuarterState;
private State hasQuarterState;
private State soldState;
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
private State state;
private int count = 0;
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public GumballMachine(int numberGumballs) {
count = numberGumballs;
soldOutState = new SoldOutState(this);
noQuarterState = new NoQuarterState(this);
hasQuarterState = new HasQuarterState(this);
soldState = new SoldState(this);
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
if (numberGumballs > 0) {
state = noQuarterState;
} else {
state = soldOutState;
}
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public void insertQuarter() {
state.insertQuarter();
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public void ejectQuarter() {
state.ejectQuarter();
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public void turnCrank() {
state.turnCrank();
state.dispense();
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public void setState(State state) {
this.state = state;
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public void releaseBall() {
System.out.println("A gumball comes rolling out the slot...");
if (count != 0) {
count -= 1;
}
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public State getSoldOutState() {
return soldOutState;
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public State getNoQuarterState() {
return noQuarterState;
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public State getHasQuarterState() {
return hasQuarterState;
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public State getSoldState() {
return soldState;
}
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public int getCount() {
return count;
}
2018-06-04 22:39:02 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
public static void main(String[] args) {
GumballMachine gumballMachine = new GumballMachine(5);
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
gumballMachine.insertQuarter();
gumballMachine.ejectQuarter();
gumballMachine.turnCrank();
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
gumballMachine.ejectQuarter();
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
gumballMachine.insertQuarter();
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
}
2018-06-04 22:39:02 +08:00
}
```
```html
2019-03-27 20:57:37 +08:00
You insert a quarter
You turned...
A gumball comes rolling out the slot...
You insert a quarter
Quarter returned
You turned, but there's no quarter
You need to pay first
You insert a quarter
You turned...
A gumball comes rolling out the slot...
You insert a quarter
You turned...
A gumball comes rolling out the slot...
You haven't insert a quarter
You insert a quarter
You can't insert another quarter
You turned...
A gumball comes rolling out the slot...
You insert a quarter
You turned...
A gumball comes rolling out the slot...
Oops, out of gumballs
You can't insert a quarter, the machine is sold out
You turned, but there are no gumballs
No gumball dispensed
```
## 9. 策略Strategy
### Intent
2018-06-04 11:48:12 +08:00
2018-06-04 17:10:20 +08:00
定义一系列算法封装每个算法并使它们可以互换
策略模式可以让算法独立于使用它的客户端
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-04 17:10:20 +08:00
2019-03-27 20:57:37 +08:00
- Strategy 接口定义了一个算法族它们都实现了 behavior() 方法
- Context 是使用到该算法族的类其中的 doSomething() 方法会调用 behavior()setStrategy(Strategy) 方法可以动态地改变 strategy 对象也就是说能动态地改变 Context 所使用的算法
2018-06-04 17:10:20 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/cd1be8c2-755a-4a66-ad92-2e30f8f47922.png"/> </div><br>
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
### 与状态模式的比较
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
状态模式的类图和策略模式类似并且都是能够动态改变对象的行为但是状态模式是通过状态转移来改变 Context 所组合的 State 对象而策略模式是通过 Context 本身的决策来改变组合的 Strategy 对象所谓的状态转移是指 Context 在运行过程中由于一些条件发生改变而使得 State 对象发生改变注意必须要是在运行过程中
2018-06-04 22:39:02 +08:00
2019-03-27 20:57:37 +08:00
状态模式主要是用来解决状态转移的问题当状态发生转移了那么 Context 对象就会改变它的行为而策略模式主要是用来封装一组可以互相替代的算法族并且可以根据需要动态地去替换 Context 使用的算法
2018-06-04 17:10:20 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-04 17:10:20 +08:00
设计一个鸭子它可以动态地改变叫声这里的算法族是鸭子的叫声行为
```java
2019-03-27 20:57:37 +08:00
public interface QuackBehavior {
void quack();
2018-06-04 17:10:20 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Quack implements QuackBehavior {
@Override
public void quack() {
System.out.println("quack!");
}
2018-06-04 17:10:20 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Squeak implements QuackBehavior{
@Override
public void quack() {
System.out.println("squeak!");
}
2018-06-04 17:10:20 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Duck {
2018-09-01 15:07:50 +08:00
2019-03-27 20:57:37 +08:00
private QuackBehavior quackBehavior;
2018-06-04 17:10:20 +08:00
2019-03-27 20:57:37 +08:00
public void performQuack() {
if (quackBehavior != null) {
quackBehavior.quack();
}
}
2018-06-04 17:10:20 +08:00
2019-03-27 20:57:37 +08:00
public void setQuackBehavior(QuackBehavior quackBehavior) {
this.quackBehavior = quackBehavior;
}
2018-06-04 17:10:20 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
2018-09-01 15:07:50 +08:00
2019-03-27 20:57:37 +08:00
public static void main(String[] args) {
Duck duck = new Duck();
duck.setQuackBehavior(new Squeak());
duck.performQuack();
duck.setQuackBehavior(new Quack());
duck.performQuack();
}
2018-06-04 17:10:20 +08:00
}
```
```html
squeak!
quack!
```
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- java.util.Comparator#compare()
- javax.servlet.http.HttpServlet
- javax.servlet.Filter#doFilter()
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 10. 模板方法Template Method
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
2018-06-04 16:19:17 +08:00
定义算法框架并将一些步骤的实现延迟到子类
通过模板方法子类可以重新定义算法的某些步骤而不用改变算法的结构
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-04 16:19:17 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/ac6a794b-68c0-486c-902f-8d988eee5766.png"/> </div><br>
2018-06-04 16:19:17 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-04 16:19:17 +08:00
冲咖啡和冲茶都有类似的流程但是某些步骤会有点不一样要求复用那些相同步骤的代码
2019-04-25 18:43:33 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/11236498-1417-46ce-a1b0-e10054256955.png"/> </div><br>
2018-06-04 16:19:17 +08:00
```java
2019-03-27 20:57:37 +08:00
public abstract class CaffeineBeverage {
2018-06-04 16:19:17 +08:00
2019-03-27 20:57:37 +08:00
final void prepareRecipe() {
boilWater();
brew();
pourInCup();
addCondiments();
}
2018-06-04 16:19:17 +08:00
2019-03-27 20:57:37 +08:00
abstract void brew();
2018-06-04 16:19:17 +08:00
2019-03-27 20:57:37 +08:00
abstract void addCondiments();
2018-06-04 16:19:17 +08:00
2019-03-27 20:57:37 +08:00
void boilWater() {
System.out.println("boilWater");
}
2018-06-04 16:19:17 +08:00
2019-03-27 20:57:37 +08:00
void pourInCup() {
System.out.println("pourInCup");
}
2018-06-04 16:19:17 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Coffee extends CaffeineBeverage {
@Override
void brew() {
System.out.println("Coffee.brew");
}
2018-06-04 16:19:17 +08:00
2019-03-27 20:57:37 +08:00
@Override
void addCondiments() {
System.out.println("Coffee.addCondiments");
}
2018-06-04 16:19:17 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Tea extends CaffeineBeverage {
@Override
void brew() {
System.out.println("Tea.brew");
}
2018-06-04 16:19:17 +08:00
2019-03-27 20:57:37 +08:00
@Override
void addCondiments() {
System.out.println("Tea.addCondiments");
}
2018-06-04 16:19:17 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
public static void main(String[] args) {
CaffeineBeverage caffeineBeverage = new Coffee();
caffeineBeverage.prepareRecipe();
System.out.println("-----------");
caffeineBeverage = new Tea();
caffeineBeverage.prepareRecipe();
}
2018-06-04 16:19:17 +08:00
}
```
```html
boilWater
Coffee.brew
pourInCup
Coffee.addCondiments
-----------
boilWater
Tea.brew
pourInCup
Tea.addCondiments
```
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- java.util.Collections#sort()
- java.io.InputStream#skip()
- java.io.InputStream#read()
- java.util.AbstractList#indexOf()
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 11. 访问者Visitor
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
2018-06-05 16:18:52 +08:00
为一个对象结构比如组合结构增加新能力
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
- Visitor访问者为每一个 ConcreteElement 声明一个 visit 操作
- ConcreteVisitor具体访问者存储遍历过程中的累计结果
- ObjectStructure对象结构可以是组合结构或者是一个集合
2018-06-05 16:18:52 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/79c6f036-bde6-4393-85a3-ef36a0327bd2.png"/> </div><br>
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-05 16:18:52 +08:00
```java
2019-03-27 20:57:37 +08:00
public interface Element {
void accept(Visitor visitor);
2018-06-05 16:18:52 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
class CustomerGroup {
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
private List<Customer> customers = new ArrayList<>();
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
void accept(Visitor visitor) {
for (Customer customer : customers) {
customer.accept(visitor);
}
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
void addCustomer(Customer customer) {
customers.add(customer);
}
2018-06-05 16:18:52 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Customer implements Element {
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
private String name;
private List<Order> orders = new ArrayList<>();
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
Customer(String name) {
this.name = name;
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
String getName() {
return name;
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
void addOrder(Order order) {
orders.add(order);
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
public void accept(Visitor visitor) {
visitor.visit(this);
for (Order order : orders) {
order.accept(visitor);
}
}
2018-06-05 16:18:52 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Order implements Element {
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
private String name;
private List<Item> items = new ArrayList();
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
Order(String name) {
this.name = name;
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
Order(String name, String itemName) {
this.name = name;
this.addItem(new Item(itemName));
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
String getName() {
return name;
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
void addItem(Item item) {
items.add(item);
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
public void accept(Visitor visitor) {
visitor.visit(this);
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
for (Item item : items) {
item.accept(visitor);
}
}
2018-06-05 16:18:52 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Item implements Element {
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
private String name;
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
Item(String name) {
this.name = name;
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
String getName() {
return name;
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
public void accept(Visitor visitor) {
visitor.visit(this);
}
2018-06-05 16:18:52 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public interface Visitor {
void visit(Customer customer);
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
void visit(Order order);
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
void visit(Item item);
2018-06-05 16:18:52 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class GeneralReport implements Visitor {
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
private int customersNo;
private int ordersNo;
private int itemsNo;
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
public void visit(Customer customer) {
System.out.println(customer.getName());
customersNo++;
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
public void visit(Order order) {
System.out.println(order.getName());
ordersNo++;
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
public void visit(Item item) {
System.out.println(item.getName());
itemsNo++;
}
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
public void displayResults() {
System.out.println("Number of customers: " + customersNo);
System.out.println("Number of orders: " + ordersNo);
System.out.println("Number of items: " + itemsNo);
}
2018-06-05 16:18:52 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
public static void main(String[] args) {
Customer customer1 = new Customer("customer1");
customer1.addOrder(new Order("order1", "item1"));
customer1.addOrder(new Order("order2", "item1"));
customer1.addOrder(new Order("order3", "item1"));
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
Order order = new Order("order_a");
order.addItem(new Item("item_a1"));
order.addItem(new Item("item_a2"));
order.addItem(new Item("item_a3"));
Customer customer2 = new Customer("customer2");
customer2.addOrder(order);
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
CustomerGroup customers = new CustomerGroup();
customers.addCustomer(customer1);
customers.addCustomer(customer2);
2018-06-05 16:18:52 +08:00
2019-03-27 20:57:37 +08:00
GeneralReport visitor = new GeneralReport();
customers.accept(visitor);
visitor.displayResults();
}
2018-06-05 16:18:52 +08:00
}
```
```html
customer1
order1
item1
order2
item1
order3
item1
customer2
order_a
item_a1
item_a2
item_a3
2019-03-27 20:57:37 +08:00
Number of customers: 2
Number of orders: 4
Number of items: 6
2018-06-05 16:18:52 +08:00
```
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- javax.lang.model.element.Element and javax.lang.model.element.ElementVisitor
- javax.lang.model.type.TypeMirror and javax.lang.model.type.TypeVisitor
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 12. 空对象Null
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
使用什么都不做的空对象来代替 NULL
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
一个方法返回 NULL意味着方法的调用端需要去检查返回值是否是 NULL这么做会导致非常多的冗余的检查代码并且如果某一个调用端忘记了做这个检查返回值而直接使用返回的对象那么就有可能抛出空指针异常
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-04 14:28:30 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/22870bbe-898f-4c17-a31a-d7c5ee5d1c10.png"/> </div><br>
2018-06-04 14:28:30 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-04 14:28:30 +08:00
```java
2019-03-27 20:57:37 +08:00
public abstract class AbstractOperation {
abstract void request();
2018-06-04 14:28:30 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class RealOperation extends AbstractOperation {
@Override
void request() {
System.out.println("do something");
}
2018-06-04 14:28:30 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class NullOperation extends AbstractOperation{
@Override
void request() {
// do nothing
}
2018-06-04 14:28:30 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
public static void main(String[] args) {
AbstractOperation abstractOperation = func(-1);
abstractOperation.request();
}
2018-06-04 14:28:30 +08:00
2019-03-27 20:57:37 +08:00
public static AbstractOperation func(int para) {
if (para < 0) {
return new NullOperation();
}
return new RealOperation();
}
2018-06-04 14:28:30 +08:00
}
```
2019-03-27 20:57:37 +08:00
# 结构型
2018-06-04 14:28:30 +08:00
2019-03-27 20:57:37 +08:00
## 1. 适配器Adapter
2018-06-04 14:28:30 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
把一个类接口转换成另一个用户需要的接口
2019-04-25 18:43:33 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/3d5b828e-5c4d-48d8-a440-281e4a8e1c92.png"/> </div><br>
2018-06-04 12:10:43 +08:00
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-04 12:10:43 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/ff5152fc-4ff3-44c4-95d6-1061002c364a.png"/> </div><br>
2018-06-04 12:10:43 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-04 12:10:43 +08:00
2019-03-27 20:57:37 +08:00
鸭子Duck和火鸡Turkey拥有不同的叫声Duck 的叫声调用 quack() 方法 Turkey 调用 gobble() 方法
2018-06-04 12:10:43 +08:00
2019-03-27 20:57:37 +08:00
要求将 Turkey gobble() 方法适配成 Duck quack() 方法从而让火鸡冒充鸭子
2018-06-04 12:10:43 +08:00
```java
2019-03-27 20:57:37 +08:00
public interface Duck {
void quack();
2018-06-04 12:10:43 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public interface Turkey {
void gobble();
2018-06-04 12:10:43 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class WildTurkey implements Turkey {
@Override
public void gobble() {
System.out.println("gobble!");
}
2018-06-04 12:10:43 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class TurkeyAdapter implements Duck {
Turkey turkey;
2018-06-04 12:10:43 +08:00
2019-03-27 20:57:37 +08:00
public TurkeyAdapter(Turkey turkey) {
this.turkey = turkey;
}
2018-06-04 12:10:43 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void quack() {
turkey.gobble();
}
2018-06-04 12:10:43 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
public static void main(String[] args) {
Turkey turkey = new WildTurkey();
Duck duck = new TurkeyAdapter(turkey);
duck.quack();
}
2018-06-04 12:10:43 +08:00
}
```
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29)
- [java.util.Collections#list()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#list-java.util.Enumeration-)
- [java.util.Collections#enumeration()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#enumeration-java.util.Collection-)
- [javax.xml.bind.annotation.adapters.XMLAdapter](http://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html#marshal-BoundType-)
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 2. 桥接Bridge
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
将抽象与实现分离开来使它们可以独立变化
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
- Abstraction定义抽象类的接口
- Implementor定义实现类接口
2018-06-05 20:15:23 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/2a1f8b0f-1dd7-4409-b177-a381c58066ad.png"/> </div><br>
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
RemoteControl 表示遥控器指代 Abstraction
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
TV 表示电视指代 Implementor
2018-06-05 20:15:23 +08:00
桥接模式将遥控器和电视分离开来从而可以独立改变遥控器或者电视的实现
```java
2019-03-27 20:57:37 +08:00
public abstract class TV {
public abstract void on();
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
public abstract void off();
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
public abstract void tuneChannel();
2018-06-05 20:15:23 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Sony extends TV {
@Override
public void on() {
System.out.println("Sony.on()");
}
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void off() {
System.out.println("Sony.off()");
}
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void tuneChannel() {
System.out.println("Sony.tuneChannel()");
}
2018-06-05 20:15:23 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class RCA extends TV {
@Override
public void on() {
System.out.println("RCA.on()");
}
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void off() {
System.out.println("RCA.off()");
}
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void tuneChannel() {
System.out.println("RCA.tuneChannel()");
}
2018-06-05 20:15:23 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public abstract class RemoteControl {
protected TV tv;
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
public RemoteControl(TV tv) {
this.tv = tv;
}
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
public abstract void on();
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
public abstract void off();
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
public abstract void tuneChannel();
2018-06-05 20:15:23 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteRemoteControl1 extends RemoteControl {
public ConcreteRemoteControl1(TV tv) {
super(tv);
}
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void on() {
System.out.println("ConcreteRemoteControl1.on()");
tv.on();
}
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void off() {
System.out.println("ConcreteRemoteControl1.off()");
tv.off();
}
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void tuneChannel() {
System.out.println("ConcreteRemoteControl1.tuneChannel()");
tv.tuneChannel();
}
2018-06-05 20:15:23 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteRemoteControl2 extends RemoteControl {
public ConcreteRemoteControl2(TV tv) {
super(tv);
}
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void on() {
System.out.println("ConcreteRemoteControl2.on()");
tv.on();
}
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void off() {
System.out.println("ConcreteRemoteControl2.off()");
tv.off();
}
2018-06-05 20:15:23 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void tuneChannel() {
System.out.println("ConcreteRemoteControl2.tuneChannel()");
tv.tuneChannel();
}
2018-06-05 20:15:23 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
public static void main(String[] args) {
RemoteControl remoteControl1 = new ConcreteRemoteControl1(new RCA());
remoteControl1.on();
remoteControl1.off();
remoteControl1.tuneChannel();
RemoteControl remoteControl2 = new ConcreteRemoteControl2(new Sony());
remoteControl2.on();
remoteControl2.off();
remoteControl2.tuneChannel();
}
2018-06-05 20:15:23 +08:00
}
```
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- AWT (It provides an abstraction layer which maps onto the native OS the windowing support.)
- JDBC
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 3. 组合Composite
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
2018-06-04 16:04:08 +08:00
将对象组合成树形结构来表示整体/部分层次关系允许用户以相同的方式处理单独对象和组合对象
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-04 16:04:08 +08:00
组件Component类是组合类Composite和叶子类Leaf的父类可以把组合类看成是树的中间节点
组合对象拥有一个或者多个组件对象因此组合对象的操作可以委托给组件对象去处理而组件对象可以是另一个组合对象或者叶子对象
2019-05-17 22:21:03 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/2b8bfd57-b4d1-4a75-bfb0-bcf1fba4014a.png"/> </div><br>
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-04 16:04:08 +08:00
```java
2019-03-27 20:57:37 +08:00
public abstract class Component {
protected String name;
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
public Component(String name) {
this.name = name;
}
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
public void print() {
print(0);
}
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
abstract void print(int level);
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
abstract public void add(Component component);
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
abstract public void remove(Component component);
2018-06-04 16:04:08 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Composite extends Component {
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
private List<Component> child;
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
public Composite(String name) {
super(name);
child = new ArrayList<>();
}
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
@Override
void print(int level) {
for (int i = 0; i < level; i++) {
System.out.print("--");
}
System.out.println("Composite:" + name);
for (Component component : child) {
component.print(level + 1);
}
}
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void add(Component component) {
child.add(component);
}
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void remove(Component component) {
child.remove(component);
}
2018-06-04 16:04:08 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Leaf extends Component {
public Leaf(String name) {
super(name);
}
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
@Override
void print(int level) {
for (int i = 0; i < level; i++) {
System.out.print("--");
}
System.out.println("left:" + name);
}
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void add(Component component) {
throw new UnsupportedOperationException(); // 牺牲透明性换取单一职责原则,这样就不用考虑是叶子节点还是组合节点
}
2018-06-04 16:04:08 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void remove(Component component) {
throw new UnsupportedOperationException();
}
2018-06-04 16:04:08 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
public static void main(String[] args) {
Composite root = new Composite("root");
Component node1 = new Leaf("1");
Component node2 = new Composite("2");
Component node3 = new Leaf("3");
root.add(node1);
root.add(node2);
root.add(node3);
Component node21 = new Leaf("21");
Component node22 = new Composite("22");
node2.add(node21);
node2.add(node22);
Component node221 = new Leaf("221");
node22.add(node221);
root.print();
}
2018-06-04 16:04:08 +08:00
}
```
```html
Composite:root
--left:1
--Composite:2
----left:21
----Composite:22
------left:221
--left:3
```
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- javax.swing.JComponent#add(Component)
- java.awt.Container#add(Component)
- java.util.Map#putAll(Map)
- java.util.List#addAll(Collection)
- java.util.Set#addAll(Collection)
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 4. 装饰Decorator
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
为对象动态添加功能
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-04 16:40:21 +08:00
2018-08-12 22:53:28 +08:00
装饰者Decorator和具体组件ConcreteComponent都继承自组件Component具体组件的方法实现不需要依赖于其它对象而装饰者组合了一个组件这样它可以装饰其它装饰者或者具体组件所谓装饰就是把这个装饰者套在被装饰者之上从而动态扩展被装饰者的功能装饰者的方法有一部分是自己的这属于它的功能然后调用被装饰者的方法实现从而也保留了被装饰者的功能可以看到具体组件应当是装饰层次的最低层因为只有具体组件的方法实现不需要依赖于其它对象
2018-06-04 16:40:21 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/6b833bc2-517a-4270-8a5e-0a5f6df8cd96.png"/> </div><br>
2018-06-04 16:40:21 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-04 16:40:21 +08:00
设计不同种类的饮料饮料可以添加配料比如可以添加牛奶并且支持动态添加新配料每增加一种配料该饮料的价格就会增加要求计算一种饮料的价格
2019-03-27 20:57:37 +08:00
下图表示在 DarkRoast 饮料上新增新添加 Mocha 配料之后又添加了 Whip 配料DarkRoast Mocha 包裹Mocha 又被 Whip 包裹它们都继承自相同父类都有 cost() 方法外层类的 cost() 方法调用了内层类的 cost() 方法
2018-06-04 16:40:21 +08:00
2019-04-25 18:43:33 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/c9cfd600-bc91-4f3a-9f99-b42f88a5bb24.jpg" width="600"/> </div><br>
2018-06-04 16:40:21 +08:00
```java
2019-03-27 20:57:37 +08:00
public interface Beverage {
double cost();
2018-06-04 16:40:21 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class DarkRoast implements Beverage {
@Override
public double cost() {
return 1;
}
2018-06-04 16:40:21 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class HouseBlend implements Beverage {
@Override
public double cost() {
return 1;
}
2018-06-04 16:40:21 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public abstract class CondimentDecorator implements Beverage {
protected Beverage beverage;
2018-06-04 16:40:21 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Milk extends CondimentDecorator {
2018-06-04 16:40:21 +08:00
2019-03-27 20:57:37 +08:00
public Milk(Beverage beverage) {
this.beverage = beverage;
}
2018-06-04 16:40:21 +08:00
2019-03-27 20:57:37 +08:00
@Override
public double cost() {
return 1 + beverage.cost();
}
2018-06-04 16:40:21 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Mocha extends CondimentDecorator {
2018-06-04 16:40:21 +08:00
2019-03-27 20:57:37 +08:00
public Mocha(Beverage beverage) {
this.beverage = beverage;
}
2018-06-04 16:40:21 +08:00
2019-03-27 20:57:37 +08:00
@Override
public double cost() {
return 1 + beverage.cost();
}
2018-06-04 16:40:21 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
2018-09-01 15:07:50 +08:00
2019-03-27 20:57:37 +08:00
public static void main(String[] args) {
Beverage beverage = new HouseBlend();
beverage = new Mocha(beverage);
beverage = new Milk(beverage);
System.out.println(beverage.cost());
}
2018-06-04 16:40:21 +08:00
}
```
```html
3.0
```
2019-03-27 20:57:37 +08:00
### 设计原则
2018-06-04 16:40:21 +08:00
2018-06-30 13:07:02 +08:00
类应该对扩展开放对修改关闭也就是添加新功能时不需要修改代码饮料可以动态添加新的配料而不需要去修改饮料的代码
不可能把所有的类设计成都满足这一原则应当把该原则应用于最有可能发生改变的地方
2018-06-04 16:40:21 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- java.io.BufferedInputStream(InputStream)
- java.io.DataInputStream(InputStream)
- java.io.BufferedOutputStream(OutputStream)
- java.util.zip.ZipOutputStream(OutputStream)
- java.util.Collections#checked[List|Map|Set|SortedSet|SortedMap]()
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 5. 外观Facade
2018-06-04 20:31:33 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 21:09:28 +08:00
提供了一个统一的接口用来访问子系统中的一群接口从而让子系统更容易使用
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-04 21:09:28 +08:00
2019-04-25 18:43:33 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/f9978fa6-9f49-4a0f-8540-02d269ac448f.png"/> </div><br>
2018-06-04 21:09:28 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-04 21:09:28 +08:00
2018-08-12 22:53:28 +08:00
观看电影需要操作很多电器使用外观模式实现一键看电影功能
2018-06-04 22:15:58 +08:00
2018-06-04 21:09:28 +08:00
```java
2019-03-27 20:57:37 +08:00
public class SubSystem {
public void turnOnTV() {
System.out.println("turnOnTV()");
}
2018-06-04 21:09:28 +08:00
2019-03-27 20:57:37 +08:00
public void setCD(String cd) {
System.out.println("setCD( " + cd + " )");
}
2018-06-04 21:09:28 +08:00
2019-03-27 20:57:37 +08:00
public void startWatching(){
System.out.println("startWatching()");
}
2018-06-04 21:09:28 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Facade {
private SubSystem subSystem = new SubSystem();
2018-06-04 21:09:28 +08:00
2019-03-27 20:57:37 +08:00
public void watchMovie() {
subSystem.turnOnTV();
subSystem.setCD("a movie");
subSystem.startWatching();
}
2018-06-04 21:09:28 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
public static void main(String[] args) {
Facade facade = new Facade();
facade.watchMovie();
}
2018-06-04 21:09:28 +08:00
}
```
2019-03-27 20:57:37 +08:00
### 设计原则
2018-06-04 21:09:28 +08:00
2018-08-12 22:53:28 +08:00
最少知识原则只和你的密友谈话也就是说客户对象所需要交互的对象应当尽可能少
2018-06-04 21:09:28 +08:00
2019-03-27 20:57:37 +08:00
## 6. 享元Flyweight
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
2018-06-05 17:23:42 +08:00
利用共享的方式来支持大量细粒度的对象这些对象一部分内部状态是相同的
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-05 17:23:42 +08:00
2019-03-27 20:57:37 +08:00
- Flyweight享元对象
- IntrinsicState内部状态享元对象共享内部状态
- ExtrinsicState外部状态每个享元对象的外部状态不同
2018-06-05 17:23:42 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/5f5c22d5-9c0e-49e1-b5b0-6cc7032724d4.png"/> </div><br>
2018-06-05 17:23:42 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-05 17:23:42 +08:00
```java
2019-03-27 20:57:37 +08:00
public interface Flyweight {
void doOperation(String extrinsicState);
2018-06-05 17:23:42 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class ConcreteFlyweight implements Flyweight {
2018-06-05 17:23:42 +08:00
2019-03-27 20:57:37 +08:00
private String intrinsicState;
2018-06-05 17:23:42 +08:00
2019-03-27 20:57:37 +08:00
public ConcreteFlyweight(String intrinsicState) {
this.intrinsicState = intrinsicState;
}
2018-06-05 17:23:42 +08:00
2019-03-27 20:57:37 +08:00
@Override
public void doOperation(String extrinsicState) {
System.out.println("Object address: " + System.identityHashCode(this));
System.out.println("IntrinsicState: " + intrinsicState);
System.out.println("ExtrinsicState: " + extrinsicState);
}
2018-06-05 17:23:42 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class FlyweightFactory {
2018-06-05 17:23:42 +08:00
2019-03-27 20:57:37 +08:00
private HashMap<String, Flyweight> flyweights = new HashMap<>();
2018-06-05 17:23:42 +08:00
2019-03-27 20:57:37 +08:00
Flyweight getFlyweight(String intrinsicState) {
if (!flyweights.containsKey(intrinsicState)) {
Flyweight flyweight = new ConcreteFlyweight(intrinsicState);
flyweights.put(intrinsicState, flyweight);
}
return flyweights.get(intrinsicState);
}
2018-06-05 17:23:42 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class Client {
2018-09-01 15:07:50 +08:00
2019-03-27 20:57:37 +08:00
public static void main(String[] args) {
FlyweightFactory factory = new FlyweightFactory();
Flyweight flyweight1 = factory.getFlyweight("aa");
Flyweight flyweight2 = factory.getFlyweight("aa");
flyweight1.doOperation("x");
flyweight2.doOperation("y");
}
2018-06-05 17:23:42 +08:00
}
```
```html
2019-03-27 20:57:37 +08:00
Object address: 1163157884
IntrinsicState: aa
ExtrinsicState: x
Object address: 1163157884
IntrinsicState: aa
ExtrinsicState: y
2018-06-05 17:23:42 +08:00
```
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### JDK
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
Java 利用缓存来加速大量小对象的访问时间
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
- java.lang.Integer#valueOf(int)
- java.lang.Boolean#valueOf(boolean)
- java.lang.Byte#valueOf(byte)
- java.lang.Character#valueOf(char)
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
## 7. 代理Proxy
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Intent
2018-06-04 11:48:12 +08:00
2018-06-21 17:36:07 +08:00
控制对其它对象的访问
2018-06-04 11:48:12 +08:00
2019-03-27 20:57:37 +08:00
### Class Diagram
2018-06-05 21:33:34 +08:00
代理有以下四类
2019-03-27 20:57:37 +08:00
- 远程代理Remote Proxy控制对远程对象不同地址空间的访问它负责将请求及其参数进行编码并向不同地址空间中的对象发送已经编码的请求
- 虚拟代理Virtual Proxy根据需要创建开销很大的对象它可以缓存实体的附加信息以便延迟对它的访问例如在网站加载一个很大图片时不能马上完成可以用虚拟代理缓存图片的大小信息然后生成一张临时图片代替原始图片
- 保护代理Protection Proxy按权限控制对象的访问它负责检查调用者是否具有实现一个请求所必须的访问权限
- 智能代理Smart Reference取代了简单的指针它在访问对象时执行一些附加操作记录对象的引用次数当第一次引用一个对象时将它装入内存在访问一个实际对象前检查是否已经锁定了它以确保其它对象不能改变它
2018-06-05 21:33:34 +08:00
2019-05-15 10:41:36 +08:00
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/9b679ff5-94c6-48a7-b9b7-2ea868e828ed.png"/> </div><br>
2018-06-05 21:33:34 +08:00
2019-03-27 20:57:37 +08:00
### Implementation
2018-06-05 21:33:34 +08:00
以下是一个虚拟代理的实现模拟了图片延迟加载的情况下使用与图片大小相等的临时内容去替换原始图片直到图片加载完成才将图片显示出来
```java
2019-03-27 20:57:37 +08:00
public interface Image {
void showImage();
2018-06-05 21:33:34 +08:00
}
```
```java
2019-03-27 20:57:37 +08:00
public class HighResolutionImage implements Image {
2018-06-05 21:33:34 +08:00
2019-03-27 20:57:37 +08:00
private URL imageURL;
private long startTime;
private int height;
private int width;
2018-06-05 21:33:34 +08:00
2019-03-27 20:57:37 +08:00
public int getHeight() {
return height;
}
2018-06-05 21:33:34 +08:00
2019-03-27 20:57:37 +08:00
public int getWidth() {
return width;
}
2018-06-05 21:33:34 +08:00
2019-03-27 20:57:37 +08:00
public HighResolutionImage(URL imageURL) {
this.imageURL = imageURL;
this.startTime = System.currentTimeMillis();
this.width = 600;
this.height = 600;
}
public boolean isLoad() {
// 模拟图片加载,延迟 3s 加载完成
long endTime = System.currentTimeMillis();
return endTime - startTime > 3000;
}
@Override
public void showImage() {
System.out.println("Real Image: " + imageURL);
}
}
```
```java
public class ImageProxy implements Image {
private HighResolutionImage highResolutionImage;
public ImageProxy(HighResolutionImage highResolutionImage) {
this.highResolutionImage = highResolutionImage;
}
@Override
public void showImage() {
while (!highResolutionImage.isLoad()) {
try {
System.out.println("Temp Image: " + highResolutionImage.getWidth() + " " + highResolutionImage.getHeight());
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
highResolutionImage.showImage();
}
}
```
```java
public class ImageViewer {
public static void main(String[] args) throws Exception {
String image = "http://image.jpg";
URL url = new URL(image);
HighResolutionImage highResolutionImage = new HighResolutionImage(url);
ImageProxy imageProxy = new ImageProxy(highResolutionImage);
imageProxy.showImage();
}
}
```
### JDK
- java.lang.reflect.Proxy
- RMI
# 参考资料
- 弗里曼. Head First 设计模式 [M]. 中国电力出版社, 2007.
- Gamma E. 设计模式: 可复用面向对象软件的基础 [M]. 机械工业出版社, 2007.
- Bloch J. Effective java[M]. Addison-Wesley Professional, 2017.
- [Design Patterns](http://www.oodesign.com/)
- [Design patterns implemented in Java](http://java-design-patterns.com/)
- [The breakdown of design patterns in JDK](http://www.programering.com/a/MTNxAzMwATY.html)
2019-06-09 22:32:10 +08:00
<img width="580px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/other/公众号海报1.png"></img>