命令设计模式 - 定义及使用

1. 定义?


将一个请求封装成一个对象,从而让用户使用不同的请求把客户端参数化,对请求排队或者记录日志,以及支持可撤销的工作;

2. 举例说明


俄罗斯方块
思路分析:
1>:定义俄罗斯方块 - 接收者,TetrisMachine;
2>:定义命令设计模式的命令接口,Command,并且定义执行方法 execute();
3>:定义命令 - 向左移动,LeftCommand,实现命令设计模式接口Command,并实现execute()方法;
4>:定义命令 - 向右移动,RightCommand,实现命令设计模式接口Command,并实现execute()方法;
5>:定义向左、向右的滑动的控件Buttons;
6>:定义测试类Client;

3. 代码如下


1>:定义俄罗斯方块 - 接收者,TetrisMachine:
/**
 * Email: [email protected]
 * Created by Novate 2018/6/9 13:20
 * Version 1.0
 * Params:
 * Description:    俄罗斯方块 - 接收者
*/

public class TetrisMachine {

    /**
     * 向左移动
     */
    public void toLeft(){
        System.out.println("toLeft");
    }

    /**
     * 向右移动
     */
    public void toRight(){
        System.out.println("toRight");
    }

    /**
     * 快速到达底部
     */
    public void fastToBottom(){
        System.out.println("fastToBottom");
    }

    /**
     * 改变形状
     */
    public void transform(){
        System.out.println("transform");
    }
}
2>:定义命令设计模式的命令接口,Command,并且定义执行方法 execute():
/**
 * Email: [email protected]
 * Created by Novate 2018/6/9 13:27
 * Version 1.0
 * Params:
 * Description:    命令设计模式的命令接口
*/

public interface Command {
    // 执行
    void execute() ;
}
3>:定义命令 - 向左移动,LeftCommand,实现命令设计模式接口Command,并实现execute()方法:
/**
 * Email: [email protected]
 * Created by Novate 2018/6/9 13:24
 * Version 1.0
 * Params:
 * Description:    命令 - 向左移动
*/

public class LeftCommand implements Command {

    private TetrisMachine machine ;
    public LeftCommand(TetrisMachine machine){
        this.machine = machine ;
    }

    @Override
    public void execute() {
        machine.toLeft();
    }
}
4>:定义命令 - 向右移动,RightCommand,实现命令设计模式接口Command,并实现execute()方法:
/**
 * Email: [email protected]
 * Created by Novate 2018/6/9 13:24
 * Version 1.0
 * Params:
 * Description:    命令 - 向右移动
*/

public class RightCommand implements Command {

    private TetrisMachine machine ;
    public RightCommand(TetrisMachine machine){
        this.machine = machine ;
    }

    @Override
    public void execute() {
        machine.toRight();
    }
}
5>:定义向左、向右的滑动的控件Buttons:
/**
 * Email: [email protected]
 * Created by Novate 2018/6/9 13:28
 * Version 1.0
 * Params:
 * Description:
*/

public class Buttons {
    LeftCommand leftCommand ;
    RightCommand rightCommand ;

    public LeftCommand getLeftCommand() {
        return leftCommand;
    }

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

    public RightCommand getRightCommand() {
        return rightCommand;
    }

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


    /**
     * 向左移动
     */
    public void toLeft(){
        leftCommand.execute();
    }

    /**
     * 向右移动
     */
    public void toRight(){
        rightCommand.execute();
    }
}
6>:定义测试类Client:
/**
 * Email: [email protected]
 * Created by Novate 2018/6/9 13:25
 * Version 1.0
 * Params:
 * Description:    命令设计模式 - 测试类
*/

public class Client {
    public static void main(String[] args){

        // 复杂版,需要注意3个角色:命令角色、命令接收者(执行者)、命令的发送者
        TetrisMachine machine = new TetrisMachine() ;
        LeftCommand leftCommand = new LeftCommand(machine) ;
        RightCommand rightCommand = new RightCommand(machine) ;

        // 设置命令
        Buttons buttons = new Buttons() ;
        buttons.setLeftCommand(leftCommand);
        buttons.setRightCommand(rightCommand);


        // 向右移动、向左移动、向右移动
        buttons.toRight();
        buttons.toLeft();
        buttons.toRight();


        // 简单版,下边两行代码即可实现
        machine.toRight();
        machine.toLeft();

    }
}

具体代码已上传至github:
https://github.com/shuai999/Architect_day20.git

你可能感兴趣的:(命令设计模式 - 定义及使用)