Gof 笔记 - 3

五、Behavioral Patterns

  • Chain of Responsibility

1、意图

 

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

避免一个请求的发送者和接受者的耦合,通过使多个对象都有机会处理请求。将这些接受对象连成链,沿着链传递请求,指导有对象处理它。

2、先从当前按钮找帮助,找不到则向上找,找到则显示

3、structure

  • Command

1、意图

 

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

将一个请求封装成对象,从而使你可以【使用不同的请求来参数化client,对请求排队或记录请求日志,支持可撤销的操作】。

2、一个文档工具箱,通过菜单实现各种命令。

实现Paste:

实现Open:

实现组合命令:

3、structure

4、补充

 

Command decouples the object that invokes the operation from the one that knows how to perform it.

 

command将【调用操作的对象】和【知道怎样实现他的对象】解耦。

  • Interpreter

1、意图

 

Given a language, define a represention for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

给定一个语言,定义它的文法的一种表示,并定义一个解释器,使用该表示来解释语言中的句子。

2、主要对文法进行解释

下图实现了 raining & (dogs | cats) * 

3、structure

 

  • Iterator

1、意图

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

提供一种方法来顺序访问一个聚合对象的元素,且不暴露他的内部表示。

2、实现list的遍历

添加一个抽象层

3、structure

  • Mediator

1、意图

 

Define an object that encapsulates how a set of objects interact.Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

定义一个对象来封装一组对象怎么交互,Mediator 使各对象不需要显式的相互引用,从而使其松耦合,而且可以独立改变他们的交互。

2、当你选中下拉框的一项,文本框和各个单选框要随着改变,或者当你按下...其他...要随着改变

3、structure

4、补充

主要就是将多对多的交互,改成单对多的交互,使其更易于理解、维护、扩展。

  • Memento

1、意图

 

Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.

在不破坏封装性的前提下,捕获一个对象的内部状态,存储在外部,以使以后这个对象能够恢复到这个状态。

2、structure

3、补充

将Originator设成Memento的友元,这样Originator可以访问Memento的私有函数

 

  • Observer

1、意图

 

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

定义一个对象间一对多的依赖关系,使得当一个对象状态发生改变,所有他的依赖者都能够被通知和自动更新。

 

2、structure

  • State

1、意图

 

Allow an object to alter its behavior when its internal state changes.The object will appear to change its class.

允许一个对象在其内部状态发生改变时,修改其行为。看起来像是在修改它的类。

2、表示网络连接的TCPCOnnection,可能有多种状态,他会根据当前的状态做出不同的切换,变成两一种状态:

3、structure

  • Strategy

1、意图

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

定义一系列算法,把他们封装起来,并使他们可以相互交换。Strategy可以让算法变化不依赖于使用它的客户端。

2、下面描述的是对一个正文流进行分行

3、structure

4、补充

Strategy在Client里建立不同的算法;而State的状态对象通常是singleton,或者在Context构造函数里建立。

  • Template Method

1、意图

 

Define the skeleton of an algorithm in an operation, deferring somesteps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

定义一个操作的算法框架,延迟一些步骤到子类中。Template Method让子类重新定义了算法的特定步骤,在不改变算法结构的前提下。

2、一个提供Application 和 Document 类的应用框架,Application负责打开一个文档,OpenDocument定义了打开和读取文档的算法:

3、structure

4、补充

Factory Method常被Template Method调用。比如DoCreateDocument是一个Factory Method,它被OpenDocument调用。

 

  • Visitor

1、意图

 

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

表示这样一个操作:作用于一个对象结构的各个元素。visitor 让你定义一个新的操作,在不改变所要操作的元素类的前提下。

 

2、如下,将各个不同元素的操作 TypeCheck() 等,具体内容放到 visitor 中

3、structure

4、补充

关于双分派Double dispatch,Accept是个double dispatch操作,具体的执行依赖于Element和Visitor的类型

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值