浏览 1495 次
锁定老帖子 主题:hand first 设计模式 -外观模式
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-06-29
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(); } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |