关闭

03. Abstract Factory模式代码

387人阅读 评论(0) 收藏 举报

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


 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:43130次
    • 积分:687
    • 等级:
    • 排名:千里之外
    • 原创:23篇
    • 转载:0篇
    • 译文:0篇
    • 评论:22条
    最新评论