自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

設(shè)計模式系列|之外觀(門面)模式

開發(fā) 前端
外觀模式是一種結(jié)構(gòu)型設(shè)計模式, 能為程序庫、 框架或其他復雜類提供一個簡單的接口。避免多種不相關(guān)的功能污染單一外觀, 使其變成又一個復雜結(jié)構(gòu)??蛻舳撕推渌庥^都可使用附加外觀。

[[388466]]

本文轉(zhuǎn)載自微信公眾號「狼王編程」,作者狼王。轉(zhuǎn)載本文請聯(lián)系狼王編程公眾號。

1、概述

外觀模式是一種結(jié)構(gòu)型設(shè)計模式, 能為程序庫、 框架或其他復雜類提供一個簡單的接口。

避免多種不相關(guān)的功能污染單一外觀, 使其變成又一個復雜結(jié)構(gòu)。客戶端和其他外觀都可使用附加外觀。

2、適用場景

1)如果你需要一個指向復雜子系統(tǒng)的直接接口, 且該接口的功能有限, 則可以使用外觀模式。外觀將會提供指向子系統(tǒng)中最常用功能的快捷方式, 能夠滿足客戶端的大部分需求。

2)如果需要將子系統(tǒng)組織為多層結(jié)構(gòu), 可以使用外觀。你可以為每個層次創(chuàng)建一個外觀, 然后要求各層的類必須通過這些外觀進行交互。

3、實例

有以下場景:

當前有學生子系統(tǒng),該系統(tǒng)有三個接口,查詢學生姓名,查詢學生年齡,查詢學生家庭地址。

有一個教學系統(tǒng),要分別去調(diào)用這三個接口。

有一個成績系統(tǒng),要分別調(diào)用者三個接口。

有一個考試系統(tǒng),也要分別調(diào)用這三個系統(tǒng)。

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.     } 

三個外部服務(wù)

  1. /** 
  2.  * 教育服務(wù) 
  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.  * 考試服務(wù) 
  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.  * 成績服務(wù) 
  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 使用外觀模式

在學生服務(wù)這里增加一個外觀service

  1. /** 
  2.  * 外觀模式服務(wù) 
  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.  

三個調(diào)用服務(wù)只需要引入外觀服務(wù)

  1. /** 
  2.  * 教育服務(wù) 
  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.  * 考試服務(wù) 
  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.  * 成績服務(wù) 
  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、分析

以上兩種方式代碼結(jié)構(gòu)如下所示:

從上面兩張圖可以看到,對于外部服務(wù)來說,極大的縮減了代碼復雜度,只需要調(diào)用學生服務(wù)的一個接口。

5、總結(jié)

優(yōu)點:

讓客戶端代碼獨立獨立于復雜的子系統(tǒng),且減少對于子系統(tǒng)的依賴。

缺點:

過于龐大的外觀,會使得該外觀稱成為上帝對象,造成所有類的耦合,可通過它操作所有的類功能。

 

好了。今天就說到這了,我還會不斷分享自己的所學所想,希望我們一起走在成功的道路上!

 

責任編輯:武曉燕 來源: 狼王編程
相關(guān)推薦

2022-02-15 22:45:00

前端設(shè)計模式

2020-10-23 09:40:26

設(shè)計模式

2023-05-06 07:51:22

JavaFacade設(shè)計模式

2021-04-18 21:07:32

門面模式設(shè)計

2024-02-19 13:11:38

門面模式系統(tǒng)

2022-11-14 08:44:56

前端門面模式接口

2022-01-12 13:33:25

工廠模式設(shè)計

2020-11-03 13:05:18

命令模式

2020-11-04 08:54:54

狀態(tài)模式

2010-01-21 09:08:53

.NET設(shè)計模式

2021-06-09 08:53:34

設(shè)計模式策略模式工廠模式

2020-10-19 09:28:00

抽象工廠模式

2021-09-29 13:53:17

抽象工廠模式

2022-01-14 09:22:22

設(shè)計模式橋接

2013-11-26 15:48:53

Android設(shè)計模式SDK

2021-03-02 08:50:31

設(shè)計單例模式

2020-10-21 14:29:15

原型模式

2020-11-09 08:20:33

解釋器模式

2020-10-20 13:33:00

建造者模式

2012-01-13 15:59:07

點贊
收藏

51CTO技術(shù)棧公眾號