蘇阿細 - 命令模式
type: channel
link: https://www.wangchouchou.com/tag/%E5%91%BD%E4%BB%A4%E6%A8%A1%E5%BC%8F/
items: 行为型模式-命令模式

行为型模式-命令模式
type: item
date: 2022-09-16T15:00:00+00:00
description: （1）概述将一个请求封装为一个对象，使发出请求的责任和执行请求的责任分割开，使两者之间通过命令对象来进行沟通，便于命令对象的存储、传递、调用、增加及管理（2）结构抽象命令角色：定义命令的接口，声明执行的方法具体命令角色：具体的命令，实现命令接口，持有接收者，并调用接收者的功能来完成命令要执行的操作实现者/接收者角色：执行命令的对象（任何类都可能成为一个接收者，只要它能够实现命令要求实现的相应功能）调用者/请求者角色：要求命令对象执行请求，持有命令对象（一个或多个），客户端真正触发命令并要求命令执行相应操作的地方（即使用命令对象的入口）（3）案例以点餐为例抽象命令角色public interface Command { /** * 执行命令 */ void execute(); }public class Order { private int diningTable; private Map<String, Integer> foodDic = new HashMap<>(); public int getDiningTable() { return diningTable; } public void setDiningTable(int diningTable) { this.diningTable = diningTable; } public Map<String, Integer> getFoodDic() { return foodDic; } public void setFood(String foodName, int num) { foodDic.put(foodName, num); } }具体命令角色public class OrderCommand implements Command { //持有接收者对象 private SeniorChef receiver; //持有订单对象 private Order order; public OrderCommand(SeniorChef receiver, Order order) { this.receiver = receiver; this.order = order; } @Override public void execute() { System.out.println(order.getDiningTable() + "号桌的订单："); Map<String, Integer> foodDic = order.getFoodDic(); for (String foodName : foodDic.keySet()) { receiver.makeFood(foodName, foodDic.get(foodName)); } System.out.println(order.getDiningTable() + "号桌的菜品已准备完毕！"); } }实现者/接收者角色public class SeniorChef { public void makeFood(String foodName, int num) { System.out.println("制作了" + num + "份 " + foodName); } }调用者/请求者角色public class Waiter { //持有多个命令对象 private List<Command> commands = new ArrayList<>(); public void command(Command command) { commands.add(command); } public void orderUp() { System.out.println("新订单来了"); for (Command command : commands) { if (command != null) { command.execute(); } } } }Clientpublic class Client { public static void main(String[] args) { //创建订单 Order order1 = new Order(); order1.setDiningTable(1); order1.setFood("小混沌", 1); order1.setFood("煎饼", 1); Order order2 = new Order(); order2.setDiningTable(2); order2.setFood("小炒肉盖饭", 1); order2.setFood("酸梅汤", 1); //创建厨师 SeniorChef receiver = new S…