设计模式之----命令模式(command-pattern)的理解

1.前言

在软件开发系统中,“方法的请求者”与“方法的实现者”之间经常存在紧密的耦合关系,这不利于软件功能的扩展与维护。因此“如何将方法的请求者与实现者解耦?”变得很重要,命令模式就能很好地解决这个问题。

在现实生活中,命令模式的例子也很多。比如看电视时,我们只需要轻轻一按遥控器就能完成频道的切换,这就是命令模式,将换台请求和换台处理完全解耦了。电视机遥控器(命令发送者)通过按钮(具体命令)来遥控电视机(命令接收者)。


2.概念

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


3.结构与实现


可以将相关操作抽象成命令,使调用者与实现者相关分离。

3.1 模式的结构

命令模式包含以下主要角色:

  1. 抽象命令类(Command)角色:声明执行命令的接口,拥有执行命令的抽象方法 execute()。
  2. 具体命令类(Concrete Command)角色:是抽象命令类或者接口的具体实现类,它拥有接收者对象,并通过调用接收者的功能来完成命令要执行的操作。
  3. 实现者/接收者(Receiver)角色:执行命令功能的相关操作,是具体命令对象业务的真正实现者。
  4. 调用者/请求者(Invoker)角色:是请求的发送者,它通常拥有很多的命令对象,并通过访问命令对象来执行相关请求,它不直接访问接收者。

3.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. 新的命令可以很容易添加到系统中去。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值