zoukankan      html  css  js  c++  java
  • 设计模式10 外观模式

    外观模式(Facade)定义:是一种通过为多个复杂的子系统提供一个一致的接口,而使这些子系统更加容易被访问的模式。

    外观模式的优点有:

    1、降低了子系统与客户端之间的耦合度,使得子系统的变化不会影响调用它的客户类。

    2、对客户屏蔽了子系统组件,减少了客户处理的对象数目,并使得子系统使用起来更加容易。

    3、降低了大型软件系统中的编译依赖性,简化了系统在不同平台之间的移植过程,因为编译一个子系统不会影响其他的子系统,也不会影响外观对象。

    外观类与内部类:

     1 public class Facade {
     2     private SubSystemA subSystemA = new SubSystemA();
     3     private SubSystemB subSystemB = new SubSystemB();
     4     private SubSystemC subSystemC = new SubSystemC();
     5 
     6     public void method() {
     7         subSystemA.method1();
     8         subSystemB.method2();
     9         subSystemC.method3();
    10     }
    11 }
    12 
    13 public class SubSystemA {
    14     public void method1() {
    15         System.out.println("方法A调用");
    16     }
    17 }
    18 
    19 public class SubSystemB {
    20     public void method2() {
    21         System.out.println("方法B调用");
    22     }
    23 }
    24 
    25 public class SubSystemC {
    26     public void method3() {
    27         System.out.println("方法C调用");
    28     }
    29 }

    调用方式:

    1 public class Client {
    2     //Servelet 就是外观模式的使用
    3     public static void main(String[] args) {
    4         Facade facade = new Facade();
    5         facade.method();
    6     }
    7 }

    执行结果:

  • 相关阅读:
    Attributes in C#
    asp.net C# 时间格式大全
    UVA 10518 How Many Calls?
    UVA 10303 How Many Trees?
    UVA 991 Safe Salutations
    UVA 10862 Connect the Cable Wires
    UVA 10417 Gift Exchanging
    UVA 10229 Modular Fibonacci
    UVA 10079 Pizza Cutting
    UVA 10334 Ray Through Glasses
  • 原文地址:https://www.cnblogs.com/asenyang/p/12111047.html
Copyright © 2011-2022 走看看