1.意图
为子系统提供一个一致的界面,facade模式定义了一个高层接口,这个接口使得
这一系统更加容易使用
2.参与者
Facade - 知道哪些子系统类负责处理请求
- 将客户请求代理给适当的子系统对象
subsystem classess -实现子系统的功能
-处理有Facade对象指派的任务
-没有Facade的相关信息
3.结构
4.代码:
public class CarTemperature {
private int temperature;//车内温度
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
}
public class System1 {
private int temperature;
//检测温度
public void checkTemperature(CarTemperature carTemperature) {
this.temperature = carTemperature.getTemperature();
}
public int getTemperature() {
return temperature;
}
}
public class System3 {
private int temperature;
//温度控制系统
public boolean play(int temperature) {
if(this.temperature<temperature){
System.out.println("发送温度超出舒适温度的警报");
return true;
}
System.out.println("发送温度低于舒适温度的警报");
return false;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
}
public class System2 {
int state = 0;//
//空调控制系统
public void play(boolean b) {
if(b){
System.out.println("打开空调开关开始降温");
state = 1;
}else{
System.out.println("关闭空调开关");
state = 0;
}
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
public class System4 {
//温度变化趋势
public void declineTemperature(CarTemperature c){
c.setTemperature(c.getTemperature()-1);
System.out.println("温度上升一度,当前温度为"+c.getTemperature());
}
public void riseTemperature(CarTemperature c){
c.setTemperature(c.getTemperature()+1);
System.out.println("温度下降一度,当前温度为"+c.getTemperature());
}
}
public class Client {
public static void main(String[] args) {
CarTemperature c =new CarTemperature();
c.setTemperature(21);//车内温度50
Facade f =Facade.instance();
f.carSystem(c, 20);//设置合适温度20
}
}