又名门面模式,是一种通过多个复杂的子系统提供一个一致的接口,而使这些子系统更加容易被访问的模式。
该模式对我有一个统一接口,外部应用程序不用关心内部子系统的具体的细节,这样会大大降低应用程序的复杂度,提高了程序的可维护性。

通过智能音响统一操作所有智能家居。
public class Light {
public void on() {
System.out.println("打开电灯");
}
public void off() {
System.out.println("关闭电灯");
}
}
public class TV {
public void on() {
System.out.println("打开电视机");
}
public void off() {
System.out.println("关闭电视机");
}
}
AISound:人工智能音响,可以控制所有智能家电。持有所有智能家电的引用,提供统一的外部访问,并适配每个家电的操作。
public class AISound {
private Light light;
private TV tv;
public AISound() {
light = new Light();
tv = new TV();
}
public void say(String msg) {
if (msg.contains("打开")) {
light.on();
tv.on();
} else if (msg.contains("关闭")) {
light.off();
tv.off();
}
}
}
public class Client {
public static void main(String[] args) {
AISound aiSound = new AISound();
aiSound.say("打开家电");
aiSound.say("关闭家电");
}
}