日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

设计模式之装饰模式20170726

發布時間:2025/4/14 asp.net 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式之装饰模式20170726 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

結構型設計模式之裝飾模式:

一、含義

動態地給一個對象添加一些額外的職責。就增加功能來說,裝飾模式相比生成子類更為靈活。

通俗來講,裝飾模式是對類的功能進行加強或減弱。

?

二、代碼說明

1.主要有兩個角色

1)構件

最核心、最原始、最基本的對象,也就是要裝飾的對象。

2)裝飾角色

把最核心、最原始、最基本的東西裝飾成其他東西

2.在用C實現過程中也是參考這種思想,以修飾成績單舉例,具體實現如下:

1)裝飾模式使用場景:

1 /***************************************************************************** 2 * Copyright (C) 2017-2018 Hanson Yu All rights reserved. 3 ------------------------------------------------------------------------------ 4 * File Module : DecoratorPatternUsage.c 5 * Description : 裝飾模式的使用 6 7 book@book-desktop:/work/projects/test/DesignPatterns/DecoratorPattern$ gcc -o DecoratorPatternUsage HighScoreDecorator.c SortDecorator.c DecoratorPattern.c DecoratorPatternUsage.c 8 book@book-desktop:/work/projects/test/DesignPatterns/DecoratorPattern$ ./DecoratorPatternUsage 9 這次語文考試最高是75,數學是78,自然是80 10 尊敬的XXX家長: 11 ....................................... 12 語文62,數學65,體育98,自然63 13 ....................................... 14 家長簽名 15 家長簽名為:老三 16 尊敬的XXX家長: 17 ....................................... 18 語文62,數學65,體育98,自然63 19 ....................................... 20 家長簽名 21 排名第33 22 家長簽名為:老三 23 24 * Created : 2017.07.25. 25 * Author : Yu Weifeng 26 * Function List : 27 * Last Modified : 28 * History : 29 ******************************************************************************/ 30 #include"stdio.h" 31 #include"malloc.h" 32 #include"stdlib.h" 33 #include"string.h" 34 #include"DecoratorPattern.h" 35 36 37 38 39 /***************************************************************************** 40 -Fuction : main 41 -Description : 42 -Input : 43 -Output : 44 -Return : 45 * Modify Date Version Author Modification 46 * ----------------------------------------------- 47 * 2017/07/25 V1.0.0 Yu Weifeng Created 48 ******************************************************************************/ 49 int main(int argc,char **argv) 50 { 51 T_SchoolReport tSchoolReport=newFourthGradeSchoolReport; 52 53 T_Decorator tDecorator=newHighScoreDecorator(tSchoolReport); 54 tDecorator.tDecoratorReport.Report(&tDecorator); 55 tDecorator.tDecoratorReport.Sign(&tDecorator,"老三"); 56 //由于向上轉型比較難實現,暫不支持多重修飾 57 tDecorator=(T_Decorator)newSortDecorator(tSchoolReport); 58 tDecorator.tDecoratorReport.Report(&tDecorator); 59 tDecorator.tDecoratorReport.Sign(&tDecorator,"老三"); 60 return 0; 61 } DecoratorPatternUsage.c

2)被調用者:

