日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
設(shè)計(jì)模式系列|之外觀(門面)模式

[[388466]]

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

1、概述

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

避免多種不相關(guān)的功能污染單一外觀, 使其變成又一個(gè)復(fù)雜結(jié)構(gòu)??蛻舳撕推渌庥^都可使用附加外觀。

2、適用場(chǎng)景

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

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

3、實(shí)例

有以下場(chǎng)景:

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

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

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

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

3.1 不使用外觀模式時(shí)候

 
 
 
 
  1. /** 
  2.  * 學(xué)生 
  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 name, int 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.  * 學(xué)生 
  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 name, int 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.     } 

三個(gè)外部服務(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("學(xué)生姓名是:" + studentNameService.getName()); 
  18.     } 
  19.  
  20.     public void getStudentAge(){ 
  21.         System.out.println("學(xué)生年齡是:" + studentAgeService.getAge()); 
  22.     } 
  23.  
  24.     public void getStudentAddress(){ 
  25.         System.out.println("學(xué)生地址是:" + 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("學(xué)生姓名是:" + studentNameService.getName()); 
  18.     } 
  19.  
  20.     public void getStudentAge(){ 
  21.         System.out.println("學(xué)生年齡是:" + studentAgeService.getAge()); 
  22.     } 
  23.  
  24.     public void getStudentAddress(){ 
  25.         System.out.println("學(xué)生地址是:" + studentAddressService.getAddress()); 
  26.     } 
 
 
 
 
  1. /** 
  2.  * 成績(jī)服務(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("學(xué)生姓名是:" + studentNameService.getName()); 
  18.     } 
  19.  
  20.     public void getStudentAge(){ 
  21.         System.out.println("學(xué)生年齡是:" + studentAgeService.getAge()); 
  22.     } 
  23.  
  24.     public void getStudentAddress(){ 
  25.         System.out.println("學(xué)生地址是:" + studentAddressService.getAddress()); 
  26.     } 

3.2 使用外觀模式

在學(xué)生服務(wù)這里增加一個(gè)外觀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.  

三個(gè)調(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("學(xué)生姓名是:" + studentFacedService.getStudentName()); 
  12.     } 
  13.  
  14.     public void getStudentAge() { 
  15.         System.out.println("學(xué)生年齡是:" + studentFacedService.getStudentAge()); 
  16.     } 
  17.  
  18.     public void getStudentAddress() { 
  19.         System.out.println("學(xué)生地址是:" + 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("學(xué)生姓名是:" + studentFacedService.getStudentName()); 
  12.     } 
  13.  
  14.     public void getStudentAge() { 
  15.         System.out.println("學(xué)生年齡是:" + studentFacedService.getStudentAge()); 
  16.     } 
  17.  
  18.     public void getStudentAddress() { 
  19.         System.out.println("學(xué)生地址是:" + studentFacedService.getStudentAddress()); 
  20.     } 
 
 
 
 
  1. /** 
  2.  * 成績(jī)服務(wù) 
  3.  */ 
  4. @Service 
  5. public class ScoreService { 
  6.  
  7.     @Autowired 
  8.     private StudentFacedService studentFacedService; 
  9.  
  10.     public void getStudentName() { 
  11.         System.out.println("學(xué)生姓名是:" + studentFacedService.getStudentName()); 
  12.     } 
  13.  
  14.     public void getStudentAge() { 
  15.         System.out.println("學(xué)生年齡是:" + studentFacedService.getStudentAge()); 
  16.     } 
  17.  
  18.     public void getStudentAddress() { 
  19.         System.out.println("學(xué)生地址是:" + studentFacedService.getStudentAddress()); 
  20.     } 

4、分析

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

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

5、總結(jié)

優(yōu)點(diǎn):

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

缺點(diǎn):

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

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


分享名稱:設(shè)計(jì)模式系列|之外觀(門面)模式
轉(zhuǎn)載注明:http://www.dlmjj.cn/article/dpsjsdo.html