外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性
 
使用场景 
为复杂的模块或子系统提供外界访问的模块。 
子系统相对独立。 
预防低水平人员带来的风险。 
 
优点 
松散耦合,外观模式松散了客户端与子系统的耦合关系,让子系统内部的模块能更容易扩展和维护。
 
简单易用,外观模式让子系统更加易用,客户端不再需要了解子系统内部的实现,也不需要跟众多子系统内部的模块进行交互,只需要跟门面类交互就可以了。
 
更好的划分访问层次-通过合理使用 Facade,可以帮助我们更好地划分访问的层次。有些方法是对系统外的,有些方法是系统内部使用的。把需要暴露给外部的功能集中到门面中,这样既方便客户端使用,也很好地隐藏了内部的细节。
 
 
缺点 
不符合开闭原则,如果要改东西很麻烦,继承重写都不合适。
应用实例 
tomcat中,通过RequestFacade类,实现了HttpRequest(或HttpResponse)对象的安全访问,防止在servlet中向上转型获取到Requset(或Response)对象 
 
实现 
子系统类 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public  class  CPU      public  void  startup ()          System.out.println("cpu startup!" );     }     public  void  shutdown ()          System.out.println("cpu shutdown!" );     } } public  class  Disk      public  void  startup ()          System.out.println("disk startup!" );     }     public  void  shutdown ()          System.out.println("disk shutdown!" );     } } public  class  Memory      public  void  startup ()          System.out.println("memory startup!" );     }     public  void  shutdown ()          System.out.println("memory shutdown!" );     } } 
外观类 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public  class  Computer      private  CPU cpu;     private  Memory memory;     private  Disk disk;     public  Computer ()          cpu = new  CPU();         memory = new  Memory();         disk = new  Disk();     }     public  void  startup ()          System.out.println("start the computer!" );         cpu.startup();         memory.startup();         disk.startup();         System.out.println("start computer finished!" );     }     public  void  shutdown ()          System.out.println("begin to close the computer!" );         cpu.shutdown();         memory.shutdown();         disk.shutdown();         System.out.println("computer closed!" );     } } 
测试 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public  class  FacadeTest      @Test      public  void  testBootstrap ()  throws  Exception         Computer computer = new  Computer();         computer.startup();     }     @Test      public  void  testShutdown ()  throws   Exception         Computer computer = new  Computer();         computer.shutdown();     } }