最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#設(shè)計(jì)模式之裝飾器模式實(shí)例詳解

 更新時(shí)間:2022年10月02日 10:23:36   作者:Darren?Ji  
本文詳細(xì)講解了C#設(shè)計(jì)模式之裝飾器模式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

最近踢了場(chǎng)球,9人制比賽,上半場(chǎng)我們采用防守陣型效果不佳,下半場(chǎng)采用進(jìn)攻陣型取得了比賽的主動(dòng)。我們上下半場(chǎng)所采取的策略,似乎可以用"裝飾器"模式實(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();
        }
    }

以上,

通過(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)。

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

最新評(píng)論

游戏| 房山区| 金昌市| 乐山市| 肇庆市| 扬中市| 六枝特区| 安多县| 新民市| 甘洛县| 绥棱县| 南漳县| 天水市| 阿尔山市| 永城市| 新源县| 锡林浩特市| 湛江市| 漾濞| 绍兴市| 高州市| 新巴尔虎左旗| 海林市| 伊川县| 南乐县| 黄梅县| 和顺县| 承德县| 鹿泉市| 兰坪| 连平县| 蒲江县| 衡东县| 游戏| 浮梁县| 望奎县| 平塘县| 五寨县| 台前县| 南江县| 枣阳市|