博客
关于我
《研磨设计模式》chap7 抽象工厂 Abstract Factory
阅读量:62 次
发布时间:2019-02-25

本文共 4716 字,大约阅读时间需要 15 分钟。

?????????CPU???

  • ????
  • 1.1 CPU

    Intel?CPU???

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

    AMDCPU?

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

    1.2 ??

    ??????

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

    1.3 ??CPU??????

    ??CPU??????

    public class CPUFactory {
    public static CPUApi createCPUApi(int type) {
    CPUApi cpu = null;
    if (type == 1) {
    cpu = new IntelCPU(1156);
    } else if (type == 2) {
    cpu = new AMDCPU(939);
    }
    return cpu;
    }
    }

    ??????????

    public class MainboardFactory {
    public static MainboardApi createMainboardApi(int type) {
    MainboardApi mainboard = null;
    if (type == 1) {
    mainboard = new GAMainboard(1156);
    } else if (type == 2) {
    mainboard = new MSIMainboard(939);
    }
    return mainboard;
    }
    }

    1.4 ???

    public class ComputerEngineer {
    private CPUApi cpu = null;
    private MainboardApi mainboard = null;
    public void makeComputer(int cpuType, int mainboardType) {
    prepareHardwares(cpuType, mainboardType);
    // ????
    // ????
    // ????
    }
    private void prepareHardwares(int cpuType, int mainboardType) {
    this.cpu = CPUFactory.createCPUApi(cpuType);
    this.mainboard = MainboardFactory.createMainboardApi(mainboardType);
    this.cpu.calculate();
    this.mainboard.installCPU();
    }
    }

    client?

    public static void main(String[] args) {
    ComputerEngineer engineer = new ComputerEngineer();
    engineer.makeComputer(1, 2);
    }
    1. ????????
    2. ??????????

      public interface AbstractFactory {
      public AbstractProductA createProductA();
      public AbstractProductB createProductB();
      }
      public class Schema1 implements AbstractFactory {
      public CPUApi createCPUApi() {
      return new IntelCPU(1156);
      }
      public MainboardApi createMainboardApi() {
      return new GAMainboard(1156);
      }
      }
      public class ComputerEngineer {
      private CPUApi cpu = null;
      private MainboardApi mainboard = null;
      private MemoryApi memory = null;
      public void makeComputer(AbstractFactory schema) {
      prepareHardwares(schema);
      // ????
      // ????
      // ????
      }
      private void prepareHardwares(AbstractFactory schema) {
      this.cpu = (CPUApi) schema.createProduct(1);
      this.mainboard = (MainboardApi) schema.createProduct(2);
      this.memory = (MemoryApi) schema.createProduct(3);
      this.cpu.calculate();
      this.mainboard.installCPU();
      if (memory != null) {
      this.memory.cacheData();
      }
      }
      }
      1. ????????
      2. public class Schema3 implements AbstractFactory {
        public Object createProduct(int type) {
        Object retObj = null;
        if (type == 1) {
        retObj = new IntelCPU(1156);
        } else if (type == 2) {
        retObj = new GAMainboard(1156);
        } else if (type == 3) {
        retObj = new HyMemory();
        }
        return retObj;
        }
        }

        public class ComputerEngineer { private CPUApi cpu = null; private MainboardApi mainboard = null; private MemoryApi memory = null;

        public void makeComputer(AbstractFactory schema) {
        prepareHardwares(schema);
        // ????
        // ????
        // ????
        }
        private void prepareHardwares(AbstractFactory schema) {
        this.cpu = (CPUApi) schema.createProduct(1);
        this.mainboard = (MainboardApi) schema.createProduct(2);
        this.memory = (MemoryApi) schema.createProduct(3);
        this.cpu.calculate();
        this.mainboard.installCPU();
        if (memory != null) {
        this.memory.cacheData();
        }
        }

        }

        public static void main(String[] args) { ComputerEngineer engineer = new ComputerEngineer(); AbstractFactory schema = new Schema3(); engineer.makeComputer(schema); }

        ????
        ???????????
        ???????????????????????????????????

    转载地址:http://gvq.baihongyu.com/

    你可能感兴趣的文章
    nodejs封装http请求
    查看>>
    nodejs常用组件
    查看>>
    nodejs开发公众号报错 40164,白名单配置找不到,竟然是这个原因
    查看>>
    Nodejs异步回调的处理方法总结
    查看>>
    NodeJS报错 Fatal error: ENOSPC: System limit for number of file watchers reached, watch ‘...path...‘
    查看>>
    nodejs支持ssi实现include shtml页面
    查看>>
    Nodejs教程09:实现一个带接口请求的简单服务器
    查看>>
    nodejs服务端实现post请求
    查看>>
    nodejs框架,原理,组件,核心,跟npm和vue的关系
    查看>>
    Nodejs概览: 思维导图、核心技术、应用场景
    查看>>
    nodejs模块——fs模块
    查看>>
    Nodejs模块、自定义模块、CommonJs的概念和使用
    查看>>
    nodejs生成多层目录和生成文件的通用方法
    查看>>
    nodejs端口被占用原因及解决方案
    查看>>
    Nodejs简介以及Windows上安装Nodejs
    查看>>
    nodejs系列之express
    查看>>
    nodejs系列之Koa2
    查看>>
    Nodejs连接mysql
    查看>>
    nodejs连接mysql
    查看>>
    NodeJs连接Oracle数据库
    查看>>