跳至主要內容

设计模式-行为型-命令模式

引领潮流大约 1 分钟设计模式archive

1、定义

命令模式:将命令请求封装成一个对象,可以将不同请求来进行参数化

写法

public interface IReceive {

    void toLeft();
    void toRight();
    void toUp();
    void toDown();

}

/**
 * 真正引擎
 */
public class Receive implements IReceive{

    public void toLeft(){
        System.out.println("向左");
    }

    public void toRight(){
        System.out.println("向右");
    }

    public void toUp(){
        System.out.println("向上");
    }

    public void toDown(){
        System.out.println("向下");
    }

}

public interface ICommand {
    void execute();
}

public class UpCommand implements ICommand {

    private Receive receive;

    public UpCommand(Receive receive){
        this.receive = receive;
    }

    @Override
    public void execute() {
        receive.toUp();
    }
}
public class DownCommand implements ICommand {

    private Receive receive;

    public DownCommand(Receive receive){
        this.receive = receive;
    }

    @Override
    public void execute() {
        receive.toDown();
    }
}
public class LeftCommand implements ICommand {

    private Receive receive;

    public LeftCommand(Receive receive){
        this.receive = receive;
    }

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

public class RightCommand implements ICommand {

    private Receive receive;

    public RightCommand(Receive receive){
        this.receive = receive;
    }

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

public class Button implements IReceive{

    private LeftCommand leftCommand;
    private RightCommand rightCommand;
    private UpCommand upCommand;
    private DownCommand downCommand;

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

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

    public void setUpCommand(UpCommand upCommand) {
        this.upCommand = upCommand;
    }

    public void setDownCommand(DownCommand downCommand) {
        this.downCommand = downCommand;
    }

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

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

    @Override
    public void toUp() {
        upCommand.execute();
    }

    @Override
    public void toDown() {
        downCommand.execute();
    }
}

/**
 * 命令模式
 *
 * 添加一层命令,便于记录
 *
 * 多对一
 *
 */
public class Player {

    public static void main(String[] args) {

        //接收者
        Receive receive = new Receive();

        //命令
        LeftCommand leftCommand = new LeftCommand( receive );
        RightCommand rightCommand = new RightCommand( receive );
        UpCommand upCommand = new UpCommand( receive );
        DownCommand downCommand = new DownCommand( receive );

        //发送者
        Button button = new Button();
        button.setLeftCommand( leftCommand );
        button.setRightCommand( rightCommand );
        button.setDownCommand( downCommand );
        button.setUpCommand( upCommand );

        //调用
        button.toLeft();
        button.toRight();
        button.toDown();
        button.toUp();

    }

}

代码示例

https://github.com/yinlingchaoliu/23-design-pattern