1 /***************************************************************************** 2 * Copyright (C) 2017-2018 Hanson Yu All rights reserved. 3 ------------------------------------------------------------------------------ 4 * File Module : DecoratorPattern.c 5 * Description : 裝飾模式 6 本文件是成績單的具體實現(未被修飾) 7 以修飾成績單舉例 8 9 * Created : 2017.07.25. 10 * Author : Yu Weifeng 11 * Function List : 12 * Last Modified : 13 * History : 14 ******************************************************************************/ 15 #include"stdio.h" 16 #include"malloc.h" 17 #include"stdlib.h" 18 #include"string.h" 19 #include"DecoratorPattern.h" 20 21 22 /***************************************************************************** 23 -Fuction : SchoolReportReport 24 -Description : 公有函數 25 -Input : 26 -Output : 27 -Return : 28 * Modify Date Version Author Modification 29 * ----------------------------------------------- 30 * 2017/07/25 V1.0.0 Yu Weifeng Created 31 ******************************************************************************/ 32 void SchoolReportReport(void *i_ptThis) 33 { 34 printf("尊敬的XXX家長:\r\n"); 35 printf(".......................................\r\n"); 36 printf("語文62,數學65,體育98,自然63\r\n"); 37 printf(".......................................\r\n"); 38 printf(" 家長簽名 \r\n"); 39 } 40 41 /***************************************************************************** 42 -Fuction : SchoolReportSign 43 -Description : 公有函數 44 -Input : 45 -Output : 46 -Return : 47 * Modify Date Version Author Modification 48 * ----------------------------------------------- 49 * 2017/07/25 V1.0.0 Yu Weifeng Created 50 ******************************************************************************/ 51 void SchoolReportSign(void *i_ptThis,char *i_strName) 52 { 53 printf("家長簽名為:%s\r\n",i_strName); 54 } DecoratorPattern.c 1 /***************************************************************************** 2 * Copyright (C) 2017-2018 Hanson Yu All rights reserved. 3 ------------------------------------------------------------------------------ 4 * File Module : DecoratorPattern.h 5 * Description : 裝飾模式 6 7 * Created : 2017.07.25. 8 * Author : Yu Weifeng 9 * Function List : 10 * Last Modified : 11 * History : 12 ******************************************************************************/ 13 #ifndef DECORATOR_PATTERN_H 14 #define DECORATOR_PATTERN_H 15 16 struct Decorator; 17 18 typedef struct SchoolReport 19 { 20 void (*Report)(void *i_ptThis);//為保持統一,加入i_ptThis,內部可不使用, 21 void (*Sign)(void *i_ptThis,char *i_strName);//如果使用單例,可以去掉i_ptThis 22 }T_SchoolReport;//由于i_ptThis是T_Decorator 使用,內部使用是要轉換為T_Decorator *,也可以把上訴的void *改為struct Decorator* 23 24 typedef struct Decorator 25 { 26 T_SchoolReport tSchoolReport;//私有變量,不能直接訪問 27 T_SchoolReport tDecoratorReport;//修飾,加強功能 28 29 }T_Decorator; 30 31 32 void SchoolReportReport(void *i_ptThis); 33 void SchoolReportSign(void *i_ptThis,char *i_strName); 34 #define newFourthGradeSchoolReport {SchoolReportReport,SchoolReportSign} 35 36 37 void HighScoreDecoratorReport(void *i_ptThis); 38 void HighScoreDecoratorSign(void *i_ptThis,char *i_strName); 39 #define newHighScoreDecorator(SchoolReport) {SchoolReport,HighScoreDecoratorReport,HighScoreDecoratorSign} 40 41 void SortDecoratorReport(void *i_ptThis); 42 void SortDecoratorSign(void *i_ptThis,char *i_strName); 43 #define newSortDecorator(SchoolReport) {SchoolReport,SortDecoratorReport,SortDecoratorSign} 44 45 #endif DecoratorPattern.h 1 /***************************************************************************** 2 * Copyright (C) 2017-2018 Hanson Yu All rights reserved. 3 ------------------------------------------------------------------------------ 4 * File Module : HighScoreDecorator.c 5 * Description : 裝飾模式 6 本文件是高分修飾類的具體實現 7 8 * Created : 2017.07.25. 9 * Author : Yu Weifeng 10 * Function List : 11 * Last Modified : 12 * History : 13 ******************************************************************************/ 14 #include"stdio.h" 15 #include"malloc.h" 16 #include"stdlib.h" 17 #include"string.h" 18 #include"DecoratorPattern.h" 19 20 21 22 /***************************************************************************** 23 -Fuction : ReportHighScore 24 -Description : 公有函數 25 -Input : 26 -Output : 27 -Return : 28 * Modify Date Version Author Modification 29 * ----------------------------------------------- 30 * 2017/07/25 V1.0.0 Yu Weifeng Created 31 ******************************************************************************/ 32 static void ReportHighScore() 33 { 34 printf("這次語文考試最高是75,數學是78,自然是80\r\n"); 35 } 36 37 /***************************************************************************** 38 -Fuction : HighScoreDecoratorReport 39 -Description : 公有函數 40 -Input : 41 -Output : 42 -Return : 43 * Modify Date Version Author Modification 44 * ----------------------------------------------- 45 * 2017/07/25 V1.0.0 Yu Weifeng Created 46 ******************************************************************************/ 47 void HighScoreDecoratorReport(void *i_ptThis) 48 { 49 T_Decorator *ptHighScoreDecorator=(T_Decorator *)i_ptThis; 50 ReportHighScore(); 51 ptHighScoreDecorator->tSchoolReport.Report((void *)ptHighScoreDecorator); 52 } 53 54 /***************************************************************************** 55 -Fuction : DecoratorSign 56 -Description : 公有函數 57 -Input : 58 -Output : 59 -Return : 60 * Modify Date Version Author Modification 61 * ----------------------------------------------- 62 * 2017/07/25 V1.0.0 Yu Weifeng Created 63 ******************************************************************************/ 64 void HighScoreDecoratorSign(void *i_ptThis,char *i_strName) 65 { 66 T_Decorator *ptHighScoreDecorator=(T_Decorator *)i_ptThis; 67 ptHighScoreDecorator->tSchoolReport.Sign((void *)ptHighScoreDecorator,i_strName); 68 } HighScoreDecorator.c 1 /***************************************************************************** 2 * Copyright (C) 2017-2018 Hanson Yu All rights reserved. 3 ------------------------------------------------------------------------------ 4 * File Module : SortDecorator.c 5 * Description : 裝飾模式 6 本文件是排名類的具體實現 7 8 * Created : 2017.07.25. 9 * Author : Yu Weifeng 10 * Function List : 11 * Last Modified : 12 * History : 13 ******************************************************************************/ 14 #include"stdio.h" 15 #include"malloc.h" 16 #include"stdlib.h" 17 #include"string.h" 18 #include"DecoratorPattern.h" 19 20 21 22 /***************************************************************************** 23 -Fuction : ReportHighScore 24 -Description : 公有函數 25 -Input : 26 -Output : 27 -Return : 28 * Modify Date Version Author Modification 29 * ----------------------------------------------- 30 * 2017/07/25 V1.0.0 Yu Weifeng Created 31 ******************************************************************************/ 32 static void ReportSort() 33 { 34 printf("排名第33\r\n"); 35 } 36 /***************************************************************************** 37 -Fuction : SortDecoratorReport 38 -Description : 公有函數 39 -Input : 40 -Output : 41 -Return : 42 * Modify Date Version Author Modification 43 * ----------------------------------------------- 44 * 2017/07/25 V1.0.0 Yu Weifeng Created 45 ******************************************************************************/ 46 void SortDecoratorReport(void *i_ptThis) 47 { 48 T_Decorator *ptSortDecorator=(T_Decorator *)i_ptThis; 49 ptSortDecorator->tSchoolReport.Report((void *)ptSortDecorator); 50 ReportSort(); 51 } 52 53 /***************************************************************************** 54 -Fuction : SortDecoratorSign 55 -Description : 公有函數 56 -Input : 57 -Output : 58 -Return : 59 * Modify Date Version Author Modification 60 * ----------------------------------------------- 61 * 2017/07/25 V1.0.0 Yu Weifeng Created 62 ******************************************************************************/ 63 void SortDecoratorSign(void *i_ptThis,char *i_strName) 64 { 65 T_Decorator *ptSortDecorator=(T_Decorator *)i_ptThis; 66 ptSortDecorator->tSchoolReport.Sign((void *)ptSortDecorator,i_strName); 67 } SortDecorator.c

