溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2022-10-21 09:15:41 來(lái)源:億速云 閱讀:100 作者:iii 欄目:編程語(yǔ)言

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

首先肯定是抽象基類。

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

通常,在上半場(chǎng),我們一般都使用防守陣型。

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

下半場(chǎng),會(huì)根據(jù)上半場(chǎng)的態(tài)勢(shì)而調(diào)整陣型。也就是需要實(shí)現(xiàn)OurStrategy這個(gè)抽象類。不過(guò),先不急,我們還得先抽象出一個(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。

接下來(lái),實(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("下半場(chǎng)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)裝飾器模式

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

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

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

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

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI