美文网首页
外观设计模式

外观设计模式

作者: andpy | 来源:发表于2018-01-29 10:29 被阅读6次

外观模式

主要的目的在于让外部减少与子系统内部多个模块的交互,从而让外部能够更简单得使用子系统,他负责把客服端的请求转发给子系统内部的各个模块进行处理.

使用场景

  • 当你要为一个复杂的子系统提供一个简单的接口时。
  • 客户程序与抽象类的实现部分之间存在着很大的依赖性.
  • 当你需要构建一个层次接口的子系统时

案例代码:

//模块一
public class ModuleA {
    public void testA(){
        System.out.println("this is module A");
    }
}

//模块二
public class ModuleB {
    public void testB(){
        System.out.println("this is module B");
    }
}

//模块三
public class ModuleC {
    public void testC(){
        System.out.println("this is module C");
    }
}

外观类

public class Facade {
    private ModuleA moduleA = null;
    private ModuleB moduleB = null;
    private ModuleC moduleC = null;
    private static Facade mFacade = null;

    private Facade() {
        moduleA = new ModuleA();
        moduleB = new ModuleB();
        moduleC = new ModuleC();
    }

    public static Facade getInstance() {
        if (mFacade == null) {
            mFacade = new Facade();
        }
        return mFacade;
    }

    public void testOperation() {
        moduleA.testA();
        moduleB.testB();
        moduleC.testC();
    }
}

测试用例:

public static void main(String[] args) {
    Facade.getInstance().testOperation();
}

优点

  • Facade类封装了各个模块交互的过程,如果今后内部模块调用关系发生了变化,只需要修改Facade就实现就ok了.
  • Facade实现时可以被多个客户端调用的。‘

在Android中运用

contextImpl是android中的外观类

Facade01.png Facade02.png Facade03.png Facade04.png Facade05.png Facade06.png Facade07.png

相关文章

  • 11、结构型模式-外观设计模式

    1、大量使用第三方SDK-它们常用的外观设计模式你知道多少? ** 外观设计模式 Facade Pattern**...

  • 外观设计模式

    概念 外观模式的主要目的在于让外部减少与子系统内部多个模块的交互,从而让外部能够更简单地使用子系统,它负责把客户端...

  • 外观设计模式

    外观模式 主要的目的在于让外部减少与子系统内部多个模块的交互,从而让外部能够更简单得使用子系统,他负责把客服端的请...

  • 设计模式---外观设计模式

    外观模式 标签(空格分隔): 设计模式 在设计模式中有一个法则叫迪米特法则(最少知识原则),它说的是什么呢? 一个...

  • 外观设计模式(Facade)

    Facade 模式概述及作用 作用Facade(外观)模式为子系统中的各类(或结构与方法)提供一个简明一致的界面,...

  • 设计模式解析—外观设计模式

    外观模式(Facede Pattern)定义和使用场景 定义定义一个高层次、统一的接口,外部通过这个接口来操作内...

  • Java中的门面设计模式

    门面设计模式又叫外观设计模式,其核心思想正如其字面意思,向用户提供一个门户,用户只需要访问这个门户来获取他们想要的...

  • Java中的门面设计模式及如何用代码实现

    门面设计模式又叫外观设计模式,其核心思想正如其字面意思,向用户提供一个门户,用户只需要访问这个门户来获取他们想要的...

  • 外观设计专利保护的客体

    外观设计的定义 要素的结合 构成外观设计的是产品的外观设计要素的结合,其中包括形状、图案或者其结合以及色彩与形状、...

  • 设计模式-外观模式

    外观设计模式的定义 要求一个子系统的外部与其内部通信必须通过一个统一的对象进行,又称之为门面模式,提供一个高层次的...

网友评论

      本文标题:外观设计模式

      本文链接:https://www.haomeiwen.com/subject/wzxxzxtx.html