廖雪峰-设计模式

一、创建型模式

创建型模式关注点是如何创建对象,其核心思想是要把对象的创建和使用相分离,这样使得两者能相对独立地变换。

1-工厂方法(Factory Method)

工厂方法是指定义工厂接口和产品接口,但如何创建实际工厂和实际产品被推迟到子类实现,从而使调用方只和抽象工厂与抽象产品打交道。

实际更常用的是更简单的静态工厂方法,它允许工厂内部对创建产品进行优化。

调用方尽量持有接口或抽象类,避免持有具体类型的子类,以便工厂方法能随时切换不同的子类返回,却不影响调用方代码。

2-抽象工厂(Abstract Factory)

抽象工厂模式是为了让创建工厂和一组产品与使用相分离,并可以随时切换到另一个工厂以及另一组产品;

抽象工厂模式实现的关键点是定义工厂接口和产品接口,但如何实现工厂与产品本身需要留给具体的子类实现,客户端只和抽象工厂与抽象产品打交道。

3-生成器/建造者(Builder)

Builder模式是为了创建一个复杂的对象,需要多个步骤完成创建,或者需要多个零件组装的场景,且创建过程中可以灵活调用不同的步骤或组件。

4-原型(Prototype)

原型模式是根据一个现有对象实例复制出一个新的实例,复制出的类型和属性与原实例相同。

5-单例(Singleton)

Singleton模式是为了保证一个程序的运行期间,某个类有且只有一个全局唯一实例;

Singleton模式既可以严格实现,也可以以约定的方式把普通类视作单例。

二、结构型模式

结构型模式主要涉及如何组合各种对象以便获得更好、更灵活的结构。虽然面向对象的继承机制提供了最基本的子类扩展父类的功能,但结构型模式不仅仅简单地使用继承,而更多地通过组合与运行期的动态组合来实现更灵活的功能。

1-适配器

Adapter模式可以将一个A接口转换为B接口,使得新的对象符合B接口规范。

编写Adapter实际上就是编写一个实现了B接口,并且内部持有A接口的类:

public BAdapter implements B {
    private A a;
    public BAdapter(A a) {
        this.a = a;
    }
    public void b() {
        a.a();
    }
}

在Adapter内部将B接口的调用“转换”为对A接口的调用。

只有A、B接口均为抽象接口时,才能非常简单地实现Adapter模式。

2-桥接

桥接模式实现比较复杂,实际应用也非常少,但它提供的设计思想值得借鉴,即不要过度使用继承,而是优先拆分某些部件,使用组合的方式来扩展功能。

3-组合

Composite模式使得叶子对象和容器对象具有一致性,从而形成统一的树形结构,并用一致的方式去处理它们。

4-装饰器

使用Decorator模式,可以独立增加核心功能,也可以独立增加附加功能,二者互不影响;

可以在运行期动态地给核心功能增加任意个附加功能。

5-外观(Facade)

Facade模式是为了给客户端提供一个统一入口,并对外屏蔽内部子系统的调用细节。

## Facade

Provide a unified interface to a set of interfaces in a subsystem. Facade
defines a higher-level interface that makes the subsystem easier to use. 
The pattern has structural purpose and applies to objects.
为子系统中的一组接口提供统一的接口。外观模式定义更高级别的接口,使子系统更易于使用。
该模式具有结构目的,适用于对象。

### When to use

* you want to provide a simple interface to a complex subsystem
* there are many dependencies between clients and the implementation classes of an abstraction
* you want to layer your subsystems, use a facade to define an entry point to each subsystem level 
您希望为复杂的子系统提供一个简单的接口
客户端和抽象的实现类之间存在许多依赖关系
如果要分层子系统,请使用外观模式定义每个子系统级别的入口点
6享元(Flyweight)

享元模式的设计思想是尽量复用已创建的对象,常用于工厂方法内部的优化。

## Flyweight

