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

    你可能感兴趣的文章
    Netty工作笔记0011---Channel应用案例2
    查看>>
    Netty工作笔记0014---Buffer类型化和只读
    查看>>
    Netty工作笔记0050---Netty核心模块1
    查看>>
    Netty工作笔记0084---通过自定义协议解决粘包拆包问题2
    查看>>
    Netty常见组件二
    查看>>
    netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
    查看>>
    Netty核心模块组件
    查看>>
    Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
    查看>>
    Netty源码—2.Reactor线程模型一
    查看>>
    Netty源码—4.客户端接入流程一
    查看>>
    Netty源码—4.客户端接入流程二
    查看>>
    Netty源码—5.Pipeline和Handler一
    查看>>
    Netty源码—6.ByteBuf原理二
    查看>>
    Netty源码—7.ByteBuf原理三
    查看>>
    Netty源码—7.ByteBuf原理四
    查看>>
    Netty源码—8.编解码原理二
    查看>>
    Netty源码解读
    查看>>
    Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
    查看>>
    Netty相关
    查看>>
    Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
    查看>>