Monday, March 30, 2015

左腳踝

近五年受限於左腳踝的傷勢,運動約 1 - 2 個月,左腳踝就會開始疼痛,看骨科都得到相同結果,骨頭沒問題,好好休息一段時間就好,Orz

今年比較特別,跑了整個三月份的周末(5 週),左腳踝竟然都沒有疼痛感,自己都嚇了一跳,已往經驗是跑步跑 15 -20 圈這個坎棧,腳踝就會疼痛,繼續觀察
IMG_1732

Monday, March 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

Monday, March 16, 2015

[DP] 策略模式

了解策略模式時,發現這段 Youtube 教學影片,是利用折扣來說明,那就拿折扣來練習。

[Design Patern] 策略模式-1

ShoppingMall - Mall Class
namespace ShoppingMall
{
    public class Mall
    {
        public string MallName { get; set; }
        public IDiscount DiscountBehavor { get; set; }

        public decimal calPrice(decimal BillTotal)
        {
            return DiscountBehavor.Discount(BillTotal);
        }
    }

    public class Disc50 : IDiscount
    {
        public decimal Discount(decimal BillTotal)
        {
            return Math.Round(BillTotal * 5 / 10, 0, MidpointRounding.AwayFromZero);
        }

        public string Descript
        {
            get { return "5 折"; }
        }
    }

    public class Disc88 :IDiscount
    {
        public decimal Discount(decimal BillTotal)
        {
            return Math.Round(BillTotal * 88 / 100, 0, MidpointRounding.AwayFromZero);
        }

        public string Descript
        {
            get { return "88 折"; }
        }
    }

    public class Coupon1000 : IDiscount
    {
        public string Descript
        {
            get { return "買萬送千"; }
        }

        public decimal Discount(decimal BillTotal)
        {
            if (BillTotal > 10000)
            BillTotal -= 1000;
            return BillTotal;
        }
    }

    public interface IDiscount
    {
        string Descript { get; }
        decimal Discount(decimal BillTotal);
    }
}
ShoppingMallDemo - Web.Config
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <appSettings>
    <add key="discount" value="ShoppingMall.Coupon1000,ShoppingMall"/>
  </appSettings>
</configuration>

ShoppingMallDemo - cs 檔
using ShoppingMall;

public partial class _Default : System.Web.UI.Page
{
    protected void btnCal_Click(object sender, EventArgs e)
    {

        Mall mall = new Mall();
        IDiscount disc = GetDiscount();
        mall.DiscountBehavor = disc;

        lblPay.Text = mall.calPrice(Convert.ToDecimal(txtBillTotal.Text)).ToString();
        lblDisc.Text = disc.Descript;
    }

    IDiscount GetDiscount()
    {
        string setting = System.Configuration.ConfigurationManager.AppSettings["discount"];
        string[] arr = setting.Split(',');
        string dllName = arr[1];
        string className = arr[0];
        IDiscount disc = (IDiscount)System.Reflection.Assembly.Load(dllName).CreateInstance(className);

        return disc;
    }
}
[Design Patern] 策略模式-2

Monday, March 09, 2015

[C#] Event 練習

利用最低薪資(假設是 20,000)來練習建立 class 的 event

[C#] Event 練習-1

Saturday, March 07, 2015

0Y9M - 童心未泯

一個硯晡來晡去就算了,現在再加一個模仿者,哈 ~~

IMG_2637