• facade模式


    好吧,又见到这个模式了。。

    在项目中,这个模式被用来构建整个system,作为对外交互的接口,这是facade的擅长的。

    A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can:

    • make a software library easier to use, understand and test, since the facade has convenient methods for common tasks;
    • make code that uses the library more readable, for the same reason;
    • reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;
    • wrap a poorly-designed collection of APIs with a single well-designed API (as per task needs).

    An Adapter is used when the wrapper must respect a particular interface and must support a polymorphic behavior. On the other hand, a facade is used when one wants an easier or simpler interface to work with.

    上图,from wiki:

    FacadeDesignPattern

    上代码:

    * Complex parts */
     
    class CPU {
     
        public void freeze() { ... }
        public void jump(long position) { ... }
        public void execute() { ... }
     
    }
     
    class Memory {
     
        public void load(long position, byte[] data) { ... }
     
    }
     
    class HardDrive {
     
        public byte[] read(long lba, int size) { ... }
     
    }
     
    /* Facade */
     
    class Computer {
     
        private CPU cpu;
        private Memory memory;
        private HardDrive hardDrive;
     
        public Computer() {
            this.cpu = new CPU();
            this.memory = new Memory();
            this.hardDrive = new HardDrive();
        }
     
        public void startComputer() {
            cpu.freeze();
            memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));
            cpu.jump(BOOT_ADDRESS);
            cpu.execute();
        }
     
    }
     
    /* Client */
     
    class You {
     
        public static void main(String[] args) {
            Computer facade = new Computer();
    	facade.startComputer();
        }
     

    比较简单,就不解释了

  • 相关阅读:
    机器学习中 margin loss 、hinge loss 和 ramp loss 的区别
    ML 论文中 用到的 temperature
    对一系列 pickle load 进行解包,只保留最后几个
    Linux 常用命令速览
    Numpy 的 dtype 和 astype 的区别
    远程配置 tensorflow 环境
    pytorch 的一些坑
    Conda 配置虚拟 pytorch 环境 和 Tensorflow 环境
    Ubuntu 远程离线配置 pytorch 运行环境
    3.Vue起步
  • 原文地址:https://www.cnblogs.com/justin_s/p/1905084.html
Copyright © 2020-2023  润新知