3)執行結果:

book@book-desktop:/work/projects/test/DesignPatterns/DecoratorPattern$ gcc -o DecoratorPatternUsage HighScoreDecorator.c SortDecorator.c DecoratorPattern.c DecoratorPatternUsage.c

book@book-desktop:/work/projects/test/DesignPatterns/DecoratorPattern$ ./DecoratorPatternUsage

這次語文考試最高是75,數學是78,自然是80

尊敬的XXX家長:

.......................................

語文62,數學65,體育98,自然63

.......................................

???????????????????????????????? 家長簽名??????????????????????

家長簽名為:老三

尊敬的XXX家長:

.......................................

語文62,數學65,體育98,自然63

.......................................

???????????????????????????????? 家長簽名??????????????????????

排名第33

家長簽名為:老三

?

4)詳細代碼:

https://github.com/fengweiyu/DesignThinking/tree/master/DesignPatterns/StructuralDesignPatterns/DecoratorPattern

?

三、使用場景

1.需要擴展一個類的功能,或給一個類增加附加功能

2.需要動態地給一個對象增加功能,這些功能可以再動態地撤銷

3.需要為一批的兄弟類進行改裝或加裝功能,當然是首選裝飾模式

?

四、優點

1.裝飾類和被裝飾類可以獨立發展,而不會相互耦合。

