日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

[[388466]]

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

1、概述

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

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

2、適用場景

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

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

3、實例

有以下場景:

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

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

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

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

3.1 不使用外觀模式時候

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

三個外部服務(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.  * 成績服務(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ù)這里增加一個外觀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("學(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.  * 成績服務(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)如下所示:

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

5、總結(jié)

優(yōu)點:

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

缺點:

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

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


新聞標題:設(shè)計模式系列|之外觀(門面)模式
標題鏈接:http://www.5511xx.com/article/dpsjsdo.html