命令模式 | 神奇的魔法卷轴:无尽的潜能

在梦境的尽头,艾瑞克和丽莎发现了一间尘封的古老图书馆。其中,一本厚重的魔法书吸引了他们的目光,书的封面刻有"命令之书"四个大字。他们翻开书页,只见上面记载了各种各样的魔法卷轴,每一张卷轴都可以被召唤出来执行特定的魔法。

interface Command {
    void execute();
}

class FireballSpell implements Command {
    @Override
    public void execute() {
        System.out.println("Casting a fireball spell!");
    }
}

class HealingSpell implements Command {
    @Override
    public void execute() {
        System.out.println("Casting a healing spell!");
    }
}

class Mage {
    private Command spell;

    public void setSpell(Command spell) {
        this.spell = spell;
    }

    public void castSpell() {
        spell.execute();
    }
}

丽莎召唤出一张火球术的卷轴,接着召唤出治愈术的卷轴。艾瑞克感慨地说:“这正是命令模式的体现,每一张卷轴都是一个具体的命令,而魔法师则充当了命令的接收者和执行者。”

在业务的世界里,命令模式同样拥有广泛的应用。想象一下,当你在一个应用程序中点击按钮、选择菜单项或使用快捷键时,背后往往有一个命令被执行。通过将操作封装为对象,我们可以参数化对象,传递、存储和执行命令,甚至还可以支持撤销操作。

class Application {
    private Map<String, Command> commands = new HashMap<>();

    public void registerCommand(String commandName, Command command) {
        commands.put(commandName, command);
    }

    public void executeCommand(String commandName) {
        if(commands.containsKey(commandName)) {
            commands.get(commandName).execute();
        } else {
            System.out.println("Command not found!");
        }
    }
}

在这个示例中,Application类允许注册和执行命令。无论是菜单项、按钮点击,还是快捷键触发,都可以调用相应的命令来执行。

当两人离开这个神奇的图书馆,丽莎问道:“你觉得命令模式还能在哪些业务场景中应用呢?”

🔘 订单系统的创建、修改、取消订单操作
🔘 智能家居系统中的各种操作(如打开灯、关闭窗帘等)
🔘 图形界面的撤销和重做操作
🔘 其他

继续深入到命令模式的应用,让我们来探索一个简单的订单系统。在这个系统中,用户可以创建、修改和取消订单。为了使每一个操作都能被记录和执行,我们将使用命令模式。

首先,我们定义一个订单接口,以及它的具体实现:

class Order {
    private String orderId;
    private String product;
    private int quantity;

    public Order(String orderId, String product, int quantity) {
        this.orderId = orderId;
        this.product = product;
        this.quantity = quantity;
    }

    public void create() {
        System.out.println("Order created: " + product + " x " + quantity);
    }

    public void modify(int newQuantity) {
        this.quantity = newQuantity;
        System.out.println("Order modified: " + product + " x " + newQuantity);
    }

    public void cancel() {
        System.out.println("Order cancelled: " + product + " x " + quantity);
    }
}

接着,为每一个订单操作定义一个具体的命令:

class CreateOrderCommand implements Command {
    private Order order;

    public CreateOrderCommand(Order order) {
        this.order = order;
    }

    @Override
    public void execute() {
        order.create();
    }
}

class ModifyOrderCommand implements Command {
    private Order order;
    private int newQuantity;

    public ModifyOrderCommand(Order order, int newQuantity) {
        this.order = order;
        this.newQuantity = newQuantity;
    }

    @Override
    public void execute() {
        order.modify(newQuantity);
    }
}

class CancelOrderCommand implements Command {
    private Order order;

    public CancelOrderCommand(Order order) {
        this.order = order;
    }

    @Override
    public void execute() {
        order.cancel();
    }
}

最后,我们有一个命令调度器,它负责执行用户发出的命令:

class OrderCommandDispatcher {
    private Queue<Command> commandQueue = new LinkedList<>();

    public void queueCommand(Command command) {
        commandQueue.add(command);
    }

    public void processCommands() {
        while (!commandQueue.isEmpty()) {
            Command command = commandQueue.poll();
            command.execute();
        }
    }
}

这样,当用户发出创建、修改或取消订单的指令时,这些指令就会被添加到命令队列中,并在合适的时间被执行。此外,命令模式的设计还为我们提供了一种轻松添加更多操作或者撤销操作的方式,增强了系统的可扩展性。

艾瑞克看着这些代码说:“看,命令模式不仅仅是理论,它在实际业务中也有广泛的应用,如这个订单系统就是一个生动的例子。”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值