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

    你可能感兴趣的文章
    Nim游戏
    查看>>
    NIO ByteBuffer实现原理
    查看>>
    Nio ByteBuffer组件读写指针切换原理与常用方法
    查看>>
    NIO Selector实现原理
    查看>>
    nio 中channel和buffer的基本使用
    查看>>
    NIO基于UDP协议的网络编程
    查看>>
    NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
    查看>>
    Nitrux 3.8 发布!性能全面提升,带来非凡体验
    查看>>
    NI笔试——大数加法
    查看>>
    NLog 自定义字段 写入 oracle
    查看>>
    NLog类库使用探索——详解配置
    查看>>
    NLP 基于kashgari和BERT实现中文命名实体识别(NER)
    查看>>
    NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
    查看>>
    NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
    查看>>
    NLP学习笔记:使用 Python 进行NLTK
    查看>>
    NLP的神经网络训练的新模式
    查看>>
    NLP采用Bert进行简单文本情感分类
    查看>>
    NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
    查看>>
    NLP:使用 SciKit Learn 的文本矢量化方法
    查看>>
    Nmap扫描教程之Nmap基础知识
    查看>>