资讯专栏INFORMATION COLUMN

命令模式-接收者与执行者解耦和

Sike / 1997人阅读

老板:阿飞,咱们公司又接了个新项目,一个客户,,卧室和客厅很大,电灯电视开关也不好找,所以希望制造一个遥控器来控制一些家具的开启与关闭,目前需要5个按键,卧室的灯,卧室的电视,客厅的灯,客厅的电视,在留一个预备按键。我等会把需求文档给你。
项目组长阿飞:好的,老板
项目组长阿飞:小三,来了个需求,你看下,你先设计一下架构
阿三:好的,飞哥

三天过后:飞哥,好了,你看下

先设计了一个接口,里面包含了,每一个按钮的统一行为

package com.commandPattern.command;

/**
 * @program: testPattern
 * @description: 命令接口
 * @author: Mr.Yang
 * @create: 2018-12-08 13:54
 **/
public interface Command {

    //执行方法
    public void exceute();

}

然后建立了一个对象,代表了空对象,什么操作也不执行

package com.commandPattern.command.nullCommand;

import com.commandPattern.command.Command;

/**
 * @program: testPattern
 * @description: 建立一个空对象,在许多设计模式种,都会看到空对象的使用,甚至有些时候,空对象本身也被视为一种设计模式
 * @author: Mr.Yang
 * @create: 2018-12-08 17:40
 **/
public class NullCommand implements Command {
    public void exceute() {
        System.out.println("什么都不做处理");
    }
}

灯的具体类

package com.commandPattern.entity;

/**
 * @program: testPattern
 * @description: 灯的具体类
 * @author: Mr.Yang
 * @create: 2018-12-08 17:31
 **/
public class Lamp {
    private String name;

    /**
     * name为灯的具体装饰,即为哪里的灯
     * @param name
     */
    public  Lamp(String name){
        this.name=name;
    }
    public void  on (){
        System.out.println(name+"_灯打开");
    }
    public void off (){
        System.out.println(name+"_灯关闭");
    }
}

电视的具体类

package com.commandPattern.entity;

/**
 * @program: testPattern
 * @description: 电视的具体类
 * @author: Mr.Yang
 * @create: 2018-12-08 17:35
 **/
public class Tv {
    private String name;
    public Tv(String name){
        this.name=name;
    }
    public void  on (){
        System.out.println(name+"_电视打开");
    }
    public void off(){
        System.out.println(name+"_电视关闭");
    }
}

关闭灯的具体命令

package com.commandPattern.command.off;

import com.commandPattern.command.Command;
import com.commandPattern.entity.Lamp;

/**
 * @program: testPattern
 * @description: 灯关闭
 * @author: Mr.Yang
 * @create: 2018-12-08 17:33
 **/
public class LampOffCommand implements Command {

    Lamp lamp;

    public LampOffCommand(Lamp lamp){
        this.lamp=lamp;
    }
    //灯关闭
    public void exceute() {
        lamp.off();
    }
}

打开灯的具体命令

package com.commandPattern.command.on;

import com.commandPattern.command.Command;
import com.commandPattern.entity.Lamp;

/**
 * @program: testPattern
 * @description: 灯打开的命令
 * @author: Mr.Yang
 * @create: 2018-12-08 17:29
 **/
public class LampOnCommand implements Command {

    Lamp lamp;
    public LampOnCommand(Lamp lamp){
        this.lamp=lamp;
    }

    //灯打开的命令
    public void exceute() {
        lamp.on();
    }
}

电视关闭的具体命令

package com.commandPattern.command.off;

import com.commandPattern.command.Command;
import com.commandPattern.entity.Tv;

/**
 * @program: testPattern
 * @description: 电视关闭
 * @author: Mr.Yang
 * @create: 2018-12-08 17:36
 **/
public class TvOffCommand implements Command {
    Tv tv;
    public TvOffCommand(Tv tv){
        this.tv=tv;
    }
    public void exceute() {
        tv.off();
    }
}

电视打开的具体命令

package com.commandPattern.command.on;

import com.commandPattern.command.Command;
import com.commandPattern.entity.Tv;

