欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
C#設計模式系列:裝飾模式(Decorator)

1. 裝飾模式簡(jiǎn)介

  裝飾模式動(dòng)態(tài)地給一個(gè)對象添加額外的職責。例如一幅畫(huà)有沒(méi)有畫(huà)框都可以?huà)煸趬ι?,?huà)就是被裝飾者。但是通常都是有畫(huà)框的。在掛在墻上之前,畫(huà)可以被蒙上玻璃,裝到框子里,所以在畫(huà)上加一層畫(huà)框,并把它們組合成一個(gè)整體——有框的畫(huà)。這樣隨著(zhù)不斷有新的裝飾的加入,就給商品不斷地打上包裝,變成一個(gè)功能更讓人滿(mǎn)意的商品。這種不斷打包裝的過(guò)程就是裝飾。

1.1 定義

  裝飾模式提供了一種給類(lèi)增加功能的方法。它通過(guò)動(dòng)態(tài)地組合對象,可以給原有的類(lèi)添加新的代碼,而無(wú)須修改現有代碼。因此引入bug或產(chǎn)生意外副作用的機會(huì )將大幅度減少。

1.2 使用頻率

  

中等

2. 裝飾模式結構圖

2.1 結構圖

2.2 參與者

  裝飾模式參與者:

   Component:定義一個(gè)對象接口,可以給這些對象動(dòng)態(tài)地添加職責

   ConcreteComponent:定義一個(gè)對象,可以給這個(gè)對象添加一些職責

   Decorator:維持一個(gè)指向Component的指針,并定義一個(gè)與Component接口一致的接口

   ConcreteDecorator:負責向ConcreteComponent添加功能

  在裝飾模式中,Decorator定義了一個(gè)裝飾接口類(lèi)。因為Decorator與ConcreteComponent繼承同一個(gè)接口,所以繼承Decorator的類(lèi)ConcreteDecorator可以使用ConcreteComponent的方法,再在ConcreteDecorator里面加入一些新的方法,也就是裝飾,就成為了一個(gè)包裝好的裝飾類(lèi)。

3、裝飾模式結構實(shí)現

  

  Component.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPatterns.DecoratorPattern.Structural{    public abstract class Component    {        public abstract void Operation();    }}

  ConcreteComponent.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPatterns.DecoratorPattern.Structural{    public class ConcreteComponent : Component    {        public override void Operation()        {            Console.WriteLine("ConcreteComponent.Operation()");        }    }}

  Decorator.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPatterns.DecoratorPattern.Structural{    public abstract class Decorator : Component    {        protected Component component;        public void SetComponent(Component component)        {            this.component = component;        }                public override void Operation()        {            if (component != null)            {                component.Operation();            }        }    }}

  ConcreteDecoratorA.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPatterns.DecoratorPattern.Structural{    public class ConcreteDecoratorA : Decorator    {        public override void Operation()        {            base.Operation();            Console.WriteLine("ConcreteDecoratorA.Operation()");        }    }}

  ConcreteDecoratorB.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPatterns.DecoratorPattern.Structural{    public class ConcreteDecoratorB : Decorator    {        public override void Operation()        {            base.Operation();            AddedBehavior();            Console.WriteLine("ConcreteDecoratorB.Operation()");        }        void AddedBehavior()        {        }    }}

  Program.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using DesignPatterns.DecoratorPattern.Structural;namespace DesignPatterns.DecoratorPattern{    class Program    {        static void Main(string[] args)        {            // Create ConcreteComponent and two Decorators            ConcreteComponent c = new ConcreteComponent();            ConcreteDecoratorA d1 = new ConcreteDecoratorA();            ConcreteDecoratorB d2 = new ConcreteDecoratorB();            // Link decorators            d1.SetComponent(c);            d2.SetComponent(d1);            d2.Operation();        }    }}

  運行輸出:

ConcreteComponent.Operation()ConcreteDecoratorA.Operation()ConcreteDecoratorB.Operation()請按任意鍵繼續. . .




4、裝飾模式應用分析

  裝飾模式適用情形:

   在不影響其他對象的情況下,以動(dòng)態(tài)、透明的方式給單個(gè)對象添加職責

   處理那些可以撤銷(xiāo)的職責

  裝飾模式的特點(diǎn):

   比靜態(tài)類(lèi)更靈活。使用裝飾模式可以很容易地向對象添加職責的方式??梢杂锰砑雍头蛛x的方法,對裝飾在運行時(shí)添加和刪除職責。相比之下,繼承機制要求為每個(gè)添加的職責創(chuàng )建一個(gè)新的子類(lèi)。這會(huì )產(chǎn)生很多新的類(lèi),并會(huì )增加系統的復雜度。

   使用裝飾模式可以很容易地重復添加一個(gè)特性,而兩次繼承特性類(lèi)則極容易出錯。

   為了避免處理頂層的類(lèi)有太多的特征。裝飾模式下,你可以定義一個(gè)簡(jiǎn)單的類(lèi),并用裝飾類(lèi)給它逐漸地添加功能。這樣可以從簡(jiǎn)單的部件組合出復雜的功能,具有低依賴(lài)性和地復雜性。

   有許多小對象。采用裝飾模式進(jìn)行系統設計往往會(huì )產(chǎn)生許多看上去類(lèi)似的小對象,盡管對于了解這些系統的人來(lái)說(shuō),很容易進(jìn)行定制,但是很難學(xué)習這些系統,排錯很惡化呢困難。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
裝飾模式(Decorator)解析例子
設計模式(1)裝飾模式總結
裝飾模式(Decorator Pattern)
C# 設計模式系列教程-裝飾模式
擴展系統功能——裝飾模式(四)
Java SE的裝飾模式
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久