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);
            }
        }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值