• 设计模式-外观模式


    组建一个家庭影院:DVD播放器、投影仪、自动屏幕、环绕立体声、爆米花机

    要求完成使用家庭影院的功能,用遥控器统筹各设备开关,过程为:
    • 开爆米花机
    • 放下屏幕
    • 开投影仪
    • 开音响
    • 开DVD,选dvd
    • 去拿爆米花
    • 调暗灯光
    • 播放
    • 观影结束后,关闭各种设备


    1.传统方案

    在客户端中直接使用各个子系统(比如投影仪类、音响类等)中定义的方法完成整体流程,缺点如下:

    • 会造成调用过程混乱,没有清晰的过程
    • 不利于维护子系统的操作

    2.外观模式

    2.1 介绍

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

    2.2 原理

    在这里插入图片描述

    • 外观类(Facade):为调用端提供统一的调用接口, 外观类知道哪些子系统负责处理请求,从而将调用端的请求代理给适当子系统对象
    • 调用者(Client):外观接口的调用者
    • 子系统的集合:功能的实际提供者

    2.3 代码实现

    // 不同子系统有不同的功能,所以方法有所区别,但是结构类似
    // DVD播放器子系统
    public class DVDPlayer {
        // 使用单例模式中的饿汉式
        private static DVDPlayer instance = new DVDPlayer();
    
        public static DVDPlayer getInstanc() {
            return instance;
        }
    
        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 ..");
        }
    }
    // 投影仪子系统
    public class Projector {
        // 使用单例模式中的饿汉式
        private static Projector instance = new Projector();
    
        public static Projector getInstance() {
            return instance;
        }
    
        public void on() {
            System.out.println(" Projector on ");
        }
    
        public void off() {
            System.out.println(" Projector ff ");
        }
    
        public void focus() {
            System.out.println(" Projector is Projector  ");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    // 外观类(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.getInstance();
            this.popcorn = Popcorn.getInstance();
            this.stereo = Stereo.getInstance();
            this.projector = Projector.getInstance();
            this.screen = Screen.getInstance();
            this.dVDPlayer = DVDPlayer.getInstanc();
        }
    
        // 操作分成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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    // 调用者(Client)
    public class Client {
        public static void main(String[] args) {
            HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
            homeTheaterFacade.ready();
            homeTheaterFacade.play();
            homeTheaterFacade.end();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.注意事项和细节

    • 对外屏蔽了子系统的细节,降低了客户端对子系统使用的复杂性
    • 让子系统内部的模块更易维护和扩展
    • 当系统需要进行分层设计时,可以考虑使用外观模式

    参考

    https://www.bilibili.com/video/BV1G4411c7N4?p=81-85

  • 相关阅读:
    学习java第一百零七天
    C语言入门Day_17 循环的控制
    Cheetah-Software方案分析
    面试求职者
    git使用
    婴儿摇篮出口美国CPC认证要求
    SquareCTF-2023 Web Writeups
    2023年MySQL实战核心技术第一篇
    基于Python的邯郸地标美食导游平台设计与实现-计算机毕业设计源码+LW文档
    早、准、易、全、快,基于运维指标体系的智能事件管理
  • 原文地址:https://blog.csdn.net/qq_41398418/article/details/125901260