标签:产生 业务 迪米特法则 cli cti 关系 很多 分析 程序
引入外观角色之后,用户只需要直接与外观角色交互,用户与子系统之间的复杂关系由外观角色来实现,从而降低了系统的耦合度。
1 public class Facade 2 { 3 private SubSystemA obj1 = new SubSystemA(); 4 private SubSystemB obj2 = new SubSystemB(); 5 private SubSystemC obj3 = new SubSystemC(); 6 public void method() 7 { 8 obj1.method(); 9 obj2.method(); 10 obj3.method(); 11 } 12 }
实例代码(JAVA):
1 //外观角色类 2 public class GeneralSwitchFacade { 3 private Light lights[]=new Light[4]; 4 private Fan fan; 5 private AirConditioner ac; 6 private Television tv; 7 8 public GeneralSwitchFacade() 9 { 10 lights[0]=new Light("左前"); 11 lights[1]=new Light("右前"); 12 lights[2]=new Light("左后"); 13 lights[3]=new Light("右后"); 14 fan=new Fan(); 15 ac=new AirConditioner(); 16 tv=new Television(); 17 } 18 19 public void on() 20 { 21 lights[0].on(); 22 lights[1].on(); 23 lights[2].on(); 24 lights[3].on(); 25 fan.on(); 26 ac.on(); 27 tv.on(); 28 } 29 30 public void off() 31 { 32 lights[0].off(); 33 lights[1].off(); 34 lights[2].off(); 35 lights[3].off(); 36 fan.off(); 37 ac.off(); 38 tv.off(); 39 } 40 } 41 42 //子系统角色类 43 public class Light 44 { 45 private String position; 46 47 public Light(String position) 48 { 49 this.position=position; 50 } 51 52 public void on() 53 { 54 System.out.println(this.position + "灯打开!"); 55 } 56 57 public void off() 58 { 59 System.out.println(this.position + "灯关闭!"); 60 } 61 } 62 63 //子系统角色 64 public class Fan 65 { 66 public void on() 67 { 68 System.out.println("风扇打开!"); 69 } 70 71 public void off() 72 { 73 System.out.println("风扇关闭!"); 74 } 75 } 76 77 //子系统角色 78 public class AirConditioner 79 { 80 public void on() 81 { 82 System.out.println("空调打开!"); 83 } 84 85 public void off() 86 { 87 System.out.println("空调关闭!"); 88 } 89 } 90 91 //子系统角色 92 public class Television 93 { 94 public void on() 95 { 96 System.out.println("电视机打开!"); 97 } 98 99 public void off() 100 { 101 System.out.println("电视机关闭!"); 102 } 103 } 104 105 //客户端 106 public class Client 107 { 108 public static void main(String args[]) 109 { 110 GeneralSwitchFacade gsf=new GeneralSwitchFacade(); 111 gsf.on(); 112 System.out.println("-----------------------"); 113 gsf.off(); 114 } 115 }
1 public class JDBCFacade { 2 private Connection conn=null; 3 private Statement statement=null; 4 public void open(String driver,String jdbcUrl,String userName,String userPwd) { 5 ...... 6 } 7 public int executeUpdate(String sql) { 8 ...... 9 } 10 public ResultSet executeQuery(String sql) { 11 ...... 12 } 13 public void close() { 14 ...... 15 } 16 }
(2) Session外观模式是外观模式在Java EE框架中的应用
标签:产生 业务 迪米特法则 cli cti 关系 很多 分析 程序
原文地址:https://www.cnblogs.com/WindSun/p/10263511.html