2.裝飾模式是繼承關系的一個替代方案(如果繼承超過兩層就要考慮不再使用繼承)

3.裝飾模式可以動態地擴展一個實現類的功能(不用則不修飾(即場景中不使用即可),實現動態擴展),而繼承就必須修改程序。

4.擴展性非常好

在繼承關系中增強上層角色功能,又不影響其子類,可以通過裝飾模式重新封裝一個類,相當于創建了一個新的類,而不是通過繼承來完成,這樣對原有程序沒有變更,風險最小。

?

(橋梁模式也可以代替繼承來避免風險,使用場景不一樣(裝飾模式著重增強功能),但有點類似)

?

?

五、缺點

多層的裝飾是比較復雜的,如果是最里層的裝飾出現問題,那排查解決的工作量會很大,因此,盡量減少裝飾類的數量,以便降低系統的復雜度。

?

六、與其他模式的區別

1、裝飾模式與代理模式

裝飾模式就是代理模式的一個特殊應用,兩者的共同點是都具有相同的接口,

不同點則是代理模式著重對代理過程的控制,它不對被代理類的功能做任何處理,保證原滋原味的調用。

而裝飾模式則是對類的功能進行加強或減弱,它著重類的功能變化,它不做準入條件判斷和準入參數過濾。

2、裝飾模式與適配器模式

相似的地方:

都是包裝作用,都是通過委托方式實現其功能。

不同點是:

裝飾模式包裝的是自己的兄弟類,隸屬于同一個家族(相同接口或父類),

適配器模式則修改非血緣關系類,把一個非本家族的對象偽裝成本家族的對象,注意是偽裝,因此它的本質還是非相同接口的對象。

具體來說:

1)意圖不同

裝飾模式的意圖是加強對象的功能,

適配器模式關注的則是轉化,它的主要意圖是兩個不同對象之間的轉化

2)施與對象不同

裝飾模式裝飾的對象必須是自己的同宗,也就是相同的接口或父類,只要在具有相同的屬性和行為的情況下,才能比較行為是增強還是減弱;

適配器模式則必須是兩個不同的對象,因為它著重與轉換,只有兩個不同的對象才有轉換的必要。

3)場景不同

裝飾模式在任何時候都可以使用,只要是想增強類的功能,

而適配器模式則是一個補救模式,一般出現在系統成熟或已經構建完畢的項目中,作為一個緊急處理手段采用。

4)擴展性不同

裝飾模式很容易擴展,

但是適配器模式就不同了,它在兩個不同對象之間架起了一座溝通的橋梁,建立容易,去掉就比較困難了,需要從系統整體考慮是否能夠撤銷。

3、裝飾模式與其他包裝模式的區別:

自己不處理讓其他人處理,這種類型的模式定義一個名字,叫做包裝模式。包裝模式包括:裝飾模式、適配器模式、門面模式、代理模式、橋梁模式。

5個包裝模式都是通過委托的方式對一個對象或一系列對象施行包裝,有了包裝,設計的系統才更加靈活、穩定,并且極具擴展性。從實現的角度來看,它們都是代理的一種具體表現形式,它們在使用場景上的區別如下:

1)代理模式主要用在不希望展示一個對象內部細節的場景中,此外,代理模式還可以用在一個對象的訪問需要限制的場景中。

2)裝飾模式是一種特殊的代理模式,它倡導的是在不改變接口的前提下為對象增強功能,或者動態添加額外職責。就擴展性而言,它比子類更靈活。

3)適配器模式的主要意圖是接口轉換,把一個對象的接口轉換成系統希望的另外一個接口。這里的系統指的不僅僅是一個應用,也可能是某個環境,比如通過接口轉換可以屏蔽外界接口,以免外界接口深入系統內部,從而提高系統的穩定性和可靠性

4)橋梁模式是在抽象層產生耦合,解決的是自行擴展的問題,它可以使兩個有耦合關系的對象互不影響地擴展。

5)門面模式是一個粗粒度的封裝,它提供一個方便訪問子系統的接口,不具有任何的業務邏輯,僅僅是一個訪問復雜系統的快速通道,沒有它,子系統照樣運行。

?

轉載于:https://www.cnblogs.com/yuweifeng/p/7241662.html

總結

以上是生活随笔為你收集整理的设计模式之装饰模式20170726的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。