03. Abstract Factory模式代码

    3. Abstract Factory 本质:选择产品簇的实现
      3.1 PC DIY
        public interface CpuApi {
            public void calculate();
        }
        public class AmdCpu implements CpuApi{
            private int pins = 0;
            public AmdCpu(int pins) {
                this.pins = pins;
            }
            public void calculate() {
                System.out.println("Now in AMD CPU, pins = " + pins);
            }
        }
        public class IntelCpu implements CpuApi{
            private int pins = 0;
            public IntelCpu(int pins) {
                this.pins = pins;
            }
            public void calculate() {
                System.out.println("Now in Intel CPU, pins = " + pins);
            }
        }
        public interface MainboardApi {
            public void installCpu();
        }
        public class GaMainboard implements MainboardApi{
            private int cpuHoles = 0;
            public GaMainboard(int cpuHoles) {
                this.cpuHoles = cpuHoles;
            }
            public void installCpu() {
                System.out.println("Now in GAMainboard, cpuHoles = " + cpuHoles);
            }
        }
        public class MsiMainboard implements MainboardApi{
            private int cpuHoles = 0;
            public MsiMainboard(int cpuHoles) {
                this.cpuHoles = cpuHoles;
            }
            public void installCpu() {
                System.out.println("Now in MSIMainboard, cpuHoles = " + cpuHoles);
            }
        }
        public interface AbstractFactory {
            public CpuApi createCpuApi();
            public MainboardApi createMainboardApi();
        }
        public class Schema1 implements AbstractFactory{
            public CpuApi createCpuApi() {
                return new IntelCpu(1156);
            }
            public MainboardApi createMainboardApi() {
                return new GaMainboard(1156);
            }
        }
        public class Schema2 implements AbstractFactory{
            public CpuApi createCpuApi() {
                return new AmdCpu(939);
            }
            public MainboardApi createMainboardApi() {
                return new MsiMainboard(939);
            }
        }
        public class ComputerEngineer {
            private CpuApi cpu = null;
            private MainboardApi mainboard = null;
            public void makeComputer(AbstractFactory schema) {
                prepareHardwares(schema);
            }
            private void prepareHardwares(AbstractFactory schema) {
                this.cpu = schema.createCpuApi();
                this.mainboard = schema.createMainboardApi();
                this.cpu.calculate();
                this.mainboard.installCpu();
            }
        }
        public class AbstractFactoryClient {
            public static void main(String[] args) {
                ComputerEngineer engineer = new ComputerEngineer();
                AbstractFactory schema = new Schema1();
                engineer.makeComputer(schema);
            }
        }


 

阅读更多
个人分类: 设计模式
上一篇02. Factory Method模式代码
下一篇14. Template Method模式代码
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