设计模式

命令模式(Command)

2020-07-23  本文已影响0人  秀儿2020

定义

将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化,对请求排队或者记录请求日志,以及支持可撤销操作。

本质

封装请求

登场角色

示例代码

/**
 * 接收者角色,处理具体逻辑
 * 是整个命令模式中唯一处理具体代码逻辑的地方,
 * 其他的类都是直接或间接的调用到该类的方法
 */
public class TerisMachine {
    /**
     * 真正处理向左操作的逻辑
     */
    public void toLeft(){
        System.out.println("向左");
    }

    /**
     * 真正处理向右操作的逻辑
     */
    public void toRight(){
        System.out.println("向右");
    }

    /**
     * 真正处理快速落下操作的逻辑
     */
    public void fastToBottom(){
        System.out.println("快速落下");
    }

    /**
     * 真正处理改变形状操作的逻辑
     */
    public void transform(){
        System.out.println("改变形状");
    }
}

/**
 * 命令抽象者
 */
public interface Command {
    void execute();
}


/**
 * 具体命令,持有一个接收者角色对象
 */
public class LeftCommand implements Command{
    private TerisMachine terisMachine;

    public LeftCommand(TerisMachine terisMachine) {
        this.terisMachine = terisMachine;
    }

    @Override
    public void execute() {
        terisMachine.toLeft();
    }
}


/**
 * 具体的命令
 */
public class RightCommand implements Command{
    private TerisMachine terisMachine;

    public RightCommand(TerisMachine terisMachine) {
        this.terisMachine = terisMachine;
    }

    @Override
    public void execute() {
        terisMachine.toRight();
    }
}

/**
 * 具体的命令
 */
public class FallCommand implements Command{
    private TerisMachine terisMachine;

    public FallCommand(TerisMachine terisMachine) {
        this.terisMachine = terisMachine;
    }

    @Override
    public void execute() {
        terisMachine.fastToBottom();
    }
}

/**
 * 具体的命令
 */
public class TransforCommand implements Command{
    private TerisMachine terisMachine;

    public TransforCommand(TerisMachine terisMachine) {
        this.terisMachine = terisMachine;
    }

    @Override
    public void execute() {
        terisMachine.transform();
    }
}


/**
 * 请求者
 */
public class Buttons {
    private LeftCommand leftCommand;
    private RightCommand rightCommand;
    private FallCommand fallCommand;
    private TransforCommand transforCommand;

    public void setLeftCommand(LeftCommand leftCommand) {
        this.leftCommand = leftCommand;
    }

    public void setRightCommand(RightCommand rightCommand) {
        this.rightCommand = rightCommand;
    }

    public void setFallCommand(FallCommand fallCommand) {
        this.fallCommand = fallCommand;
    }

    public void setTransforCommand(TransforCommand transforCommand) {
        this.transforCommand = transforCommand;
    }

    public void toLeft(){
        leftCommand.execute();
    }

    public void toRight(){
        rightCommand.execute();
    }

    public void fall(){
        fallCommand.execute();
    }

    public void transform(){
        transforCommand.execute();
    }
}


public class Client {
    public static void main(String[] args){
        //构造俄罗斯方块游戏
        TerisMachine terisMachine = new TerisMachine();
        //构造四种命令
        LeftCommand leftCommand = new LeftCommand(terisMachine);
        RightCommand rightCommand = new RightCommand(terisMachine);
        FallCommand fallCommand = new FallCommand(terisMachine);
        TransforCommand transforCommand = new TransforCommand(terisMachine);
        //按钮可以执行不同的命令
        Buttons buttons = new Buttons();
        buttons.setLeftCommand(leftCommand);
        buttons.setRightCommand(rightCommand);
        buttons.setFallCommand(fallCommand);
        buttons.setTransforCommand(transforCommand);
        //具体按下哪个按钮玩家说了算
        buttons.toLeft();
        buttons.toRight();
        buttons.fall();
        buttons.transform();
    }
}

运行结果

向左
向右
快速落下
改变形状

优点

何时使用

上一篇 下一篇

猜你喜欢

热点阅读