外观设计模式

一、外观设计模式

1、介绍

  • 外观模式(Facade),也叫“过程模式”。
  • 外观模式定义了一个高层接口,用以屏蔽内部子系统的细节,使得客户端只需跟这个接口发生调用,而无需关心这个子系统的内部细节。简化了客户端操作。

2、UML原理类图

外观类(Facade):为客户端提供统一的调用接口,外观类知道哪些子系统负责处理请求,从而将客户端的请求代理给适当子系统对象

客户端(调用者):外观接口的调用者

子系统集合:指模块或者子系统,处理Facade对象指派的任务,功能的实际提供者

3、外观模式的使用

(1)外观模式对外屏蔽了子系统的细节,因此外观模式降低了客户端对子系统使用的复杂性

(2)外观模式是对客户端与子系统之间的耦合关系达到解耦的目的,让子系统内部的模块更容易维护和扩展

(3)通过合理的使用外观模式,可以帮助我们更好的划分访问的层次

(4)当系统需要进行分成设计时,可以考虑使用Facade模式

(5)在维护一个遗留的大型系统时,可能这个系统已经变得非常难以维护和扩展,此时可以考虑为新系统开发一个Facade类,来提供遗留系统中比较清晰简单的接口,让新系统与Facade类交互,提高复用性

(6)不能过多或者不合理的使用外观模式。无论是使用外观模式,还是直接调用模块,要让系统有层次,利于维护为主要目的。

二、普通方式编程

1、介绍

(1)需求

智能家居建设,早上起床前30分钟,定时器执行命令,控制家中的智能家居执行以下步骤:
开启热水器
开启空调
开启咖啡机
30分钟后(起床)
开启灯光
播放新闻
放洗澡水-拿咖啡-洗澡
出门关闭各种设备

(2)弊端

  • 在Client的main方法中,创建了各个子系统(智能家居)的对象,并直接调用子系统(对象)相关方法,对于Client来说,这样会造成调用过程混乱,没有清晰的过程
  • 不利于在Client中,去维护对子系统(智能家居)的操作

2、UML

3、代码

  1. /**
  2. * @description: 热水器
  3. * @author: dashu
  4. * @create: 14:38
  5. */
  6. public class WaterHeater {
  7. public void on(){
  8. System.out.println("打开热水器");
  9. }
  10. public void off(){
  11. System.out.println("关闭热水器");
  12. }
  13. }
  1. /**
  2. * @description: 空调
  3. * @author: dashu
  4. * @create: 14:39
  5. */
  6. public class AirConditioner {
  7. public void on(){
  8. System.out.println("打开空调");
  9. }
  10. public void off(){
  11. System.out.println("关闭空调");
  12. }
  13. }
  1. /**
  2. * @description: 咖啡机
  3. * @author: dashu
  4. * @create: 14:40
  5. */
  6. public class CoffeeMaker {
  7. public void on(){
  8. System.out.println("打开咖啡机");
  9. }
  10. public void off(){
  11. System.out.println("关闭咖啡机");
  12. }
  13. }
  1. /**
  2. * @description: 灯光
  3. * @author: dashu
  4. * @create: 14:43
  5. */
  6. public class Lamplight {
  7. public void on(){
  8. System.out.println("打开灯光");
  9. }
  10. public void off(){
  11. System.out.println("关闭灯光");
  12. }
  13. }
  1. /**
  2. * @description: 新闻
  3. * @author: dashu
  4. * @create: 14:44
  5. */
  6. public class Journalism {
  7. public void on(){
  8. System.out.println("打开新闻");
  9. }
  10. public void off(){
  11. System.out.println("关闭新闻");
  12. }
  13. }
  1. /**
  2. * @description: 客户端
  3. * @author: dashu
  4. * @create: 14:24
  5. */
  6. public class Client {
  7. /**
  8. * 智能家居
  9. * 1、早上起床前30分钟,定时器执行命令,控制家中的智能家居执行以下步骤:
  10. * 开启热水器
  11. * 开启空调
  12. * 开启咖啡机
  13. * 30分钟后(起床)
  14. * 开启灯光
  15. * 播放新闻
  16. * 放洗澡水-拿咖啡-洗澡
  17. * 出门关闭各种设备
  18. */
  19. public static void main(String[] args) {
  20. System.out.println("---起床前30分钟---");
  21. WaterHeater waterHeater = new WaterHeater();
  22. waterHeater.on();
  23. AirConditioner airConditioner = new AirConditioner();
  24. airConditioner.on();
  25. CoffeeMaker coffeeMaker = new CoffeeMaker();
  26. coffeeMaker.on();
  27. Lamplight lamplight = new Lamplight();
  28. lamplight.on();
  29. Journalism journalism = new Journalism();
  30. journalism.on();
  31. System.out.println("---起床---");
  32. System.out.println("放洗澡水-拿咖啡-洗澡");
  33. System.out.println("---出门---");
  34. waterHeater.off();
  35. airConditioner.off();
  36. coffeeMaker.off();
  37. lamplight.off();
  38. journalism.off();
  39. }
  40. }

