這篇文章主要介紹“如何使用設(shè)計模式的外觀模式”,在日常操作中,相信很多人在如何使用設(shè)計模式的外觀模式問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何使用設(shè)計模式的外觀模式”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)建站是少有的成都網(wǎng)站設(shè)計、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、營銷型企業(yè)網(wǎng)站、小程序制作、手機(jī)APP,開發(fā)、制作、設(shè)計、買友情鏈接、推廣優(yōu)化一站式服務(wù)網(wǎng)絡(luò)公司,自2013年起,堅持透明化,價格低,無套路經(jīng)營理念。讓網(wǎng)頁驚喜每一位訪客多年來深受用戶好評
外觀模式是一種結(jié)構(gòu)型設(shè)計模式, 能為程序庫、 框架或其他復(fù)雜類提供一個簡單的接口。
避免多種不相關(guān)的功能污染單一外觀, 使其變成又一個復(fù)雜結(jié)構(gòu)??蛻舳撕推渌庥^都可使用附加外觀。
1)如果你需要一個指向復(fù)雜子系統(tǒng)的直接接口, 且該接口的功能有限, 則可以使用外觀模式。外觀將會提供指向子系統(tǒng)中最常用功能的快捷方式, 能夠滿足客戶端的大部分需求。
2)如果需要將子系統(tǒng)組織為多層結(jié)構(gòu), 可以使用外觀。你可以為每個層次創(chuàng)建一個外觀, 然后要求各層的類必須通過這些外觀進(jìn)行交互。
有以下場景:
當(dāng)前有學(xué)生子系統(tǒng),該系統(tǒng)有三個接口,查詢學(xué)生姓名,查詢學(xué)生年齡,查詢學(xué)生家庭地址。
有一個教學(xué)系統(tǒng),要分別去調(diào)用這三個接口。
有一個成績系統(tǒng),要分別調(diào)用者三個接口。
有一個考試系統(tǒng),也要分別調(diào)用這三個系統(tǒng)。
/** * 學(xué)生 */ public class Student { private String name = "狼王"; private int age = 25; private String address = "上海"; public Student(String name, int age, String address) { this.name = name; this.age = age; this.address = address; } public Student(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
/** * 學(xué)生 */ public class Student { private String name = "狼王"; private int age = 25; private String address = "上海"; public Student(String name, int age, String address) { this.name = name; this.age = age; this.address = address; } public Student(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
/** * 年齡接口 */ @Service public class StudentAgeService implements IStudentAge{ @Override public int getAge() { Student student = new Student(); return student.getAge(); } }
@Service public class StudentNameService implements IStudentName{ @Override public String getName() { Student student = new Student(); return student.getName(); } }
/** * 教育服務(wù) */ @Service public class EduService { @Autowired private StudentNameService studentNameService; @Autowired private StudentAgeService studentAgeService; @Autowired private StudentAddressService studentAddressService; public void getStudentName(){ System.out.println("學(xué)生姓名是:" + studentNameService.getName()); } public void getStudentAge(){ System.out.println("學(xué)生年齡是:" + studentAgeService.getAge()); } public void getStudentAddress(){ System.out.println("學(xué)生地址是:" + studentAddressService.getAddress()); } }
/** * 考試服務(wù) */ @Service public class ExamService { @Autowired private StudentNameService studentNameService; @Autowired private StudentAgeService studentAgeService; @Autowired private StudentAddressService studentAddressService; public void getStudentName(){ System.out.println("學(xué)生姓名是:" + studentNameService.getName()); } public void getStudentAge(){ System.out.println("學(xué)生年齡是:" + studentAgeService.getAge()); } public void getStudentAddress(){ System.out.println("學(xué)生地址是:" + studentAddressService.getAddress()); } }
/** * 成績服務(wù) */ @Service public class ScoreService { @Autowired private StudentNameService studentNameService; @Autowired private StudentAgeService studentAgeService; @Autowired private StudentAddressService studentAddressService; public void getStudentName(){ System.out.println("學(xué)生姓名是:" + studentNameService.getName()); } public void getStudentAge(){ System.out.println("學(xué)生年齡是:" + studentAgeService.getAge()); } public void getStudentAddress(){ System.out.println("學(xué)生地址是:" + studentAddressService.getAddress()); } }
在學(xué)生服務(wù)這里增加一個外觀service
/** * 外觀模式服務(wù) */ @Service public class StudentFacedService { @Autowired private StudentNameService studentNameService; @Autowired private StudentAgeService studentAgeService; @Autowired private StudentAddressService studentAddressService; public String getStudentName(){ return studentNameService.getName(); } public int getStudentAge(){ return studentAgeService.getAge(); } public String getStudentAddress(){ return studentAddressService.getAddress(); } }
/** * 教育服務(wù) */ @Service public class EduService { @Autowired private StudentFacedService studentFacedService; public void getStudentName() { System.out.println("學(xué)生姓名是:" + studentFacedService.getStudentName()); } public void getStudentAge() { System.out.println("學(xué)生年齡是:" + studentFacedService.getStudentAge()); } public void getStudentAddress() { System.out.println("學(xué)生地址是:" + studentFacedService.getStudentAddress()); } }
/** * 考試服務(wù) */ @Service public class ExamService { @Autowired private StudentFacedService studentFacedService; public void getStudentName() { System.out.println("學(xué)生姓名是:" + studentFacedService.getStudentName()); } public void getStudentAge() { System.out.println("學(xué)生年齡是:" + studentFacedService.getStudentAge()); } public void getStudentAddress() { System.out.println("學(xué)生地址是:" + studentFacedService.getStudentAddress()); } }
/** * 成績服務(wù) */ @Service public class ScoreService { @Autowired private StudentFacedService studentFacedService; public void getStudentName() { System.out.println("學(xué)生姓名是:" + studentFacedService.getStudentName()); } public void getStudentAge() { System.out.println("學(xué)生年齡是:" + studentFacedService.getStudentAge()); } public void getStudentAddress() { System.out.println("學(xué)生地址是:" + studentFacedService.getStudentAddress()); } }
從上面兩張圖可以看到,對于外部服務(wù)來說,極大的縮減了代碼復(fù)雜度,只需要調(diào)用學(xué)生服務(wù)的一個接口。
讓客戶端代碼獨立獨立于復(fù)雜的子系統(tǒng),且減少對于子系統(tǒng)的依賴。
過于龐大的外觀,會使得該外觀稱成為上帝對象,造成所有類的耦合,可通過它操作所有的類功能。
到此,關(guān)于“如何使用設(shè)計模式的外觀模式”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
文章名稱:如何使用設(shè)計模式的外觀模式
網(wǎng)頁路徑:http://chinadenli.net/article46/jgjseg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站營銷、網(wǎng)站制作、定制開發(fā)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)