Flyweight pattern has has structural purpose, applies to objects and uses sharing to support large numbers of fine-grained objects efficiently. The pattern can be used to reduce memory usage when you need to create a large number of similar objects.
享元模式具有结构化的目的,适用于对象,并使用共享来有效地支持大量细粒度对象。当需要创建大量类似对象时,可以使用该模式来减少内存使用。

### When to use

* when one instance of a class can be used to provide many "virtual instances"
* when all of the following are true
 * an application uses a large number of objects
 * storage costs are high because of the sheer quantity of objects
 * most object state can be made extrinsic
 * many groups of objects may be replaced by relatively few shared objects once extrinsic state is removed
 * the application doesn't depend on object identity 
 * 当一个类的一个实例可以用来提供许多“虚拟实例”时
 * 当以下所有条件都成立时
 * 应用程序使用大量对象
 * 由于物品数量庞大,储存成本很高
 * 大多数对象状态都可以是外部的
 * 一旦外部状态被移除,许多对象组可能会被相对较少的共享对象替换
 * 应用程序不依赖于对象标识 
7-代理(Proxy)

代理模式通过封装一个已有接口,并向调用方返回相同的接口类型,能让调用方在不改变任何代码的前提下增强某些功能(例如,鉴权、延迟加载、连接池复用等)。

使用Proxy模式要求调用方持有接口,作为Proxy的类也必须实现相同的接口类型。

## Proxy

Proxy pattern provides a surrogate or placeholder for another object to control access to it.
The pattern has structural purpose and applies to objects. 
代理模式为另一个对象提供代理项或占位符来控制对它的访问。
代理模式具有结构目的,适用于对象。 
### When to use

* whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer
* 每当需要一个比简单指针更通用或更复杂的对象引用时 

三、行为型模式

行为型模式主要涉及算法和对象间的职责分配。通过使用对象组合,行为型模式可以描述一组对象应该如何协作来完成一个整体任务。

1-责任链(chain-of-responsibility)

责任链模式是一种把多个处理器组合在一起,依次处理请求的模式;

责任链模式的好处是添加新的处理器或者重新排列处理器非常容易;

责任链模式经常用在拦截、预处理请求等。

## Chain of Responsibility
Chain of Responsibility pattern avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. The pattern chains the receiving objects and passes the request along the chain until an object handles it. 
It has a behavioral purpose and deals with relationships between objects. 
责任链模式通过给多个对象一个处理请求的机会,避免了将请求的发送者耦合到其接收者。模式链接接收对象并沿着链传递请求,直到对象处理它。
它有一个行为目的,处理对象之间的关系。 

### When to use

* more than one object may handle a request, and the handler should be ascertained automatically
* you want to issue a request to one of several objects without specifying the receiver explicitly
* the set of objects that can handle a request should be specified dynamically
* 一个请求可以由多个对象处理,处理程序应该自动确定
* 您希望向多个对象中的一个发出请求,而不显式指定接收者
* 应该动态指定可以处理请求的对象集
2-命令(Command)

命令模式的设计思想是把命令的创建和执行分离,使得调用者无需关心具体的执行过程。

通过封装Command对象,命令模式可以保存已执行的命令,从而支持撤销、重做等操作。

## Command

Command pattern encapsulates a request as an object, thereby letting you parameterize
clients with different requests, queue or log requests, and supports undoable
operations. The pattern has a behavioral purpose and deals with relationships between objects. 
命令模式将请求封装为一个对象,从而允许您参数化具有不同请求的客户端、队列或日志请求,并支持可撤消的操作。该模式具有行为目的,处理对象之间的关系。 
### When to use

* want to parameterize objects by an action to perform
* want to specify, queue, and execute requests at different times
* support undo
* support logging changes so that they can be reapplied in case of a system crash
* structure a system around high-level operations built on primitives operations
* 希望通过要执行的操作参数化对象
* 希望在不同的时间指定、队列和执行请求
* 支持撤消
* 支持日志记录更改,以便在系统崩溃时重新应用这些更改
* 围绕基于基本体操作的高级操作构建系统
3-解释器

解释器模式通过抽象语法树实现对用户输入的解释执行。

