hand first 设计模式 -外观模式

外观模式:提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用.

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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值