前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >命令模式

命令模式

作者头像
提莫队长
发布2019-02-21 13:36:22
2760
发布2019-02-21 13:36:22
举报
文章被收录于专栏:刘晓杰刘晓杰

1.定义

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

2.实现(俄罗斯方块)

代码语言:javascript
复制
/**
 *  俄罗斯方块操作的真正逻辑代码(左移,右移,快速落下,改变形状)
 */
public class RealOperation {
    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");
    } 
}

/**
 * 命令的抽象,便于实现四个具体的命令
 */
public interface Command {
    public void execute();
}

/**
 * 左移的具体实现
 */
public class LeftCommand implements Command {
    private RealOperation operation;

    public LeftCommand(RealOperation operation) {
        this.operation = operation;
    }

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

}

/**
 * 右移的具体实现
 */
public class RightCommand implements Command {
    private RealOperation operation;

    public RightCommand(RealOperation operation) {
        this.operation = operation;
    }

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

}

/**
 * 快速落下的具体实现
 */
public class FallCommand implements Command {
    private RealOperation operation;

    public FallCommand(RealOperation operation) {
        this.operation = operation;
    }

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

}

/**
 * 改变形状的具体实现
 */
public class TransFormCommand implements Command {
    private RealOperation operation;

    public TransFormCommand(RealOperation operation) {
        this.operation = operation;
    }

    @Override
    public void execute() {
        operation.transForm();
    }

}

/**
 * 四个命令的管理者
 */
public class Commander {
    private LeftCommand leftCommand;
    private RightCommand rightCommand;
    private FallCommand fallCommand;
    private TransFormCommand transFormCommand;

    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 FallCommand getFallCommand() {
        return fallCommand;
    }

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

    public TransFormCommand getTransFormCommand() {
        return transFormCommand;
    }

    public void setTransFormCommand(TransFormCommand transFormCommand) {
        this.transFormCommand = transFormCommand;
    }

    //调用具体实现方法实现命令

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

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

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

    public void transForm(){
        transFormCommand.execute();
    }
}

    public static void main(String[] args) {
        RealOperation realOperation = new RealOperation();

        //构造四种命令
        LeftCommand leftCommand = new LeftCommand(realOperation);
        RightCommand rightCommand = new RightCommand(realOperation);
        FallCommand fallCommand = new FallCommand(realOperation);
        TransFormCommand transFormCommand = new TransFormCommand(realOperation);

        Commander commander = new Commander();
        commander.setLeftCommand(leftCommand);
        commander.setRightCommand(rightCommand);
        commander.setFallCommand(fallCommand);
        commander.setTransFormCommand(transFormCommand);

        commander.toLeft();
        commander.toRight();
        commander.fall();
        commander.transForm();

        //如果写realOperation.toLeft();是不是更容易接受?
        //问题的是如果我需要一个命令是由已知的命令的组合怎么办?
        //如果是命令模式,只需要新建一个Command然后自己实现就行,只需要拓展不需要修改(RealOperation)
        //如果不采用命令模式是需要修改RealOperation类的
    }

3.总结

1.优点 命令模式的封装性很好,更弱的耦合性,更灵活的控制性以及更好的扩展性。 2.缺点 类的膨胀,大量衍生类的创建。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年03月12日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.定义
  • 2.实现(俄罗斯方块)
  • 3.总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档