/**
 * @program: testPattern
 * @description: 电视打开
 * @author: Mr.Yang
 * @create: 2018-12-08 17:37
 **/
public class TvOnCommand implements Command {
    Tv tv;
    public TvOnCommand(Tv tv){
        this.tv=tv;
    }

    public void exceute() {
        tv.on();
    }
}

建立一个遥控器

package com.commandPattern.control;

import com.commandPattern.command.Command;
import com.commandPattern.command.nullCommand.NullCommand;

import java.util.Arrays;

/**
 * @program: testPattern
 * @description: 遥控器
 * @author: Mr.Yang
 * @create: 2018-12-08 17:39
 **/
public class RemoteControl {

    Command[] onCommand;
    Command[] offCommand;

    //初始化每个操作为空操作
    public RemoteControl(){
        onCommand=new Command[5];
        offCommand=new Command[5];
        NullCommand nullCommand = new NullCommand();
        for (int i = 0; i < 5; i++) {
            onCommand[i]=nullCommand;
            offCommand[i]=nullCommand;
        }
    }

    public void setCommond(int index,Command onCommand,Command offCommand){
        this.offCommand[index]=offCommand;
        this.onCommand[index]=onCommand;
    }

    public void clickOn(int index){
        onCommand[index].exceute();
    }

    public void clickOff(int index){
        offCommand[index].exceute();
    }

    /**
     * 输出每个按钮的具体代表类
     * @return
     */
    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < onCommand.length; i++) {
            sb.append("[index : "+i+"]   ");
            sb.append(onCommand[i].getClass().getName());
            sb.append("    ");
            sb.append(offCommand[i].getClass().getName());
            sb.append("
");
        }
        return sb.toString();
    }
}

测试类

package com.commandPattern.testPattern;

import com.commandPattern.command.off.LampOffCommand;
import com.commandPattern.command.off.TvOffCommand;
import com.commandPattern.command.on.LampOnCommand;
import com.commandPattern.command.on.TvOnCommand;
import com.commandPattern.control.RemoteControl;
import com.commandPattern.entity.Lamp;
import com.commandPattern.entity.Tv;

/**
 * @program: test
 * @description:
 * @author: Mr.Yang
 * @create: 2018-12-08 17:48
 **/
public class TestPattern {
    public static void main(String[] args) {
        RemoteControl remoteControl = new RemoteControl();

        /**
         * 创建装置到合适位置
         */
        Tv bedRoomTV = new Tv("卧室");
        Tv drawiTV = new Tv("客厅");

        Lamp bedRoomLamp = new Lamp("卧室");
        Lamp drawiLamp = new Lamp("客厅");

        /**
         * 创建所有命令操作对象
         */
        //卧室灯关闭对象
        LampOffCommand bedLampOffCommand = new LampOffCommand(bedRoomLamp);
        //卧室灯打开对象
        LampOnCommand bedLampOnCommand = new LampOnCommand(bedRoomLamp);
        //卧室TV关闭对象
        TvOffCommand bedTvOffCommand = new TvOffCommand(bedRoomTV);
        //卧室TV打开对象
        TvOnCommand bedTVcommand = new TvOnCommand(bedRoomTV);
        //客厅灯打开对象
        LampOnCommand drawLampOnCommand = new LampOnCommand(drawiLamp);
        //客厅灯关闭对象
        LampOffCommand drawLampOffCommand = new LampOffCommand(drawiLamp);
        //客厅TV关闭对象
        TvOffCommand drawTVOffCommand = new TvOffCommand(drawiTV);
        //客厅TV打开对象
        TvOnCommand drawTVOnCommand = new TvOnCommand(drawiTV);

        System.out.println("---------------------------------------------未赋值之前------------------------------------------------");
        System.out.println(remoteControl);
        System.out.println("******************************************************************************************************");

        /**
         * //将操作对象与卡槽一一对应
         */
        //赋值卧室灯打开与关闭
        remoteControl.setCommond(0,bedLampOnCommand,bedLampOffCommand);
        //赋值卧室TV打开与关闭
        remoteControl.setCommond(1,bedTVcommand,bedTvOffCommand);
        //赋值客厅灯打开与关闭
        remoteControl.setCommond(2,drawLampOnCommand,drawLampOffCommand);
        //赋值客厅TV打开与关闭
        remoteControl.setCommond(3,drawTVOnCommand,drawTVOffCommand);

        System.out.println("---------------------------------------------赋值之后------------------------------------------------");
        System.out.println(remoteControl);
        System.out.println("******************************************************************************************************");


        /**
         * 测试每一个按钮
         */
        remoteControl.clickOn(0);
        remoteControl.clickOff(0);

        remoteControl.clickOn(1);
        remoteControl.clickOff(1);

        remoteControl.clickOn(2);
        remoteControl.clickOff(2);

        remoteControl.clickOn(3);
        remoteControl.clickOff(3);


    }
}

