• [译]Java 设计模式之外观


    (文章翻译自Java Design Pattern: Facade)

    外观设计模式隐藏了任务的复杂性而只是提供了一个简单的接口。一个非常好的例子就是计算机的启动。当一个计算机启动的时候,它涉及CUP、内存
    、硬件驱动等等的工作。为了更容易让用户去使用,我们我提供了一个封装任务的复杂性的外观方式,提供了一个简单的接口用来替代。

    1.外观模式类图

    facade-design-pattern1

    2.Java外观模式例子

    //the components of a computer
     
    class CPU {
        public void processData() { }
    }
     
    class Memory {
        public void load() { }
    }
     
    class HardDrive {
        public void readdata() { }
    }
     
    /* 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 run() {
            cpu.processData();
            memory.load();
            hardDrive.readdata();
        }
    }
     
     
    class User {
        public static void main(String[] args) {
            Computer computer = new Computer();
            computer.run();
        }
    }
    

    3.在实际项目中的例子

    在javax.faces.contex中,ExternalContext 内部使用了ServletContext、HttpSession、HttpServletRequest、HttpServletResponse等等。它允许 Faces AP不用知道它包含的应用程序环境的本质。

    这个例子是基于维基百科上面的外观设计模式,所以所有权是属于wiki的。

    引用:

    1.ExternalContext

    2.Wiki Facade

  • 相关阅读:
    函数表达式
    BOM
    让超出容器高度的内容滚动显示但不出现滚动条
    插件书写示例
    php中redis的安装
    日常工作bug总结
    pip freeze requirements.txt命令迁移模块
    Django18-中间件和cache实现限制用户访问频率
    Django17-文件上传下载
    Django16-cache缓存
  • 原文地址:https://www.cnblogs.com/zhangminghui/p/4214705.html
Copyright © 2020-2023  润新知