设计模式系列|之外观(门面)模式

开发 前端
外观模式是一种结构型设计模式, 能为程序库、 框架或其他复杂类提供一个简单的接口。避免多种不相关的功能污染单一外观, 使其变成又一个复杂结构。客户端和其他外观都可使用附加外观。

[[388466]]

本文转载自微信公众号「狼王编程」,作者狼王。转载本文请联系狼王编程公众号。

1、概述

外观模式是一种结构型设计模式, 能为程序库、 框架或其他复杂类提供一个简单的接口。

避免多种不相关的功能污染单一外观, 使其变成又一个复杂结构。客户端和其他外观都可使用附加外观。

2、适用场景

1)如果你需要一个指向复杂子系统的直接接口, 且该接口的功能有限, 则可以使用外观模式。外观将会提供指向子系统中最常用功能的快捷方式, 能够满足客户端的大部分需求。

2)如果需要将子系统组织为多层结构, 可以使用外观。你可以为每个层次创建一个外观, 然后要求各层的类必须通过这些外观进行交互。

3、实例

有以下场景:

当前有学生子系统,该系统有三个接口,查询学生姓名,查询学生年龄,查询学生家庭地址。

有一个教学系统,要分别去调用这三个接口。

有一个成绩系统,要分别调用者三个接口。

有一个考试系统,也要分别调用这三个系统。

