如何用C#代碼實(shí)現(xiàn)裝飾器模式

這篇“如何用C#代碼實(shí)現(xiàn)裝飾器模式”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“如何用C#代碼實(shí)現(xiàn)裝飾器模式”文章吧。

創(chuàng)新互聯(lián)專注于撫順網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供撫順營銷型網(wǎng)站建設(shè),撫順網(wǎng)站制作、撫順網(wǎng)頁設(shè)計(jì)、撫順網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造撫順網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供撫順網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

首先肯定是抽象基類。

    public abstract class OurStrategy
    {
        public abstract void Play(string msg);
    }

通常,在上半場,我們一般都使用防守陣型。

    public class OurDefaultStategy : OurStrategy
    {
        public override void Play(string msg)
        {
            Console.WriteLine("上半場4-1-2-1防守陣型");
        }
    }

下半場,會根據(jù)上半場的態(tài)勢而調(diào)整陣型。也就是需要實(shí)現(xiàn)OurStrategy這個(gè)抽象類。不過,先不急,我們還得先抽象出一個(gè)實(shí)現(xiàn)OurStrategy這個(gè)抽象類、充當(dāng)裝飾器的一個(gè)抽象類。

    public abstract class OurDecorator : OurStrategy
    {
        private OurStrategy _ourStrategy;
        public OurDecorator(OurStrategy ourStrategy)
        {
            this._ourStrategy = ourStrategy;
        }
        public override void Play(string msg)
        {
            if (_ourStrategy != null)
            {
                _ourStrategy.Play(msg);
            }
        }
    }

以上,這個(gè)充當(dāng)裝飾器的抽象類,接收某個(gè)實(shí)現(xiàn)OurStrategy抽象基類的子類實(shí)例,并執(zhí)行OurStrategy抽象基類的方法Play。

接下來,實(shí)現(xiàn)OurDecorator這個(gè)充當(dāng)裝飾器的類。

    public class AttackStategy : OurDecorator
    {
        public AttackStategy(OurStrategy ourStrategy) : base(ourStrategy)
        {
            
        }
        public override void Play(string msg)
        {
            base.Play(msg);
            Console.WriteLine("下半場3-1-3-1進(jìn)攻陣型");
        }
    }

以上,當(dāng)然還可以寫出很多OurDecorator的派生類。

客戶端這樣調(diào)用:

    class Program
    {
        static void Main(string[] args)
        {
            OurDecorator ourDecorator = new AttackStategy(new OurDefaultStategy());
            ourDecorator.Play("haha");
            Console.ReadKey();
        }
    }

如何用C#代碼實(shí)現(xiàn)裝飾器模式

以上,通過new AttackStategy(new OurDefaultStategy())把new OurDefaultStategy()實(shí)例賦值給類充當(dāng)裝飾墻的抽象基類OurDecorator的_ourStrategy字段。

當(dāng)執(zhí)行ourDecorator.Play("haha")方法,首先來到AttackStategy的Play方法,執(zhí)行base.Play(msg),這里的base就是AttackStategy的抽象父類OurDecorator,再執(zhí)行OurDecorator的Play方法,由于已經(jīng)給OurDecorator的_ourStrategy字段賦值,_ourStrategy字段存儲的是OurDefaultStategy實(shí)例,所以,base.Play(msg)最終執(zhí)行的是OurDefaultStategy的Play方法,即把"上半場4-1-2-1防守陣型"顯示出來。

最后執(zhí)行AttackStategy的Play方法中的Console.WriteLine("下半場3-1-3-1進(jìn)攻陣型")部分,把"下半場3-1-3-1進(jìn)攻陣型"顯示出來。

以上就是關(guān)于“如何用C#代碼實(shí)現(xiàn)裝飾器模式”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文標(biāo)題:如何用C#代碼實(shí)現(xiàn)裝飾器模式
瀏覽路徑:http://www.muchs.cn/article6/geesog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、軟件開發(fā)、定制網(wǎng)站、服務(wù)器托管商城網(wǎng)站、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)