外观模式
package com.life.design.facade; public class SmartAppliancesFacade { private Light light; private TV tv; private AirCondition airCondition; public SmartAppliancesFacade() { light = new Light(); tv = new TV(); airCondition = new AirCondition(); } public void say(String message) { if (message.contains("打开")) { on(); } else if (message.contains("关闭")) { off(); } else { System.out.println("我还听不懂你说的"); } } public void on() { light.on(); tv.on(); airCondition.on(); } public void off() { light.off(); tv.off(); airCondition.off(); } }
package com.life.design.facade; public class Light { public void on() { System.out.println("打开电灯"); } public void off() { System.out.println("关闭电灯"); } }
package com.life.design.facade; public class Client { public static void main(String[] args) { SmartAppliancesFacade facade = new SmartAppliancesFacade(); facade.say("打开"); facade.say("关闭"); } }