[Design Patterns] Behavioral Patterns

write in front

We already know that there are 23 design patterns:

Design Pattern Type include
Creation mode (5 types) Singleton pattern, abstract factory pattern, builder pattern, factory method pattern, prototype pattern
Structural Patterns (7 types) Adapter mode, bridge mode, decoration mode, composition mode, appearance mode, flyweight mode, proxy mode
Behavioral patterns (11 types) Template method pattern, command pattern, iterator pattern, observer pattern, mediator pattern, memorandum pattern, interpreter pattern (Interpreter pattern), state pattern, strategy pattern, responsibility chain pattern, visitor pattern

Next, we will give a detailed introduction and description of the behavioral model.

Template method pattern:

Define the skeleton of an algorithm in an operation, while deferring some steps to subclasses. The template method pattern allows subclasses to redefine certain steps of an algorithm without changing its structure

优点:
1,在父类中定义一个算法,由它的子类来实现细节的处理
2,模板方法是一种代码复用技术,可以将公共行为放在父类中
3,可以通过子类来覆盖父类的基本方法
缺点:
需要为每一个基本方法的不同实现提供一个子类,如果父类中可变的基本方法太多,会导致系统更加庞大,降低系统性能
Command mode:

Encapsulates a request as an object, allowing us to parameterize clients with different requests; queue or log requests; and support undoable operations.

优点:
1,降低耦合度。因为请求者和接收者之间不存在引用,因为请求者和接收者之间是完全解耦的
2,增加灵活性。由于增加新的具体命令类不会影响其他类,所以增加新的类时很容易
3,可以比较容易的设计一个命令队列
4,为请求的撤销和恢复操作提供了一种设计和实现方案
缺点:
可能会导致某些系统有过多的具体命令类。因为针对每一个请求接收者的调用操作都需要设计一个具体命令类
Iterator pattern:

Provides a way to access an aggregate object without exposing the object's internal representation

优点:
1,支持以不同的方式遍历一个聚合对象
2,简化了聚合类
3,在迭代器模式中,增加新的聚合类和迭代器类都很方便,无须修改原有代码
缺点:
设计难度较大
Observer pattern:

Define a one-to-many dependency relationship between objects, so that whenever an object's state changes, its related dependent objects are notified and automatically updated

优点:
1,可以实现表示层和数据逻辑层的分离
2,在观察目标和观察者之间建立一个抽象的耦合
3,支持广播通信
4,增加新的具体观察者时,不需要修改原有系统代码
缺点:
1,如果一个观察目标对象有很多直接和间接观察者,将所有观察者都通知,会花费很多时间
2,如果观察者和观察目标之间存在循环依赖,会导致循环调用,从而导致系统崩溃
Mediator Mode:

An intermediary object (mediator) is used to encapsulate a series of object interactions. The intermediary makes the objects do not need to refer to each other explicitly, so that the coupling is loose, and the interaction between them can be changed independently. A typical application of the special law

优点:
简化了对象之间的交互,
缺点:
可能会导致中介者类非常复杂,使得系统难以维护
Memo Mode:

Without breaking the encapsulation, capture the internal state of an object and save this state outside the object, so that the object can be restored to the original saved state later

优点:
提供了一种状态恢复的机制
缺点:
资源消耗较大
Interpreter mode (Interpreter mode):

Define the grammar of a language and build an interpreter to interpret sentences in that language, where "language" means code using a prescribed format and grammar

优点:
1,易于改变和扩展文法
2,每一条文法规则都可以表示为一个类,因此可以方便的实现一个简单的语言
3,增加新的解释表达式比较方便。如果用户需要增加新的解释表达式,只需要对应增加一个新的表达式即可,原有表达式类不需要做修改
缺点:
1,对于复杂文法难以维护。在解释器模式中,每一条规则至少需要定义一个类,因此如果一个语言包含太多文法规则,会导致系统难以管理和维护
2,执行效率较低。由于在解释器模式中使用了大量的循环和递归调用,所以可能导致系统的执行效率变低
Status mode:

Allows an object to change its behavior when its internal state changes, the object appears to modify its class

优点:
1,封装了状态的转换规则,可以对状态转换代码进行集中管理,而不是分散在一个个业务方法中
2,将所有与某个状态有关的行为放到一个类中
3,可以让多个环境对象共享一个状态对象
缺点:
1,状态模式使用,会导致系统运行开销增大
2,状态模式如果使用不当,会增加系统设计难度
Strategy Mode:

Define a series of algorithm classes, encapsulate each algorithm, and make them interchangeable. The strategy pattern allows the algorithm to change independently of the client using it

优点:
1,算法切换简单
2,避免多重条件判断
3,具有良好的扩展性
缺点:
1,客户端必须知道所有的策略类,并自行决定使用哪一个策略类。这就意味着客户端必须理解这些算法的区别,以便适时选择恰当的算法。换言之,策略模式只适用于客户端知道所有的算法或行为的情况。
2,策略模式将造成系统产生很多具体策略类,任何细小的变化都将导致系统要增加一个新的具体策略类。
3,无法同时在客户端使用多个策略类,也就是说,在使用策略模式时,客户端每次只能使用一个策略类,不支持使用一个策略类完成部分功能后再使用另一个策略类来完成剩余功能的情况。
Chain of Responsibility Pattern:

Avoid coupling request sender and receiver, make it possible for multiple objects to receive the request, connect these objects into a chain, and pass the request along the chain until an object handles it.

优点:
1,对象不需要知道是哪儿一个对象处理他的请求,只需要知道这个请求会被处理,从而降低了系统的耦合度
2,增加灵活性,可以通过在运行时对该链进行动态的增加或修改来增加或改变处理一个请求的职责
3,在系统中增加一个新的具体请求处理者时,不需要修改原有系统的代码,只需要在客户端重新建链即可
缺点:
1,由于一个请求没有明确的接受者,这个请求不一定会被处理
2,对于比较长的职责链,可能涉及到多个处理对象,系统性能受到影响
3,如果职责链建立的不是太合理,可能会造成循环调用,使系统陷入死循环
Visitor pattern:

Provides a representation of operations that act on elements in an object structure, allowing us to define new operations that act on elements without changing their classes

优点:
1,增加新的访问操作很方便
2,将有关元素对象的访问行为集中到一个访问者对象中,而不会分散在一个个的元素类中
缺点:
1,增加新的类很困难
2,破坏封装

Summarize

Here is just a simple understanding, and I will learn more in the future process.
Thanks for reading~~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324929911&siteId=291194637