{C#}重写还是策略注入

2023-08-06  本文已影响0人  码农猫爸

汇总

抽象方法

using static System.Console;

namespace OverrideMethod
{
    abstract class AbstractAninal
    {
        // 无默认行为,全部子类中重写
        public abstract void Do();
    }

    class AbstractBird : AbstractAninal
    {
        public override void Do() => WriteLine("I am a bird");
    }

    class AbstractFish : AbstractAninal
    {
        public override void Do() => WriteLine("I am a fish");
    }
}

虚拟方法

using static System.Console;

namespace OverrideMethod
{
    class VirtualAninal
    {
        // 默认行为,子类按需重写
        public virtual void Do() => WriteLine("I am a bird");
    }

    class VirtualBird : VirtualAninal
    {
        // 采用默认行为,不必重写
    }

    class VirtualFish1 : VirtualAninal
    {
        // 重写非默认行为
        public override void Do() => WriteLine("I am a fish");
    }

    class VirtualFish2 : VirtualAninal
    {
        // 重写非默认行为
        // 但与VirtualFish1重复
        public override void Do() => WriteLine("I am a fish");
    }
}

虚拟方法中共用代码

using static System.Console;

namespace OverrideMethod
{
    class ReadyVirtualAninal
    {
        // 默认行为,子类按需重写
        public virtual void Do() => WriteLine("I am a bird");

        // 仅限鱼子类使用,产生混淆
        protected void DoLikeAFish()=> WriteLine("I am a fish");
    }

    class ReadyVirtualBird : ReadyVirtualAninal
    {
        // 采用默认行为,不必重写
    }

    class ReadyVirtualFish1 : ReadyVirtualAninal
    {
        // 借用基类行为
        public override void Do() => DoLikeAFish();
    }

    class ReadyVirtualFish2 : ReadyVirtualAninal
    {
        // 借用基类行为
        public override void Do() => DoLikeAFish();
    }
}

策略注入

using static System.Console;

namespace OverrideMethod
{
    // 引入策略接口,解决具体策略的重复使用问题
    interface IStrategy
    {
        void Do();
    }

    class BirdStrategy : IStrategy
    {
        public void Do() => WriteLine("I am a bird");
    }

    class FishStrategy : IStrategy
    {
        public void Do() => WriteLine("I am a fish");
    }

    class StrategyAninal
    {
        private readonly IStrategy _strategy;

        // 内联策略改为注入
        public StrategyAninal(IStrategy s) => _strategy = s;

        public void Do() => _strategy.Do();
    }

    class StrategyBird : StrategyAninal
    {
        public StrategyBird(IStrategy s) : base(s) { }
    }

    class StrategyFish1 : StrategyAninal
    {
        public StrategyFish1(IStrategy s) : base(s) { }
    }

    class StrategyFish2 : StrategyAninal
    {
        public StrategyFish2(IStrategy s) : base(s) { }
    }
}

客户代码

namespace OverrideMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            var abstractFish = new AbstractFish();
            abstractFish.Do();

            var virtualFish = new VirtualFish2();
            virtualFish.Do();

            var readyFish = new ReadyVirtualFish2();
            readyFish.Do();

            var strategyFish = new StrategyFish2(new FishStrategy());
            strategyFish.Do();
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读