博客
关于我
《研磨设计模式》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/

    你可能感兴趣的文章
    NIFI同步MySql数据_到SqlServer_错误_驱动程序无法通过使用安全套接字层(SSL)加密与SQL Server_Navicat连接SqlServer---大数据之Nifi工作笔记0047
    查看>>
    Nifi同步过程中报错create_time字段找不到_实际目标表和源表中没有这个字段---大数据之Nifi工作笔记0066
    查看>>
    NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
    查看>>
    NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
    查看>>
    NIFI数据库同步_多表_特定表同时同步_实际操作_MySqlToMysql_可推广到其他数据库_Postgresql_Hbase_SqlServer等----大数据之Nifi工作笔记0053
    查看>>
    NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南001---大数据之Nifi工作笔记0068
    查看>>
    NIFI集群_内存溢出_CPU占用100%修复_GC overhead limit exceeded_NIFI: out of memory error ---大数据之Nifi工作笔记0017
    查看>>
    NIFI集群_队列Queue中数据无法清空_清除队列数据报错_无法删除queue_解决_集群中机器交替重启删除---大数据之Nifi工作笔记0061
    查看>>
    NIH发布包含10600张CT图像数据库 为AI算法测试铺路
    查看>>
    Nim教程【十二】
    查看>>
    Nim游戏
    查看>>
    NIO ByteBuffer实现原理
    查看>>
    Nio ByteBuffer组件读写指针切换原理与常用方法
    查看>>
    NIO Selector实现原理
    查看>>
    nio 中channel和buffer的基本使用
    查看>>
    NIO基于UDP协议的网络编程
    查看>>
    NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
    查看>>
    Nitrux 3.8 发布!性能全面提升,带来非凡体验
    查看>>
    NI笔试——大数加法
    查看>>
    NLog 自定义字段 写入 oracle
    查看>>