package cn.bunny.pattern5.demo4;

import java.util.ArrayList;
import java.util.List;

public class BakeDemo {

    // 充当客户端
    public static void main(String[] args) {
        Waiter waiter = new Waiter();
        Barbecue barbecue = new Barbecue();

        waiter.addCommand(new BakeChickenWing(barbecue));
        waiter.addCommand(new BakeMutton(barbecue));

        waiter.notifyCommand();
    }

    // 接受者-厨师
    static class Barbecue {

        /**
         * 烤串
         */
        public void bakeMutton() {
            System.out.println("开始烤串。。。");
        }

        /**
         * 烤鸡翅
         */
        public void bakeChickenWing() {
            System.out.println("开始烤鸡翅。。。");
        }
    }

    // 请求者-invoker-服务员
    public static class Waiter {
        private final List<Command> commandList = new ArrayList<>();

        public void addCommand(Command command) {
            commandList.add(command);
        }

        /**
         * 通知厨师开始制作
         */
        public void notifyCommand() {
            for (Command command : commandList) {
                command.execute();
            }
        }
    }

    public static abstract class Command {
        protected Barbecue barbecue;

        public Command(Barbecue barbecue) {
            this.barbecue = barbecue;
        }

        /**
         * 执行方法
         */
        public abstract void execute();
    }

    // 命令-烤鸡翅
    public static class BakeChickenWing extends Command {

        public BakeChickenWing(Barbecue barbecue) {
            super(barbecue);
        }

        /**
         * 执行方法
         */
        @Override
        public void execute() {
            barbecue.bakeChickenWing();
        }
    }

    // 命令-烤串
    public static class BakeMutton extends Command {

        public BakeMutton(Barbecue barbecue) {
            super(barbecue);
        }

        /**
         * 执行方法
         */
        @Override
        public void execute() {
            barbecue.bakeMutton();
        }
    }
}
