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

本文共 4578 字,大约阅读时间需要 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/

    你可能感兴趣的文章
    Node-RED中将CSV数据写入txt文件并从文件中读取解析数据
    查看>>
    Node-RED中建立TCP服务端和客户端
    查看>>
    Node-RED中建立Websocket客户端连接
    查看>>
    Node-RED中解析高德地图天气api的json数据显示天气仪表盘
    查看>>
    Node-RED中通过node-red-ui-webcam节点实现访问摄像头并截取照片预览
    查看>>
    Node-RED安装图形化节点dashboard实现订阅mqtt主题并在仪表盘中显示温度
    查看>>
    Node-RED订阅MQTT主题并调试数据
    查看>>
    node-request模块
    查看>>
    Node.js 8 中的 util.promisify的详解
    查看>>
    node.js url模块
    查看>>
    Node.js Web 模块的各种用法和常见场景
    查看>>
    Node.js 函数是什么样的?
    查看>>
    Node.js 切近实战(七) 之Excel在线(文件&文件组)
    查看>>
    node.js 初体验
    查看>>
    Node.js 历史
    查看>>
    Node.js 在个推的微服务实践:基于容器的一站式命令行工具链
    查看>>
    Node.js 实现类似于.php,.jsp的服务器页面技术,自动路由
    查看>>
    Node.js 异步模式浅析
    查看>>
    node.js 怎么新建一个站点端口
    查看>>
    Node.js 文件系统的各种用法和常见场景
    查看>>