设计模式之外观模式

外观模式基本介绍

  1. 外观模式(Facade),也叫过程模式,外观模式为子系统中的一组接口提供
    一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加
    容易使用;
  2. 外观模式通过定义一个一致的接口,用以屏蔽内部子系统的细节,使得调用端
    只需跟这个接口发生调用,而无需关心这个子系统的内部细节

示例

影院例子

package com.lesson11.waiguan;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/2 19:21
 */
public class DVDPlay
{
    private static DVDPlay instance = new DVDPlay();

    private DVDPlay()
    {
    }

    public static DVDPlay getInstance(){
        return instance;
    }

    public void on(){
        System.out.println("on");
    }

    public void off(){
        System.out.println("off");
    }

    public void play(){
        System.out.println("play");
    }

    public void pause(){
        System.out.println("pause");
    }
}

package com.lesson11.waiguan;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/2 19:23
 */
public class BaoMiHua
{
    private BaoMiHua(){}

    private static BaoMiHua instance = new BaoMiHua();

    public static BaoMiHua getInstance(){
        return instance;
    }

    public void on(){
        System.out.println("爆米花机on");
    }

    public void off(){
        System.out.println("爆米花机off");
    }

    public void pop(){
        System.out.println("爆米花机pop");
    }
}

package com.lesson11.waiguan;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/2 19:29
 */
public class Light
{
    private Light(){}

    private static Light instance = new Light();

    public static Light getInstance(){
        return instance;
    }

    public void on(){
        System.out.println("灯光on");
    }

    public void off(){
        System.out.println("灯光off");
    }
}

package com.lesson11.waiguan;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/2 19:27
 */
public class Screen
{
    private Screen(){}

    private static Screen instance = new Screen();

    public static Screen getInstance(){
        return instance;
    }

    public void up(){
        System.out.println("屏幕up");
    }

    public void down(){
        System.out.println("屏幕down");
    }
}

package com.lesson11.waiguan;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/2 19:25
 */
public class TouYing
{
    private TouYing(){}

    private static TouYing instance = new TouYing();

    public static TouYing getInstance(){
        return instance;
    }

    public void on(){
        System.out.println("投影仪on");
    }

    public void off(){
        System.out.println("投影仪off");
    }

    public void focus(){
        System.out.println("投影仪focus");
    }
}

package com.lesson11.waiguan;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/2 19:28
 */
public class YinXiang
{
    private YinXiang(){}

    private static YinXiang instance = new YinXiang();

    public static YinXiang getInstance(){
        return instance;
    }

    public void on(){
        System.out.println("音响on");
    }

    public void off(){
        System.out.println("音响off");
    }
}

package com.lesson11.waiguan;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/2 19:33
 */
public class Home
{
    //定义子系统对象
    private BaoMiHua baoMiHua;
    private DVDPlay dvdPlay;
    private Light light;
    private Screen screen;
    private TouYing touYing;
    private YinXiang yinXiang;

    public Home()
    {
        this.baoMiHua = BaoMiHua.getInstance();
        this.dvdPlay = DVDPlay.getInstance();
        this.light = Light.getInstance();
        this.screen = Screen.getInstance();
        this.touYing = TouYing.getInstance();
        this.yinXiang = YinXiang.getInstance();
    }

    public void ready(){
        baoMiHua.on();
        baoMiHua.pop();
        screen.down();
        touYing.on();
        yinXiang.on();
        dvdPlay.on();
    }

    public void play(){
        dvdPlay.play();
    }

    public void pause(){
        dvdPlay.pause();
    }

    public void end(){
        baoMiHua.off();
        screen.up();
        touYing.off();
        yinXiang.off();
        dvdPlay.off();
    }
}

package com.lesson11.waiguan;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/2 19:30
 */
public class Client
{
    public static void main(String[] args)
    {
        Home home = new Home();
        home.ready();
        home.play();
    }
}

灵魂在于设计分层 ——沃兹基朔德

小结

  1. 外观模式对外屏蔽了子系统的细节,因此外观模式降低了客户端对子系统使用的复杂性;
  2. 外观模式对客户端与子系统的耦合关系,让子系统内部的模块更易维护和扩展;
  3. 通过合理的使用外观模式,可以帮我们更好的划分访问的层次;
  4. 当系统需要进行分层设计时,可以考虑使用Facade模式;
  5. 在维护一个遗留的大型系统时,可能这个系统已经变得非常难以维护和扩展,此时可以考虑为新系统开发一个Facade类,来提供遗留系统的比较清晰简单的接口,让新系统与Facade类交互,提高复用性;
  6. 不能过多的或者不合理的使用外观模式,使用外观模式好,还是直接调用模块好。要以让系统有层次,利于维护为目的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值