策略模式示例 -- C#语言

定义

  • 定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。

UML图

代码

  • 为了方便写到了一起,实际开发中类要单独页面写。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pattern_Duck
{
    class Program
    {
        static void Main(string[] args)
        {
            Duck mallard = new MallardDuck();
            mallard.PerformFly();
            mallard.PerformQuack();
            mallard.Display();

            Console.WriteLine("___________");
            Duck model = new ModelDuck();
            model.PerformFly();
            model.SetIFlyBehavior(new FlyRocketPowered());
            model.PerformFly();
            Console.ReadKey();
        }
    }

    //抽象基类:鸭子
    public abstract class Duck
    {
        //为行为类型接口类型声明两个引用变量
        public IFlyBehavior flyBehavior;
        public IQuackBehavior quackBehavior;
        public Duck()
        {
        }
        public abstract void Display();
        public void PerformFly()
        {
            flyBehavior.Fly();
        }

        public void PerformQuack()
        {
            quackBehavior.Quack();
        }
        public void Swim()
        {
            Console.WriteLine("All ducks float, even decoys!");
        }

        //设定方法,可以在鸭子子类中通过设定方法来设定鸭子的行为,而不是在鸭子的构造器内实例化
        public void SetIFlyBehavior(IFlyBehavior fb)
        {
            flyBehavior = fb;
        }

        public void SetIQuackBehavior(IQuackBehavior qb)
        {
            quackBehavior = qb;
        }
    }
    

    //行为飞的接口
    public interface IFlyBehavior
    {
        void Fly();
    }
    //继承飞的接口的飞的类1
    public class FlyWithWings : IFlyBehavior
    {
        public void Fly()
        {
            Console.WriteLine("I'm flying!");
        }
    }
    //继承飞的接口的飞的类2
    public class FlyNoWay : IFlyBehavior
    {
        public void Fly()
        {
            Console.WriteLine("I can't fly!");
        }
    }
    //继承飞的接口的飞的类3
    public class FlyRocketPowered : IFlyBehavior
    {
        public void Fly()
        {
            Console.WriteLine("I'm flying with a rocket!");
        }
    }
    //行为叫的接口
    public interface IQuackBehavior
    {
        void Quack();
    }
    //叫的类1
    public class SQuack : IQuackBehavior
    {
        public void Quack()
        {
            Console.WriteLine("Quack!");
        }
    }
    //叫的类2
    public class MuteQuack : IQuackBehavior
    {
        public void Quack()
        {
            Console.WriteLine("Silence");
        }
    }
    //叫的类3
    public class Squeak : IQuackBehavior
    {
        public void Quack()
        {
            Console.WriteLine("Squeak");
        }
    }


    //继承鸭子类的绿头鸭类
    public class MallardDuck : Duck
    {
        public MallardDuck()
        {
            quackBehavior = new SQuack();
            flyBehavior = new FlyWithWings();
        }

        public override void Display()
        {
            Console.WriteLine("I'm a real Mallard duck!");
        }
    }

    //继承鸭子类的模型鸭类
    public class ModelDuck : Duck
    {
        public ModelDuck()
        {
            flyBehavior = new FlyNoWay();
            quackBehavior = new SQuack();
        }

        public override void Display()
        {
            Console.WriteLine("I'm a model duck!");
        }
    }
}

posted @ 2019-11-20 14:59  爱学习的刘某某  阅读(162)  评论(0编辑  收藏  举报