设计模式之策略模式浅谈以及简单例子

                设计模式之策略模式

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

  策略模式是对算法的包装,是把使用的责任和算法本身分割开来,委派给不同的对象管理。策略模式通常把一个系列的算法包装到一系列的策略类里面,作为一个抽象策略类的子类。

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

环境(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();

        }

    }

 

转载于:https://www.cnblogs.com/wjrelax/p/10076772.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值