读设计模式之禅--命令模式

  • 将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。
package design.command;
/**
 * @Author: hyh
 * @Date: 2021/8/17 20:19
 * 抽象一个机器人
 **/
public abstract class Robot {
    public abstract void doThing();
}
/**
 * 跳舞接收器接受命令执行
 **/
class Dance extends Robot {
    @Override
    public void doThing() {
        System.out.println("机器人跳舞");
    }
}
/**
 * 唱歌接收器接受命令执行
 **/
 class Sing extends Robot {
    @Override
    public void doThing() {
        System.out.println("机器人唱歌");
    }
}
package design.command;


/**
 * 抽象命令接口
 **/
abstract class Command {
    // 执行命令
    public abstract void execute();
}

/**
 * 跳舞命令
 **/
class DanceCommand extends Command {

    private Robot r;

    public DanceCommand(Robot r) {
        this.r = r;
    }

    @Override
    public void execute() {
        r.doThing();
    }
}

// 唱歌命令
class SingCommand extends Command {

    private Robot r;

    public SingCommand(Robot r) {
        this.r = r;
    }

    @Override
    public void execute() {
        r.doThing();
    }
}

//  我
class Me {
    private Command c;

    public void setCommand(Command c) {
        this.c = c;
    }

    public void action() {
        this.c.execute();
    }

    public static void main(String[] args) {
        // new 一个自己
        Me me = new Me();
        // new 一个接收器
        Robot sing = new Sing();
        // 发送唱歌命令
        Command singCommand = new SingCommand(sing);
        // 自己发命令
        me.setCommand(singCommand);
        // 执行
        me.action();
    }
}
输出:
机器人唱歌

修改后:

package command;

/**
 * @Author: hyh
 * @Date: 2021/8/17 20:19
 * 抽象一个机器人
 **/
public abstract class Robot {
    public abstract void doThing();
}

/**
 * 接收跳舞命令执行
 **/
class Dance extends Robot {
    @Override
    public void doThing() {
        System.out.println("机器人跳舞");
    }
}

/**
 * 接收唱歌命令执行
 **/
class Sing extends Robot {
    @Override
    public void doThing() {
        System.out.println("机器人唱歌");
    }
}


/**
 * 抽象命令接口
 **/
abstract class Command {

    // 唱歌功能
    protected Sing s = new Sing();
    // 跳舞功能
    protected Dance d = new Dance();

    // 执行命令
    public abstract void execute();
}

/**
 * 跳舞命令
 **/
class DanceCommand extends Command {
    @Override
    public void execute() {
        super.d.doThing();
    }
}

// 唱歌命令
class SingCommand extends Command {
    @Override
    public void execute() {
        super.s.doThing();
        //当然也可以既场各又跳舞
        //super.d.doThing();
    }
}

//  我
class Me {
    private Command c;
    public void setCommand(Command c) {
        this.c = c;
    }
    public void action() {
        this.c.execute();
    }
    public static void main(String[] args) {
        // new 一个自己
        Me me = new Me();
        // 发送唱歌命令
        Command singCommand = new SingCommand();
        // 自己发命令
        me.setCommand(singCommand);
        // 执行
        me.action();
    }
}
输出:
机器人唱歌

class Me {
    private Command c;

    public void setCommand(Command c) {
        this.c = c;
    }

    public void action() {
        this.c.execute();
    }

    public static void main(String[] args) {
        // new 一个自己
        Me me = new Me();
        // 发送唱歌命令
        Command danceCommand = new DanceCommand();
        // 自己发命令
        me.setCommand(danceCommand);
        // 执行
        me.action();
    }
}
输出:
机器人跳舞

再次修改:

package command;

/**
 * @Author: hyh
 * @Date: 2021/8/17 20:19
 * 抽象一个机器人
 **/
public abstract class Robot {

    public abstract void doThing();
}

/**
 * 接收跳舞命令执行
 **/
class Dance extends Robot {
    @Override
    public void doThing() {
        System.out.println("机器人跳舞");
    }
}

/**
 * 接收唱歌命令执行
 **/
class Sing extends Robot {
    @Override
    public void doThing() {
        System.out.println("机器人唱歌");
    }
}


/**
 * 抽象命令接口
 **/
abstract class Command {
    // 
    protected Robot r;
    
    public Command(Robot r) {
        this.r = r;
    }
    // 执行命令
    public abstract void execute();
}

/**
 * 跳舞命令
 **/
class DanceCommand extends Command {
    public DanceCommand(){
        super(new Dance());
    }
    @Override
    public void execute() {
        super.r.doThing();

    }
}

// 唱歌命令
class SingCommand extends Command {

    public SingCommand(){
        super(new Sing());
    }

    @Override
    public void execute() {
        super.r.doThing();
    }
}

//  我
class Me {
    private Command c;

    public void setCommand(Command c) {
        this.c = c;
    }

    public void action() {
        this.c.execute();
    }

    public static void main(String[] args) {
        // new 一个自己
        Me me = new Me();
        // 发送唱歌命令
        Command danceCommand = new DanceCommand();
        // 自己发命令
        me.setCommand(danceCommand);
        // 执行
        me.action();
    }
}
输出:
机器人跳舞
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值