Facade Pattern 外观模式
提供一个统一的接口去访问多个子系统的多个不同的接口。外观模式定义了一个高层次的接口。
Facade外形类 知道哪写子系统负责处理哪些请求 将客户请求传递给相应的子系统处理 Subsystem 子系统类 实现子系统功能 处理由Facade传来的任务 子系统补用知道Facade,在任何地方页没有引用Facade demo 额 我觉得这个例子不好。。这个不是简单的封装么?= =到底简单的封装和facadepattern 什么区别 T_T public class Client {//这个就是所谓的客户端啦~~他要调用interface public static void main(String[] args){ FacadeCuppaMaker cuppaMaker = new FacadeCuppaMaker(); TeaCup teaCup = cuppaMaker.makeACuppa(); System.out.println(teaCup); } } public class FacadeCuppaMaker {//这是个复杂的子系统的对外接口 = = boolean teaBagIsSteeped; public FacadeCuppaMaker(){ System.out.println("FacadeCuppaMaker准备好为您冲茶了!"); } public TeaCup makeACuppa(){ TeaCup cup = new TeaCup(); TeaBag teaBag = new TeaBag(); Water water = new Water(); cup.addFacadeTeaBag(teaBag); water.boilFacadeWater(); cup.addFacadeWater(water); cup.steepTeaBag(); return cup; } } public class Water { boolean waterIsBoiling; public Water(){ setWaterIsBoiling(false); System.out.println("纯净的水准备好了"); } public void boilFacadeWater(){ setWaterIsBoiling(true); System.out.println("水在沸腾"); } public void setWaterIsBoiling(boolean isWaterBoiling){ waterIsBoiling = isWaterBoiling; } public boolean getWaterIsBoiling(){ return waterIsBoiling; } } public class TeaBag { public TeaBag(){ System.out.println("清香的茶包准备好了"); } } public class TeaCup { boolean teaBagIsSteeped; Water facadeWater; TeaBag facadeTeaBag; public TeaCup(){ setTeaBagIsSteeped(false); System.out.println("茶杯准备好了"); } public void setTeaBagIsSteeped(boolean isTeaBagSteeped){ teaBagIsSteeped = isTeaBagSteeped; } public boolean getTeaBagIsSteeped(){ return teaBagIsSteeped; } public void addFacadeTeaBag(TeaBag facadeTeaBagIn){ facadeTeaBag = facadeTeaBagIn; System.out.println("茶包放在茶杯了"); } public void addFacadeWater(Water facadeWaterIn){ facadeWater = facadeWaterIn; System.out.println("水倒入杯子了"); } public void steepTeaBag(){ if((facadeTeaBag!=null) && (facadeWater!=null) && (facadeWater.getWaterIsBoiling())){ System.out.println("茶渗透到杯子中"); setTeaBagIsSteeped(true); } else{ System.out.println("茶没有渗透到杯子中"); setTeaBagIsSteeped(false); } } public String toString(){ if(this.getTeaBagIsSteeped()){ return "一杯又香又浓的茶冲好了!"; } else{ String tempString = new String("一个杯子"); if(facadeWater != null){ if(facadeWater.getWaterIsBoiling()){ tempString += "放有沸腾的水 "; } else{ tempString += "放有凉水 "; } } else{ tempString += "没有放水 "; } if(facadeTeaBag != null){ tempString += "有一个茶包"; } else{ tempString += "没有茶包"; } return tempString; } } } |
===================================================
Facade Pattern
我找到的另外一个例子:
/* * Facade Pattern(外观模式) *将一组复杂的行为封装起来,通过简单接口来实现统一调用。 *例子:在家里看电影,可能需要一系列步骤:打开dvd,放盘,打开tv,打开音响,调好音调,关灯等诸如此类动作 *如果需要每一个都在客户代码来控制,将来修改或者维护比较麻烦,但是如果将这些统一通过facade模式封装到一个 *watchMovie接口中,客户知道我只需按watchMovie就会形成一系列动作,具体干什么可以不清楚。另外对于被封装的类 *具体使用并没受到影响,依然可以自己打开tv,打开hifi等。 * *在Head First中提到一个 *Least Knowledge Principle:talk only to your immediate friends; *这个原则是说:向直接的类保持联系,不必要的可以封装起来,通过一个封装类统一打交道; *其中head First指出几个guidelines * *only invoke methods that belong to: *1)The object itself; //方法属于本身 *2)objects passed in as a parameter to the method //方法是属于参数传入的对象 类似setParam(ClassA ca) ca.methods() *3)any object the method creates or instantiates //方法属于内部创建的实例。 *4)any components of the object //对象的组成成分 * * */ class TV{ public void on(){ System.out.println("TV On"); } public void off(){ System.out.println("TV Off"); } } class DvDplayer{ private String str; public void on(){ System.out.println("DvD On"); } public void off(){ System.out.println("DvD Off"); } public void setDvD(String DvDstr){ str = DvDstr; System.out.println("DvD setDvD " + str); } public void popDvD(){ System.out.println("DvD popDvD "+str); str = ""; } } class HiFi{ private int v; public void turnUp(){ v += 10; System.out.println("HiFi turnUp to "+10); } public void turnDown(){ v -= 10; System.out.println("HiFi turnDown to " + 10); } public void On(){ System.out.println("HiFi On"); } public void Off(){ System.out.println("HiFi Off"); } } class myFacade{ private HiFi hifi; private DvDplayer dvd; private TV tv; public myFacade(HiFi h,DvDplayer d,TV t){ hifi = h; dvd = d; tv = t; } public void watchMovie(String strDvD){ tv.on(); //一系列行为调用 hifi.On(); hifi.turnUp(); dvd.on(); dvd.setDvD(strDvD); } public void stopWatchMovie(){ hifi.Off(); dvd.popDvD(); dvd.off(); tv.off(); } } public class FacadeTest { /** * @param args */ public static void main(String[] args) { HiFi h = new HiFi(); TV t = new TV(); DvDplayer dvd = new DvDplayer(); myFacade mf = new myFacade(h,dvd,t); mf.watchMovie("War II"); //关心看电影,不关心电影通过怎样操作才可以看到 mf.stopWatchMovie(); } }
有些关于Decorator Pattern ,Facade Pattern,Adapter Pattern比较:
从它们的目的来比较:
Decorator Pattern:动态添加一些额外功能;
Facade Pattern:封装功能,使一系列复杂动作通过简单接口公开;
Adapter Pattern:转换接口功能,从一个接口转换到另一种接口形式;
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/binghuazh/archive/2009/02/06/3866465.aspx