package cn.bunny.pattern6;

import lombok.Data;

public class BuilderDemo {
    public static void main(String[] args) {
        ConcreteLowComputerBuilder concreteLowComputerBuilder = new ConcreteLowComputerBuilder();
        Director director = new Director(concreteLowComputerBuilder);
        director.constructComputer();
        System.out.println(concreteLowComputerBuilder.getComputer());

        System.out.println("\n-----------------分割线-----------------\n");

        ConcreteHighComputerBuilder concreteHighComputerBuilder = new ConcreteHighComputerBuilder();
        director = new Director(concreteHighComputerBuilder);
        director.constructComputer();
        System.out.println(concreteHighComputerBuilder.getComputer());
    }

    // 构建产品接口
    interface ComputerBuilder {

        /**
         * 构建 CPU 方法
         */
        void buildCPU();

        /**
         * 构建 RAM 方法
         */
        void buildRAM();

        /**
         * 构建 磁盘 方法
         */
        void buildHardDrive();

        /**
         * 返回产品
         *
         * @return Computer
         */
        Computer getComputer();
    }

    // 产品类
    @Data
    static class Computer {
        private String CPU;
        private String RAM;
        private String hardDrive;
    }

    // 构建低端产品
    static class ConcreteLowComputerBuilder implements ComputerBuilder {
        private final Computer computer = new Computer();

        /**
         * 构建 CPU 方法
         */
        @Override
        public void buildCPU() {
            computer.setCPU("Low CPU");
        }

        /**
         * 构建 RAM 方法
         */
        @Override
        public void buildRAM() {
            computer.setRAM("Low RAM");
        }

        /**
         * 构建 磁盘 方法
         */
        @Override
        public void buildHardDrive() {
            computer.setHardDrive("Low Driver");
        }

        /**
         * 返回产品
         *
         * @return Computer
         */
        @Override
        public Computer getComputer() {
            return computer;
        }
    }

    // 构建高端产品
    static class ConcreteHighComputerBuilder implements ComputerBuilder {
        private final Computer computer = new Computer();

        /**
         * 构建 CPU 方法
         */
        @Override
        public void buildCPU() {
            computer.setCPU("High CPU");
        }

        /**
         * 构建 RAM 方法
         */
        @Override
        public void buildRAM() {
            computer.setRAM("High RAM");
        }

        /**
         * 构建 磁盘 方法
         */
        @Override
        public void buildHardDrive() {
            computer.setHardDrive("High Driver");
        }

        /**
         * 返回产品
         *
         * @return Computer
         */
        @Override
        public Computer getComputer() {
            return computer;
        }
    }

    // 构建者
    public static class Director {
        private final ComputerBuilder builder;

        public Director(ComputerBuilder builder) {
            this.builder = builder;
        }

        public void constructComputer() {
            builder.buildCPU();
            builder.buildRAM();
            builder.buildHardDrive();
        }
    }
}
