命令模式14(18)
顾客(命令发送者)通过服务员(具体命令)来通知厨师(命令接收者)做出食物
package behavioral.command; public class Customer { Command command; public Customer(Command command){ this.command = command; } public void eatCommand2Waiter(){ System.out.println("Customer:服务员,我要吃大盘鸡"); String food = command.excute(); System.out.println(food); System.out.println("Customer:开始吃了"); } }
package behavioral.command; public interface Command { public String excute(); } package behavioral.command; public class Waiter implements Command{ private Cook cook; Waiter(){ cook = new Cook(); } @Override public String excute() { System.out.println("Waiter:顾客点餐了,开始做吧"); String food = cook.mark(); System.out.println(food); return "Waiter:客人,您的菜好了"; } }
package behavioral.command; public class Cook { public String mark() { System.out.println("Cook:我开始做了"); return "Cook:食物好了,上菜吧"; } }
package behavioral.command; public class CookMain { public static void main(String[] args) { Command waiter = new Waiter(); Customer c = new Customer(waiter); c.eatCommand2Waiter(); } }