码迷,mamicode.com
首页 > 其他好文 > 详细

《Head First 设计模式》之策略模式

时间:2014-06-12 14:22:35      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   http   com   get   

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

设计原则

1.找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起。

2.针对接口编辑,而不是针对实现编程。

3.多用组合,少用继承。

 

UML类图

bubuko.com,布布扣

代码:

/// <summary>
    /// abstract base class of Ducks
    /// </summary>
    public abstract class Duck
    {
        protected IFlyBehavior flyBehavior;
        protected IQuackBehavior quackBehavior;
       
        public IQuackBehavior QuackBehavior
        {
            get
            {
                return quackBehavior;
            }
            set
            {
                quackBehavior = value;
            }
        }

        public IFlyBehavior FlyBehavoir
        {
            get
            {
                return flyBehavior;
            }
            set
            {
                flyBehavior = value;
            }
        }

       
        public abstract object Display();

        public object PerformFly()
        {
            return FlyBehavoir.Fly();
        }

        public object PerformQuack()
        {
            return QuackBehavior.Quacking();
        }

        public string Swim()
        {
            return "All ducks float, even decoys!";
        }
    }

/// <summary>
    /// MallardDuck.
    /// </summary>
    public class MallardDuck : Duck
    {
        public MallardDuck()
        {
            quackBehavior = new Quack();
            flyBehavior = new FlyWithWings();
        }

        public override object Display()
        {
            return "I‘m a real Mallard duck";
           
        }
    }

/// <summary>
    /// RedheadDuck
    /// </summary>
    public class RedheadDuck: Duck
    {
        public RedheadDuck()
        {
            quackBehavior = new Quack();
            flyBehavior = new FlyWithWings();
        }
       
        public override object Display()
        {
            return "I‘m a Redhead duck";
        }
    }

/// <summary>
/// RubberDuck
/// </summary>
public class RubberDuck : Duck
{
    public RubberDuck()
    {
        quackBehavior = new Squeak();
        flyBehavior = new FlyNoWay();
    }
   
    public override object Display()
    {
        return "I‘m a rubber duck";
    }
}

/// <summary>
/// ModelDuck Object
/// </summary>
public class ModelDuck : Duck
{
    public ModelDuck()
    {
        flyBehavior = new FlyNoWay();
        quackBehavior = new Quack();
    }

    public override object Display()
    {
        return "I‘m a model duck";
    }

}

/// <summary>
    /// DecoyDuck
    /// </summary>
    public class DecoyDuck: Duck
    {
        public DecoyDuck()
        {
            quackBehavior = new Quack();
            flyBehavior = new FlyWithWings();
        }
       
        public override object Display()
        {
            return "I‘m a Decoy duck";
        }
    }

/// <summary>
/// IFlyBehavior interface for flying behaviors
/// </summary>
public interface IFlyBehavior
{
    object Fly();
}

/// <summary>
    /// FlyWithWings
    /// </summary>
    public class FlyNoWay : IFlyBehavior
    {
        public FlyNoWay()
        {}

        #region IFlyBehavior Members

        public object Fly()
        {
            return "I can‘t fly.";
        }

        #endregion
    }

/// <summary>
    /// FlyWithWings
    /// </summary>
    public class FlyWithWings : IFlyBehavior
    {
        public FlyWithWings()
        {}

        #region IFlyBehavior Members

        public object Fly()
        {
            return "I‘m flying!!";
        }

        #endregion
    }

/// <summary>
    /// FlyRocketPowered
    /// </summary>
    public class FlyRocketPowered : IFlyBehavior
    {
        #region IFlyBehavior Members

        public object Fly()
        {
            return "I‘m flying with a rocket!";
        }

        #endregion
    }

/// <summary>
    /// IQuackBehavior interface for Quacking behaviors
    /// </summary>
    public interface IQuackBehavior
    {
        string Quacking();
    }

/// <summary>
    /// Quack
    /// </summary>
    public class Quack : IQuackBehavior
    {
        public string Quacking()
        {
            return "Quack";
        }
    }

/// <summary>
    /// Squeak
    /// </summary>
    public class Squeak : IQuackBehavior
    {
        public string Quacking()
        {
            return "Squeak";
        }
    }

/// <summary>
    /// MuteQuack
    /// </summary>
    public class MuteQuack : IQuackBehavior
    {
        public string Quacking()
        {
            return "MuteQuack";
        }
    }

/// <summary>
    /// 入口类
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Duck mallard = new MallardDuck();
            Console.WriteLine(mallard.PerformFly());
            Console.WriteLine(mallard.PerformQuack());

            Duck model = new ModelDuck();
            Console.WriteLine(model.PerformFly());
            Console.WriteLine(model.PerformQuack());

            //动态改变飞行行为
            model.FlyBehavoir = new FlyRocketPowered();
            Console.WriteLine(model.PerformFly());
            Console.ReadKey();
        }
    }

Strategy.Duck源代码

《Head First 设计模式》之策略模式,布布扣,bubuko.com

《Head First 设计模式》之策略模式

标签:style   class   blog   http   com   get   

原文地址:http://www.cnblogs.com/JaiHome/p/3782332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!