测试结果

---------------------------------------------未赋值之前------------------------------------------------
[index : 0]   com.commandPattern.command.nullCommand.NullCommand    com.commandPattern.command.nullCommand.NullCommand
[index : 1]   com.commandPattern.command.nullCommand.NullCommand    com.commandPattern.command.nullCommand.NullCommand
[index : 2]   com.commandPattern.command.nullCommand.NullCommand    com.commandPattern.command.nullCommand.NullCommand
[index : 3]   com.commandPattern.command.nullCommand.NullCommand    com.commandPattern.command.nullCommand.NullCommand
[index : 4]   com.commandPattern.command.nullCommand.NullCommand    com.commandPattern.command.nullCommand.NullCommand

******************************************************************************************************
---------------------------------------------赋值之后------------------------------------------------
[index : 0]   com.commandPattern.command.on.LampOnCommand    com.commandPattern.command.off.LampOffCommand
[index : 1]   com.commandPattern.command.on.TvOnCommand    com.commandPattern.command.off.TvOffCommand
[index : 2]   com.commandPattern.command.on.LampOnCommand    com.commandPattern.command.off.LampOffCommand
[index : 3]   com.commandPattern.command.on.TvOnCommand    com.commandPattern.command.off.TvOffCommand
[index : 4]   com.commandPattern.command.nullCommand.NullCommand    com.commandPattern.command.nullCommand.NullCommand

******************************************************************************************************
卧室_灯打开
卧室_灯关闭
卧室_电视打开
卧室_电视关闭
客厅_灯打开
客厅_灯关闭
客厅_电视打开
客厅_电视关闭

阿三:飞哥,我这里使用的设计模式-命令模式,
将动作执行(LampOnCommand,TvOnCommand……)与接收者(Lamp,Tv)包装到对象里面,对外暴露的只有一个Command接口中的execute方法,其他对象不需要知道那个接收者执行了什么动作,只需要知道调用execute,就能完成一个请求的操作,这个对象,与其他对象没有关联,完全解耦和,如果需要做新增,不需要修改原有代码,拓展接收者类和动作执行类,就能实现功能。

项目组长阿飞:不错,不错,进步很大。
项目组长阿飞:第5个按钮可能需要做一个恢复上一步动作的效果,类似于CTRL+Z这个效果,你再改改把。
阿三:好的。
阿三:飞哥修改好了,你看下

命令接口新增撤销方法

package com.commandPattern.command;

/**
 * @program: testPattern
 * @description: 命令接口
 * @author: Mr.Yang
 * @create: 2018-12-08 13:54
 **/
public interface Command {

    //执行方法
    public void exceute();

    //撤销方法
    public void revoke();

}

建立一个空对象,实现了撤销方法

package com.commandPattern.command.nullCommand;

import com.commandPattern.command.Command;

/**
 * @program: testPattern
 * @description: 建立一个空对象,在许多设计模式种,都会看到空对象的使用,甚至有些时候,空对象本身也被视为一种设计模式
 * @author: Mr.Yang
 * @create: 2018-12-08 17:40
 **/
public class NullCommand implements Command {
    public void exceute() {
        System.out.println("什么都不做处理");
    }

    public void revoke() {
        System.out.println("什么都不做处理");
    }
}

灯关闭实现了撤销方法

