星期一, 3月 23, 2015

[DP] 策略模式

根據 Head First Design Pattern 書本內容,來練習策略模式

[DP] 策略模式-HF-1

HeadFirst Project - Dock Class
namespace HeadFirst
{
    #region Dock 相關
    public class Dock
    {
        IFlyBehaivor flyBehavior;

        public void performFly()
        {
            flyBehavior.fly();
        }

        public void setFly(IFlyBehaivor fb)
        {
            flyBehavior = fb;
        }

        IQuackBehavior quackBehavior;
        public void performQuck()
        {
            quackBehavior.quack();
        }

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

        // 預設全部的 dock 都不會飛也不會叫
        public Dock()
        {
            setFly(new FlyNoWay());
            setQuack(new MuteQuack());
        }
    }

    public class MallardDuck : Dock
    {
        // 綠頭鴨預設值
        public MallardDuck()
        {
            setFly(new FlyWithWings());
            setQuack(new Quack());
        }
    }

    public class ModelDock : Dock
    {
        // 建構子用 Dock 預設值
    }
    #endregion

    #region Quack 相關
    public class MuteQuack : IQuackBehavior
    {
        public void quack()
        {
            Console.WriteLine("<< Silence >>");
        }
    }

    public class Squeak : IQuackBehavior
    {
        public void quack()
        {
            Console.WriteLine("Squeak");
        }
    }

    public class Quack : IQuackBehavior
    {
        public void quack()
        {
            Console.WriteLine("Quack");
        }
    }

    public interface IQuackBehavior
    {
        void quack();
    } 
    #endregion

    #region Fly 相關
    public class FlyRockPowered : IFlyBehaivor
    {
        public void fly()
        {
            Console.WriteLine("I'm flying with a rocket");
        }
    }

    public class FlyNoWay : IFlyBehaivor
    {
        public void fly()
        {
            Console.WriteLine("I can't fly");
        }
    }

    public class FlyWithWings : IFlyBehaivor
    {
        public void fly()
        {
            Console.WriteLine("I'm flying!!");
        }
    }

    public interface IFlyBehaivor
    {
        void fly();
    }  
    #endregion
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <appSettings>
        <add key ="FlyRockPowered" value="HeadFirst.FlyRockPowered,HeadFirst"/>
    </appSettings>
</configuration>
HeadFirstDemo Project - Program.cs
using HeadFirst;

namespace HeadFirstDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("===== 綠頭鴨測試 =====");
            Dock mallard = new MallardDuck();
            mallard.performFly();
            mallard.performQuck();

            Console.WriteLine("===== 模型鴨測試 =====");
            Dock model = new ModelDock();

            Console.WriteLine("===== 原始能力 =====");
            model.performFly() ;
            model.performQuck();

            Console.WriteLine("===== HeadFirst 設定 =====");
            model.setFly(new FlyRockPowered());
            model.performFly() ;

            Console.WriteLine("===== 遠距教學設定 =====");
            model.setFly(GetFlyBehavior());
            model.performFly();
        }

        static IFlyBehaivor GetFlyBehavior()
        {
            string setting = System.Configuration.ConfigurationSettings.AppSettings["FlyRockPowered"];
            string[] arr = setting.Split(',');
            string dllName = arr[1];
            string className = arr[0];
            IFlyBehaivor fb = (IFlyBehaivor)System.Reflection.Assembly.Load(dllName).CreateInstance(className);
            return fb;
        }
    }
}
[DP] 策略模式-HF-2

沒有留言:

張貼留言