zoukankan      html  css  js  c++  java
  • Head First Design Patterns Command Pattern

    Command Pattern allows you to decouple the requester of an action from the object actually performs the action. A command

    object encapsulates a request to do something on a specific object.

    From dinner to the Command Pattern

    1.Implementing the Command interface

    public interface Command{

      public void execute();

    }

    2.Implementing a Command

    public class LightOnCommand implements Command{

      Light light;

      public LightOnCommand(Light light){

        this.light = light;

      }

      public void execute(){

        light.on();

      }

    }

    3.Using the Command object

    public class SimpleRemoteControl{

      Command slot;

      public SimpleRemoteControl(){}

      public void setCommand(Command command){

        slot = command;

      }

      public void buttonWasPressed(){

        slot.execute();

      }

    }

    The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with

    different requests,queue or log requests, and support undoable operations.

    Class diagram:

    1.The Command Pattern decouples an object, making a request from the one that knows how to perform it.

    2.A Command object is at the center of this decouping and encapsulates a receiver with an action(or set of actions)

    3.An invoker makes a request of a Command object by calling its execute() method 

    4.Commands mat support undo by implementing an undo method that restores the object to its previous state

    before the execute() method was last called.

  • 相关阅读:
    C#中的String.Format()方法
    JQuery中的each方法
    [开发笔记]-使用jquery获取url及url参数的方法
    ThinkPHP3.2.3学习笔记3---视图
    PHP命名空间namespace使用小结
    ThinkPHP3.2.3学习笔记2---模型
    PHP中的连贯操作
    关于新技术的学习问题
    ThinkPHP3.2.3学习笔记1---控制器
    WinXP下如何安装及御载MySQL服务
  • 原文地址:https://www.cnblogs.com/zhuqiang/p/2455707.html
Copyright © 2011-2022 走看看