1. 程式人生 > >設計模式之策略模式淺談以及簡單例子

設計模式之策略模式淺談以及簡單例子

null 引用 == names bsp args cto 封裝 方法

設計模式之策略模式

策略模式定義了算法類,分別封裝起來,讓他們之間可以相互替換,此模式讓算法的變化獨立於使用算法的客戶。

策略模式是對算法的包裝,是把使用的責任和算法本身分割開來,委派給不同的對象管理。策略模式通常把一個系列的算法包裝到一系列的策略類裏面,作為一個抽象策略類的子類。

策略模式涉及到三個角色:

環境(Context)角色:持有一個Strategy的引用,也稱策略上下文。

抽象策略(Strategy)角色:這是一個抽象角色,通常使用抽象類或者接口來實現。此角色給出所有的具體策略類所需要的接口。

具體策略(ConcreteStrategy)角色:

此角色包裝了所有的算法和行為。

Eg商場搞促銷,促銷方式有打折、滿10050等。

在本例中,抽象策略角色用一個結果實現,接口中有一個計算價格的方法。接口實現如下:

 1 namespace Strategy_Pattern
 2 
 3 {
 4 
 5     /// <summary>
 6 
 7     /// 策略接口
 8 
 9     /// </summary>
10 
11     interface IPromotion
12 
13     {
14 
15         /// <summary>
16 
17         /// 根據原價和策略計算新價格
18 19 /// </summary> 20 21 /// <param name="originalPrice">原價</param> 22 23 /// <returns></returns> 24 25 double GetPrice(double originalPrice); 26 27 } 28 29 }

具體策略角色有兩個,分別表示打折類和滿100減50,都實現策略接口。

打折類實現如下:

 1 using System;
 2 
 3  
 4 
 5 namespace Strategy_Pattern
6 7 { 8 9 /// <summary> 10 11 /// 打折策略類 12 13 /// </summary> 14 15 class Discount : IPromotion 16 17 { 18 19 20 21 22 23 #region Public Methods 24 25 public double GetPrice(double originalPrice) 26 27 { 28 29 Console.WriteLine("打八折"); 30 31 return originalPrice * 0.8; 32 33 } 34 35 #endregion 36 37 38 39 } 40 41 }

滿100減50類實現如下:

/// <summary>

    /// 返現策略類:滿100返50

    /// </summary>

    class MoneyBack : IPromotion

    {

      

        #region Public Methods

        public double GetPrice(double originalPrice)

        {

            Console.WriteLine("滿100返50");

            return originalPrice - (int)originalPrice / 100 - 50;

        }

        #endregion

 

    }

環境(Context)角色類如下:

/// <summary>

    /// 策略上下文類

    /// </summary>

    class PromotionContext

    {

        #region Fields

        private IPromotion m_promotion = null;

        #endregion

 

        #region Constructors

        public PromotionContext(IPromotion iPromotion)

        {

            this.m_promotion = iPromotion;

        }

        #endregion

 

        #region Public Methods

        /// <summary>

        /// 默認策略方法

        /// </summary>

        /// <param name="originalPrice"></param>

        /// <returns></returns>

        public double GetPrice(double originalPrice)

        {

            if (this.m_promotion == null)

            {

                this.m_promotion = new Discount();

            }

 

            return this.m_promotion.GetPrice(originalPrice);

        }

        /// <summary>

        /// 更改策略的方法

        /// </summary>

        /// <param name="iPromotion"></param>

        public void ChangePromotion(IPromotion iPromotion)

        {

            this.m_promotion = iPromotion;

        }

        #endregion

 

    }

然後再主類中調用相應的策略,主類實現如下:

class Program

    {

        static void Main(string[] args)

        {

            //默認策略:打八折的策略

            PromotionContext promotionContext=new PromotionContext(null);

            Console.WriteLine(promotionContext.GetPrice(300));

 

            //更改策略:滿100減50的策略

            promotionContext.ChangePromotion(new MoneyBack());

            Console.WriteLine(promotionContext.GetPrice(100));

            Console.ReadLine();

        }

    }

設計模式之策略模式淺談以及簡單例子