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

head first 设计模式(-) 策略模式(鸭子)

时间:2015-04-16 06:39:12      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

 目的:减少依赖

  设计模式对应不同的需求,设计原则则代表永恒的灵魂,在实践中未必时刻遵守,但要时刻牢记。

  1、依赖倒转原则(Dependence Inversion Principle)

  2、接口隔离原则(Interface Segregation Principle)

  3、里氏代换原则(Liskov Substitution Principle)

  4、开闭原则(Open Close Principle)

  5、迪米特法则(Demeter Principle)

  6、合成复用原则(Composite Reuse Principle)

 

1. 继承可以实现代码的重用,但子类不应对父类的方法重载。

 2.将总会变化的部分分离出去另行封装,以便以后可以轻易地改变扩展此部分而不影响其他不需要变化的部分。以组合的方式构成新类。

 

实现鸭子类,将易变更的飞行和鸣叫方法分离出去:使用策略模式 + 简单工厂

接口:

namespace DesignPatterns.Strategy
{
    interface IFlyBehavior
    {
        void Fly(string name);
    }
}

  

namespace DesignPatterns.Strategy
{
    interface IQuackBehavior
    {
        void ToQuack(string name);
    }
}

  

using System;

namespace DesignPatterns.Strategy
{
    abstract class Duck
    {
        private IFlyBehavior _flyBehavior;
        private IQuackBehavior _quackBehavior;
        private string _name;

        /// <summary>
        /// 名字
        /// </summary>
        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._name = value;
            }
        }

        /// <summary>
        /// 飞行方式
        /// </summary>
        public IFlyBehavior FlyBehavior
        {
            set
            {
                this._flyBehavior = value;
            }
        }

        /// <summary>
        /// 鸣叫方式
        /// </summary>
        public IQuackBehavior QuackBehavior
        {
            set
            {
                this._quackBehavior = value;
            }
        }

        /// <summary>
        /// 飞行
        /// </summary>
        public void PerformFly()
        {
            _flyBehavior.Fly(_name);
        }

        /// <summary>
        /// 鸣叫
        /// </summary>
        public void PerformQuack()
        {
            _quackBehavior.ToQuack(_name);
        }

        /// <summary>
        /// 外形
        /// </summary>
        public abstract void Display();

        /// <summary>
        /// 游泳
        /// </summary>
        public void Swim()
        {
            Console.Write(_name +" 下水游泳了!\n");
        }
    }
}

  

//飞行方式

using System;

namespace DesignPatterns.Strategy.FlyStyle
{
    /// <summary>
    /// 不会飞
    /// </summary>
    class FlyNoWay:IFlyBehavior
    {
        private string _name;
        public FlyNoWay(string name)
        {
            this._name = name;
        }
        public void Fly(string name)
        {
            Console.Write(name + " 不会飞!\n");
        }
    }
}

  

using System;

namespace DesignPatterns.Strategy.FlyStyle
{
    /// <summary>
    /// 用翅膀飞
    /// </summary>
    class FlyWithWings : IFlyBehavior
    {        
        private string _name;
        public FlyWithWings(string name)
        {
            this._name = name;
        }
        public void Fly(string name)
        {
            Console.Write(name + " 扇着翅膀飞起来了!\n");
        }
    }
}

  

namespace DesignPatterns.Strategy.FlyStyle
{
    class FlyFactory
    {
        public enum FlyStyles
        {
            Fly,
            CanNotFly
        }
        static public IFlyBehavior GetFlyType(string name, FlyStyles flyStyles)
        {
            if (flyStyles == FlyStyles.Fly)
            {
                //会飞
                return new FlyWithWings(name);
            }
            else
            {
                //不会飞
                return new FlyNoWay(name);
            }
        }
    }
}

  

//鸣叫方式

using System;

namespace DesignPatterns.Strategy.QuackStyle
{
    /// <summary>
    /// 不会叫
    /// </summary>
    class MuteQuack : IQuackBehavior
    {
        private string _name;
        public MuteQuack(string name)
        {
            this._name = name;
        }
        public void ToQuack(string name)
        {
            Console.Write(name + " 不会叫!\n");
        }
    }
}

  

using System;

namespace DesignPatterns.Strategy.QuackStyle
{
    /// <summary>
    /// 吱吱叫
    /// </summary>
    class Squeak : IQuackBehavior
    {
         private string _name;
         public Squeak(string name)
        {
            this._name = name;
        }
         public void ToQuack(string name)
        {
            Console.Write(name + " 吱吱叫!\n");
        }
    }
}

  

using System;

namespace DesignPatterns.Strategy.QuackStyle
{
    /// <summary>
    /// 呱呱叫
    /// </summary>
    class Quack : IQuackBehavior
    {
        private string _name;
        public Quack(string name)
        {
            this._name = name;
        }
        public void ToQuack(string name)
        {
            Console.Write(name + " 呱呱叫!\n");
        }
    }
}

  