package com.commandPattern.command.off;

import com.commandPattern.command.Command;
import com.commandPattern.entity.Lamp;

/**
 * @program: testPattern
 * @description: 灯关闭
 * @author: Mr.Yang
 * @create: 2018-12-08 17:33
 **/
public class LampOffCommand implements Command {

    Lamp lamp;

    public LampOffCommand(Lamp lamp){
        this.lamp=lamp;
    }
    //灯关闭
    public void exceute() {
        lamp.off();
    }
    //执行到这个具体实现类,代表上一步是灯关闭,撤销操作即为灯打开
    public void revoke() {
        lamp.on();
    }
}

灯打开实现了撤销方法

package com.commandPattern.command.on;

import com.commandPattern.command.Command;
import com.commandPattern.entity.Lamp;

/**
 * @program: testPattern
 * @description: 灯打开的命令
 * @author: Mr.Yang
 * @create: 2018-12-08 17:29
 **/
public class LampOnCommand implements Command {

    Lamp lamp;
    public LampOnCommand(Lamp lamp){
        this.lamp=lamp;
    }

    //灯打开的命令
    public void exceute() {
        lamp.on();
    }
    //执行到这个具体实现类,代表上一步是灯打开,撤销操作即为灯关闭
    public void revoke() {
        lamp.off();
    }
}

电视关闭实现了撤销方法

package com.commandPattern.command.off;

import com.commandPattern.command.Command;
import com.commandPattern.entity.Tv;

/**
 * @program: testPattern
 * @description: 电视关闭
 * @author: Mr.Yang
 * @create: 2018-12-08 17:36
 **/
public class TvOffCommand implements Command {
    Tv tv;
    public TvOffCommand(Tv tv){
        this.tv=tv;
    }
    public void exceute() {
        tv.off();
    }
    //执行到这个具体实现类,代表上一步是电视关闭,撤销操作即为电视打开
    public void revoke() {
        tv.on();
    }
}

电视打开实现了撤销方法

package com.commandPattern.command.on;

import com.commandPattern.command.Command;
import com.commandPattern.entity.Tv;

/**
 * @program: testPattern
 * @description: 电视打开
 * @author: Mr.Yang
 * @create: 2018-12-08 17:37
 **/
public class TvOnCommand implements Command {
    Tv tv;
    public TvOnCommand(Tv tv){
        this.tv=tv;
    }

    public void exceute() {
        tv.on();
    }
    //执行到这个具体实现类,代表上一步是电视打开,撤销操作即为电视关闭
    public void revoke() {
        tv.off();
    }
}

遥控器类,新增变量记录上一步操作

package com.commandPattern.control;

import com.commandPattern.command.Command;
import com.commandPattern.command.nullCommand.NullCommand;

import java.util.Arrays;

/**
 * @program: testPattern
 * @description: 遥控器
 * @author: Mr.Yang
 * @create: 2018-12-08 17:39
 **/
public class RemoteControl {

    Command[] onCommand;
    Command[] offCommand;
    //这个变量来记录上一个命令
    Command upStepCommand;
    //初始化每个操作为空操作
    public RemoteControl(){
        onCommand=new Command[5];
        offCommand=new Command[5];
        NullCommand nullCommand = new NullCommand();
        for (int i = 0; i < 5; i++) {
            onCommand[i]=nullCommand;
            offCommand[i]=nullCommand;
        }
        upStepCommand=nullCommand;
    }

    public void setCommond(int index,Command onCommand,Command offCommand){
        this.offCommand[index]=offCommand;
        this.onCommand[index]=onCommand;
    }
    //新增upStepCommand记录上一步命令
    public void clickOn(int index){
        onCommand[index].exceute();
        upStepCommand=onCommand[index];
    }
    //新增upStepCommand记录上一步命令
    public void clickOff(int index){
        offCommand[index].exceute();
        upStepCommand=offCommand[index];
    }

