外观模式:提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用.
摇控器
测试类
最少知识原则
上面的代码耦合了过多的对象(Station,Thermometer)
public class Light{
private String name;
public Light(String name){
this.name = name;
}
public void on(){
System.out.println(name +" is on");
}
public void off(){
System.out.println(name +" is off");
}
}
public class TV {
private String name;
public TV(String name) {
this.name = name;
}
public void off() {
// TODO Auto-generated method stub
System.out.println(name + " is off");
}
public void on() {
// TODO Auto-generated method stub
System.out.println(name + " is on");
}
}
摇控器
public class Controler {
private TV tv;
private Light light;
public Controler(TV tv, Light light) {
this.tv = tv;
this.light = light;
}
//看电视
public void watchTv() {
tv.on();
light.off();
}
//关电视
public void closeTv() {
tv.off();
light.on();
}
}
测试类
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//利用外观模式
Controler control = new Controler(new TV("room tv"),new Light("room light"));
//用户根本不知道执行的细节
control.watchTv();
control.closeTv();
}
}
最少知识原则
pulbic float getTemp(){
return station.getThermometer().getTemperature();
}
上面的代码耦合了过多的对象(Station,Thermometer)
public float getTemp(){
//将Temperature对象放在Station,
return station.getTemperature();
}