3.1 不使用外观模式时候

  1. /** 
  2.  * 学生 
  3.  */ 
  4. public class Student { 
  5.  
  6.     private String name = "狼王"
  7.  
  8.     private int age = 25; 
  9.  
  10.     private String address = "上海"
  11.  
  12.     public Student(String nameint age, String address) { 
  13.         this.name = name
  14.         this.age = age; 
  15.         this.address = address; 
  16.     } 
  17.  
  18.     public Student(){ 
  19.  
  20.     } 
  21.  
  22.     public String getName() { 
  23.         return name
  24.     } 
  25.  
  26.     public void setName(String name) { 
  27.         this.name = name
  28.     } 
  29.  
  30.     public int getAge() { 
  31.         return age; 
  32.     } 
  33.  
  34.     public void setAge(int age) { 
  35.         this.age = age; 
  36.     } 
  37.  
  38.     public String getAddress() { 
  39.         return address; 
  40.     } 
  41.  
  42.     public void setAddress(String address) { 
  43.         this.address = address; 
  44.     } 
  1. /** 
  2.  * 学生 
  3.  */ 
  4. public class Student { 
  5.  
  6.     private String name = "狼王"
  7.  
  8.     private int age = 25; 
  9.  
  10.     private String address = "上海"
  11.  
  12.     public Student(String nameint age, String address) { 
  13.         this.name = name
  14.         this.age = age; 
  15.         this.address = address; 
  16.     } 
  17.  
  18.     public Student(){ 
  19.  
  20.     } 
  21.  
  22.     public String getName() { 
  23.         return name
  24.     } 
  25.  
  26.     public void setName(String name) { 
  27.         this.name = name
  28.     } 
  29.  
  30.     public int getAge() { 
  31.         return age; 
  32.     } 
  33.  
  34.     public void setAge(int age) { 
  35.         this.age = age; 
  36.     } 
  37.  
  38.     public String getAddress() { 
  39.         return address; 
  40.     } 
  41.  
  42.     public void setAddress(String address) { 
  43.         this.address = address; 
  44.     } 
  1. /** 
  2.  * 年龄接口 
  3.  */ 
  4. @Service 
  5. public class StudentAgeService implements IStudentAge{ 
  6.  
  7.     @Override 
  8.     public int getAge() { 
  9.         Student student = new Student(); 
  10.         return student.getAge(); 
  11.     } 
  1. @Service 
  2. public class StudentNameService implements IStudentName{ 
  3.  
  4.     @Override 
  5.     public String getName() { 
  6.         Student student = new Student(); 
  7.         return student.getName(); 
  8.     } 

三个外部服务

  1. /** 
  2.  * 教育服务 
  3.  */ 
  4. @Service 
  5. public class EduService { 
  6.  
  7.     @Autowired 
  8.     private StudentNameService studentNameService; 
  9.  
  10.     @Autowired 
  11.     private StudentAgeService studentAgeService; 
  12.  
  13.     @Autowired 
  14.     private StudentAddressService studentAddressService; 
  15.  
  16.     public void getStudentName(){ 
  17.         System.out.println("学生姓名是:" + studentNameService.getName()); 
  18.     } 
  19.  
  20.     public void getStudentAge(){ 
  21.         System.out.println("学生年龄是:" + studentAgeService.getAge()); 
  22.     } 
  23.  
  24.     public void getStudentAddress(){ 
  25.         System.out.println("学生地址是:" + studentAddressService.getAddress()); 
  26.     } 
  1. /** 
  2.  * 考试服务 
  3.  */ 
  4. @Service 
  5. public class ExamService { 
  6.  
  7.     @Autowired 
  8.     private StudentNameService studentNameService; 
  9.  
  10.     @Autowired 
  11.     private StudentAgeService studentAgeService; 
  12.  
  13.     @Autowired 
  14.     private StudentAddressService studentAddressService; 
  15.  
  16.     public void getStudentName(){ 
  17.         System.out.println("学生姓名是:" + studentNameService.getName()); 
  18.     } 
  19.  
  20.     public void getStudentAge(){ 
  21.         System.out.println("学生年龄是:" + studentAgeService.getAge()); 
  22.     } 
  23.  
  24.     public void getStudentAddress(){ 
  25.         System.out.println("学生地址是:" + studentAddressService.getAddress()); 
  26.     } 
  1. /** 
  2.  * 成绩服务 
  3.  */ 
  4. @Service 
  5. public class ScoreService { 
  6.  
  7.     @Autowired 
  8.     private StudentNameService studentNameService; 
  9.  
  10.     @Autowired 
  11.     private StudentAgeService studentAgeService; 
  12.  
  13.     @Autowired 
  14.     private StudentAddressService studentAddressService; 
  15.  
  16.     public void getStudentName(){ 
  17.         System.out.println("学生姓名是:" + studentNameService.getName()); 
  18.     } 
  19.  
  20.     public void getStudentAge(){ 
  21.         System.out.println("学生年龄是:" + studentAgeService.getAge()); 
  22.     } 
  23.  
  24.     public void getStudentAddress(){ 
  25.         System.out.println("学生地址是:" + studentAddressService.getAddress()); 
  26.     } 

3.2 使用外观模式

在学生服务这里增加一个外观service

  1. /** 
  2.  * 外观模式服务 
  3.  */ 
  4. @Service 
  5. public class StudentFacedService { 
  6.  
  7.     @Autowired 
  8.     private StudentNameService studentNameService; 
  9.  
  10.     @Autowired 
  11.     private StudentAgeService studentAgeService; 
  12.  
  13.     @Autowired 
  14.     private StudentAddressService studentAddressService; 
  15.  
  16.     public String getStudentName(){ 
  17.         return studentNameService.getName(); 
  18.     } 
  19.  
  20.     public int getStudentAge(){ 
  21.         return studentAgeService.getAge(); 
  22.     } 
  23.  
  24.     public String getStudentAddress(){ 
  25.         return studentAddressService.getAddress(); 
  26.     } 
  27.  

三个调用服务只需要引入外观服务

  1. /** 
  2.  * 教育服务 
  3.  */ 
  4. @Service 
  5. public class EduService { 
  6.  
  7.     @Autowired 
  8.     private StudentFacedService studentFacedService; 
  9.  
  10.     public void getStudentName() { 
  11.         System.out.println("学生姓名是:" + studentFacedService.getStudentName()); 
  12.     } 
  13.  
  14.     public void getStudentAge() { 
  15.         System.out.println("学生年龄是:" + studentFacedService.getStudentAge()); 
  16.     } 
  17.  
  18.     public void getStudentAddress() { 
  19.         System.out.println("学生地址是:" + studentFacedService.getStudentAddress()); 
  20.     } 
  1. /** 
  2.  * 考试服务 
  3.  */ 
  4. @Service 
  5. public class ExamService { 
  6.  
  7.     @Autowired 
  8.     private StudentFacedService studentFacedService; 
  9.  
  10.     public void getStudentName() { 
  11.         System.out.println("学生姓名是:" + studentFacedService.getStudentName()); 
  12.     } 
  13.  
  14.     public void getStudentAge() { 
  15.         System.out.println("学生年龄是:" + studentFacedService.getStudentAge()); 
  16.     } 
  17.  
  18.     public void getStudentAddress() { 
  19.         System.out.println("学生地址是:" + studentFacedService.getStudentAddress()); 
  20.     } 
  1. /** 
  2.  * 成绩服务 
  3.  */ 
  4. @Service 
  5. public class ScoreService { 
  6.  
  7.     @Autowired 
  8.     private StudentFacedService studentFacedService; 
  9.  
  10.     public void getStudentName() { 
  11.         System.out.println("学生姓名是:" + studentFacedService.getStudentName()); 
  12.     } 
  13.  
  14.     public void getStudentAge() { 
  15.         System.out.println("学生年龄是:" + studentFacedService.getStudentAge()); 
  16.     } 
  17.  
  18.     public void getStudentAddress() { 
  19.         System.out.println("学生地址是:" + studentFacedService.getStudentAddress()); 
  20.     } 

4、分析

以上两种方式代码结构如下所示:

从上面两张图可以看到,对于外部服务来说,极大的缩减了代码复杂度,只需要调用学生服务的一个接口。

5、总结

优点:

让客户端代码独立独立于复杂的子系统,且减少对于子系统的依赖。

缺点:

过于庞大的外观,会使得该外观称成为上帝对象,造成所有类的耦合,可通过它操作所有的类功能。

 

好了。今天就说到这了,我还会不断分享自己的所学所想,希望我们一起走在成功的道路上!

 

责任编辑:武晓燕 来源: 狼王编程
相关推荐

2022-02-15 22:45:00

前端设计模式

2020-10-23 09:40:26

设计模式

2023-05-06 07:51:22

JavaFacade设计模式

2021-04-18 21:07:32

门面模式设计

2024-02-19 13:11:38

门面模式系统

2022-11-14 08:44:56

前端门面模式接口

2010-01-21 09:08:53

.NET设计模式

2022-01-12 13:33:25

工厂模式设计

2020-11-03 13:05:18

命令模式

2020-11-04 08:54:54

状态模式

2021-09-29 13:53:17

抽象工厂模式

2021-03-02 08:50:31

设计单例模式

2020-10-21 14:29:15

原型模式

2022-01-14 09:22:22

设计模式桥接

2013-11-26 15:48:53

Android设计模式SDK

2021-06-09 08:53:34

设计模式策略模式工厂模式

2020-10-19 09:28:00

抽象工厂模式

2021-10-28 19:09:09

模式原型Java

2021-10-26 00:21:19

设计模式建造者

2012-01-13 15:59:07

点赞
收藏

51CTO技术栈公众号