    public void toUpStepClick(){
        System.out.println("---撤销---");
        upStepCommand.revoke();
    }
    /**
     * 输出每个按钮的具体代表类
     * @return
     */
    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < onCommand.length; i++) {
            sb.append("[index : "+i+"]   ");
            sb.append(onCommand[i].getClass().getName());
            sb.append("    ");
            sb.append(offCommand[i].getClass().getName());
            sb.append("
");
        }
        return sb.toString();
    }
}

测试类新增撤销测试

package com.commandPattern.testPattern;

import com.commandPattern.command.off.LampOffCommand;
import com.commandPattern.command.off.TvOffCommand;
import com.commandPattern.command.on.LampOnCommand;
import com.commandPattern.command.on.TvOnCommand;
import com.commandPattern.control.RemoteControl;
import com.commandPattern.entity.Lamp;
import com.commandPattern.entity.Tv;

/**
 * @program: test
 * @description:
 * @author: Mr.Yang
 * @create: 2018-12-08 17:48
 **/
public class TestPattern {
    public static void main(String[] args) {
        RemoteControl remoteControl = new RemoteControl();

        /**
         * 创建装置到合适位置
         */
        Tv bedRoomTV = new Tv("卧室");
        Tv drawiTV = new Tv("客厅");

        Lamp bedRoomLamp = new Lamp("卧室");
        Lamp drawiLamp = new Lamp("客厅");

        /**
         * 创建所有命令操作对象
         */
        //卧室灯关闭对象
        LampOffCommand bedLampOffCommand = new LampOffCommand(bedRoomLamp);
        //卧室灯打开对象
        LampOnCommand bedLampOnCommand = new LampOnCommand(bedRoomLamp);
        //卧室TV关闭对象
        TvOffCommand bedTvOffCommand = new TvOffCommand(bedRoomTV);
        //卧室TV打开对象
        TvOnCommand bedTVcommand = new TvOnCommand(bedRoomTV);
        //客厅灯打开对象
        LampOnCommand drawLampOnCommand = new LampOnCommand(drawiLamp);
        //客厅灯关闭对象
        LampOffCommand drawLampOffCommand = new LampOffCommand(drawiLamp);
        //客厅TV关闭对象
        TvOffCommand drawTVOffCommand = new TvOffCommand(drawiTV);
        //客厅TV打开对象
        TvOnCommand drawTVOnCommand = new TvOnCommand(drawiTV);

        System.out.println("---------------------------------------------未赋值之前------------------------------------------------");
        System.out.println(remoteControl);
        System.out.println("******************************************************************************************************");

        /**
         * //将操作对象与卡槽一一对应
         */
        //赋值卧室灯打开与关闭
        remoteControl.setCommond(0,bedLampOnCommand,bedLampOffCommand);
        //赋值卧室TV打开与关闭
        remoteControl.setCommond(1,bedTVcommand,bedTvOffCommand);
        //赋值客厅灯打开与关闭
        remoteControl.setCommond(2,drawLampOnCommand,drawLampOffCommand);
        //赋值客厅TV打开与关闭
        remoteControl.setCommond(3,drawTVOnCommand,drawTVOffCommand);

        System.out.println("---------------------------------------------赋值之后------------------------------------------------");
        System.out.println(remoteControl);
        System.out.println("******************************************************************************************************");


        /**
         * 测试每一个按钮
         */
        remoteControl.clickOn(0);
        remoteControl.clickOff(0);

        //撤销一次
        remoteControl.toUpStepClick();
        System.out.println("
");

        //撤销一次
        remoteControl.toUpStepClick();
        System.out.println("
");

        remoteControl.clickOn(1);
        remoteControl.clickOff(1);

        //撤销一次
        remoteControl.toUpStepClick();
        System.out.println("
");

        remoteControl.clickOn(2);
        remoteControl.clickOff(2);

        //撤销一次
        remoteControl.toUpStepClick();
        System.out.println("
");

        remoteControl.clickOn(3);
        remoteControl.clickOff(3);

        //撤销一次
        remoteControl.toUpStepClick();
        System.out.println("
");

    }
}

修改之后的测试结果

---------------------------------------------未赋值之前------------------------------------------------
[index : 0]   com.commandPattern.command.nullCommand.NullCommand    com.commandPattern.command.nullCommand.NullCommand
[index : 1]   com.commandPattern.command.nullCommand.NullCommand    com.commandPattern.command.nullCommand.NullCommand
[index : 2]   com.commandPattern.command.nullCommand.NullCommand    com.commandPattern.command.nullCommand.NullCommand
[index : 3]   com.commandPattern.command.nullCommand.NullCommand    com.commandPattern.command.nullCommand.NullCommand
[index : 4]   com.commandPattern.command.nullCommand.NullCommand    com.commandPattern.command.nullCommand.NullCommand

******************************************************************************************************
---------------------------------------------赋值之后------------------------------------------------
[index : 0]   com.commandPattern.command.on.LampOnCommand    com.commandPattern.command.off.LampOffCommand
[index : 1]   com.commandPattern.command.on.TvOnCommand    com.commandPattern.command.off.TvOffCommand
[index : 2]   com.commandPattern.command.on.LampOnCommand    com.commandPattern.command.off.LampOffCommand
[index : 3]   com.commandPattern.command.on.TvOnCommand    com.commandPattern.command.off.TvOffCommand
[index : 4]   com.commandPattern.command.nullCommand.NullCommand    com.commandPattern.command.nullCommand.NullCommand

******************************************************************************************************
卧室_灯打开
卧室_灯关闭
---撤销---
卧室_灯打开


---撤销---
卧室_灯打开


卧室_电视打开
卧室_电视关闭
---撤销---
卧室_电视打开


客厅_灯打开
客厅_灯关闭
---撤销---
客厅_灯打开


客厅_电视打开
客厅_电视关闭
---撤销---
客厅_电视打开

项目组长阿飞:不错,不错,以后给你涨工资。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72613.html

相关文章

  • Objective-C设计模式解析-命令

    摘要:分析命令模式的本质是对命令进行封装,将发出命令的责任和执行命令的责任分割开。系统需要将一组操作组合在一起,即支持宏命令优点缺点优点命令模式的优点降低系统的耦合度。可以比较容易地设计一个命令队列和宏命令组合命令。 看图识模式 前提&场景 我们有若干个军团,军团有若干功能:1.陆地攻击目标 2.轰炸目标 3.保护目标 4.从目标撤退等。将军可以下达指令给军团,调用军团的功能。 需求 在一场...

    Clect 评论0 收藏0
  • js设计模式 --- 命令设计模式

    摘要:命令设计模式涉及三类对象抽象命令类抽象命令类一般是一个抽象类或接口,在其中声明了用于执行请求的等方法,通过这些方法可以调用请求接收者的相关操作。 命令模式 命令模式定义为:Encapsulate a request as an object,there by letting you parameterize clients with different requests,queue o...

    iflove 评论0 收藏0
  • JavaScript设计模式(七):命令模式

    摘要:支持撤销,队列,宏命令等功能。宏命令宏命令一组命令集合命令模式与组合模式的产物发布者发布一个请求,命令对象会遍历命令集合下的一系列子命令并执行,完成多任务。 showImg(https://segmentfault.com/img/bVbu3CN?w=800&h=600); 命令模式:请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相...

    lixiang 评论0 收藏0
  • python 设计模式-命令模式

    摘要:命令模式先来看下命令模式的定义命令模式将请求封装成对象,以便使用不同的请求队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。通过新增两个方法,命令模式能够支持这一点。 命令模式 题目: 现在要做一个智能家居控制遥控器,功能如下图所示。 showImg(https://segmentfault.com/img/remote/1460000012774337?w=1730&h=1...

    tomlingtm 评论0 收藏0
  • python设计模式

    摘要:在本节实验中,我们学习了四种设计模式策略模式,观察者模式,命令模式以及模板方法模式。这四种设计模式都是行为型模式。这就是适配器模式。下面让我们看看适配器模式在实验楼中使用吧。准确来说,装饰者模式能动态的给对象添加行为。 1、策略模式 策略模式将各种操作(算法)进行封装,并使它们之间可以互换。互换的意思是说可以动态改变对象的操作方式(算法)。 -- coding: utf-8 -- im...

    array_huang 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<