外观设计模式解读

简介: 外观设计模式解读

80680a4669ea49d7a698ea6a0178e5f3.png

s统的内部细节 => 外观模式

外观模式基本介绍

概念

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

外观模式原理类图

分类外观模式的角色

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

外观模式解决影院管理

传统方式解决影院管理说明

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

外观模式应用实例

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

DVDPlayer

1. public class DVDPlayer {
2. 
3. //使用单例模式, 使用饿汉式
4. private static DVDPlayer instance = new DVDPlayer();
5. 
6. public static DVDPlayer getInstanc() {
7. return instance;
8.     }
9. 
10. public void on() {
11.         System.out.println(" dvd on ");
12.     }
13. 
14. public void off() {
15.         System.out.println(" dvd off ");
16.     }
17. 
18. public void play() {
19.         System.out.println(" dvd is playing ");
20.     }
21. 
22. public void pause() {
23.         System.out.println(" dvd pause ..");
24.     }
25. }

Popcorn

1. public class Popcorn {
2. private static Popcorn instance = new Popcorn();
3. public static Popcorn getInstance() {
4. return instance;
5.     }
6. public void on() {
7.         System.out.println(" popcorn on ");
8.     }
9. public void off() {
10.         System.out.println(" popcorn ff ");
11.     }
12. public void pop() {
13.         System.out.println(" popcorn is poping ");
14.     }
15. 
16. }

Projector

1. public class Projector {
2. private static Projector instance = new Projector();
3. public static Projector getInstance() {
4. return instance;
5.     }
6. public void on() {
7.         System.out.println(" Projector on ");
8.     }
9. public void off() {
10.         System.out.println(" Projector ff ");
11.     }
12. public void focus() {
13.         System.out.println(" Projector is Projector ");
14.     }
15. 
16. }

Stereo

1. public class Stereo {
2. private static Stereo instance = new Stereo();
3. public static Stereo getInstance() {
4. return instance;
5.     }
6. public void on() {
7.         System.out.println(" Stereo on ");
8.     }
9. public void off() {
10.         System.out.println(" Screen off ");
11.     }
12. public void up(){
13.         System.out.println(" Screen up.. ");
14.     }
15. }

TheaterLight

1. public class TheaterLight {
2. private static TheaterLight instance = new TheaterLight();
3. public static TheaterLight getInstance() {
4. return instance;
5.     }
6. public void on() {
7.         System.out.println(" TheaterLight on ");
8.     }
9. public void off() {
10.         System.out.println(" TheaterLight off ");
11.     }
12. public void dim() {
13.         System.out.println(" TheaterLight dim.. ");
14.     }
15. public void bright() {
16.         System.out.println(" TheaterLight bright.. ");
17.     }
18. 
19. }

Screen

1. public class Screen {
2. private static Screen instance = new Screen();
3. public static Screen getInstance() {
4. return instance;
5.     }
6. public void up() {
7.         System.out.println(" Screen up ");
8.     }
9. public void down() {
10.         System.out.println(" Screen down ");
11.     }
12. }

HomeTheaterFacade

1. public class HomeTheaterFacade {
2. //定义各个子系统对象
3. private TheaterLight theaterLight;
4. private Popcorn popcorn;
5. private Stereo stereo;
6. private Projector projector;
7. private Screen screen;
8. private DVDPlayer dVDPlayer;
9. //构造器
10. public HomeTheaterFacade() {
11. super();
12. this.theaterLight = TheaterLight.getInstance();
13. this.popcorn = Popcorn.getInstance();
14. this.stereo = Stereo.getInstance();
15. this.projector = Projector.getInstance();
16. this.screen = Screen.getInstance();
17. this.dVDPlayer = DVDPlayer.getInstanc();
18.     }
19. //操作分成 4 步
20. public void ready() {
21.         popcorn.on();
22.         popcorn.pop();
23.         screen.down();
24.         projector.on();
25.         stereo.on();
26.         dVDPlayer.on();
27.         theaterLight.dim();
28.     }
29. public void play() {
30.         dVDPlayer.play();
31.     }
32. public void pause() {
33.         dVDPlayer.pause();
34.     }
35. public void end() {
36.         popcorn.off();
37.         theaterLight.bright();
38.         screen.up();
39.         projector.off();
40.         stereo.off();
41.         dVDPlayer.off();
42.     }
43. }

Client

1. public class Client {
2. public static void main(String[] args) {
3. 
4. HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
5.         homeTheaterFacade.ready();
6.         homeTheaterFacade.play();
7.         homeTheaterFacade.end();
8.     }
9. }

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

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



相关文章
|
9月前
|
设计模式
装饰者设计模式(一)
装饰者设计模式(一)
|
9月前
|
设计模式
装饰者设计模式(二)番外篇 装饰者设计模式和静态代理设计模式区别
装饰者设计模式(二)番外篇 装饰者设计模式和静态代理设计模式区别
|
设计模式 应用服务中间件 uml
解锁设计模式的神秘面纱:编写无懈可击的代码之外观设计模式
解锁设计模式的神秘面纱:编写无懈可击的代码之外观设计模式
65 1
|
设计模式 缓存 领域建模
23种设计模式漫画版系列—外观设计模式
23种设计模式漫画版系列—外观设计模式
190 0
|
存储 设计模式 uml
漫画:设计模式中的 “观察者模式”
场景1:游戏操作界面 场景2:游戏迷宫
220 0
漫画:设计模式中的 “观察者模式”
|
设计模式 前端开发 Java
常用的面向过程风格的代码|设计模式基础(二)
在Java开发中,我们实际上会利用Java这种面向对象语言,在无意中写出很多面向过程风格的代码。
|
设计模式
装饰者设计模式
装饰者设计模式
58 0
|
设计模式 中间件
【设计模式】外观
【设计模式】外观
126 0
【设计模式】外观
|
设计模式
漫画:设计模式之 “外观模式”
这里所谓的“套餐”,就是底层细粒度接口的不同组合。在保留底层接口不变的前提下,中间层为调用方提供了便利。 这正是外观模式(Facade Pattern)的设计思想: To make a complex subsystem easier to use, a simple interface should be provided for a set of interfaces in the subsystem. 为了使复杂的子系统更容易被使用,应当为子系统的众多接口提供一个简洁的高层接口。
155 0
漫画:设计模式之 “外观模式”
|
设计模式 自动驾驶 Java
漫画设计模式:什么是 “装饰器模式” ?
装饰器模式都包含哪些核心角色呢? 1. Component接口 2. ConcreteComponent类 3. Decorator抽象类 4. ConcreteDecorator类
177 0
漫画设计模式:什么是 “装饰器模式” ?