鱼C论坛

 找回密码
 立即注册
查看: 2329|回复: 0

[学习笔记] 外观模式

[复制链接]
发表于 2019-7-19 10:12:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
影院管理项目
    组建一个家庭影院:
        DVD播放器、投影仪、自动屏幕、环绕立体声、爆米花机、要求完成使用家庭影院的功能,其过程为:
        直接用遥控器:统筹各设备开关
        开爆米花机,放下屏幕,开投影仪,开音响,开DVD,选dvd,去拿爆米花,调暗灯光,播放,观影结束后,关闭各个设备
传统方法解决影院管理

  1. ClientTest{
  2.     public static void main(){
  3.         //1.创建相关的对象
  4.         //2.调用创建的各个对象的一系列方法
  5.         //3.调用DVDPlayer 对象的play方法
  6.     }
  7. }
复制代码


传统方式解决影院管理问题分析
    1.在ClientTest的main方法中,创建各个子系统的对象,并直接去调用子系统(对象)相关方法,会造成调用过程坤乱,没有清晰的过程
    2.不利于在ClientTest中,去维护对子系统的操作
    3.解决思路:定义一个高层接口,给子系统中的一组接口提供一个一致的界面(比如在高层接口提供四个方法ready
    ,play,pause,end),用来访问子系统中的一群接口
    4.也就是说,就是通过定义一个一致的接口(界面类),用以屏蔽内部子系统的细节,使得调用段只需跟这个接口发生调用,而不需关系这个
    子系统的内部细节 =>外观模式

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

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

传统方式解决影院管理说明
    1.外观模式可以理解为转换一群接口,客户只要调用一个接口,而不用调用多个接口才能达到目的。比如
    :在pc上安装软件的时候,经常有一键安装选项(省去选择安装目录、安装的组件等等),还有就是手机
    的重启功能(把关机和启动合为一个操作)
    2.外观模式就是解决多个复杂接口带来的使用困难,起到简化用户操作的作用
    3.示意图说明

外观模式应用实例
    1.应用实例要求
    使用外观模式来完成家庭影院项目
    2.思路分析和图解
    3.代码实现


  1. public class DVDPlayer{
  2.    
  3.     //使用单例模式
  4.     private static DVDPlayer instance = new DVDPlayer();
  5.     public static DVDPlayer getInstance(){return instance;}

  6.     public void on(){System.out.println("dvd on");}
  7.     public void off(){System.out.println("dvd off");}
  8.     public void play(){System.out.println("dvd is playing);}
  9.     public void pause(){System.out.println("dvd is pause");}
  10. }

  11. public class Popcorn{
  12.     private static Popcorn instance = new Popcorn();

  13.     public static Popcorn getInstance(){return this.instance;}

  14.     public void on(){System.out.println("popcorn on");}
  15.     public void off(){System.out.println("popcorn off");}
  16.     public void pop(){System.out.println("popcorn is poping");}
  17. }


  18. public class Projector{
  19.     private static Projector instance = new Projector();

  20.     public static Projector getInstance(){return this.instance;}

  21.     public void on(){System.out.println("Projector on");}
  22.     public void off(){System.out.println("Projector off");}
  23.     public void focuse(){System.out.println("Projector is focuse");}
  24. }

  25. public class Screen {
  26.     private static Screen instance = new Screen();
  27.     public static Screen getInstance(){return this.instance;}

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

  29.     public void down(){System.out.println("screen down");}
  30. }

  31. public class Stereo{
  32.     private static Stereo instance = new Stereo();
  33.     public static Stereo getInstance(){return this.instance;}

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

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

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

  38. public class TheaterLight{
  39.     private static TheaterLight instance = new TheaterLight();
  40.     public static TheaterLight getInstance(){return this.instance;}

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

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

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

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

  46. public class HomeTheaterFacade{

  47.     //定义各个子系统的对象
  48.     private TheaterLight theaterLight;
  49.     private Popcorn popcorn;
  50.     private Stereo stereo;
  51.     private Projector projector;
  52.     private Screen screen;
  53.     private DVDPlayer dvDPlayer;

  54.     public HomeTheaterFacade(){
  55.         super();
  56.         this.theaterLight = TheaterLight.getInstance();
  57.         this.popcorn = Popcorn.getInstance();
  58.         this.stereo = Stereo.getInstance();
  59.         this.projector = Projector.getInstance();
  60.         this.screen = Screen.getInstance();
  61.         this.dvDPlayer = DVDPlayer.getInstance();
  62.     }

  63.         //操作分成四步
  64.     public void ready(){
  65.         popcorn.on();
  66.         popcorn.pop();
  67.         screen.down();
  68.         projector.on();
  69.         stereo.on();
  70.         dvDPlayer.on();
  71.         theaterLight.dim();
  72.     }

  73.     public void play(){
  74.         dvDPlayer.play();
  75.     }

  76.     public void pause(){
  77.         dvDPlayer.pause();
  78.     }

  79.     public void end(){
  80.         popcorn.off();
  81.         theaterLight.bright();
  82.         screen.up();
  83.         projector.off();
  84.         stereo.off();
  85.         dvDPlayer.off();
  86.     }

  87.     public static void main(String[]args){

  88.     }
  89. }


  90. public class Client{
  91.     public static void main(String[]args){
  92.         HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
  93.         homeTheaterFacade.ready();
  94.         homeTheaterFacade.play();
  95.         homeTheaterFacade.pause();
  96.         homeTheaterFacade.end();
  97.     }
  98. }
复制代码


外观模式在MyBatis框架应用的源码分析
    1.MyBatis中的Configuration去创建MetaObject 对象使用到外观模式
    2.代码分析+debug源码+示意图

外观模式的注意事项和细节
    1.外观模式对外屏蔽了子系统的细节,因此外观模式降低了客户端对子系统使用的复杂性
    2.外观模式对客户端与子系统的耦合关系,让子系统内部的模块更易维护和扩展
    3.通过合理的使用外观模式,可以帮我们更好的划分访问的层次
    4.当系统需要进行分层设计时,可以考虑使用facade模式
    5.在维护一个遗留的大型系统时,可能这个系统已经变得非常难以维护和扩展,此时可以考虑为新
    系统开发一个facade类,来提供遗留系统的比较清晰简单的接口,让新系统与facade类交互,提高复用性
    6.不能过多的或者不合理的使用外观模式,使用外观模式好,还是直接调用模块好,要以让系统有层次,利于
    维护为目的。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 19:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表