二、外观设计模式编程

1、介绍

外观模式可以理解为转换一群接口,客户端只需要调用一个接口,而不用调用多个接口才能达到目的

外观模式就是解决多个复杂接口带来的使用困难,起到简化用户操作的作用

2、UML

3、代码

  1. /**
  2. * @description: 热水器
  3. * @author: dashu
  4. * @create: 14:38
  5. */
  6. public class WaterHeater {
  7. private static WaterHeater waterHeater = new WaterHeater();
  8. private WaterHeater() { }
  9. public static WaterHeater instance(){
  10. return waterHeater;
  11. }
  12. public void on(){
  13. System.out.println("打开热水器");
  14. }
  15. public void off(){
  16. System.out.println("关闭热水器");
  17. }
  18. }
  1. /**
  2. * @description: 空调
  3. * @author: dashu
  4. * @create: 14:39
  5. */
  6. public class AirConditioner {
  7. private static AirConditioner airConditioner = new AirConditioner();
  8. private AirConditioner() { }
  9. public static AirConditioner instance(){
  10. return airConditioner;
  11. }
  12. public void on(){
  13. System.out.println("打开空调");
  14. }
  15. public void off(){
  16. System.out.println("关闭空调");
  17. }
  18. }
  1. /**
  2. * @description: 咖啡机
  3. * @author: dashu
  4. * @create: 14:40
  5. */
  6. public class CoffeeMaker {
  7. private static CoffeeMaker coffeeMaker = new CoffeeMaker();
  8. private CoffeeMaker() { }
  9. public static CoffeeMaker instance(){
  10. return coffeeMaker;
  11. }
  12. public void on(){
  13. System.out.println("打开咖啡机");
  14. }
  15. public void off(){
  16. System.out.println("关闭咖啡机");
  17. }
  18. }
  1. /**
  2. * @description: 灯光
  3. * @author: dashu
  4. * @create: 14:43
  5. */
  6. public class Lamplight {
  7. private static Lamplight lamplight = new Lamplight();
  8. private Lamplight() { }
  9. public static Lamplight instance(){
  10. return lamplight;
  11. }
  12. public void on(){
  13. System.out.println("打开灯光");
  14. }
  15. public void off(){
  16. System.out.println("关闭灯光");
  17. }
  18. }
  1. /**
  2. * @description: 新闻
  3. * @author: dashu
  4. * @create: 14:44
  5. */
  6. public class Journalism {
  7. private static Journalism journalism = new Journalism();
  8. private Journalism() { }
  9. public static Journalism instance(){
  10. return journalism;
  11. }
  12. public void on(){
  13. System.out.println("打开新闻");
  14. }
  15. public void off(){
  16. System.out.println("关闭新闻");
  17. }
  18. }
  1. /**
  2. * @description: 智能家居外观者模式
  3. * @author: dashu
  4. * @create: 14:56
  5. */
  6. public class SmartHomeFacade {
  7. private WaterHeater waterHeater;
  8. private AirConditioner airConditioner;
  9. private CoffeeMaker coffeeMaker;
  10. private Lamplight lamplight;
  11. private Journalism journalism;
  12. public SmartHomeFacade(){
  13. this.waterHeater = WaterHeater.instance();
  14. this.airConditioner = AirConditioner.instance();
  15. this.coffeeMaker = CoffeeMaker.instance();
  16. this.lamplight = Lamplight.instance();
  17. this.journalism = Journalism.instance();
  18. }
  19. /**
  20. * 开启
  21. */
  22. public void on(){
  23. waterHeater.on();
  24. airConditioner.on();
  25. coffeeMaker.on();
  26. lamplight.on();
  27. journalism.on();
  28. }
  29. /**
  30. * 关闭
  31. */
  32. public void off(){
  33. waterHeater.off();
  34. airConditioner.off();
  35. coffeeMaker.off();
  36. lamplight.off();
  37. journalism.off();
  38. }
  39. }
  1. /**
  2. * @description: 客户端
  3. * @author: dashu
  4. * @create: 15:04
  5. */
  6. public class Client {
  7. /**
  8. * 智能家居
  9. * 1、早上起床前30分钟,定时器执行命令,控制家中的智能家居执行以下步骤:
  10. * 开启热水器
  11. * 开启空调
  12. * 开启咖啡机
  13. * 30分钟后(起床)
  14. * 开启灯光
  15. * 播放新闻
  16. * 放洗澡水-拿咖啡-洗澡
  17. * 出门关闭各种设备
  18. */
  19. public static void main(String[] args) {
  20. System.out.println("---起床前30分钟---");
  21. SmartHomeFacade smartHomeFacade = new SmartHomeFacade();
  22. smartHomeFacade.on();
  23. System.out.println("---起床---");
  24. System.out.println("放洗澡水-拿咖啡-洗澡");
  25. System.out.println("---出门---");
  26. smartHomeFacade.off();
  27. }
  28. }