[关闭]
@linux1s1s 2017-01-12T03:07:44.000000Z 字数 1767 阅读 1503

Base Time-Design Patterns

Base 2016-12


系列博文
Base Time-Http Protocol
Base Time-Bitmap
Base Time-Database
Base Time-Java
Base Time-Design Patterns
Base Time-Java Algorithms

设计模式简单回顾

创建型模式

Factory Method 工厂方法模式

Abstract Factory 抽象工厂

Singleton 单例模式

什么场景使用单例模式?

类似交易所的核心交易引擎以及军队指挥系统等只能存在一个实例的场景,试想,如果有多个实例存在,岂不是乱套了。

单例模式有什么好处?
实例
  1. public static Singleton getInstance() {
  2. if (singleton == null) {
  3. synchronized (Singleton.class) {
  4. if (singleton == null) {
  5. singleton = new Singleton();
  6. }
  7. }
  8. }
  9. return singleton;
  10. }

有部分博客写的单例是这样子的

  1. public static Singleton getInstance() {
  2. if (instance == null) {
  3. synchronized (instance) {
  4. if (instance == null) {
  5. instance = new Singleton();
  6. }
  7. }
  8. }
  9. return instance;
  10. }

上面的代码,对一个null的instance加锁,所以不要尽信书。

Build 建造者模式

Prototype 原型模式

适配器模式

Class Adapter 类适配器

Decorator 装饰器模式

Proxy 代理模式

Facade 外观模式

Bridge 桥接模式

Composite 组合模式

Flyweight 享元模式

关系型模式

父类与子类

Strategy 策略模式
Template Method 模板方法模式

类与类

Observer 观察者模式
Iterator 迭代子模式
Chain of Responsibility 责任链模式
Command 命令模式

类的状态

Memento 备忘录模式
State 状态模式

通过中间类

Visitor 访问者模式
Mediator 中介模式
Interpreter 解释器模式

参考博文
java常用设计模式
设计模式(Design Patterns)
里氏替换原则

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注