相关文章
【合集】Java设计模式
总结
“迪米特法则”的典型应用
外观模式
子系统角色
1
2
3
4
5
6
7
8
|
public static class Light {
public void on() {
System.out.println("打开了灯....");
}
public void off() {
System.out.println("关闭了灯....");
}
}
|
1
2
3
4
5
6
7
8
|
public static class TV {
public void on() {
System.out.println("打开了电视....");
}
public void off() {
System.out.println("关闭了电视....");
}
}
|
1
2
3
4
5
6
7
8
|
public static class AirCondition {
public void on() {
System.out.println("打开了空调....");
}
public void off() {
System.out.println("关闭了空调....");
}
}
|
外观类
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
|
@Builder
public static class SmartAppliancesFacade {
private Light light;
private TV tv;
private AirCondition airCondition;
public void say(String message) {
if (message.contains("打开")) {
on();
}
if (message.contains("关闭")) {
off();
}
System.out.println("我还听不懂你说的!!!");
}
private void on() {
System.out.println("起床了");
light.on();
tv.on();
airCondition.on();
}
private void off() {
System.out.println("睡觉了");
light.off();
tv.off();
airCondition.off();
}
}
|
客户端调用
1
2
3
4
5
6
7
|
SmartAppliancesFacade facade = SmartAppliancesFacade.builder()
.light(new Light())
.tv(new TV())
.airCondition(new AirCondition())
.build();
facade.say("打开家电");
facade.say("关闭家电");
|
应用
{@link org.apache.catalina.connector.RequestFacade}组合了Request对象,都是调用的request对象的私有方法,既用了 Request ,又能防止其中方法被不合理的访问。
- 注意Request和RequestFacade同样实现了HttpServletRequest。