命令模式

1.定义

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

2.实现(俄罗斯方块)

/**
 *  俄罗斯方块操作的真正逻辑代码(左移,右移,快速落下,改变形状)
 */
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.缺点
类的膨胀,大量衍生类的创建。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值