解释器模式的实现通常非常复杂,且一般只能解决一类特定问题。

## Interpreter

Given a language, the pattern defines a represention for its grammar along with an
interpreter that uses the representation to interpret sentences in the language. 
The Interpreter pattern has behavioral purpose and applies to the classes.
给定一种语言,该模式定义了其语法的表示形式,以及使用该表示形式来解释该语言中句子的解释器。
解释器模式具有行为目的,适用于类。 
### When to use

* when the grammar is simple (in case of complex grammars, there are better alternatives)
* efficiency is not a critical concern
* 当语法简单时(对于复杂语法,有更好的选择)
* 效率不是一个关键问题
4-迭代器

Iterator模式常用于遍历集合,它允许集合提供一个统一的Iterator接口来遍历元素,同时保证调用者对集合内部的数据结构一无所知,从而使得调用者总是以相同的接口遍历各种不同类型的集合。

## Iterator
Iterator pattern has behavioral purpose and applies to objects. The pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. 
迭代器模式具有行为目的并应用于对象。该模式提供了一种顺序访问聚合对象元素的方法,而不暴露其底层表示。
### When to use

* to access an aggregate object's contents without exposing its internal representation
* to support multiple traversals of aggregate objects
* to provide a uniform interface for traversing different aggregate structures 
(to support polymorphic iteration) 
* 访问聚合对象的内容而不暴露其内部表示
* 支持聚合对象的多次遍历
* 为遍历不同的聚合结构提供统一的接口(支持多态迭代) 
5-中介

中介模式是通过引入一个中介对象,把多边关系变成多个双边关系,从而简化系统组件的交互耦合度。

## Mediator

Mediator pattern has behavioral purpose and applies on objects.
The pattern defines an object that encapsulates how a set of objects interact.
It promotes loose coupling by keeping objects from referring to each
other explicitly, and it lets you vary their interaction independently. 
中介模式具有行为目的,适用于对象。
模式定义了一个对象,该对象封装了一组对象如何交互。
它通过防止对象相互引用来促进松散耦合
另一个是显式的,它允许您独立地改变它们的交互。
### When to use

* a set of objects communicate in well-defined but complex ways
* reusing an object is difficult because it refers to and communicates with many other objects
* a behavior that's distributed between several classes should be customizable without a lot of subclassing
* 一组对象以定义良好但复杂的方式进行通信
* 重用一个对象是困难的,因为它引用了许多其他对象并与之通信
* 分布在几个类之间的行为应该是可定制的,而不需要太多的子类
6-备忘录

备忘录模式是为了保存对象的内部状态,并在将来恢复,大多数软件提供的保存、打开,以及编辑过程中的Undo、Redo都是备忘录模式的应用。

## Memento

Memento without violating encapsulation, captures and externalizes an object's internal state so that the object can be restored to this state later. The pattern has behavioral purpose and applies to the objects.
Memento在不违反封装的情况下,捕获对象的内部状态并将其外部化,以便稍后可以将对象恢复到该状态。模式具有行为目的,适用于对象。
### When to use

* a snapshot of an object's state must be saved so that it can be restored to that state later
* a direct interface to obtaining the state would expose implementation details and break the object's encapsulation 
* 必须保存对象状态的快照,以便稍后将其恢复到该状态
* 获取状态的直接接口将公开实现细节并破坏对象的封装
7-观察者

观察者模式,又称发布-订阅模式,是一种一对多的通知机制,使得双方无需关心对方,只关心通知本身。

## Observer

Observer defines a one-to-many dependency between objects so that when one object 
changes state, all its dependents are notified and updated automatically. The pattern has behavioral purpose and applies to the objects.
Observer在对象之间定义了一个一对多的依赖关系,这样当一个对象改变状态时,它的所有依赖关系都会被自动通知和更新。模式具有行为目的,适用于对象。 

### When to use

