国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

設計模式c#描述——裝飾(Decorator)模

2024-07-21 02:23:43
字體:
來源:轉載
供稿:網友
設計模式c#語言描述——裝飾(decorator)模式



*本文參考了《java與模式》的部分內容,適合于設計模式的初學者。



裝飾模式又名包裝模式,以對客戶端透明的方式擴展對象的功能,是繼承關系的一個替代方案。它使用原來被裝飾的類的一個子類的實例,把客戶端的調用委派到被裝飾類,客戶端并不會覺得對象在裝飾前和裝飾后有什么不同。在以下情況下應使用裝飾模式:需要擴展一個類的功能,或給一個類增加附加責任。動態地給一個對象增加功能,這些功能可以再動態地撤銷。需要增加由一些基本功能的排列組合而產生的非常大量的功能,從而使繼承關系變得不現實。



類圖如下所示:






裝飾模式包括如下角色:

抽象構件(component):給出一個抽象接口,以規范準備接收附加責任的對象。

具體構件(concrete component):定義一個將要接收附加責任的類。

裝飾(decorator):持有一個構件對象的實例,并定義一個與抽象構件接口一致的接口。

具體裝飾(concrete decorator):負責給構件對象“貼上”附加的責任。



component:

public interface component

{

void sampleoperation();

}// end interface definition component



decorator:

public class decorator : component

{

private component component;



public decorator(component component)

{

this.component=component;

}



public virtual void sampleoperation()

{

component.sampleoperation();

}



}// end class definition decorator



concretecomponent:

public class concretecomponent : component

{



public void sampleoperation()

{

console.writeline ("concretecomponent sampleoperation");

}



}// end class definition concretecomponent



concretedecorator1:

public class concretedecorator1 : decorator

{



public concretedecorator1(component component):base(component)

{



}

override public void sampleoperation()

{

base.sampleoperation ();

console.writeline ("concretedecorator1 sampleoperation");

}



}// end class definition concretedecorator1



concretedecorator2:

public class concretedecorator2 : decorator

{

public concretedecorator2 (component component):base(component)

{

}

override public void sampleoperation()

{

base.sampleoperation ();

console.writeline ("concretedecorator2 sampleoperation");

}



}// end class definition concretedecorator2



client:

static void main(string[] args)

{

component component=new concretecomponent ();

component concretedecorator1=new concretedecorator1 (component); // 包裝

component concretedecorator2=new concretedecorator2 (concretedecorator1);



concretedecorator2.sampleoperation ();

}



程序輸出如下:

concretecomponent sampleoperation

concretedecorator1 sampleoperation

concretedecorator2 sampleoperation


收集最實用的網頁特效代碼!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 惠水县| 舟山市| 通城县| 浪卡子县| 桐柏县| 杨浦区| 南昌市| 清徐县| 巨鹿县| 富宁县| 香港| 江津市| 镇江市| 娱乐| 连江县| 新郑市| 沂源县| 乐昌市| 金乡县| 修武县| 监利县| 长泰县| 乌拉特前旗| 桓台县| 仪征市| 屏边| 恩平市| 曲水县| 兖州市| 海晏县| 沙洋县| 万山特区| 双城市| 安岳县| 神木县| 北宁市| 界首市| 樟树市| 澳门| 古蔺县| 兴义市|