namespace DesignPatterns.Strategy.QuackStyle
{
    class QuackFactory
    {
        public enum QuackStyles
        {
            MuteQuack,  //不会叫
            Squeak,     //吱吱叫
            Quack       //呱呱叫
        }

        static public IQuackBehavior GetQuackType(string name, QuackStyles quackStyles)
        {
            if (quackStyles == QuackStyles.MuteQuack)
            {
                return new MuteQuack(name);
            }
            else if (quackStyles == QuackStyles.Squeak)
            {
                return new Squeak(name);
            }
            else
            {
                return new Quack(name);
            }
        }     
    }
}

  

//鸭子类

using System;
using DesignPatterns.Strategy.FlyStyle;
using DesignPatterns.Strategy.QuackStyle;
namespace DesignPatterns.Strategy.DuckModel
{
    class BlueDuck : Duck
    {
        public BlueDuck(string name)
        {
            this.Name = name;
            this.FlyBehavior = FlyFactory.GetFlyType(name, FlyFactory.FlyStyles.CanNotFly);
            this.QuackBehavior = QuackFactory.GetQuackType(name, QuackFactory.QuackStyles.MuteQuack);
            Display();
        }
        public override void Display()
        {
            Console.Write(this.Name + " 有蓝色的羽毛!\n");
        }
    }
}

  

using System;
using DesignPatterns.Strategy.FlyStyle;
using DesignPatterns.Strategy.QuackStyle;

namespace DesignPatterns.Strategy.DuckModel
{
    class RedDuck : Duck
    {
        public RedDuck(string name)
        {
            this.Name = name;
            this.FlyBehavior = FlyFactory.GetFlyType(name, FlyFactory.FlyStyles.CanNotFly); 
            this.QuackBehavior = QuackFactory.GetQuackType(name, QuackFactory.QuackStyles.Squeak);
            Display();
        }
        public override void Display()
        {
            Console.Write(this.Name + " 有红色的羽毛!\n");
        }
    }
}

  

using System;
using DesignPatterns.Strategy.FlyStyle;
using DesignPatterns.Strategy.QuackStyle;

namespace DesignPatterns.Strategy.DuckModel
{
    class YellowDuck : Duck
    {
        public YellowDuck(string name)
        {
            this.Name = name;
            this.FlyBehavior = FlyFactory.GetFlyType(name, FlyFactory.FlyStyles.Fly);
            this.QuackBehavior = QuackFactory.GetQuackType(name, QuackFactory.QuackStyles.Quack);
            Display();
        }
        public override void Display()
        {
            Console.Write(this.Name + " 有黄色的羽毛!\n");
        }
    }
}

  

namespace DesignPatterns.Strategy.DuckModel
{
    class DuckFactory
    {
        public enum DuckTypes
        {
            Red,
            Yellow,
            Blue
        }
        static public Duck GetOneDuck(string name, DuckTypes duckTypes)
        {
            if (duckTypes == DuckTypes.Red)
            {
                return new RedDuck(name);
            }
            else if (duckTypes == DuckTypes.Yellow)
            {
                return new YellowDuck(name);
            }
            else
            {
                return new BlueDuck(name);
            }
            
        }
    }
}

  

//测试

using System;
using DesignPatterns.Strategy;
using DesignPatterns.Strategy.DuckModel;
using DesignPatterns.Strategy.FlyStyle;
using DesignPatterns.Strategy.QuackStyle;
namespace DesignPatterns
{
    class Program
    {
        static void Main(string[] args)
        {
            Duck red = DuckFactory.GetOneDuck("红毛鸭子",DuckFactory.DuckTypes.Red);
            red.PerformFly();
            red.PerformQuack();
            red.Swim();

            Console.Write("\n");
            red.FlyBehavior = FlyFactory.GetFlyType(red.Name, FlyFactory.FlyStyles.Fly);
            red.QuackBehavior = QuackFactory.GetQuackType(red.Name, QuackFactory.QuackStyles.Quack);
            red.PerformFly();
            red.PerformQuack();
            Console.Write("\n");

            Duck yellow = DuckFactory.GetOneDuck("黄毛鸭子", DuckFactory.DuckTypes.Yellow);
            yellow.PerformFly();
            yellow.PerformQuack();
            yellow.Swim();
            Console.Write("\n");

            Duck blue = DuckFactory.GetOneDuck("蓝毛鸭子", DuckFactory.DuckTypes.Blue);
            blue.PerformFly();
            blue.PerformQuack();
            blue.Swim();
            Console.ReadKey();

        }
    }
}

 

 

head first 设计模式(-) 策略模式(鸭子)

标签:

原文地址:http://www.cnblogs.com/wj033/p/4430791.html

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