Facade把子系统的功能都包含进来了,这些子系统都有一个doSomething(),来给Client1和Client2调用的.
缺点是客户端是直接调用这些功能模块的.
//外观 //最大的一个好处是我们的老百姓MainClass不用再调用子系统,因为你调用子系统的话还需要判断要调用哪些子系统, // public class Facade { private SystemA systemA; private SystemB systemB; private SystemC systemC; public Facade() { //super(); systemA = new SystemA(); systemB = new SystemB(); systemC = new SystemC(); } //public void doSomething(){ public void doABC(){ this.systemA.doSomething(); this.systemB.doSomething(); this.systemC.doSomething(); } public void doAB(){ this.systemA.doSomething(); this.systemB.doSomething(); } }
//客户端... //缺点是客户端是直接调用这些功能模块的. //外观模式将客户端与具体的功能模块相区分,客户端所要知道的只有这个外观了.以后会接触门面模式,与外观模式是 //完全不同的概念. //客户端只调用外观的方法,至于外观的方法里面怎么将功能模块组合和调用我不管. public class MainClass { /* public static void main(String[] args) { //实现A子系统功能 SystemA systemA = new SystemA(); systemA.doSomething(); SystemB systemB = new SystemB(); systemB.doSomething(); SystemC systemC = new SystemC(); systemC.doSomething(); } */ public static void main(String[] args) { Facade facade = new Facade();//这样客户端不与子系统打交道了,只与外观Facade打交道了. //facade.doSomething(); facade.doABC(); } }
public class MainClass2 { public static void main(String[] args) { /* //实现A子系统功能 SystemA systemA = new SystemA(); systemA.doSomething(); SystemB systemB = new SystemB(); systemB.doSomething();*/ Facade facade = new Facade(); facade.doAB(); } }
/** * A子系统 * @author zhongzh * */ public class SystemA { public void doSomething(){ System.out.println("实现A子系统功能"); } }
/** * B子系统 * @author zhongzh * */ public class SystemB { /* * B子系统实现 * */ public void doSomething(){ System.out.println("实现B子系统功能"); } }
/** * C子系统 * @author zhongzh * */ public class SystemC { /* * C子系统实现功能 * */ public void doSomething(){ System.out.println("实现C子系统功能"); } }
package com.ibeifeng.news; public class GuoZai { public void mai(){ System.out.println("买国债"); } }
package com.ibeifeng.news; public class Gupiao { public void mai(){ System.out.println("买股票"); } }
package com.ibeifeng.news; //基金有很多种,有货币型的基金, //外观模式include创建一个引用. public class JiJin { private Gupiao gupiao; private GuoZai guozai; private Qihuo qihuo; public JiJin() { this.guozai = new GuoZai(); this.gupiao = new Gupiao(); this.qihuo = new Qihuo(); } public void maiJijinA(){ this.guozai.mai(); this.gupiao.mai(); } public void maiJijinB(){ this.guozai.mai(); this.gupiao.mai(); this.qihuo.chao(); } }
package com.ibeifeng.news; public class MainClass { public static void main(String[] args) { /* //80年代,基金出现之前 Gupiao gupiao = new Gupiao(); gupiao.mai(); Qihuo qihuo = new Qihuo(); qihuo.chao(); GuoZai guozhai = new GuoZai(); guozhai.mai();*/ //有了基金之后 /*JiJin jijin = new JiJin(); jijin.maiJijinA();*/ JiJin jijin = new JiJin();//另外一个老百姓... jijin.maiJijinB(); } }
package com.ibeifeng.news; public class Qihuo { public void chao(){ System.out.println("买期货"); } }