* when an abstraction has two aspects, one dependent on the other
* when a change to one object requires changing others, and you don't know how many objects need to be changed
* when an object should be able to notify other objects without making assumptions about who these objects are
* 当一个抽象有两个方面时,一个依赖于另一个
* 当更改一个对象需要更改其他对象时,您不知道需要更改多少对象
* 当一个对象应该能够通知其他对象而不必假设这些对象是谁时 
8-状态

状态模式的设计思想是把不同状态的逻辑分离到不同的状态类中,从而使得增加新状态更容易;

状态模式的实现关键在于状态转换。简单的状态转换可以直接由调用方指定,复杂的状态转换可以在内部根据条件触发完成。

## State

The pattern allows an object to alter its behavior when its internal state changes.
The object will appear to change its class. It has behavioral purpose and applies 
to the objects.
模式允许对象在其内部状态改变时改变其行为。
对象将显示为更改其类。它具有行为目的,适用于对象。
### When to use

* when an object's behavior depends on its state, and it must change its behavior at run-time depending on that state
* operations have large, multipart conditional statements that depend on the object's state
* 当一个对象的行为依赖于它的状态时,它必须在运行时根据该状态改变它的行为
* 操作具有大型的、多部分的条件语句,这些语句依赖于对象的状态
9-策略

策略模式是为了允许调用方选择一个算法,从而通过不同策略实现不同的计算结果。

通过扩展策略,不必修改主逻辑,即可获得新策略的结果。

## Strategy

Strategy defines a family of algorithms, encapsulates each one, and makes them 
interchangeable. It lets the algorithm vary independently fromclients that use it. 
The pattern has behavioral purpose and applies to the objects.
策略定义了一系列算法,封装了每一个算法,并使它们可以互换。它允许算法独立于使用它的客户机而变化。模式具有行为目的,适用于对象。
### When to use

* many related classes differ only in their behavior
* you need different variants of an algorithm
* an algorithm uses data that clients shouldn't know about
* 许多相关的类只在行为上有所不同
* 你需要不同的算法变体
* 算法使用客户端不应该知道的数据
10-模板方法

模板方法是一种高层定义骨架,底层实现细节的设计模式,适用于流程固定,但某些步骤不确定或可替换的情况。

## Template Method

Template method defines the skeleton of an algorithm in an operation, deferring some
steps to subclasses. It lets subclasses redefine certain steps of an algorithm 
without changing the algorithm's structure. The pattern has behavioral purpose and
applies to the classes.
模板方法定义了操作中算法的框架,推迟了一些操作
子类的步骤。它允许子类重新定义算法的某些步骤
在不改变算法结构的情况下。这一模式具有行为目的和意义,适用于类。
### When to use

* to implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary
* when common behavior among subclasses should be factored and localizedin a common class to avoid code duplication
* to control subclasses extensions
* 一次实现一个算法的不变部分,然后让子类来实现可以变化的行为
* 当子类之间的共同行为应该被分解并本地化到一个共同的类中以避免代码重复时
* 控制子类扩展
11-访问者

访问者模式是为了抽象出作用于一组复杂对象的操作,并且后续可以新增操作而不必对现有的对象结构做任何改动。

## Visitor

Visitor represents an operation to be performed on the elements of an object
structure. It lets you define a new operation without changing the classes of 
the elements on which it operates. The pattern has behavioral purpose and applies 
to the objects.
Visitor表示要对对象的元素执行的操作
结构。它允许您定义一个新操作,而无需更改
它运行的元素。模式具有行为目的,适用于对象。
### When to use

* an object structure contains many classes of objects with differing interfaces, 
and you want to perform operations on these objects that depend on their concrete classes
* many distinct and unrelated operations need to be performed on objects in an object structure, 
and you want to avoid "polluting" their classes with these operations
* the classes defining the object structure rarely change, but you often want
to define new operations over the structure
* 对象结构包含许多具有不同接口的对象类,您希望对这些依赖于具体类的对象执行操作
* 在一个对象结构中,需要对对象执行许多不同的和不相关的操作,你要避免用这些操作“污染”他们的类
* 定义对象结构的类很少更改,但您通常需要在结构上定义新操作
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可峰科技

生活不易

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值