设计模式

创建型模式

  • 单例模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Singleton {
private static volatile Singleton instance;

private Singleton() {}

public static Singleton getInstance() {
if (instance == null) {
synchronized(Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
  • 简单工厂
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class ShapeFactory {

//使用 getShape 方法获取形状类型的对象
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
} else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
} else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
}
  • 抽象工厂
1
2
3
4
5
6
7
8
9
10
11
// 创建工厂的工厂
public class FactoryProducer {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("SHAPE")){
return new ShapeFactory();
} else if(choice.equalsIgnoreCase("COLOR")){
return new ColorFactory();
}
return null;
}
}
  • 建造者模式
1
// 使用多个简单的对象一步一步构建成一个复杂的对象
  • 原型模式
1
// 原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能
  • 适配器模式
1
// 统一一套接口给外部使用,不同的适配器,可以把不同的数据类型整合到一个接口里
  • 桥接模式
1
// 把变化的部分隔离开,当发生变化的时候,互不影响
  • 过滤器模式
1
// XXXFilter 就做简单数据筛选
  • 组合模式
1
2
// View里包含子view
// 组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象
  • 装饰器模式
1
2
// java中的IO流, 一层套一层的装饰
// 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构
  • 外观模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用
public class ShapeMaker {
private Shape circle;
private Shape rectangle;
private Shape square;

public ShapeMaker() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}

public void drawCircle(){
circle.draw();
}
public void drawRectangle(){
rectangle.draw();
}
public void drawSquare(){
square.draw();
}
}
  • 享元模式
1
2
// 共享原型的模式
// JAVA 中的 String,如果有则返回,如果没有则创建一个字符串保存在字符串缓存池里面
  • 代理模式
1
// 为其他对象提供一种代理以控制对这个对象的访问
  • 责任链模式
1
// okhttp里面的拦截器
  • 命令模式
1
2
// Message就是命令,Message里持有Handler的引用
// 命令模式(Command Pattern)是一种数据驱动的设计模式,它属于行为型模式。请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。
  • 解释器模式
1
2
3
// 编译器
// 解析外链
// 提供一个接口,解析表达式
  • 迭代器模式
1
2
3
// JAVA 中的 iterator。
// hasNext/next
// 提供一种方法顺序访问一个聚合对象中各个元素, 而又无须暴露该对象的内部表示
  • 中介者模式
1
2
// 多对多关系,转成多对1关系
// 中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护。中介者模式属于行为型模式。
  • 备忘录模式
1
// 备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。
  • 观察者模式
1
// 点击监听
  • 状态模式
1
// 就是状态机
  • 策略模式
1
2
3
4
5
6
7
8
9
10
11
12
public class StrategyPatternDemo {
public static void main(String[] args) {
Context context = new Context(new OperationAdd());
System.out.println("10 + 5 = " + context.executeStrategy(10, 5));

context = new Context(new OperationSubtract());
System.out.println("10 - 5 = " + context.executeStrategy(10, 5));

context = new Context(new OperationMultiply());
System.out.println("10 * 5 = " + context.executeStrategy(10, 5));
}
}