<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

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

          共 5644字,需瀏覽 12分鐘

           ·

          2021-03-19 17:31

          大家好,我是狼王,一個愛打球的程序員

          這是設計模式的第九篇,這篇讓我們來認識一下外觀(門面)模式


          1、概述

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

          避免多種不相關的功能污染單一外觀, 使其變成又一個復雜結構??蛻舳撕推渌庥^都可使用附加外觀。

          2、適用場景

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

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

          3、實例

          有以下場景:

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

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

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

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

          3.1 不使用外觀模式時候

          /**
          ?*?學生
          ?*/

          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?StudentAddressService?implements?IStudentAddress{

          ????@Override
          ????public?String?getAddress()?{
          ????????Student?student?=?new?Student();
          ????????return?student.getAddress();
          ????}
          }
          /**
          ?*?年齡接口
          ?*/

          @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();
          ????}
          }

          三個外部服務

          /**
          ?*?教育服務
          ?*/

          @Service
          public?class?EduService?{

          ????@Autowired
          ????private?StudentNameService?studentNameService;

          ????@Autowired
          ????private?StudentAgeService?studentAgeService;

          ????@Autowired
          ????private?StudentAddressService?studentAddressService;

          ????public?void?getStudentName(){
          ????????System.out.println("學生姓名是:"?+?studentNameService.getName());
          ????}

          ????public?void?getStudentAge(){
          ????????System.out.println("學生年齡是:"?+?studentAgeService.getAge());
          ????}

          ????public?void?getStudentAddress(){
          ????????System.out.println("學生地址是:"?+?studentAddressService.getAddress());
          ????}
          }
          /**
          ?*?考試服務
          ?*/

          @Service
          public?class?ExamService?{

          ????@Autowired
          ????private?StudentNameService?studentNameService;

          ????@Autowired
          ????private?StudentAgeService?studentAgeService;

          ????@Autowired
          ????private?StudentAddressService?studentAddressService;

          ????public?void?getStudentName(){
          ????????System.out.println("學生姓名是:"?+?studentNameService.getName());
          ????}

          ????public?void?getStudentAge(){
          ????????System.out.println("學生年齡是:"?+?studentAgeService.getAge());
          ????}

          ????public?void?getStudentAddress(){
          ????????System.out.println("學生地址是:"?+?studentAddressService.getAddress());
          ????}
          }
          /**
          ?*?成績服務
          ?*/

          @Service
          public?class?ScoreService?{

          ????@Autowired
          ????private?StudentNameService?studentNameService;

          ????@Autowired
          ????private?StudentAgeService?studentAgeService;

          ????@Autowired
          ????private?StudentAddressService?studentAddressService;

          ????public?void?getStudentName(){
          ????????System.out.println("學生姓名是:"?+?studentNameService.getName());
          ????}

          ????public?void?getStudentAge(){
          ????????System.out.println("學生年齡是:"?+?studentAgeService.getAge());
          ????}

          ????public?void?getStudentAddress(){
          ????????System.out.println("學生地址是:"?+?studentAddressService.getAddress());
          ????}
          }

          3.2 使用外觀模式

          在學生服務這里增加一個外觀service

          /**
          ?*?外觀模式服務
          ?*/

          @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();
          ????}

          }

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

          /**
          ?*?教育服務
          ?*/

          @Service
          public?class?EduService?{

          ????@Autowired
          ????private?StudentFacedService?studentFacedService;

          ????public?void?getStudentName()?{
          ????????System.out.println("學生姓名是:"?+?studentFacedService.getStudentName());
          ????}

          ????public?void?getStudentAge()?{
          ????????System.out.println("學生年齡是:"?+?studentFacedService.getStudentAge());
          ????}

          ????public?void?getStudentAddress()?{
          ????????System.out.println("學生地址是:"?+?studentFacedService.getStudentAddress());
          ????}
          }
          /**
          ?*?考試服務
          ?*/

          @Service
          public?class?ExamService?{

          ????@Autowired
          ????private?StudentFacedService?studentFacedService;

          ????public?void?getStudentName()?{
          ????????System.out.println("學生姓名是:"?+?studentFacedService.getStudentName());
          ????}

          ????public?void?getStudentAge()?{
          ????????System.out.println("學生年齡是:"?+?studentFacedService.getStudentAge());
          ????}

          ????public?void?getStudentAddress()?{
          ????????System.out.println("學生地址是:"?+?studentFacedService.getStudentAddress());
          ????}
          }
          /**
          ?*?成績服務
          ?*/

          @Service
          public?class?ScoreService?{

          ????@Autowired
          ????private?StudentFacedService?studentFacedService;

          ????public?void?getStudentName()?{
          ????????System.out.println("學生姓名是:"?+?studentFacedService.getStudentName());
          ????}

          ????public?void?getStudentAge()?{
          ????????System.out.println("學生年齡是:"?+?studentFacedService.getStudentAge());
          ????}

          ????public?void?getStudentAddress()?{
          ????????System.out.println("學生地址是:"?+?studentFacedService.getStudentAddress());
          ????}
          }
          4、分析

          以上兩種方式代碼結構如下所示:

          77ebde99040eca2ec6064f0be7db4e4d.webp

          654f48c81d9cf3c9cdd64928e2e185d6.webp

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

          5、總結

          優(yōu)點:

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

          缺點:

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

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

          樂于輸出干貨的Java技術公眾號:狼王編程。公眾號內(nèi)有大量的技術文章、海量視頻資源、精美腦圖,不妨來關注一下!回復資料領取大量學習資源和免費書籍!

          轉發(fā)朋友圈是對我最大的支持!
          38af0cc0631fb2c9b47b382482e1fc7e.webp?覺得有點東西就點一下“贊和在看”吧!感謝大家的支持了!



          瀏覽 54
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  美女肏逼视频免费看黄色 | 色色网址| 欧美A区 欧美狂操 | 日韩色在线观看 | www.在线看黄 |