2018-03-02 10:58:04 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
2018-03-03 15:22:37 +08:00
|
|
|
|
* [前言](#前言)
|
2018-03-02 10:58:04 +08:00
|
|
|
|
* [第一章 设计模式入门](#第一章-设计模式入门)
|
|
|
|
|
* [第二章 观察者模式](#第二章-观察者模式)
|
|
|
|
|
* [第三章 装饰模式](#第三章-装饰模式)
|
|
|
|
|
* [第四章 工厂模式](#第四章-工厂模式)
|
|
|
|
|
* [1. 简单工厂](#1-简单工厂)
|
|
|
|
|
* [2. 工厂方法模式](#2--工厂方法模式)
|
|
|
|
|
* [3. 抽象工厂模式](#3--抽象工厂模式)
|
|
|
|
|
* [第五章 单件模式](#第五章-单件模式)
|
|
|
|
|
* [第六章 命令模式](#第六章-命令模式)
|
|
|
|
|
* [第七章 适配器模式与外观模式](#第七章-适配器模式与外观模式)
|
|
|
|
|
* [1. 适配器模式](#1-适配器模式)
|
|
|
|
|
* [2. 外观模式](#2-外观模式)
|
|
|
|
|
* [第八章 模板方法模式](#第八章-模板方法模式)
|
|
|
|
|
* [第九章 迭代器和组合模式](#第九章-迭代器和组合模式)
|
|
|
|
|
* [1. 迭代器模式](#1-迭代器模式)
|
|
|
|
|
* [2. Java 内置的迭代器](#2-java-内置的迭代器)
|
|
|
|
|
* [3. 组合模式](#3-组合模式)
|
|
|
|
|
* [第十章 状态模式](#第十章-状态模式)
|
|
|
|
|
* [第十一章 代理模式](#第十一章-代理模式)
|
|
|
|
|
* [第十二章 复合模式](#第十二章-复合模式)
|
|
|
|
|
* [MVC](#mvc)
|
|
|
|
|
* [第十三章 与设计模式相处](#第十三章-与设计模式相处)
|
|
|
|
|
* [第十四章 剩下的模式](#第十四章-剩下的模式)
|
|
|
|
|
<!-- GFM-TOC -->
|
|
|
|
|
|
|
|
|
|
|
2018-03-03 15:22:37 +08:00
|
|
|
|
# 前言
|
|
|
|
|
|
2018-03-03 21:21:59 +08:00
|
|
|
|
文中涉及一些 UML 类图,为了更好地理解,可以先阅读 [UML 类图](https://github.com/CyC2018/Interview-Notebook/blob/master/notes/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E6%80%9D%E6%83%B3.md#1-%E7%B1%BB%E5%9B%BE)。
|
2018-03-03 15:22:37 +08:00
|
|
|
|
|
2018-03-03 21:03:05 +08:00
|
|
|
|
需要说明的一点是,文中的 UML 类图和规范的 UML 类图不大相同,其中组合关系使用以下箭头表示:
|
2018-03-03 15:22:37 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//09e398d8-9c6e-48f6-b48b-8b4f9de61d1d.png"/> </div>
|
2018-03-03 15:22:37 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
# 第一章 设计模式入门
|
|
|
|
|
|
|
|
|
|
**1. 设计模式概念**
|
|
|
|
|
|
|
|
|
|
设计模式不是代码,而是解决问题的方案,学习现有的设计模式可以做到经验复用。
|
|
|
|
|
|
|
|
|
|
拥有设计模式词汇,在沟通时就能用更少的词汇来讨论,并且不需要了解底层细节。
|
|
|
|
|
|
|
|
|
|
**2. 问题描述**
|
|
|
|
|
|
|
|
|
|
设计不同种类的鸭子拥有不同的叫声和飞行方式。
|
|
|
|
|
|
|
|
|
|
**3. 简单实现方案**
|
|
|
|
|
|
|
|
|
|
使用继承的解决方案如下,这种方案代码无法复用,如果两个鸭子类拥有同样的飞行方式,就有两份重复的代码。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//144d28a0-1dc5-4aba-8961-ced5bc88428a.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**4. 设计原则**
|
|
|
|
|
|
|
|
|
|
**封装变化**在这里变化的是鸭子叫和飞行的行为方式。
|
|
|
|
|
|
|
|
|
|
**针对接口编程,而不是针对实现编程** 变量声明的类型为父类,而不是具体的某个子类。父类中的方法实现不在父类,而是在各个子类。程序在运行时可以动态改变变量所指向的子类类型。
|
|
|
|
|
|
|
|
|
|
运用这一原则,将叫和飞行的行为抽象出来,实现多种不同的叫和飞行的子类,让子类去实现具体的叫和飞行方式。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//1c8ccf5c-7ecd-4b8a-b160-3f72a510ce26.png"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**多用组合,少用继承** 组合也就是 has-a 关系,通过组合,可以在运行时动态改变实现,只要通过改变父类对象具体指向哪个子类即可。而继承就不能做到这些,继承体系在创建类时就已经确定。
|
|
|
|
|
|
|
|
|
|
运用这一原则,在 Duck 类中组合 FlyBehavior 和 QuackBehavior 类,performQuack() 和 performFly() 方法委托给这两个类去处理。通过这种方式,一个 Duck 子类可以根据需要去实例化 FlyBehavior 和 QuackBehavior 的子类对象,并且也可以动态地进行改变。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//29574e6f-295c-444e-83c7-b162e8a73a83.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**5. 整体设计图**
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//e13833c8-e215-462e-855c-1d362bb8d4a0.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**6. 模式定义**
|
|
|
|
|
|
|
|
|
|
**策略模式** :定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
|
|
|
|
|
|
|
|
|
|
**7. 实现代码**
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public abstract class Duck {
|
|
|
|
|
FlyBehavior flyBehavior;
|
|
|
|
|
QuackBehavior quackBehavior;
|
|
|
|
|
|
|
|
|
|
public Duck(){
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void performFly(){
|
|
|
|
|
flyBehavior.fly();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setFlyBehavior(FlyBehavior fb){
|
|
|
|
|
flyBehavior = fb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void performQuack(){
|
|
|
|
|
quackBehavior.quack();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setQuackBehavior(QuackBehavior qb){
|
|
|
|
|
quackBehavior = qb;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class MallarDuck extends Duck{
|
|
|
|
|
public MallarDuck(){
|
|
|
|
|
flyBehavior = new FlyWithWings();
|
|
|
|
|
quackBehavior = new Quack();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public interface FlyBehavior {
|
|
|
|
|
void fly();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class FlyNoWay implements FlyBehavior{
|
|
|
|
|
@Override
|
|
|
|
|
public void fly() {
|
|
|
|
|
System.out.println("FlyBehavior.FlyNoWay");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class FlyWithWings implements FlyBehavior{
|
|
|
|
|
@Override
|
|
|
|
|
public void fly() {
|
|
|
|
|
System.out.println("FlyBehavior.FlyWithWings");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public interface QuackBehavior {
|
|
|
|
|
void quack();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class Quack implements QuackBehavior{
|
|
|
|
|
@Override
|
|
|
|
|
public void quack() {
|
|
|
|
|
System.out.println("QuackBehavior.Quack");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class MuteQuack implements QuackBehavior{
|
|
|
|
|
@Override
|
|
|
|
|
public void quack() {
|
|
|
|
|
System.out.println("QuackBehavior.MuteQuack");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class Squeak implements QuackBehavior{
|
|
|
|
|
@Override
|
|
|
|
|
public void quack() {
|
|
|
|
|
System.out.println("QuackBehavior.Squeak");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class MiniDuckSimulator {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Duck mallarDuck = new MallarDuck();
|
|
|
|
|
mallarDuck.performQuack();
|
|
|
|
|
mallarDuck.performFly();
|
|
|
|
|
mallarDuck.setFlyBehavior(new FlyNoWay());
|
|
|
|
|
mallarDuck.performFly();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
执行结果
|
|
|
|
|
```html
|
|
|
|
|
QuackBehavior.Quack
|
|
|
|
|
FlyBehavior.FlyWithWings
|
|
|
|
|
FlyBehavior.FlyNoWay
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# 第二章 观察者模式
|
|
|
|
|
|
|
|
|
|
**1. 模式定义**
|
|
|
|
|
|
|
|
|
|
定义了对象之间的一对多依赖,当一个对象改变状态时,它的所有依赖者都会受到通知并自动更新。主题(Subject)是被观察的对象,而其所有依赖者(Observer)成为观察者。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//26cb5e7e-6fa3-44ad-854e-fe24d1a5278c.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**2. 模式类图**
|
|
|
|
|
|
|
|
|
|
主题中具有注册和移除观察者,并通知所有注册者的功能,主题是通过维护一张观察者列表来实现这些操作的。
|
|
|
|
|
|
|
|
|
|
观察者拥有一个主题对象的引用,因为注册、移除还有数据都在主题当中,必须通过操作主题才能完成相应功能。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//5c558190-fccd-4b5e-98ed-1896653fc97f.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**3. 问题描述**
|
|
|
|
|
|
|
|
|
|
天气数据布告板会在天气信息发生改变时更新其内容,布告板有多个,并且在将来会继续增加。
|
|
|
|
|
|
|
|
|
|
**4. 解决方案类图**
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//760a5d63-d96d-4dd9-bf9a-c3d126b2f401.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**5. 设计原则**
|
|
|
|
|
|
|
|
|
|
**为交互对象之间的松耦合设计而努力** 当两个对象之间松耦合,它们依然可以交互,但是不太清楚彼此的细节。由于松耦合的两个对象之间互相依赖程度很低,因此系统具有弹性,能够应对变化。
|
|
|
|
|
|
|
|
|
|
**6. 实现代码**
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public interface Subject {
|
|
|
|
|
public void resisterObserver(Observer o);
|
|
|
|
|
public void removeObserver(Observer o);
|
|
|
|
|
public void notifyObserver();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class WeatherData implements Subject {
|
|
|
|
|
private List<Observer> observers;
|
|
|
|
|
private float temperature;
|
|
|
|
|
private float humidity;
|
|
|
|
|
private float pressure;
|
|
|
|
|
|
|
|
|
|
public WeatherData() {
|
|
|
|
|
observers = new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void resisterObserver(Observer o) {
|
|
|
|
|
observers.add(o);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void removeObserver(Observer o) {
|
|
|
|
|
int i = observers.indexOf(o);
|
|
|
|
|
if (i >= 0) {
|
|
|
|
|
observers.remove(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void notifyObserver() {
|
|
|
|
|
for (Observer o : observers) {
|
|
|
|
|
o.update(temperature, humidity, pressure);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setMeasurements(float temperature, float humidity, float pressure) {
|
|
|
|
|
this.temperature = temperature;
|
|
|
|
|
this.humidity = humidity;
|
|
|
|
|
this.pressure = pressure;
|
|
|
|
|
notifyObserver();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public interface Observer {
|
|
|
|
|
public void update(float temp, float humidity, float pressure);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class CurrentConditionsDisplay implements Observer {
|
|
|
|
|
private Subject weatherData;
|
|
|
|
|
|
|
|
|
|
public CurrentConditionsDisplay(Subject weatherData) {
|
|
|
|
|
this.weatherData = weatherData;
|
|
|
|
|
weatherData.resisterObserver(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void update(float temp, float humidity, float pressure) {
|
|
|
|
|
System.out.println("CurrentConditionsDisplay.update:" + temp + " " + humidity + " " + pressure);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class StatisticsDisplay implements Observer {
|
|
|
|
|
private Subject weatherData;
|
|
|
|
|
|
2018-03-03 20:49:24 +08:00
|
|
|
|
public StatisticsDisplay(Subject weatherData) {
|
2018-03-02 10:58:04 +08:00
|
|
|
|
this.weatherData = weatherData;
|
|
|
|
|
weatherData.resisterObserver(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void update(float temp, float humidity, float pressure) {
|
|
|
|
|
System.out.println("StatisticsDisplay.update:" + temp + " " + humidity + " " + pressure);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class WeatherStation {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
WeatherData weatherData = new WeatherData();
|
|
|
|
|
CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);
|
|
|
|
|
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
|
|
|
|
|
|
|
|
|
|
weatherData.setMeasurements(0, 0, 0);
|
|
|
|
|
weatherData.setMeasurements(1, 1, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
执行结果
|
|
|
|
|
```html
|
|
|
|
|
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
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# 第三章 装饰模式
|
|
|
|
|
|
|
|
|
|
**1. 问题描述**
|
|
|
|
|
|
|
|
|
|
设计不同种类的饮料,并且每种饮料可以动态添加新的材料,比如可以添加牛奶。计算一种饮料的价格。
|
|
|
|
|
|
|
|
|
|
**2. 模式定义**
|
|
|
|
|
|
|
|
|
|
动态地将责任附加到对象上。在扩展功能上,装饰者提供了比继承更有弹性的替代方案。
|
|
|
|
|
|
|
|
|
|
下图中 DarkRoast 对象被 Mocha 包裹,Mocha 对象又被 Whip 包裹,并且他们都继承自相同父类,都有 cost() 方法,但是外层对象的 cost() 方法实现调用了内层对象的 cost() 方法。因此,如果要在 DarkRoast 上添加 Mocha,那么只需要用 Mocha 包裹 DarkRoast,如果还需要 Whip ,就用 Whip 包裹 Mocha,最后调用 cost() 方法能把三种对象的价格都包含进去。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//41a4cb30-f393-4b3b-abe4-9941ccf8fa1f.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**3. 模式类图**
|
|
|
|
|
|
|
|
|
|
装饰者和具体组件都继承自组件类型,其中具体组件的方法实现不需要依赖于其它对象,而装饰者拥有一个组件类型对象,这样它可以装饰其它装饰者或者具体组件。所谓装饰,就是把这个装饰者套在被装饰的对象之外,从而动态扩展被装饰者的功能。装饰者的方法有一部分是自己的,这属于它的功能,然后调用被装饰者的方法实现,从而也保留了被装饰者的功能。可以看到,具体组件应当是装饰层次的最低层,因为只有具体组件有直接实现而不需要委托给其它对象去处理。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//3dc454fb-efd4-4eb8-afde-785b2182caeb.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**4. 问题解决方案的类图**
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//9c997ac5-c8a7-44fe-bf45-2c10eb773e53.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**5. 设计原则**
|
|
|
|
|
|
|
|
|
|
**类应该对扩展开放,对修改关闭。** 也就是添加新功能时不需要修改代码。在本章问题中该原则体现在,在饮料中添加新的材料,而不需要去修改饮料的代码。观察则模式也符合这个原则。不可能所有类都能实现这个原则,应当把该原则应用于设计中最有可能改变的地方。
|
|
|
|
|
|
|
|
|
|
**6. Java I/O 中的装饰者模式**
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//2a40042a-03c8-4556-ad1f-72d89f8c555c.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**7. 代码实现**
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public interface Beverage {
|
|
|
|
|
public double cost();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class HouseBlend implements Beverage{
|
|
|
|
|
@Override
|
|
|
|
|
public double cost() {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class DarkRoast implements Beverage{
|
|
|
|
|
@Override
|
|
|
|
|
public double cost() {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public abstract class CondimentDecorator implements Beverage{
|
|
|
|
|
protected Beverage beverage;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class Mocha extends CondimentDecorator {
|
|
|
|
|
|
|
|
|
|
public Mocha(Beverage beverage) {
|
|
|
|
|
this.beverage = beverage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public double cost() {
|
|
|
|
|
return 1 + beverage.cost();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class Milk extends CondimentDecorator {
|
|
|
|
|
|
|
|
|
|
public Milk(Beverage beverage) {
|
|
|
|
|
this.beverage = beverage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public double cost() {
|
|
|
|
|
return 1 + beverage.cost();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class StartbuzzCoffee {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Beverage beverage = new HouseBlend();
|
|
|
|
|
beverage = new Mocha(beverage);
|
|
|
|
|
beverage = new Milk(beverage);
|
|
|
|
|
System.out.println(beverage.cost());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
输出
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
3.0
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# 第四章 工厂模式
|
|
|
|
|
|
|
|
|
|
## 1. 简单工厂
|
|
|
|
|
|
|
|
|
|
**1. 问题描述**
|
|
|
|
|
|
|
|
|
|
有不同的 Pizza,根据不同的情况用不同的子类实例化一个 Pizza 对象。
|
|
|
|
|
|
|
|
|
|
**2. 定义**
|
|
|
|
|
|
|
|
|
|
简单工厂不是设计模式,更像是一种编程习惯。在实例化一个超类的对象时,可以用它的所有子类来进行实例化,要根据具体需求来决定使用哪个子类。在这种情况下,把实例化的操作放到工厂来中,让工厂类来决定应该用哪个子类来实例化。这样做把客户对象和具体子类的实现解耦,客户对象不再需要知道有哪些子类以及实例化哪个子类。因为客户类往往有多个,如果不使用简单工厂,那么所有的客户类都要知道所有子类的细节,一旦子类发生改变,例如增加子类,那么所有的客户类都要发生改变。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//c470eb9b-fb05-45c5-8bb7-1057dc3c16de.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**3. 解决方案类图**
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//dc3e704c-7c57-42b8-93ea-ddd068665964.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**4. 代码实现**
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public interface Pizza {
|
|
|
|
|
public void make();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class CheesePizza implements Pizza{
|
|
|
|
|
@Override
|
|
|
|
|
public void make() {
|
|
|
|
|
System.out.println("CheesePizza");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class GreekPizza implements Pizza{
|
|
|
|
|
@Override
|
|
|
|
|
public void make() {
|
|
|
|
|
System.out.println("GreekPizza");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class SimplePizzaFactory {
|
|
|
|
|
public Pizza createPizza(String type) {
|
|
|
|
|
if (type.equals("cheese")) {
|
|
|
|
|
return new CheesePizza();
|
|
|
|
|
} else if (type.equals("greek")) {
|
|
|
|
|
return new GreekPizza();
|
|
|
|
|
} else {
|
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class PizzaStore {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
SimplePizzaFactory simplePizzaFactory = new SimplePizzaFactory();
|
|
|
|
|
Pizza pizza = simplePizzaFactory.createPizza("cheese");
|
|
|
|
|
pizza.make();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
运行结果
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
CheesePizza
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 2. 工厂方法模式
|
|
|
|
|
|
|
|
|
|
**1. 问题描述**
|
|
|
|
|
|
|
|
|
|
每个地区的 Pizza 店虽然种类相同,但是都有自己的风味,需要单独区分。例如,一个客户点了纽约的 cheese 种类的 Pizza 和在芝加哥点的相同种类的 Pizza 是不同的。
|
|
|
|
|
|
|
|
|
|
**2. 模式定义**
|
|
|
|
|
|
|
|
|
|
定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。
|
|
|
|
|
|
|
|
|
|
**3. 模式类图**
|
|
|
|
|
|
|
|
|
|
在简单工厂中,创建对象的是另一个类,而在工厂方法中,是由子类来创建对象。下图中,Creator 有一个 anOperation() 方法,这个方法需要用到一组产品类,这组产品类由每个子类来创建。
|
|
|
|
|
|
|
|
|
|
可以为每个子类创建单独的简单工厂来创建每一个产品类,但是把简单工厂中创建对象的代码放到子类中来可以减少类的数目,因为子类不算是产品类,因此完全可以这么做。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//903093ec-acc8-4f9b-bf2c-b990b9a5390c.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**4. 解决方案类图**
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//664f8901-5dc7-4644-a072-dad88cc5133a.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**5. 代码实现**
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public interface Pizza {
|
|
|
|
|
public void make();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public interface PizzaStore {
|
|
|
|
|
public Pizza orderPizza(String item);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class NYStyleCheesePizza implements Pizza{
|
|
|
|
|
@Override
|
|
|
|
|
public void make() {
|
|
|
|
|
System.out.println("NYStyleCheesePizza is making..");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class NYStyleVeggiePizza implements Pizza {
|
|
|
|
|
@Override
|
|
|
|
|
public void make() {
|
|
|
|
|
System.out.println("NYStyleVeggiePizza is making..");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class ChicagoStyleCheesePizza implements Pizza{
|
|
|
|
|
@Override
|
|
|
|
|
public void make() {
|
|
|
|
|
System.out.println("ChicagoStyleCheesePizza is making..");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class ChicagoStyleVeggiePizza implements Pizza{
|
|
|
|
|
@Override
|
|
|
|
|
public void make() {
|
|
|
|
|
System.out.println("ChicagoStyleVeggiePizza is making..");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class NYPizzaStore implements PizzaStore {
|
|
|
|
|
@Override
|
|
|
|
|
public Pizza orderPizza(String item) {
|
|
|
|
|
Pizza pizza = null;
|
|
|
|
|
if (item.equals("cheese")) {
|
|
|
|
|
pizza = new NYStyleCheesePizza();
|
|
|
|
|
} else if (item.equals("veggie")) {
|
|
|
|
|
pizza = new NYStyleVeggiePizza();
|
|
|
|
|
} else {
|
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
|
}
|
|
|
|
|
pizza.make();
|
|
|
|
|
return pizza;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class ChicagoPizzaStore implements PizzaStore {
|
|
|
|
|
@Override
|
|
|
|
|
public Pizza orderPizza(String item) {
|
|
|
|
|
Pizza pizza = null;
|
|
|
|
|
if (item.equals("cheese")) {
|
|
|
|
|
pizza = new ChicagoStyleCheesePizza();
|
|
|
|
|
} else if (item.equals("veggie")) {
|
|
|
|
|
pizza = new ChicagoStyleVeggiePizza();
|
|
|
|
|
} else {
|
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
|
}
|
|
|
|
|
pizza.make();
|
|
|
|
|
return pizza;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class PizzaTestDrive {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
PizzaStore nyStore = new NYPizzaStore();
|
|
|
|
|
nyStore.orderPizza("cheese");
|
|
|
|
|
PizzaStore chicagoStore = new ChicagoPizzaStore();
|
|
|
|
|
chicagoStore.orderPizza("cheese");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
运行结果
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
NYStyleCheesePizza is making..
|
|
|
|
|
ChicagoStyleCheesePizza is making..
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 3. 抽象工厂模式
|
|
|
|
|
|
|
|
|
|
**1. 设计原则**
|
|
|
|
|
|
|
|
|
|
**依赖倒置原则**:要依赖抽象,不要依赖具体类。听起来像是针对接口编程,不针对实现编程,但是这个原则说明了:不能让高层组件依赖底层组件,而且,不管高层或底层组件,两者都应该依赖于抽象。例如,下图中 PizzaStore 属于高层组件,它依赖的是 Pizza 的抽象类,这样就可以不用关心 Pizza 的具体实现细节。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//ddf72ca9-c0be-49d7-ab81-57a99a974c8e.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**2. 模式定义**
|
|
|
|
|
|
|
|
|
|
提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
|
|
|
|
|
|
|
|
|
|
**3. 模式类图**
|
|
|
|
|
|
|
|
|
|
抽象工厂模式创建的是对象家族,也就是很多对象而不是一个对象,并且这些对象是相关的,也就是说必须一起创建出来。而工厂模式只是用于创建一个对象,这和抽象工厂模式有很大不同。并且,抽象工厂模式也用到了工厂模式来创建单一对象,在类图左部,AbstractFactory 中的 CreateProductA 和 CreateProductB 方法都是让子类来实现,这两个方法单独来看就是在创建一个对象,这符合工厂模式的定义。至于创建对象的家族这一概念是在 Client 体现,Client 要通过 AbstractFactory 同时调用两个方法来创建出两个对象,在这里这两个对象就有很大的相关性,Client 需要这两个对象的协作才能完成任务。从高层次来看,抽象工厂使用了组合,即 Cilent 组合了 AbstractFactory ,而工厂模式使用了继承。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//d301774f-e0d2-41f3-95f4-bfe39859b52e.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**4. 解决方案类图**
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//8785dabd-1285-4bd0-b3aa-b05cc060a24a.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**5. 代码实现**
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public interface Dough {
|
|
|
|
|
public String doughType();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class ThickCrustDough implements Dough{
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String doughType() {
|
|
|
|
|
return "ThickCrustDough";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class ThinCrustDough implements Dough {
|
|
|
|
|
@Override
|
|
|
|
|
public String doughType() {
|
|
|
|
|
return "ThinCrustDough";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public interface Sauce {
|
|
|
|
|
public String sauceType();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class MarinaraSauce implements Sauce {
|
|
|
|
|
@Override
|
|
|
|
|
public String sauceType() {
|
|
|
|
|
return "MarinaraSauce";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class PlumTomatoSauce implements Sauce {
|
|
|
|
|
@Override
|
|
|
|
|
public String sauceType() {
|
|
|
|
|
return "PlumTomatoSauce";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public interface PizzaIngredientFactory {
|
|
|
|
|
public Dough createDough();
|
|
|
|
|
public Sauce createSauce();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class NYPizzaIngredientFactory implements PizzaIngredientFactory{
|
|
|
|
|
@Override
|
|
|
|
|
public Dough createDough() {
|
|
|
|
|
return new ThickCrustDough();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Sauce createSauce() {
|
|
|
|
|
return new MarinaraSauce();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory{
|
|
|
|
|
@Override
|
|
|
|
|
public Dough createDough() {
|
|
|
|
|
return new ThinCrustDough();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Sauce createSauce() {
|
|
|
|
|
return new PlumTomatoSauce();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class NYPizzaStore {
|
|
|
|
|
private PizzaIngredientFactory ingredientFactory;
|
|
|
|
|
|
|
|
|
|
public NYPizzaStore() {
|
|
|
|
|
ingredientFactory = new NYPizzaIngredientFactory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void makePizza() {
|
|
|
|
|
Dough dough = ingredientFactory.createDough();
|
|
|
|
|
Sauce sauce = ingredientFactory.createSauce();
|
|
|
|
|
System.out.println(dough.doughType());
|
|
|
|
|
System.out.println(sauce.sauceType());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class NYPizzaStoreTestDrive {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
NYPizzaStore nyPizzaStore = new NYPizzaStore();
|
|
|
|
|
nyPizzaStore.makePizza();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
运行结果
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
ThickCrustDough
|
|
|
|
|
MarinaraSauce
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# 第五章 单件模式
|
|
|
|
|
|
|
|
|
|
**1. 模式定义**
|
|
|
|
|
|
|
|
|
|
确保一个类只有一个实例,并提供了一个全局访问点。
|
|
|
|
|
|
|
|
|
|
**2. 模式类图**
|
|
|
|
|
|
2018-03-03 10:33:01 +08:00
|
|
|
|
使用一个私有构造器、一个私有静态变量以及一个公有静态函数来实现。私有构造函数保证了不能通过构造函数来创建对象实例,只能通过公有静态函数返回唯一的私有静态变量。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//59aff6c1-8bc5-48e4-9e9c-082baeb2f274.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 21:21:59 +08:00
|
|
|
|
**3. 懒汉式-线程不安全**
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 10:58:53 +08:00
|
|
|
|
以下实现中,私有静态变量被延迟化实例化,这样做的好处是,如果没有用到该类,那么就不会创建私有静态变量,从而节约资源。
|
|
|
|
|
|
|
|
|
|
这个实现在多线程环境下是不安全的,如果多个线程能够同时进入 if(uniqueInstance == null) 内的语句块,那么就会多次实例化 uniqueInstance 私有静态变量。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public class Singleton {
|
|
|
|
|
|
|
|
|
|
private static Singleton uniqueInstance;
|
|
|
|
|
|
|
|
|
|
private Singleton() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Singleton getUniqueInstance() {
|
|
|
|
|
if (uniqueInstance == null) {
|
|
|
|
|
uniqueInstance = new Singleton();
|
|
|
|
|
}
|
|
|
|
|
return uniqueInstance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-03-03 21:21:59 +08:00
|
|
|
|
**4. 懒汉式-线程安全**
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 10:58:53 +08:00
|
|
|
|
只需要对 getUniqueInstance() 方法加锁,那么在一个时间点只能有一个线程能够进入该方法,从而避免了对 uniqueInstance 进行多次实例化的问题。但是这样有一个问题,就是当一个线程进入该方法之后,其它线程视图进入该方法都必须等待,因此性能上有一定的损耗。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public static synchronized Singleton getUniqueInstance() {
|
|
|
|
|
if (uniqueInstance == null) {
|
|
|
|
|
uniqueInstance = new Singleton();
|
|
|
|
|
}
|
|
|
|
|
return uniqueInstance;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-03-03 21:21:59 +08:00
|
|
|
|
**5. 饿汉式-线程安全**
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 10:58:53 +08:00
|
|
|
|
线程不安全问题主要是由于静态实例变量被初始化了多次,那么静态实例变量采用直接实例化就可以解决问题。但是直接初始化的方法也丢失了延迟初始化节约资源的优势。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
private static Singleton uniqueInstance = new Singleton();
|
|
|
|
|
```
|
|
|
|
|
|
2018-03-03 21:21:59 +08:00
|
|
|
|
**6. 双重校验锁-线程安全**
|
2018-03-03 20:49:24 +08:00
|
|
|
|
|
2018-03-03 21:21:59 +08:00
|
|
|
|
因为 uniqueInstance 只需要被初始化一次,之后就可以直接使用了。加锁操作只需要对初始化那部分的代码进行,也就是说,只有当 uniqueInstance 没有被初始化时,才需要进行加锁。
|
2018-03-03 20:49:24 +08:00
|
|
|
|
|
2018-03-03 21:21:59 +08:00
|
|
|
|
双重校验锁先判断 uniqueInstance 是否已经被初始化了,如果没有被初始化,那么才对初始化的语句进行加锁。如果只做一次判断,那么多个线程还是有可能同时进入实例化语句块的,因此需要仅此第二次的判断。
|
2018-03-03 20:49:24 +08:00
|
|
|
|
|
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public class Singleton {
|
|
|
|
|
|
|
|
|
|
private volatile static Singleton uniqueInstance;
|
|
|
|
|
|
|
|
|
|
private Singleton() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Singleton getUniqueInstance() {
|
|
|
|
|
if (uniqueInstance == null) {
|
|
|
|
|
synchronized (Singleton.class) {
|
|
|
|
|
if (uniqueInstance == null) {
|
|
|
|
|
uniqueInstance = new Singleton();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return uniqueInstance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# 第六章 命令模式
|
|
|
|
|
|
|
|
|
|
**1. 问题描述**
|
|
|
|
|
|
2018-03-03 11:18:39 +08:00
|
|
|
|
设计一个遥控器,它有很多按钮,每个按钮可以发起一个命令,让一个家电完成相应操作。
|
|
|
|
|
|
|
|
|
|
有非常多的家电,并且之后会增加家电。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//7b8f0d8e-a4fa-4c9d-b9a0-3e6a11cb3e33.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//c3ca36b2-8459-4cf1-98b0-cc95a0e94f20.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**2. 模式定义**
|
|
|
|
|
|
|
|
|
|
将命令封装成对象,以便使用不同的命令来参数化其它对象。
|
|
|
|
|
|
2018-03-03 11:18:39 +08:00
|
|
|
|
**3. 解决方案类图**
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 11:18:39 +08:00
|
|
|
|
- RemoteControl 是遥控器,它可以为每个按钮设置命令对象,并且调用命令对象的 execute() 方法。
|
2018-03-03 11:35:37 +08:00
|
|
|
|
|
2018-03-03 11:18:39 +08:00
|
|
|
|
- Command 就是命令对象,命令模式正式将各种命令封装成 Commad 对象来实现的。
|
2018-03-03 11:35:37 +08:00
|
|
|
|
|
2018-03-03 11:18:39 +08:00
|
|
|
|
- Light 是命令真正的执行者。可以注意到 LightOnCommand 和 LightOffCommand 类组合了一个 Light 对象,通过组合的方法,就可以将 excute() 方法委托给 Light 对象来执行。
|
2018-03-03 11:35:37 +08:00
|
|
|
|
|
2018-03-03 11:18:39 +08:00
|
|
|
|
- RemoteLoader 是客户端,注意它与 RemoteControl 的区别。因为 RemoteControl 不能主动地调用自身的方法,因此也就不能当成是客户端。客户端好比人,只有人才能去真正去使用遥控器。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//5ef94f62-98ce-464d-a646-842d9c72c8b8.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 11:18:39 +08:00
|
|
|
|
**4. 模式类图**
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//1e09d75f-6268-4425-acf8-8ecd1b4a0ef3.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**5. 代码实现**
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public interface Command {
|
|
|
|
|
public void execute();
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 11:18:39 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public class Light {
|
|
|
|
|
|
|
|
|
|
public void on() {
|
|
|
|
|
System.out.println("Light is on!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void off() {
|
|
|
|
|
System.out.println("Light is off!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 11:18:39 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public class LightOnCommand implements Command{
|
|
|
|
|
Light light;
|
|
|
|
|
|
|
|
|
|
public LightOnCommand(Light light) {
|
|
|
|
|
this.light = light;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void execute() {
|
|
|
|
|
light.on();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 11:18:39 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
/**
|
2018-03-03 21:21:59 +08:00
|
|
|
|
* 遥控器类
|
|
|
|
|
*/
|
2018-03-02 10:58:04 +08:00
|
|
|
|
public class SimpleRemoteControl {
|
|
|
|
|
Command slot;
|
|
|
|
|
|
|
|
|
|
public SimpleRemoteControl() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCommand(Command command) {
|
|
|
|
|
this.slot = command;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void buttonWasPressed() {
|
|
|
|
|
slot.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 11:18:39 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
2018-03-03 11:18:39 +08:00
|
|
|
|
/**
|
2018-03-03 21:21:59 +08:00
|
|
|
|
* 客户端
|
|
|
|
|
*/
|
2018-03-02 10:58:04 +08:00
|
|
|
|
public class RemoteLoader {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
SimpleRemoteControl remote = new SimpleRemoteControl();
|
|
|
|
|
Light light = new Light();
|
|
|
|
|
LightOnCommand lightOnCommand = new LightOnCommand(light);
|
|
|
|
|
remote.setCommand(lightOnCommand);
|
|
|
|
|
remote.buttonWasPressed();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
输出
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Light is on!
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# 第七章 适配器模式与外观模式
|
|
|
|
|
|
|
|
|
|
## 1. 适配器模式
|
|
|
|
|
|
|
|
|
|
**1. 模式定义**
|
|
|
|
|
|
|
|
|
|
将一个类的接口,转换为客户期望的另一个接口。适配器让原本不兼容的类可以合作无间。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//8e8ba824-7a9e-4934-a212-e6a41dcc1602.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**2. 模式类图**
|
|
|
|
|
|
2018-03-03 11:35:37 +08:00
|
|
|
|
有两种适配器模式的实现,一种是对象方式,一种是类方式。对象方式是通过组合的方法,让适配器类(Adapter)拥有一个待适配的对象(Adaptee),从而把相应的处理委托给待适配的对象。类方式用到多重继承,Adapter 继承 Target 和 Adaptee,先把 Adapter 当成 Adaptee 类型然后实例化一个对象,再把它当成 Target 类型的,这样 Client 就可以把这个对象当成 Target 的对象来处理,同时拥有 Adaptee 的方法。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//253bd869-ea48-4092-9aed-6906ccb2f3b0.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//a797959a-0ed5-475b-8d97-df157c672019.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**3. 问题描述**
|
|
|
|
|
|
2018-03-03 11:35:37 +08:00
|
|
|
|
鸭子(Duck)和火鸡(Turkey)拥有不同的叫声,Duck 调用的是 quack() 方法,而 Turkey 调用 gobble() 方法。
|
|
|
|
|
|
|
|
|
|
要求将 Turkey 的 gobble() 方法适配成 Duck 的 quack() 方法。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**4. 解决方案类图**
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//1a511c76-bb6b-40ab-b8aa-39eeb619d673.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**5. 代码实现**
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public interface Duck {
|
|
|
|
|
public void quack();
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 11:35:37 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public interface Turkey {
|
|
|
|
|
public void gobble();
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 11:35:37 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public class WildTurkey implements Turkey{
|
|
|
|
|
@Override
|
|
|
|
|
public void gobble() {
|
|
|
|
|
System.out.println("gobble!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 11:35:37 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public class TurkeyAdapter implements Duck{
|
|
|
|
|
Turkey turkey;
|
|
|
|
|
|
|
|
|
|
public TurkeyAdapter(Turkey turkey) {
|
|
|
|
|
this.turkey = turkey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void quack() {
|
|
|
|
|
turkey.gobble();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 11:35:37 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public class DuckTestDrive {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Turkey turkey = new WildTurkey();
|
|
|
|
|
Duck duck = new TurkeyAdapter(turkey);
|
|
|
|
|
duck.quack();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
运行结果
|
2018-03-03 11:35:37 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```html
|
|
|
|
|
gobble!
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 2. 外观模式
|
|
|
|
|
|
|
|
|
|
**1. 模式定义**
|
|
|
|
|
|
2018-03-03 11:41:55 +08:00
|
|
|
|
提供了一个统一的接口,用来访问子系统中的一群接口,从而让子系统更容易使用。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**2. 模式类图**
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//78f2314e-2643-41df-8f3d-b7e28294094b.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**3. 问题描述**
|
|
|
|
|
|
2018-03-03 11:41:55 +08:00
|
|
|
|
家庭影院中有众多电器,当要进行观看电影时需要对很多电器进行操作。要求简化这些操作,使得家庭影院类只提供一个简化的接口,例如提供一个看电影相关的接口。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//106f5585-b2e7-4718-be5d-3b322d1ef42a.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**4. 解决方案类图**
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//25387681-89f8-4365-a2fa-83b86449ee84.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**5. 设计原则**
|
|
|
|
|
|
2018-03-03 11:41:55 +08:00
|
|
|
|
**最少知识原则**:只和你的密友谈话。也就是应当使得客户对象所需要交互的对象尽可能少。
|
|
|
|
|
|
|
|
|
|
**6. 代码实现**
|
|
|
|
|
|
|
|
|
|
过于简单,无实现。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
# 第八章 模板方法模式
|
|
|
|
|
|
|
|
|
|
**1. 模式定义**
|
|
|
|
|
|
|
|
|
|
在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。
|
|
|
|
|
|
|
|
|
|
模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。
|
|
|
|
|
|
|
|
|
|
**2. 模式类图**
|
|
|
|
|
|
2018-03-03 11:56:29 +08:00
|
|
|
|
模板方法 templateMethod() 定义了算法的骨架,确定了 primitiveOperation1() 和 primitiveOperation2() 方法执行的顺序,而 primitiveOperation1() 和 primitiveOperation2() 让子类去实现。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//ed62f400-192c-4185-899b-187958201f0c.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**3. 问题描述**
|
|
|
|
|
|
2018-03-03 11:56:29 +08:00
|
|
|
|
冲咖啡和冲茶都有类似的流程,但是某些步骤会有点不一样,要求复用那些相同步骤的代码。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//d8f873fc-00bc-41ee-a87c-c1b4c0172844.png"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**4. 解决方案类图**
|
|
|
|
|
|
2018-03-03 11:56:29 +08:00
|
|
|
|
其中 prepareRecipe() 方法就是模板方法,它确定了其它四个方法的具体执行步骤。其中 brew() 和 addCondiments() 方法在子类中实现。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//aa20c123-b6b5-432a-83d3-45dc39172192.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**5. 设计原则**
|
|
|
|
|
|
2018-03-03 11:56:29 +08:00
|
|
|
|
**好莱坞原则**:别调用(打电话给)我们,我们会调用(打电话给)你。这一原则可以防止依赖腐败,即防止高层组件依赖低层组件,低层组件又依赖高层组件。该原则在模板方法的体现为,只有父类会调用子类,子类不会调用父类。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**6. 钩子**
|
|
|
|
|
|
2018-03-03 11:56:29 +08:00
|
|
|
|
钩子(hock):某些步骤在不同实现中可有可无,可以先定义一个什么都不做的方法,把它加到模板方法中,如果子类需要它就覆盖默认实现并加上自己的实现。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**7. 代码实现**
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public abstract class CaffeineBeverage {
|
|
|
|
|
|
|
|
|
|
final void prepareRecipe(){
|
|
|
|
|
boilWater();
|
|
|
|
|
brew();
|
|
|
|
|
pourInCup();
|
|
|
|
|
addCondiments();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abstract void brew();
|
|
|
|
|
|
|
|
|
|
abstract void addCondiments();
|
|
|
|
|
|
|
|
|
|
void boilWater(){
|
|
|
|
|
System.out.println("boilWater");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pourInCup(){
|
|
|
|
|
System.out.println("pourInCup");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 11:56:29 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public class Coffee extends CaffeineBeverage{
|
|
|
|
|
@Override
|
|
|
|
|
void brew() {
|
|
|
|
|
System.out.println("Coffee.brew");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
void addCondiments() {
|
|
|
|
|
System.out.println("Coffee.addCondiments");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 11:56:29 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public class Tea extends CaffeineBeverage{
|
|
|
|
|
@Override
|
|
|
|
|
void brew() {
|
|
|
|
|
System.out.println("Tea.brew");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
void addCondiments() {
|
|
|
|
|
System.out.println("Tea.addCondiments");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 11:56:29 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public class CaffeineBeverageTestDrive {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
CaffeineBeverage caffeineBeverage = new Coffee();
|
|
|
|
|
caffeineBeverage.prepareRecipe();
|
|
|
|
|
System.out.println("-----------");
|
|
|
|
|
caffeineBeverage = new Tea();
|
|
|
|
|
caffeineBeverage.prepareRecipe();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
运行结果
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
boilWater
|
|
|
|
|
Coffee.brew
|
|
|
|
|
pourInCup
|
|
|
|
|
Coffee.addCondiments
|
|
|
|
|
-----------
|
|
|
|
|
boilWater
|
|
|
|
|
Tea.brew
|
|
|
|
|
pourInCup
|
|
|
|
|
Tea.addCondiments
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# 第九章 迭代器和组合模式
|
|
|
|
|
|
|
|
|
|
## 1. 迭代器模式
|
|
|
|
|
|
|
|
|
|
**1. 模式定义**
|
|
|
|
|
|
2018-03-03 12:05:21 +08:00
|
|
|
|
提供顺序访问一个聚合对象中的各个元素的方法,而又不暴露聚合对象内部的表示。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**2. 模式类图**
|
|
|
|
|
|
2018-03-03 12:05:21 +08:00
|
|
|
|
- Aggregate 是聚合类,其中 createIterator() 方法可以产生一个 Iterator 对象;
|
|
|
|
|
|
|
|
|
|
- Iterator 主要定义了 hasNext() 和 next() 方法。
|
|
|
|
|
|
|
|
|
|
- Client 需要拥有一个 Aggregate 对象,这是很明显的。为了迭代变量 Aggregate 对象,也需要拥有 Iterator 对象。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//439deca7-fed0-4c89-87e5-7088d10f1fdb.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**3. 代码实现**
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public class Aggregate {
|
|
|
|
|
|
|
|
|
|
private int[] items;
|
|
|
|
|
|
|
|
|
|
public Aggregate() {
|
|
|
|
|
items = new int[10];
|
|
|
|
|
for (int i = 0; i < items.length; i++) {
|
|
|
|
|
items[i] = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Iterator createIterator() {
|
|
|
|
|
return new ConcreteIterator(items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 12:05:21 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public interface Iterator {
|
|
|
|
|
boolean hasNext();
|
|
|
|
|
int next();
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 12:05:21 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public class ConcreteIterator implements Iterator {
|
|
|
|
|
|
|
|
|
|
private int[] items;
|
|
|
|
|
private int position = 0;
|
|
|
|
|
|
|
|
|
|
public ConcreteIterator(int[] items) {
|
|
|
|
|
this.items = items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
|
return position < items.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int next() {
|
|
|
|
|
return items[position++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class Client {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Aggregate aggregate = new Aggregate();
|
|
|
|
|
Iterator iterator = aggregate.createIterator();
|
|
|
|
|
while(iterator.hasNext()){
|
|
|
|
|
System.out.println(iterator.next());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
运行结果
|
|
|
|
|
```html
|
|
|
|
|
0
|
|
|
|
|
1
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 2. Java 内置的迭代器
|
|
|
|
|
|
|
|
|
|
**1. 实现接口**
|
|
|
|
|
|
2018-03-03 12:05:21 +08:00
|
|
|
|
在使用 Java 的迭代器实现时,需要让聚合对象去实现 Iterable 接口,该接口有一个 iterator() 方法会返回一个 Iterator 对象。
|
|
|
|
|
|
|
|
|
|
使用 Java 内置的迭代器实现,客户对象可以使用 foreach 循环来遍历聚合对象中的每个元素。
|
|
|
|
|
|
|
|
|
|
Java 中的集合类基本都实现了 Iterable 接口。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**2. 代码实现**
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
|
|
public class Aggregate implements Iterable<Integer>{
|
|
|
|
|
|
|
|
|
|
private int[] items;
|
|
|
|
|
|
|
|
|
|
public Aggregate() {
|
|
|
|
|
items = new int[10];
|
|
|
|
|
for (int i = 0; i < items.length; i++) {
|
|
|
|
|
items[i] = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Iterator<Integer> iterator() {
|
|
|
|
|
return new ConcreteIterator(items);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
|
|
public class ConcreteIterator implements Iterator<Integer> {
|
|
|
|
|
|
|
|
|
|
private int[] items;
|
|
|
|
|
private int position = 0;
|
|
|
|
|
|
|
|
|
|
public ConcreteIterator(int[] items) {
|
|
|
|
|
this.items = items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
|
return position < items.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Integer next() {
|
|
|
|
|
return items[position++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class Client {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Aggregate aggregate = new Aggregate();
|
|
|
|
|
for (int item : aggregate) {
|
|
|
|
|
System.out.println(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 3. 组合模式
|
|
|
|
|
|
|
|
|
|
**1. 设计原则**
|
|
|
|
|
|
|
|
|
|
一个类应该只有一个引起改变的原因。
|
|
|
|
|
|
|
|
|
|
**2. 模式定义**
|
|
|
|
|
|
2018-03-03 21:21:59 +08:00
|
|
|
|
允许将对象组合成树形结构来表现“整体/部分”层次结构。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 14:27:56 +08:00
|
|
|
|
组合能让客户以一致的方式处理个别对象以及组合对象。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**3. 模式类图**
|
|
|
|
|
|
2018-03-03 14:27:56 +08:00
|
|
|
|
组件(Component)类是组合类(Composite)和叶子类(Leaf)的父类,可以把组合类看成是树的中间节点。
|
|
|
|
|
|
|
|
|
|
组合类拥有一个组件对象,因此组合类的操作可以委托给组件对象去处理,而组件对象可以是另一个组合类或者叶子类。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//f99c019e-7e91-4c2e-b94d-b031c402dcb5.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**4. 代码实现**
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public abstract class Component {
|
|
|
|
|
protected String name;
|
|
|
|
|
|
|
|
|
|
public Component(String name) {
|
|
|
|
|
this.name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abstract public void addChild(Component component);
|
|
|
|
|
|
|
|
|
|
public void print() {
|
|
|
|
|
print(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abstract protected void print(int level);
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 14:27:56 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
public class Leaf extends Component {
|
|
|
|
|
public Leaf(String name) {
|
|
|
|
|
super(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void addChild(Component component) {
|
2018-03-03 14:27:56 +08:00
|
|
|
|
throw new UnsupportedOperationException(); // 牺牲透明性换取单一职责原则,这样就不用考虑是叶子节点还是组合节点
|
2018-03-02 10:58:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void print(int level) {
|
|
|
|
|
for (int i = 0; i < level; i++) {
|
|
|
|
|
System.out.print("--");
|
|
|
|
|
}
|
|
|
|
|
System.out.println("left:" + name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-03-03 14:27:56 +08:00
|
|
|
|
```java
|
2018-03-02 10:58:04 +08:00
|
|
|
|
public class Composite extends Component {
|
|
|
|
|
|
|
|
|
|
private List<Component> childs;
|
|
|
|
|
|
|
|
|
|
public Composite(String name) {
|
|
|
|
|
super(name);
|
|
|
|
|
childs = new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void addChild(Component component) {
|
|
|
|
|
childs.add(component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void print(int level) {
|
|
|
|
|
for (int i = 0; i < level; i++) {
|
|
|
|
|
System.out.print("--");
|
|
|
|
|
}
|
|
|
|
|
System.out.println("Composite:" + name);
|
|
|
|
|
for (Component component : childs) {
|
|
|
|
|
component.print(level + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-03-03 14:27:56 +08:00
|
|
|
|
|
2018-03-02 10:58:04 +08:00
|
|
|
|
```java
|
|
|
|
|
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.addChild(node1);
|
|
|
|
|
root.addChild(node2);
|
|
|
|
|
root.addChild(node3);
|
|
|
|
|
Component node21 = new Leaf("21");
|
|
|
|
|
Component node22 = new Composite("22");
|
|
|
|
|
node2.addChild(node21);
|
|
|
|
|
node2.addChild(node22);
|
|
|
|
|
Component node221 = new Leaf("221");
|
|
|
|
|
node22.addChild(node221);
|
|
|
|
|
root.print();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
运行结果
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Composite:root
|
|
|
|
|
--left:1
|
|
|
|
|
--Composite:2
|
|
|
|
|
----left:21
|
|
|
|
|
----Composite:22
|
|
|
|
|
------left:221
|
|
|
|
|
--left:3
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# 第十章 状态模式
|
|
|
|
|
|
|
|
|
|
**1. 模式定义**
|
|
|
|
|
|
2018-03-03 14:27:56 +08:00
|
|
|
|
允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它所属的类。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**2. 模式类图**
|
|
|
|
|
|
2018-03-03 15:22:37 +08:00
|
|
|
|
Context 的 request() 方法委托给 State 对象去处理。当 Context 组合的 State 对象发生改变时,它的行为也就发生了改变。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//c28fd93a-0d55-4a19-810f-72652feee00d.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 15:22:37 +08:00
|
|
|
|
**3. 与策略模式的比较**
|
|
|
|
|
|
|
|
|
|
状态模式的类图和策略模式一样,并且都是能够动态改变对象的行为。
|
|
|
|
|
|
|
|
|
|
但是状态模式是通过状态转移来改变 Context 所组合的 State 对象,而策略模式是通过 Context 本身的决策来改变组合的 Strategy 对象。
|
|
|
|
|
|
|
|
|
|
所谓的状态转移,是指 Context 在运行过程中由于一些条件发生改变而使得 State 对象发生改变,主要必须要是在运行过程中。
|
|
|
|
|
|
|
|
|
|
状态模式主要是用来解决状态转移的问题,当状态发生庄毅了,那么 Context 对象就会改变它的行为;而策略模式主要是用来封装一组可以互相替代的算法族,并且可以根据需要动态地去替换 Context 需要使用哪个算法。
|
|
|
|
|
|
|
|
|
|
**4. 问题描述**
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
糖果销售机有多种状态,每种状态下销售机有不同的行为,状态可以发生转移,使得销售机的行为也发生改变。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//f7d880c9-740a-4a16-ac6d-be502281b4b2.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 15:22:37 +08:00
|
|
|
|
**5. 直接解决方案**
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 15:22:37 +08:00
|
|
|
|
在糖果机的每个操作函数里面,判断当前的状态,根据不同的状态进行不同的处理,并且发生不同的状态转移。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 15:22:37 +08:00
|
|
|
|
这种解决方案在需要增加状态的时候,必须对每个操作的代码都进行修改。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//62ebbb63-8fd7-4488-a866-76a9dc911662.png"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 15:22:37 +08:00
|
|
|
|
**6 代码实现**
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 15:22:37 +08:00
|
|
|
|
糖果销售机即 Context。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
2018-03-03 15:22:37 +08:00
|
|
|
|
下面的实现中每个 State 都组合了 Context 对象,这是因为状态转移的操作在 State 对象中,而状态转移过程又必须改变 Context 对象的 state 对象,因此 State 必须拥有 Context 对象。
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public interface State {
|
|
|
|
|
/**
|
2018-03-03 21:21:59 +08:00
|
|
|
|
* 投入25 分钱
|
2018-03-02 10:58:04 +08:00
|
|
|
|
*/
|
|
|
|
|
void insertQuarter();
|
|
|
|
|
|
|
|
|
|
/**
|
2018-03-03 21:21:59 +08:00
|
|
|
|
* 退回25 分钱
|
2018-03-02 10:58:04 +08:00
|
|
|
|
*/
|
|
|
|
|
void ejectQuarter();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 转动曲柄
|
|
|
|
|
*/
|
|
|
|
|
void turnCrank();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发放糖果
|
|
|
|
|
*/
|
|
|
|
|
void dispense();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class HasQuarterState implements State{
|
|
|
|
|
private GumballMachine gumballMachine;
|
|
|
|
|
|
|
|
|
|
public HasQuarterState(GumballMachine gumballMachine){
|
|
|
|
|
this.gumballMachine = gumballMachine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void insertQuarter() {
|
|
|
|
|
System.out.println("You can't insert another quarter");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void ejectQuarter() {
|
|
|
|
|
System.out.println("Quarter returned");
|
|
|
|
|
gumballMachine.setState(gumballMachine.getNoQuarterState());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void turnCrank() {
|
|
|
|
|
System.out.println("You turned...");
|
|
|
|
|
gumballMachine.setState(gumballMachine.getSoldState());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void dispense() {
|
|
|
|
|
System.out.println("No gumball dispensed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class NoQuarterState implements State {
|
|
|
|
|
GumballMachine gumballMachine;
|
|
|
|
|
|
|
|
|
|
public NoQuarterState(GumballMachine gumballMachine) {
|
|
|
|
|
this.gumballMachine = gumballMachine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void insertQuarter() {
|
|
|
|
|
System.out.println("You insert a quarter");
|
|
|
|
|
gumballMachine.setState(gumballMachine.getHasQuarterState());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void ejectQuarter() {
|
|
|
|
|
System.out.println("You haven't insert a quarter");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void turnCrank() {
|
|
|
|
|
System.out.println("You turned, but there's no quarter");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void dispense() {
|
|
|
|
|
System.out.println("You need to pay first");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class SoldOutState implements State {
|
|
|
|
|
|
|
|
|
|
GumballMachine gumballMachine;
|
|
|
|
|
|
|
|
|
|
public SoldOutState(GumballMachine gumballMachine) {
|
|
|
|
|
this.gumballMachine = gumballMachine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void insertQuarter() {
|
|
|
|
|
System.out.println("You can't insert a quarter, the machine is sold out");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void ejectQuarter() {
|
|
|
|
|
System.out.println("You can't eject, you haven't inserted a quarter yet");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void turnCrank() {
|
|
|
|
|
System.out.println("You turned, but there are no gumballs");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void dispense() {
|
|
|
|
|
System.out.println("No gumball dispensed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class SoldState implements State {
|
|
|
|
|
GumballMachine gumballMachine;
|
|
|
|
|
|
|
|
|
|
public SoldState(GumballMachine gumballMachine) {
|
|
|
|
|
this.gumballMachine = gumballMachine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void insertQuarter() {
|
|
|
|
|
System.out.println("Please wait, we're already giving you a gumball");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void ejectQuarter() {
|
|
|
|
|
System.out.println("Sorry, you already turned the crank");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void turnCrank() {
|
|
|
|
|
System.out.println("Turning twice doesn't get you another gumball!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class GumballMachine {
|
|
|
|
|
private State soldOutState;
|
|
|
|
|
private State noQuarterState;
|
|
|
|
|
private State hasQuarterState;
|
|
|
|
|
private State soldState;
|
|
|
|
|
|
|
|
|
|
private State state;
|
|
|
|
|
private int count = 0;
|
|
|
|
|
|
|
|
|
|
public GumballMachine(int numberGumballs) {
|
|
|
|
|
count = numberGumballs;
|
|
|
|
|
soldOutState = new SoldOutState(this);
|
|
|
|
|
noQuarterState = new NoQuarterState(this);
|
|
|
|
|
hasQuarterState = new HasQuarterState(this);
|
|
|
|
|
soldState = new SoldState(this);
|
|
|
|
|
|
|
|
|
|
if (numberGumballs > 0) {
|
|
|
|
|
state = noQuarterState;
|
|
|
|
|
} else {
|
|
|
|
|
state = soldOutState;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void insertQuarter() {
|
|
|
|
|
state.insertQuarter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ejectQuarter() {
|
|
|
|
|
state.ejectQuarter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void turnCrank() {
|
|
|
|
|
state.turnCrank();
|
|
|
|
|
state.dispense();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setState(State state) {
|
|
|
|
|
this.state = state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void releaseBall() {
|
|
|
|
|
System.out.println("A gumball comes rolling out the slot...");
|
|
|
|
|
if (count != 0) {
|
|
|
|
|
count -= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public State getSoldOutState() {
|
|
|
|
|
return soldOutState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public State getNoQuarterState() {
|
|
|
|
|
return noQuarterState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public State getHasQuarterState() {
|
|
|
|
|
return hasQuarterState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public State getSoldState() {
|
|
|
|
|
return soldState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getCount() {
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
```java
|
|
|
|
|
public class GumballMachineTestDrive {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
GumballMachine gumballMachine = new GumballMachine(5);
|
|
|
|
|
|
|
|
|
|
gumballMachine.insertQuarter();
|
|
|
|
|
gumballMachine.turnCrank();
|
|
|
|
|
|
|
|
|
|
gumballMachine.insertQuarter();
|
|
|
|
|
gumballMachine.ejectQuarter();
|
|
|
|
|
gumballMachine.turnCrank();
|
|
|
|
|
|
|
|
|
|
gumballMachine.insertQuarter();
|
|
|
|
|
gumballMachine.turnCrank();
|
|
|
|
|
gumballMachine.insertQuarter();
|
|
|
|
|
gumballMachine.turnCrank();
|
|
|
|
|
gumballMachine.ejectQuarter();
|
|
|
|
|
|
|
|
|
|
gumballMachine.insertQuarter();
|
|
|
|
|
gumballMachine.insertQuarter();
|
|
|
|
|
gumballMachine.turnCrank();
|
|
|
|
|
gumballMachine.insertQuarter();
|
|
|
|
|
gumballMachine.turnCrank();
|
|
|
|
|
gumballMachine.insertQuarter();
|
|
|
|
|
gumballMachine.turnCrank();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
运行结果
|
|
|
|
|
```html
|
|
|
|
|
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
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# 第十一章 代理模式
|
|
|
|
|
|
|
|
|
|
# 第十二章 复合模式
|
|
|
|
|
|
|
|
|
|
## MVC
|
|
|
|
|
|
|
|
|
|
**传统 MVC**
|
|
|
|
|
|
|
|
|
|
视图使用组合模式,模型使用了观察者模式,控制器使用了策略模式。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//4f67611d-492f-4958-9fa0-4948010e345f.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
**Web 中的 MVC**
|
|
|
|
|
|
|
|
|
|
模式不再使用观察者模式。
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//1dd56e61-2970-4d27-97c2-6e81cee86978.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
# 第十三章 与设计模式相处
|
|
|
|
|
|
|
|
|
|
定义:在某 **情境** 下,针对某 **问题** 的某种 **解决方案**。
|
|
|
|
|
|
|
|
|
|
过度使用设计模式可能导致代码被过度工程化,应该总是用最简单的解决方案完成工作,并在真正需要模式的地方才使用它。
|
|
|
|
|
|
|
|
|
|
反模式:不好的解决方案来解决一个问题。主要作用是为了警告不要使用这些解决方案。
|
|
|
|
|
|
|
|
|
|
模式分类:
|
|
|
|
|
|
2018-03-03 23:19:58 +08:00
|
|
|
|
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//524a237c-ffd7-426f-99c2-929a6bf4c847.jpg"/> </div>
|
2018-03-02 10:58:04 +08:00
|
|
|
|
|
|
|
|
|
# 第十四章 剩下的模式
|