Design Patterns - Behavioral Patterns

 

Please indicate the source of the reprint: http://blog.csdn.net/ndzjx/article/details/78027426

Design Patterns

                                                                                             ---------- Behavioral Patterns

content

1. Iterator mode

2. Strategy mode

3. Status Mode

4. Template method pattern

5. Command Mode

6. Observer Pattern

7. Chain of Responsibility Model

8. Interpreter

9. The Mediator Model

10. Memo Mode

11. Visitor Pattern

 

 

 

1. Iterator mode

       Provides a way to sequentially access elements in an aggregate object without exposing its internal representation.

Putting the roaming task on the iterator instead of the aggregate simplifies the interface and implementation of the aggregate, and makes the responsibilities go their own way.

 

Example: Visit the traversal of the combined pattern

       and an example in the project

 

 

2. Strategy mode

       The algorithm family is defined and encapsulated separately so that they can be replaced with each other. This mode makes the changes of the algorithm independent of the customers who use the algorithm. (More composition, less inheritance. / Encapsulating changes)

 

 

Class Diagram:

 

 

so. Defines algorithm families and encapsulates them separately so that they can be replaced with each other. This mode makes algorithm changes independent of the customers who use the algorithm. (More composition, less inheritance. / Encapsulating changes)

 

3. Status Mode

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

The rookie example is not good.

 

Candy Vending Machine:

 

Class Diagram:


         The state transition is automatically handled in the handle, and the client of the context does not know much about the state object, or even does not know it at all.

 

         It is the same as the class diagram of the strategy pattern, the difference is:

Strategy Mode:

1. Usually actively specify which policy object is to be combined by the context. (dependency between state classes)

2. Considered a flexible alternative to inheritance, by combining different objects to change behavior.

 

Status mode:

1.      客户对于状态对象了解不多。固然策略模式更有弹性,但是对于某个context来说,通常只有一个最适当的策略对象。

2.      认为是不用再Context中放置许多条件判断的替代方案。可以通过context简单的改变状态来改变context的行为。

两者的意图十分不同。

 

so. 允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。

菜鸟的例子不好。

4.模板方法模式

       在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法的某些步骤。

 

 

类图:

 

 

应用:

(算法,钩子等)

std::find_if (myvector.begin(),myvector.end(), IsOdd); (怎么不是策略)

 

if (fun())

{

        fun2();

}

 

 

和策略模式的区别:

1:模板方法模式

       使用继承

       超类提供基础的方法,达到复用,对于创建框架很有用。

 

2.策略模式

       使用组合,更有弹性,可以在运行时改变行为

       定义整个算法

 

so. 在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法的某些步骤。

5.命令模式

         将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。

 

 

 

1.命令接口

public interfaceCommand {
       public void execute();
}

2.关台灯命令

public classLightOffCommand implements Command {
       Light light;
 
       public LightOffCommand(Light light) {
              this.light = light;
       }
 
       public void execute() {
              light.off();
       }
}


 

3.开台灯命令

public classLightOnCommand implements Command {
       Light light;
 
       public LightOnCommand(Light light) {
              this.light = light;
       }
 
       public void execute() {
              light.on();
       }
}


 

4.台灯

public class Light{
 
       public Light() {
       }
 
       public void on() {
              System.out.println("Light ison");
       }
 
       public void off() {
              System.out.println("Light isoff");
       }
}


 

5.遥控器:

public classSimpleRemoteControl {
       Command slot;
 
       public SimpleRemoteControl() {}
 
       public void setCommand(Command command) {
              slot = command;
       }
 
       public void buttonWasPressed() {
              slot.execute();
       }
}


 

6.主函数:

public classRemoteControlTest {
       public static void main(String[] args) {
              SimpleRemoteControl remote = newSimpleRemoteControl();
              Light light = new Light();
              GarageDoor garageDoor = newGarageDoor();
              LightOnCommand lightOn = newLightOnCommand(light);
              GarageDoorOpenCommand garageOpen =new GarageDoorOpenCommand(garageDoor);
 
              remote.setCommand(lightOn);
              remote.buttonWasPressed();
              remote.setCommand(garageOpen);
              remote.buttonWasPressed();
       }
}


 

类图:

 

1.     Receiver一定要存在吗?可以和Command合并吗?-》聪明的命令对象。这样调用者和接收者的解耦程度不如 分开的时候。

2.     多层次撤销,用栈

3.     发送者Invoker与Receiver通过命令对象沟通。

 

so. 将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。

 

 

6.观察者模式

       定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,他的所有依赖者都会收到通知并自动更新。

       (为了交互对象之间的松耦合设计而努力。松耦合建立有弹性的OO系统,能够应对变化,对象间依赖降到了最低。)

       两个角色:主题/观察者。

不举例子了,项目中许多:attach/detach

 

类图:

 

7. 责任链模式

 

例子

8.解释器模式(Interpreter)

 

例子

9. 中介者模式

 

 

类图:

 

例子:聊天室

10. 备忘录模式

 

例子

类图:

 

11.访问者模式

 

例子

元素通过参数传进访问者接口指针,将自己注册进访问者。

 

类图:

 

 

 

 

复习一下:

 

最后提醒:

不要为了使用而使用。要总是使用满足需要的最简单解决方案,不管它用不用模式。

 

 

 

 

 

 

Guess you like

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