标签:
外观模式:外观模式为子系统中的各类(或结构与方法)提供一个简明一致的界面,隐藏子系统的复杂性,使子系统更加容易使用。即当子系统复杂或者繁锁时,我们为子系统提供一个接口,其它程序或者对象就通过这个接口与此子系统联系。接口可以是类或方法等。这样就简化了子系统的使用。
简单例子:
1 public interface Camera 2 { 3 public void setPosition(); 4 public void setShutter(); 5 public void setAperture(); 6 public void takePhoto(); 7 } 8 9 public class SonyCamera implements Camera 10 { 11 @Override 12 public void setPosition() 13 { 14 System.out.println("set position..."); 15 } 16 17 @Override 18 public void setShutter() 19 { 20 System.out.println("set shutter..."); 21 } 22 23 @Override 24 public void setAperture() 25 { 26 System.out.println("set aperture..."); 27 } 28 29 @Override 30 public void takePhoto() 31 { 32 System.out.println("take photo..."); 33 } 34 } 35 36 public class Facade 37 { 38 private Camera camera; 39 40 public Facade(Camera camera) 41 { 42 this.camera = camera; 43 } 44 45 public void takePhoto() 46 { 47 camera.setPosition(); 48 camera.setShutter(); 49 camera.setAperture(); 50 camera.takePhoto(); 51 } 52 } 53 54 public class Test 55 { 56 public static void main(String[] args) 57 { 58 Facade facade = new Facade(new SonyCamera()); 59 facade.takePhoto(); 60 } 61 }
标签:
原文地址:http://www.cnblogs.com/huoxiayu/p/5622560.html