外观模式

什么是外观模式
Facade模式也叫外观模式,是由GoF提出的23种设计模式中的一种。Facade模式为一组具有类似功能的类群,比如类库,子系统等等,提供一个一致的简单的界面。这个一致的简单的界面被称作facade。

二、外观模式的结构

 

三、外观模式的角色和职责
Facade
为调用方定义简单的调用接口。
Clients
调用者。通过Facade接口调用提供某功能的内部类群。
Packages
功能提供者。指提供功能的类群(模块或子系统)。

SystemA.java
 
/*
 * A子系统
 */
public class SystemA {
    /*
     * A子系统实现功能
     */
    public void doSomething() {
        System.out.println("实现A子系统功能");
    }
}/*<br/> * A子系统<br/> */<br/>public class SystemA {<br/>    /*<br/>     * A子系统实现功能<br/>     */<br/>    public void doSomething() {<br/>        System.out.println("实现A子系统功能");<br/>    }<br/>}

SystemB.java
/*
 * B子系统
 */
public class SystemB {
 
    /*
     * B子系统实现功能
     */
    public void doSomething() {
        System.out.println("实现B子系统功能");
    }
}/*<br/> * B子系统<br/> */<br/>public class SystemB {<br/><br/>    /*<br/>     * B子系统实现功能<br/>     */<br/>    public void doSomething() {<br/>        System.out.println("实现B子系统功能");<br/>    }<br/>}

SystemC.java
/*
 * C子系统
 */
public class SystemC {
 
    /*
     * C子系统实现功能
     */
    public void doSomething() {
        System.out.println("实现C子系统功能");
    }
}/*<br/> * C子系统<br/> */<br/>public class SystemC {<br/><br/>    /*<br/>     * C子系统实现功能<br/>     */<br/>    public void doSomething() {<br/>        System.out.println("实现C子系统功能");<br/>    }<br/>}

Facade.java
public class Facade {
    private SystemA systemA;
    private SystemB systemB;
    private SystemC systemC;
 
    public Facade() {
        systemA = new SystemA();
        systemB = new SystemB();
        systemC = new SystemC();
    }
 
    public void doABC() {
        this.systemA.doSomething();
        this.systemB.doSomething();
        this.systemC.doSomething();
    }
 
    public void doAB() {
        this.systemA.doSomething();
        this.systemB.doSomething();
    }
}public class Facade {<br/>    private SystemA systemA;<br/>    private SystemB systemB;<br/>    private SystemC systemC;<br/><br/>    public Facade() {<br/>        systemA = new SystemA();<br/>        systemB = new SystemB();<br/>        systemC = new SystemC();<br/>    }<br/><br/>    public void doABC() {<br/>        this.systemA.doSomething();<br/>        this.systemB.doSomething();<br/>        this.systemC.doSomething();<br/>    }<br/><br/>    public void doAB() {<br/>        this.systemA.doSomething();<br/>        this.systemB.doSomething();<br/>    }<br/>}

MainClass.java
 
public class MainClass {
    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.doABC();
    }
}public class MainClass {<br/>    public static void main(String[] args) {<br/>        Facade facade = new Facade();<br/>        facade.doABC();<br/>    }<br/>}

MainClass2.java
public class MainClass2 {
    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.doAB();
    }
}public class MainClass2 {<br/>    public static void main(String[] args) {<br/>        Facade facade = new Facade();<br/>        facade.doAB();<br/>    }<br/>}

下面是一个关于基金(股票,国债,期货)的例子

Gupiao.java
package com.ibeifeng.news;
 
public class Gupiao {
 
    public void mai() {
        System.out.println("买股票");
    }
}package com.ibeifeng.news;<br/><br/>public class Gupiao {<br/><br/>    public void mai() {<br/>        System.out.println("买股票");<br/>    }<br/>}

GuoZai.java
package com.ibeifeng.news;
 
public class GuoZai {
 
    public void mai() {
        System.out.println("买国债");
    }
}package com.ibeifeng.news;<br/><br/>public class GuoZai {<br/><br/>    public void mai() {<br/>        System.out.println("买国债");<br/>    }<br/>}

Qihuo.java
package com.ibeifeng.news;
 
public class Qihuo {
 
    public void chao() {
        System.out.println("买期货");
    }
}package com.ibeifeng.news;<br/><br/>public class Qihuo {<br/><br/>    public void chao() {<br/>        System.out.println("买期货");<br/>    }<br/>}

JiJin.java
package com.ibeifeng.news;
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;<br/>public class JiJin {<br/>    private Gupiao gupiao;<br/>    private GuoZai guozai;<br/>    private Qihuo qihuo;<br/><br/>    public JiJin() {<br/>        this.guozai = new GuoZai();<br/>        this.gupiao = new Gupiao();<br/>        this.qihuo = new Qihuo();<br/>    }<br/><br/>    public void maiJijinA() {<br/>        this.guozai.mai();<br/>        this.gupiao.mai();<br/>    }<br/><br/>    public void maiJijinB() {<br/>        this.guozai.mai();<br/>        this.gupiao.mai();<br/>        this.qihuo.chao();<br/>    }<br/>}

MainClass.java
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();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值