其他分享
首页 > 其他分享> > 设计模式之----命令模式的理解

设计模式之----命令模式的理解

作者:互联网

1.概念

命令模式(Command Pattern)是一种行为型模式。请求以命令的形式包裹在对象中,调用者(invoker)可以处理该命令的对象,并把该命令传给相应的接受者(receiver)对象,该对象执行命令。
比如要对行为进行记录、撤销/重做、各种操作等处理,这种无法抵御变化的紧耦合是不合适的。在这种情况下,如何将"行为请求者"与"行为实现者"解耦?将一组行为抽象为对象,可以实现二者之间的松耦合。

2.模式的实现

场景:假如,客户要求你写个遥控器的程序,要求遥控器可以控制多个家用电器(比如电扇,灯之类的),看下图,每个按钮都对应着一个命令,这里还有uodo redo的实现!
在这里插入图片描述
我们来分析一个用命令模式怎么写这个程序,先把类图画出来
在这里插入图片描述

Invoker就是调用者,这里就是遥控器,命令对象就是那些命令 command(比如开灯,关灯等),但是要执行这些命令需要执行者,这里就是接受者(receive)的意思,接受者去执行它,顺序:调用者→命令→接受者。

1.首选我们创建一个命令接口 ICommand,一个接受者接口 IReceive

//ICommand.ts
export interface ICommand {
    execuse();
    uodo();
    redo();
}

//IReceive.ts
export interface IReceive {
    on();
    off();
}

2.分别实现开(OnCommand)和关(OffCommand)的接口

//OnCommand.ts
import { ICommand } from "./Icommand"
export class OnCommand implements ICommand {

    public receive: pattern.IReceive;

    constructor(electirc:  pattern.IReceive) {
        this.receive = electirc;
    }

    public execuse() {
        this.receive.on();
    }

    public uodo() {
        this.receive.off();
    }

    public redo() {
        this.receive.on();
    }
}


//OffCommand.ts
import { ICommand } from "./Icommand"
export class OffCommand implements ICommand {

    public receive: pattern.IReceive;

    constructor(receive: pattern.IReceive) {
        this.receive = receive;
    }

    public execuse() {
        this.receive.off();
    }

    public uodo() {
        this.receive.on();
    }

    public redo() {
        this.receive.off();
    }
}

3.分别实现接受者,灯(Lighjt)和电扇(Electirc)的类

//Electirc.ts
import { IReceive } from "./IReceive"
export class Electirc implements IReceive {
    public on() {
        alert("开扇");
    }
    public off() {
        alert("关扇");
    }
}

//Light.ts
import { IReceive } from "./IReceive"
export class Light implements IReceive {
    public on() {
        alert("开灯");
    }
    public off() {
        alert("关灯");
    }
}

4.命令对象有了,接受者也有了,那么下面再来实现调用者(遥控器)OperationInvoker类

export class OperationInvoker {
    private command: pattern.ICommand;
    public uodoCommandArray: Array<pattern.ICommand> = [];
    public redoCommandArray: Array<pattern.ICommand> = [];

    public setCommand(command: pattern.ICommand) {
        this.command = command;
    }

    public executeCommands() {
        this.command.execuse();
        this.uodoCommandArray.push(this.command);
    }

    public undo() {
        if (this.uodoCommandArray.length == 0) {
            return;
        }
        //取uodoCommand数组里面第一个
        let cmd = this.uodoCommandArray.pop() as pattern.ICommand | undefined;
        if (cmd) {
            //执行完加入到redoCommand数组里面
            cmd && cmd.uodo();
            this.redoCommandArray.push(cmd);
        }
    }


    public redo() {
        if (this.redoCommandArray.length == 0) {
            return;
        }
        //取redoCommand数组里面第一个
        let cmd = this.redoCommandArray.pop() as pattern.ICommand | undefined;
        if (cmd) {
            cmd && cmd.redo();
            //执行完加入到uodoCommand数组里面
            this.uodoCommandArray.push(cmd);
        }
    }
}

5.最后看下外面怎么实现的?下面是html页面
在这里插入图片描述
6.下面是外层的Index.controller

//Index.controller.ts
import angular from "angular";
import { OperationInvoker } from "./OperationInvoker"
import { Light } from "./Light"
import { Electirc } from "./Electirc"
import { OnCommand } from "./OnCommand"
import { OffCommand } from "./OffCommand"

export class IndexController implements angular.IController {

    public controller: OperationInvoker

    constructor() {
        this.controller = new OperationInvoker();
    }

    lightOn() {
        let light:Light= new Light();
        this.controller.setCommand(new OnCommand(light));
        this.controller.executeCommands();

    }

    lightOff() {
        let light:Light = new Light();
        this.controller.setCommand(new OffCommand(light));
        this.controller.executeCommands();
    }

    electircOn() {
        let electirc:Electirc = new Electirc();
        this.controller.setCommand(new OnCommand(electirc));
        this.controller.executeCommands();
    }

    electircOff() {
        let electirc:Electirc = new Electirc();
        this.controller.setCommand(new OffCommand(electirc));
        this.controller.executeCommands();
    }

    undo() {
        this.controller.undo();
    }

    redo() {
        this.controller.redo();
    }
}

最后看下实现效果:
在这里插入图片描述

总结一下次模式的优缺点

优点

  1. 降低了系统耦合度。
  2. 新的命令可以很容易添加到系统中去。

缺点:使用命令模式可能会导致某些系统有过多的具体命令类。

标签:ICommand,IReceive,receive,cmd,----,controller,理解,设计模式,public
来源: https://blog.csdn.net/ws9029/article/details/115529548