设计模式学习——外观模式



基本介绍

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

原理类图

在这里插入图片描述
说明(外观模式的角色分析):

  1. 外观类(Facade):为调用端提供统一的调用接口,外观类知道哪些子系统负责处理请求,从而将调用端的请求代理给适当的子系统对象。
  2. 调用者(Client):外观接口的调用者
  3. 子系统的集合:指的是模块或者子系统,处理Facade对象指派的任务,它是功能的实际提供者。

代码实例

DVDPlayer (子系统之一)

public class DVDPlayer {

    //使用单例模式,使用饿汉式
    private DVDPlayer() {
    }

    private static DVDPlayer dvdPlayer = new DVDPlayer();

    public static DVDPlayer getDvdPlayer() {
        return dvdPlayer;
    }

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

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

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

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

    //...
}

Popcorn (子系统)

public class Popcorn {

    private Popcorn() {}

    private static Popcorn popcorn = null;

    static {
        popcorn = new Popcorn();
    }

    public static Popcorn getPopcorn() {
        return popcorn;
    }

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

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

    public void pop(){
        System.out.println(" popcorn is pop ");
    }
}

Projector (子系统)

public class Projector {

    private Projector() {}

    private static Projector projector = null;

    static {
        projector = new Projector();
    }

    public static Projector getProjector() {
        return projector;
    }

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

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

    public void focus(){
        System.out.println(" Projector is focus ");
    } }

Screen (子系统)

public class Screen {

    private Screen() {}

    private static Screen screen = null;

    static {
        screen = new Screen();
    }

    public static Screen getScreen() {
        return screen;
    }

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

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

}

Stereo (子系统)

public class Stereo {

    private Stereo() {}

    private static Stereo stereo = null;

    static {
        stereo = new Stereo();
    }

    public static Stereo getStereo() {
        return stereo;
    }

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

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

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

}

TheaterLight (子系统)

public class TheaterLight {

    private TheaterLight() {}

    private static TheaterLight theaterLight = null;

    static {
        theaterLight = new TheaterLight();
    }

    public static TheaterLight getTheaterLight() {
        return theaterLight;
    }

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

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

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

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

HomeTheaterFacade (Facade,外观模式)

public class HomeTheaterFacade {

    //定义各个子系统对象
    private TheaterLight theaterLight;
    private Popcorn popcorn;
    private Stereo stereo;
    private Projector projector;
    private Screen screen;
    private DVDPlayer dvdPlayer;

    public HomeTheaterFacade() {
        this.theaterLight = TheaterLight.getTheaterLight();
        this.popcorn = Popcorn.getPopcorn();
        this.stereo = Stereo.getStereo();
        this.projector = Projector.getProjector();
        this.screen = Screen.getScreen();
        this.dvdPlayer = DVDPlayer.getDvdPlayer();
    }

    //操作分成4步
    public void ready() {
        popcorn.on();
        popcorn.pop();
        screen.down();
        projector.on();
        stereo.on();
        dvdPlayer.on();
        theaterLight.dim();
    }

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

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

    public void end() {
        popcorn.off();
        theaterLight.bright();
        screen.up();
        projector.off();
        stereo.off();
        dvdPlayer.off();
    }

}
 

测试用例

public static void main(String[] args) {
        HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
        homeTheaterFacade.ready();
        homeTheaterFacade.play();
        homeTheaterFacade.pause();
        homeTheaterFacade.end();
    }

外观模式在MyBatis框架的源码分析

MyBatis中的Configuration去创建MetaObject对象使用到外观模式

编写测试用例,引入mybatis的jar包(在pom文件中引入),点击Configuration

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

源码类图

在这里插入图片描述


外观模式的注意事项和细节

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值