1745 lines
43 KiB
Markdown
1745 lines
43 KiB
Markdown
<!-- GFM-TOC -->
|
||
* [引子](#引子)
|
||
* [第 1 章 设计模式入门](#第-1-章-设计模式入门)
|
||
* [第 2 章 观察者模式](#第-2-章-观察者模式)
|
||
* [第 3 章 装饰模式](#第-3-章-装饰模式)
|
||
* [第 4 章 工厂模式](#第-4-章-工厂模式)
|
||
* [4.1 简单工厂](#41-简单工厂)
|
||
* [4.2 工厂方法模式](#42-工厂方法模式)
|
||
* [4.3 抽象工厂模式](#43-抽象工厂模式)
|
||
* [第 5 章 单件模式](#第-5-章-单件模式)
|
||
* [第 6 章 命令模式](#第-6-章-命令模式)
|
||
* [第 7 章 适配器模式与外观模式](#第-7-章-适配器模式与外观模式)
|
||
* [7.1 适配器模式](#71-适配器模式)
|
||
* [7.2 外观模式](#72-外观模式)
|
||
* [第 8 章 模板方法模式](#第-8-章-模板方法模式)
|
||
* [第 9 章 迭代器和组合模式](#第-9-章-迭代器和组合模式)
|
||
* [9.1 迭代器模式](#91-迭代器模式)
|
||
* [9.2 Java 内置的迭代器](#92-java-内置的迭代器)
|
||
* [9.3 组合模式](#93-组合模式)
|
||
* [第 10 章 状态模式](#第-10-章-状态模式)
|
||
* [第 11 章 代理模式 // TODO](#第-11-章-代理模式--todo)
|
||
* [第 12 章 复合模式](#第-12-章-复合模式)
|
||
* [12.1 MVC](#121-mvc)
|
||
* [12.1.1 传统 MVC](#1211-传统-mvc)
|
||
* [12.1.2 Web 中的 MVC](#1212-web-中的-mvc)
|
||
* [第 13 章 与设计模式相处](#第-13-章-与设计模式相处)
|
||
* [第 14 章 剩下的模式 // TODO](#第-14-章-剩下的模式--todo)
|
||
<!-- GFM-TOC -->
|
||
|
||
# 引子
|
||
|
||
- 学习图片式和交谈式的资料效率会更高。
|
||
|
||
- 只有在自己认为一样东西很重要时,才会去认真对待它。就像遇到老虎,那么大脑会立马发生化学反应,调动一切神经做出反应,因为大脑觉得生存特别重要。
|
||
|
||
- 采用重复手法,运用多种形式,让一个内容在大脑的多个区都有容身之地。
|
||
|
||
- 通过影响情绪,让大脑投入更大的注意力。
|
||
|
||
- 采用拟人的手法,因为人还是比较喜欢和人打交道。
|
||
|
||
- 如果看书效率低下,浮光掠影地在翻书,就要停下来休息一下。
|
||
|
||
# 第 1 章 设计模式入门
|
||
|
||
**1. 设计模式概念**
|
||
|
||
设计模式不是代码,而是解决问题的方案,学习现有的设计模式可以做到经验复用。
|
||
|
||
拥有设计模式词汇,在沟通时就能用更少的词汇来讨论,并且不需要了解底层细节。
|
||
|
||
**2. 问题描述**
|
||
|
||
设计不同种类的鸭子拥有不同的叫声和飞行方式。
|
||
|
||
**3. 简单实现方案**
|
||
|
||
使用继承的解决方案如下,这种方案代码无法复用,如果两个鸭子类拥有同样的飞行方式,就有两份重复的代码。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/144d28a0-1dc5-4aba-8961-ced5bc88428a.jpg)
|
||
|
||
**4. 设计原则**
|
||
|
||
**封装变化**在这里变化的是鸭子叫和飞行的行为方式。
|
||
|
||
**针对接口编程,而不是针对实现编程** 变量声明的类型为父类,而不是具体的某个子类。父类中的方法实现不在父类,而是在各个子类。程序在运行时可以动态改变变量所指向的子类类型。
|
||
|
||
运用这一原则,将叫和飞行的行为抽象出来,实现多种不同的叫和飞行的子类,让子类去实现具体的叫和飞行方式。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1c8ccf5c-7ecd-4b8a-b160-3f72a510ce26.png)
|
||
|
||
**多用组合,少用继承** 组合也就是 has-a 关系,通过组合,可以在运行时动态改变实现,只要通过改变父类对象具体指向哪个子类即可。而继承就不能做到这些,继承体系在创建类时就已经确定。
|
||
|
||
运用这一原则,在 Duck 类中组合 FlyBehavior 和 QuackBehavior 类,performQuack() 和 performFly() 方法委托给这两个类去处理。通过这种方式,一个 Duck 子类可以根据需要去实例化 FlyBehavior 和 QuackBehavior 的子类对象,并且也可以动态地进行改变。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/29574e6f-295c-444e-83c7-b162e8a73a83.jpg)
|
||
|
||
**5. 整体设计图**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e13833c8-e215-462e-855c-1d362bb8d4a0.jpg)
|
||
|
||
**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
|
||
```
|
||
|
||
# 第 2 章 观察者模式
|
||
|
||
**1. 模式定义**
|
||
|
||
定义了对象之间的一对多依赖,当一个对象改变状态时,它的所有依赖者都会受到通知并自动更新。主题(Subject)是被观察的对象,而其所有依赖者(Observer)成为观察者。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/26cb5e7e-6fa3-44ad-854e-fe24d1a5278c.jpg)
|
||
|
||
**2. 模式类图**
|
||
|
||
主题中具有注册和移除观察者,并通知所有注册者的功能,主题是通过维护一张观察者列表来实现这些操作的。
|
||
|
||
观察者拥有一个主题对象的引用,因为注册、移除还有数据都在主题当中,必须通过操作主题才能完成相应功能。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5c558190-fccd-4b5e-98ed-1896653fc97f.jpg)
|
||
|
||
**3. 问题描述**
|
||
|
||
天气数据布告板会在天气信息发生改变时更新其内容,布告板有多个,并且在将来会继续增加。
|
||
|
||
**4. 解决方案类图**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/760a5d63-d96d-4dd9-bf9a-c3d126b2f401.jpg)
|
||
|
||
**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;
|
||
|
||
public StatisticsDisplay(Subject weatherData) {
|
||
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
|
||
```
|
||
|
||
# 第 3 章 装饰模式
|
||
|
||
**1. 问题描述**
|
||
|
||
设计不同种类的饮料,并且每种饮料可以动态添加新的材料,比如可以添加牛奶。计算一种饮料的价格。
|
||
|
||
**2. 模式定义**
|
||
|
||
动态地将责任附加到对象上。在扩展功能上,装饰者提供了比继承更有弹性的替代方案。
|
||
|
||
下图中 DarkRoast 对象被 Mocha 包裹,Mocha 对象又被 Whip 包裹,并且他们都继承自相同父类,都有 cost() 方法,但是外层对象的 cost() 方法实现调用了内层对象的 cost() 方法。因此,如果要在 DarkRoast 上添加 Mocha,那么只需要用 Mocha 包裹 DarkRoast,如果还需要 Whip ,就用 Whip 包裹 Mocha,最后调用 cost() 方法能把三种对象的价格都包含进去。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/41a4cb30-f393-4b3b-abe4-9941ccf8fa1f.jpg)
|
||
|
||
**3. 模式类图**
|
||
|
||
装饰者和具体组件都继承自组件类型,其中具体组件的方法实现不需要依赖于其它对象,而装饰者拥有一个组件类型对象,这样它可以装饰其它装饰者或者具体组件。所谓装饰,就是把这个装饰者套在被装饰的对象之外,从而动态扩展被装饰者的功能。装饰者的方法有一部分是自己的,这属于它的功能,然后调用被装饰者的方法实现,从而也保留了被装饰者的功能。可以看到,具体组件应当是装饰层次的最低层,因为只有具体组件有直接实现而不需要委托给其它对象去处理。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/3dc454fb-efd4-4eb8-afde-785b2182caeb.jpg)
|
||
|
||
**4. 问题解决方案的类图**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9c997ac5-c8a7-44fe-bf45-2c10eb773e53.jpg)
|
||
|
||
**5. 设计原则**
|
||
|
||
**类应该对扩展开放,对修改关闭。**也就是添加新功能时不需要修改代码。在本章问题中该原则体现在,在饮料中添加新的材料,而不需要去修改饮料的代码。观察则模式也符合这个原则。不可能所有类都能实现这个原则,应当把该原则应用于设计中最有可能改变的地方。
|
||
|
||
**6. Java I/O 中的装饰者模式**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2a40042a-03c8-4556-ad1f-72d89f8c555c.jpg)
|
||
|
||
**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
|
||
```
|
||
|
||
# 第 4 章 工厂模式
|
||
|
||
## 4.1 简单工厂
|
||
|
||
**1. 问题描述**
|
||
|
||
有不同的 Pizza,根据不同的情况用不同的子类实例化一个 Pizza 对象。
|
||
|
||
**2. 定义**
|
||
|
||
简单工厂不是设计模式,更像是一种编程习惯。在实例化一个超类的对象时,可以用它的所有子类来进行实例化,要根据具体需求来决定使用哪个子类。在这种情况下,把实例化的操作放到工厂来中,让工厂类来决定应该用哪个子类来实例化。这样做把客户对象和具体子类的实现解耦,客户对象不再需要知道有哪些子类以及实例化哪个子类。因为客户类往往有多个,如果不使用简单工厂,那么所有的客户类都要知道所有子类的细节,一旦子类发生改变,例如增加子类,那么所有的客户类都要发生改变。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c470eb9b-fb05-45c5-8bb7-1057dc3c16de.jpg)
|
||
|
||
**3. 解决方案类图**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/dc3e704c-7c57-42b8-93ea-ddd068665964.jpg)
|
||
|
||
|
||
**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
|
||
```
|
||
|
||
## 4.2 工厂方法模式
|
||
|
||
**1. 问题描述**
|
||
|
||
每个地区的 Pizza 店虽然种类相同,但是都有自己的风味,需要单独区分。例如,一个客户点了纽约的 cheese 种类的 Pizza 和在芝加哥点的相同种类的 Pizza 是不同的。
|
||
|
||
**2. 模式定义**
|
||
|
||
定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。
|
||
|
||
**3. 模式类图**
|
||
|
||
在简单工厂中,创建对象的是另一个类,而在工厂方法中,是由子类来创建对象。工厂方法常用在在每个子类都有自己的一组产品类。可以为每个子类创建单独的简单工厂,但是把简单工厂中创建对象的代码放到子类中来可以减少类的数目,因为子类不算是产品类,因此完全可以这么做。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/903093ec-acc8-4f9b-bf2c-b990b9a5390c.jpg)
|
||
|
||
**4. 解决方案类图**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/664f8901-5dc7-4644-a072-dad88cc5133a.jpg)
|
||
|
||
**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..
|
||
```
|
||
|
||
## 4.3 抽象工厂模式
|
||
|
||
**1. 设计原则**
|
||
|
||
**依赖倒置原则**:要依赖抽象,不要依赖具体类。听起来像是针对接口编程,不针对实现编程,但是这个原则说明了:不能让高层组件依赖底层组件,而且,不管高层或底层组件,两者都应该依赖于抽象。例如,下图中 PizzaStore 属于高层组件,但是它依赖于底层组件 Pizza 的具体类。如果依赖的是 Pizza 的抽象类,那么就可以不用关心 Pizza 的具体实现细节。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ddf72ca9-c0be-49d7-ab81-57a99a974c8e.jpg)
|
||
|
||
**2. 模式定义**
|
||
|
||
提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
|
||
|
||
**3. 模式类图**
|
||
|
||
抽象工厂模式创建的是对象家族,也就是很多对象而不是一个对象,并且这些对象是相关的,也就是说必须一起创建出来。而工厂模式只是用于创建一个对象,这和抽象工厂模式有很大不同。并且,抽象工厂模式也用到了工厂模式来创建单一对象,在类图左部,AbstractFactory 中的 CreateProductA 和 CreateProductB 方法都是让子类来实现,这两个方法单独来看就是在创建一个对象,这符合工厂模式的定义。至于创建对象的家族这一概念是在 Client 体现,Client 要通过 AbstractFactory 同时调用两个方法来创建出两个对象,在这里这两个对象就有很大的相关性,Client 需要这两个对象的协作才能完成任务。从高层次来看,抽象工厂使用了组合,即 Cilent 组合了 AbstractFactory ,而工厂模式使用了继承。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d301774f-e0d2-41f3-95f4-bfe39859b52e.jpg)
|
||
|
||
**4. 解决方案类图**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8785dabd-1285-4bd0-b3aa-b05cc060a24a.jpg)
|
||
|
||
**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
|
||
```
|
||
|
||
# 第 5 章 单件模式
|
||
|
||
**1. 模式定义**
|
||
|
||
确保一个类只有一个实例,并提供了一个全局访问点。
|
||
|
||
**2. 模式类图**
|
||
|
||
单件模式的 Java 实现用一个私有构造器、一个私有静态变量以及一个公有静态函数,该函数返回私有变量,使得所有通过该函数获取的对象都指向这个唯一的私有静态变量。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/59aff6c1-8bc5-48e4-9e9c-082baeb2f274.jpg)
|
||
|
||
**3. 经典实现**
|
||
|
||
以下实现中,私有静态变量被延迟化实例化,这样做的好处是,如果没有用到该类,那么就不会创建该私有静态变量,从而节约资源。这个实现在多线程环境下是不安全的,因为多个线程能够同时进入 if(uniqueInstance == null) 内的语句块,那么就会多次实例化 uniqueInstance 私有静态变量。
|
||
|
||
```java
|
||
public class Singleton {
|
||
|
||
private static Singleton uniqueInstance;
|
||
|
||
private Singleton() {
|
||
}
|
||
|
||
public static Singleton getUniqueInstance() {
|
||
if (uniqueInstance == null) {
|
||
uniqueInstance = new Singleton();
|
||
}
|
||
return uniqueInstance;
|
||
}
|
||
}
|
||
```
|
||
|
||
**4. 线程不安全问题的解决方案一**
|
||
|
||
只需要对 getUniqueInstance() 方法加锁,就能让该方法一次只能一个线程访问,从而避免了对 uniqueInstance 变量进行多次实例化的问题。但是这样有一个问题是一次只能一个线程进入,性能上会有一定的浪费。
|
||
|
||
```java
|
||
public static synchronized Singleton getUniqueInstance() {
|
||
if (uniqueInstance == null) {
|
||
uniqueInstance = new Singleton();
|
||
}
|
||
return uniqueInstance;
|
||
}
|
||
```
|
||
|
||
**5. 线程不安全问题的解决方案二**
|
||
|
||
不用延迟实例化,采用直接实例化。
|
||
|
||
```java
|
||
private static Singleton uniqueInstance = new Singleton();
|
||
```
|
||
|
||
**6. 线程不安全问题的解决方案三**
|
||
|
||
考虑第一个解决方案,它是直接对 getUniqueInstance() 方法进行加锁,而实际上只需要对 uniqueInstance = new Singleton(); 这条语句加锁即可。使用两个条件语句来判断 uniqueInstance 是否已经实例化,如果没有实例化才需要加锁。
|
||
|
||
```java
|
||
public class Singleton {
|
||
|
||
private volatile static Singleton uniqueInstance;
|
||
|
||
private Singleton() {
|
||
}
|
||
|
||
public static synchronized Singleton getUniqueInstance() {
|
||
if (uniqueInstance == null) {
|
||
synchronized (Singleton.class) {
|
||
if (uniqueInstance == null) {
|
||
uniqueInstance = new Singleton();
|
||
}
|
||
}
|
||
}
|
||
return uniqueInstance;
|
||
}
|
||
}
|
||
```
|
||
|
||
# 第 6 章 命令模式
|
||
|
||
**1. 问题描述**
|
||
|
||
设计一个遥控器,它有很多按钮,每个按钮可以发起一个命令,让一个家电完成相应操作。有非常多的家电,并且也会增加家电。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7b8f0d8e-a4fa-4c9d-b9a0-3e6a11cb3e33.jpg)
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c3ca36b2-8459-4cf1-98b0-cc95a0e94f20.jpg)
|
||
|
||
**2. 模式定义**
|
||
|
||
将命令封装成对象,以便使用不同的命令来参数化其它对象。
|
||
|
||
**3. 模式类图**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1e09d75f-6268-4425-acf8-8ecd1b4a0ef3.jpg)
|
||
|
||
**4. 解决方案类图**
|
||
|
||
Invoker 是遥控器,它可以设置命令,并且调用命令对象的 execute() 方法。Receiver 是电灯,是命令真正的执行者。ConcreteCommand 类组合了一个 Receiver 对象,命令的执行委托给 Receiver 对象来处理,也就是 LightOnCommand 命令的 excute 方法委托给 Light 对象来处理,Light 对象通过调用 on() 方法来完成操作。Invoker 不是 Client 对象,是因为命令的创建不是在 Invoker 中完成的,因此需要额外的 Client 对象来处理这些操作。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5ef94f62-98ce-464d-a646-842d9c72c8b8.jpg)
|
||
|
||
**5. 代码实现**
|
||
|
||
```java
|
||
public interface Command {
|
||
public void execute();
|
||
}
|
||
```
|
||
```java
|
||
public class Light {
|
||
|
||
public void on() {
|
||
System.out.println("Light is on!");
|
||
}
|
||
|
||
public void off() {
|
||
System.out.println("Light is off!");
|
||
}
|
||
}
|
||
```
|
||
```java
|
||
public class LightOnCommand implements Command{
|
||
Light light;
|
||
|
||
public LightOnCommand(Light light) {
|
||
this.light = light;
|
||
}
|
||
|
||
@Override
|
||
public void execute() {
|
||
light.on();
|
||
}
|
||
}
|
||
```
|
||
```java
|
||
/**
|
||
* 遥控器类
|
||
*/
|
||
public class SimpleRemoteControl {
|
||
Command slot;
|
||
|
||
public SimpleRemoteControl() {
|
||
|
||
}
|
||
|
||
public void setCommand(Command command) {
|
||
this.slot = command;
|
||
}
|
||
|
||
public void buttonWasPressed() {
|
||
slot.execute();
|
||
}
|
||
|
||
}
|
||
```
|
||
```java
|
||
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!
|
||
```
|
||
|
||
# 第 7 章 适配器模式与外观模式
|
||
|
||
## 7.1 适配器模式
|
||
|
||
**1. 模式定义**
|
||
|
||
将一个类的接口,转换为客户期望的另一个接口。适配器让原本不兼容的类可以合作无间。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8e8ba824-7a9e-4934-a212-e6a41dcc1602.jpg)
|
||
|
||
**2. 模式类图**
|
||
|
||
有两种适配器模式的实现,一种是对象方式,一种是类方式。对象方式是通过组合的方法,让适配器类(Adapter)拥有一个待适配的对象(Adaptee),从而把相应的处理委托给待适配的对象。类方式用到多重继承,Adapter 可以看成 Target 和 Adaptee 类型,先把它当成 Adaptee 类型然后实例化一个 Adapter 对象,再把它当成 Target 类型的,这样 Client 就可以把这个对象当成 Target 的对象来处理。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/253bd869-ea48-4092-9aed-6906ccb2f3b0.jpg)
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a797959a-0ed5-475b-8d97-df157c672019.jpg)
|
||
|
||
**3. 问题描述**
|
||
|
||
让鸭子(Duck)适配火鸡(Turkey),Duck 有 quack() 方法,而 Turkey 只有 gobble() 方法。也就是要让 Turkey 也有 Duck 的 quack() 方法。
|
||
|
||
**4. 解决方案类图**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1a511c76-bb6b-40ab-b8aa-39eeb619d673.jpg)
|
||
|
||
**5. 代码实现**
|
||
|
||
```java
|
||
public interface Duck {
|
||
public void quack();
|
||
public void fly();
|
||
}
|
||
```
|
||
```java
|
||
public interface Turkey {
|
||
public void gobble();
|
||
public void fly();
|
||
}
|
||
```
|
||
```java
|
||
public class WildTurkey implements Turkey{
|
||
@Override
|
||
public void gobble() {
|
||
System.out.println("gobble!");
|
||
}
|
||
|
||
@Override
|
||
public void fly() {
|
||
System.out.println("fly!");
|
||
}
|
||
}
|
||
```
|
||
```java
|
||
public class TurkeyAdapter implements Duck{
|
||
Turkey turkey;
|
||
|
||
public TurkeyAdapter(Turkey turkey) {
|
||
this.turkey = turkey;
|
||
}
|
||
|
||
@Override
|
||
public void quack() {
|
||
turkey.gobble();
|
||
}
|
||
|
||
@Override
|
||
public void fly() {
|
||
turkey.fly();
|
||
}
|
||
}
|
||
```
|
||
```java
|
||
public class DuckTestDrive {
|
||
public static void main(String[] args) {
|
||
Turkey turkey = new WildTurkey();
|
||
Duck duck = new TurkeyAdapter(turkey);
|
||
duck.quack();
|
||
duck.fly();
|
||
}
|
||
}
|
||
```
|
||
|
||
运行结果
|
||
```html
|
||
gobble!
|
||
fly!
|
||
```
|
||
|
||
## 7.2 外观模式
|
||
|
||
**1. 模式定义**
|
||
|
||
提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。
|
||
|
||
**2. 模式类图**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/78f2314e-2643-41df-8f3d-b7e28294094b.jpg)
|
||
|
||
**3. 问题描述**
|
||
|
||
家庭影院中有众多电器,当要进行观看电影时需要对很多电器进行操作。要求简化这些操作,使得家庭影院类只提供一个简化的接口,例如提供一个看电影的接口而不用具体操作众多电器。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/106f5585-b2e7-4718-be5d-3b322d1ef42a.jpg)
|
||
|
||
**4. 解决方案类图**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/25387681-89f8-4365-a2fa-83b86449ee84.jpg)
|
||
|
||
**5. 设计原则**
|
||
|
||
**最少知识原则**:只和你的密友谈话。也就是应当使得客户对象所需要交互的对象应当尽可能少。
|
||
|
||
# 第 8 章 模板方法模式
|
||
|
||
**1. 模式定义**
|
||
|
||
在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。
|
||
|
||
模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。
|
||
|
||
**2. 模式类图**
|
||
|
||
模板方法 templateMethod() 定义了算法的骨架,确定了 primitiveOperation1() 和 primitiveOperation2() 方法执行的顺序,而 primitiveOperation1() 和 primitiveOperation2() 让子类去具体实现。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ed62f400-192c-4185-899b-187958201f0c.jpg)
|
||
|
||
**3. 问题描述**
|
||
|
||
冲咖啡和冲茶都有类似的流程,但是某些步骤会有点不一样。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d8f873fc-00bc-41ee-a87c-c1b4c0172844.png)
|
||
|
||
**4. 解决方案类图**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/aa20c123-b6b5-432a-83d3-45dc39172192.jpg)
|
||
|
||
**5. 设计原则**
|
||
|
||
**好莱坞原则**:别调用(打电话给)我们,我们会调用(打电话给)你。这一原则可以防止依赖腐败,即防止高层组件依赖底层组件,底层组件又依赖高层组件。该原则在模板方法的体现为,只有父类会调用子类,子类不会调用父类。
|
||
|
||
**6. 钩子**
|
||
|
||
钩子(hock):某些步骤在不同实现中可有可无,可以先定义一个什么都不做的方法,把它加到模板方法 templteMethod() 中,如果子类需要它就覆盖默认实现并加上自己的实现。
|
||
|
||
**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");
|
||
}
|
||
}
|
||
```
|
||
```java
|
||
public class Coffee extends CaffeineBeverage{
|
||
@Override
|
||
void brew() {
|
||
System.out.println("Coffee.brew");
|
||
}
|
||
|
||
@Override
|
||
void addCondiments() {
|
||
System.out.println("Coffee.addCondiments");
|
||
}
|
||
}
|
||
```
|
||
```java
|
||
public class Tea extends CaffeineBeverage{
|
||
@Override
|
||
void brew() {
|
||
System.out.println("Tea.brew");
|
||
}
|
||
|
||
@Override
|
||
void addCondiments() {
|
||
System.out.println("Tea.addCondiments");
|
||
}
|
||
}
|
||
```
|
||
```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
|
||
```
|
||
|
||
# 第 9 章 迭代器和组合模式
|
||
|
||
## 9.1 迭代器模式
|
||
|
||
**1. 模式定义**
|
||
|
||
提供一种顺序访问一个聚合对象中的各个元素的方法,而又不暴露其内部的表示。
|
||
|
||
**2. 模式类图**
|
||
|
||
客户类拥有一个聚合对象和迭代器对象,迭代器对象是聚合对象生成的。只需要迭代器定义好移动的操作,就可以让聚合对象能够顺序遍历。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/439deca7-fed0-4c89-87e5-7088d10f1fdb.jpg)
|
||
|
||
**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);
|
||
}
|
||
|
||
}
|
||
```
|
||
```java
|
||
public interface Iterator {
|
||
boolean hasNext();
|
||
int next();
|
||
}
|
||
```
|
||
```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
|
||
```
|
||
|
||
## 9.2 Java 内置的迭代器
|
||
|
||
**1. 实现接口**
|
||
|
||
Java 中已经有了 Iterator 接口,在使用 Java 实现时,需要让聚合对象实现 Iterable 接口,该接口有一个 iterator() 方法会返回一个 Iterator 对象。使用 Java 内置的迭代器实现,客户对象可以使用 foreach 循环来遍历聚合对象中的每个元素。
|
||
|
||
**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);
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
## 9.3 组合模式
|
||
|
||
**1. 设计原则**
|
||
|
||
一个类应该只有一个引起改变的原因。
|
||
|
||
**2. 模式定义**
|
||
|
||
允许将对象组合成树形结构来表现“整体 / 部分”层次结构。
|
||
|
||
组合能让客户以一致的方式处理个别对象以及对象组合。
|
||
|
||
**3. 模式类图**
|
||
|
||
由于组合(Composite)类拥有一个组件(Component)对象,因此组合对象位于树形结构的中间,它还可以继续操作这个组件对象,并忽略组件对象的具体类型。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f99c019e-7e91-4c2e-b94d-b031c402dcb5.jpg)
|
||
|
||
**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);
|
||
}
|
||
```
|
||
```java
|
||
public class Leaf extends Component {
|
||
public Leaf(String name) {
|
||
super(name);
|
||
}
|
||
|
||
@Override
|
||
public void addChild(Component component) {
|
||
throw new UnsupportedOperationException(); // 牺牲透明性换取单一职责原则 , 这样就不用考虑是叶子节点还是组合节点
|
||
}
|
||
|
||
@Override
|
||
protected void print(int level) {
|
||
for (int i = 0; i < level; i++) {
|
||
System.out.print("--");
|
||
}
|
||
System.out.println("left:" + name);
|
||
}
|
||
}
|
||
```
|
||
```java
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
```
|
||
```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
|
||
```
|
||
|
||
# 第 10 章 状态模式
|
||
|
||
**1. 模式定义**
|
||
|
||
允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。
|
||
|
||
状态模式的类图和策略模式一样,并且都是能够动态改变对象的行为。但是状态模式是通过状态对象的状态转移来改变客户对象组合的状态对象,而策略模式是通过客户对象本身的决策来改变组合的策略对象。例如,状态模式下,客户对象委托状态对象进行一个处理操作,那么状态对象有可能发生状态转移,使得客户对象拥有的状态对象发生改变。状态对象组合了客户对象,状态转移是状态对象通过改变客户对象所组合的状态对象实现的。
|
||
|
||
**2. 模式类图**
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c28fd93a-0d55-4a19-810f-72652feee00d.jpg)
|
||
|
||
**3. 问题描述**
|
||
|
||
糖果销售机有多种状态,每种状态下销售机有不同的行为,状态可以发生转移,使得销售机的行为也发生改变。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f7d880c9-740a-4a16-ac6d-be502281b4b2.jpg)
|
||
|
||
**4. 直接解决方案**
|
||
|
||
在糖果机的每个操作函数里面,判断当前的状态,根据不同的状态进行不同的处理,并且发生不同的状态转移。这种解决方案把所有的实现细节都放到客户类,这样在新增状态的时候就要去修改客户类的代码。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/62ebbb63-8fd7-4488-a866-76a9dc911662.png)
|
||
|
||
**5. 使用状态模式的解决方案**
|
||
|
||
状态的转移被移到状态类里面,客户类的每个操作只需要委托给状态类即可,而不需要知道当前是什么状态以及状态时如何进行转移的。
|
||
|
||
|
||
**6. 代码实现**
|
||
|
||
```java
|
||
public interface State {
|
||
/**
|
||
* 投入25 分钱
|
||
*/
|
||
void insertQuarter();
|
||
|
||
/**
|
||
* 退回25 分钱
|
||
*/
|
||
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
|
||
```
|
||
|
||
# 第 11 章 代理模式 // TODO
|
||
|
||
# 第 12 章 复合模式
|
||
|
||
## 12.1 MVC
|
||
|
||
### 12.1.1 传统 MVC
|
||
|
||
视图使用组合模式,模型使用了观察者模式,控制器使用了策略模式。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4f67611d-492f-4958-9fa0-4948010e345f.jpg)
|
||
|
||
### 12.1.2 Web 中的 MVC
|
||
|
||
模式不再使用观察者模式。
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1dd56e61-2970-4d27-97c2-6e81cee86978.jpg)
|
||
|
||
# 第 13 章 与设计模式相处
|
||
|
||
定义:在某 **情境** 下,针对某 **问题** 的某种 **解决方案**。
|
||
|
||
过度使用设计模式可能导致代码被过度工程化,应该总是用最简单的解决方案完成工作,并在真正需要模式的地方才使用它。
|
||
|
||
反模式:不好的解决方案来解决一个问题。主要作用是为了警告不要使用这些解决方案。
|
||
|
||
模式分类:
|
||
|
||
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/524a237c-ffd7-426f-99c2-929a6bf4c847.jpg)
|
||
|
||
# 第 14 章 剩下的模式 // TODO
|