设计模式中的策略模式与状态模式

开发 架构
在软件设计模式中有两种模式很相似,它们就是状态模式和策略模式。本文将为您分析他们的优缺点。

使用场景

状态模式:当对象的行为随对象的状态的改变而改变时,我们为了解耦多重判断条件,封装行为的变化,可以定义一个抽象的状态类,提供对象行为接口。具体与状态相关的行为,由它的子类去实现。

策略模式:“策略”二字等同于算法,当现实系统中需要对算法动态指定,并且可以互相替换,可以抽象出算法的调用接口,具体的算法实现由具体的策略角色去实现,根据里氏替换原则,任何出现父类的地方都可以使用它的子类去替换,这样符合我们的业务需求。

比较

虽然两者都是抽象出父类规范调用接口,具体的行为由子类实现,环境对象同时包含父类的引用,但是这两者模式应用的场景完全不同。例如:你去ATM机取款,如果你的账户处于冻结状态,就不能正常取款。这里你的银行账户至少有两个状态就是冻结与非冻结。这样的业务解决方案你应该不会想到用策略模式去解决。又如商场打折,有很多打折的策略,儿童用品打7折,老人用品打5折,这个问题不会跟状态模式扯上关系吧,这里商品你不能说有老人状态和儿童状态!

ATM取款案例分析

设计到的角色:取款人、账户、ATM机。

用例图

这边简单起见,就分析取款用例。

基本事件路径

(1) 取款人插入银行卡,输入密码;

(2) 选择取款操作,输入取款金额;

(3) 等待出钞,取款。

在这个用例中,如果取款人账户已冻结,就发生一个可选事件路径,取款用例就被终止。

关键的业务需求是用户取款事实,所以领域建模可以从标识取款和取款人这两个类开始。这边考虑到取款人不仅有取款这个用例,我们将取款泛化成交易。

建模

用例实化

取款:基本事件路径的实化。

细化领域模型

这边引入了ATMSystem这个边界对象作为控制器负责与用户交互(与用户交互的ATM操作界面),Trade是负责处理用户交易的类。

取款人插入银行卡输入密码,ATMSystem负责将验证账户信息的消息传递给Trade交易类,Trade根据传过来的账户密码验证用户是否存在,然后将处理结束的消息传递给ATMSystem这个边界对象反映给用户。

分析模型中的一些类

取款设计——应用状态模式

账户的状态分为冻结状态和激活状态。账户在激活的状态下可以进行交易,而在冻结状态下不能做任何交易。在账户验证的时候,应该返回当前账户的状态,如果账户处于冻结状态,当取款人进行取款操作的时候直接返回错误提示信息。状态图表示如下:

取款应用状态模式结构图:

在验证账户阶段,如果账户处于冻结状态,则直接返回DeadAccount谢绝一切交易,否则返回ActiveAccount处理后面的交易。

示意代码:

这边为了简单实现,省去了ATMSystem这个边界对象。

用户账号信息抽象类

  1. /// <summary> 
  2. /// 用户账号信息 
  3. /// </summary> 
  4. public abstract class Account 
  5.     /// <summary> 
  6.     /// 账号 
  7.     /// </summary> 
  8.     private string account; 
  9.     /// <summary> 
  10.     /// 密码 
  11.     /// </summary> 
  12.     private string pwd; 
  13.     public abstract decimal getBalance(decimal d); 

冻结账号类

  1. /// <summary> 
  2. /// 冻结账户类 
  3. /// </summary> 
  4. public class DeadAccount:Account 
  5.   
  6.     public override decimal getBalance(decimal d) 
  7.     { 
  8.         return 0; 
  9.     } 

激活账户类

  1. /// <summary> 
  2. /// 激活账户类 
  3. /// </summary> 
  4. public class ActiveAccount:Account 
  5.     public override decimal getBalance(decimal d) 
  6.     { 
  7.         return d; 
  8.     } 

交易类

  1. /// <summary> 
  2. /// 交易类 负责用户交易处理的类 
  3. /// </summary> 
  4. public class Trade 
  5.     /// <summary> 
  6.     /// 保存用户账号信息 
  7.     /// </summary> 
  8.     private Account account; 
  9.     public Account VolidateLogin(Account a) 
  10.     {            
  11.         //query the database to validate the user exists 
  12.         //For Example 
  13.         this.account = new DeadAccount(); 
  14.         return this.account; 
  15.     } 
  16.   
  17.     /// <summary> 
  18.     /// 商业逻辑 取款 
  19.     /// </summary> 
  20.     /// <param name="d"></param> 
  21.     /// <returns></returns> 
  22.     public decimal GetBalance(decimal d) 
  23.     { 
  24.         return this.account.getBalance(d); 
  25.     } 
  26.  } 

用户类

  1. /// <summary> 
  2. /// 取款人 
  3. /// </summary> 
  4. public class User 
  5.     /// <summary> 
  6.     /// 用户账号信息 
  7.     /// </summary> 
  8.     private Account account; 
  9.     /// <summary> 
  10.     /// 交易处理类 
  11.     /// </summary> 
  12.     private Trade trade; 
  13.   
  14.     public User(Account a, Trade t) 
  15.     { 
  16.         this.account = a; 
  17.         tthis.trade = t; 
  18.     } 
  19.     /// <summary> 
  20.     /// 用户登录类 
  21.     /// </summary> 
  22.     public void Login() 
  23.     { 
  24.         trade.VolidateLogin(account); 
  25.     } 
  26.   
  27.     /// <summary> 
  28.     /// 取款 
  29.     /// </summary> 
  30.     /// <param name="d"></param> 
  31.     /// <returns></returns> 
  32.     public decimal GetBalance(decimal d) 
  33.     { 
  34.         return trade.GetBalance(d); 
  35.     } 

客户端代码

  1. class Client 
  2.     { 
  3.         static void Main(string[] args) 
  4.         { 
  5.             //开始用户取款,默认是激活账户 
  6.             ActiveAccount aa = new ActiveAccount(); 
  7.             Trade t = new Trade(); 
  8.             User u = new User(aa,t); 
  9.             //先登录,后取款 
  10.             u.Login(); 
  11.             Console.WriteLine(u.GetBalance(100)); 
  12.             Console.ReadLine(); 
  13.         } 
  14.     } 

用户必须先登录(插入银行卡,输入密码,点确定),才能选择取款业务(选择取款)。登录之后,返回的账号对象作为trade类成员,当进行取款业务的时候直接引用该账号成员获得取款信息。如果该账号属于冻结账号,则直接返回0。否则返回取款的值。

商品折扣案例

案例描述:某家超市国庆节为了促销,某些类商品打折,比如运动鞋打8折、秋装打9折等,张三去购物为了一双运动鞋、一件秋装、一瓶洗发水。。。,张三买完东西回家,心想今天自己总共“赚”了多少钱? 

案例分析:商家对于商品打折可能有很多策略,这里使用策略模式,封装商品打折策略,这样以便以后扩展了打折策略,不用去修改原来的代码,具有很好的灵活性。

模式涉及的角色: 

抽象策略角色:通常由一个接口或者抽象实现;

具体策略角色:包装了相关的算法和行为;

环境角色:包含抽象策略角色的引用,最终供用户使用。

商品折扣案例设计图

顾客购物涉及到的角色有:购物车、商品、收银台、抽象策略角色和具体策略角色。

购物车:是摆放商品的容器,提供添加和删除操作;

商品:商品实体,有类型,商品名、价格等属性;

收银台:负责收钱,主要是计算顾客购买所有商品的价格和折扣总额;

抽象策略角色:提供折扣策略接口。

具体策略角色:实现具体折扣算法。

商品折扣示意代码:

 
  1. /// <summary> 
  2. /// 具体商品类 
  3. /// </summary> 
  4. public class goods 
  5.     /// <summary> 
  6.     /// 商品类型 
  7.     /// </summary> 
  8.     public string Type 
  9.     { 
  10.         set; 
  11.         get; 
  12.     } 
  13.   
  14.     /// <summary> 
  15.     /// 商品名称 
  16.     /// </summary> 
  17.     public string Name 
  18.     { 
  19.         get; 
  20.         set; 
  21.     } 
  22.   
  23.   
  24.     /// <summary> 
  25.     /// 商品价格 
  26.     /// </summary> 
  27.     public decimal Price 
  28.     { 
  29.         get; 
  30.         set; 
  31.     } 
  32.   

抽象策略角色

  1. /// <summary> 
  2. /// 抽象策略接口 
  3. /// </summary> 
  4. public interface IDiscountStrategy 
  5.     decimal GetDiscount(goods g); 

具体策略角色

 

  1. /// <summary> 
  2. /// 秋装打折策略 
  3. /// </summary> 
  4. public class AutumnDressDiscountStrategy:IDiscountStrategy 
  5.     #region IDiscountStrategy Members 
  6.   
  7.     public decimal GetDiscount(goods g) 
  8.     { 
  9.         return (decimal)0.9 * g.Price; 
  10.     } 
  11.   
  12.     #endregion 
  13.   
  14.   
  15. /// <summary> 
  16. /// 运动鞋打折策略 
  17. /// </summary> 
  18. public class SportShoesDiscountStrategy:IDiscountStrategy 
  19.     #region IDiscountStrategy Members 
  20.   
  21.     public decimal GetDiscount(goods g) 
  22.     { 
  23.         return g.Price * (decimal)0.8; 
  24.     } 
  25.   
  26.     #endregion 

购物车

 
  1. /// <summary> 
  2. /// 购物车类 负责商品的维护 
  3. /// </summary> 
  4. public class ShoppingCar 
  5.     private List<goods> goodsList=new List<goods>(); 
  6.   
  7.     /// <summary> 
  8.     /// 将商品加入到购物车 
  9.     /// </summary> 
  10.     /// <param name="g"></param> 
  11.     public void AddGoods(goods g) 
  12.     { 
  13.         goodsList.Add(g); 
  14.     } 
  15.   
  16.     /// <summary> 
  17.     /// 将商品从购物车当中移除 
  18.     /// </summary> 
  19.     /// <param name="g"></param> 
  20.     public void RemoveGoods(goods g) 
  21.     { 
  22.         goodsList.Remove(g); 
  23.     } 
  24.   
  25.     public List<goods> GoodsList 
  26.     { 
  27.         get 
  28.         { 
  29.             return goodsList; 
  30.         } 
  31.     } 
  32.   

收银台角色

 

  1. /// <summary> 
  2. /// 收银台 
  3. /// </summary> 
  4. public class CashierDesk 
  5.     /// <summary> 
  6.     /// 购物车 
  7.     /// </summary> 
  8.     private ShoppingCar shoppingCar; 
  9.     /// <summary> 
  10.     /// 策略字典 
  11.     /// </summary> 
  12.     private Dictionary<string, IDiscountStrategy> strategies; 
  13.   
  14.     public CashierDesk(ShoppingCar sc, Dictionary<string, IDiscountStrategy> s) 
  15.     { 
  16.         this.shoppingCar = sc
  17.         this.strategies = s; 
  18.     } 
  19.   
  20.     /// <summary> 
  21.     /// 获得所有商品的价格 
  22.     /// </summary> 
  23.     /// <returns></returns> 
  24.     public decimal GetTotalPrice() 
  25.     { 
  26.         return shoppingCar.GoodsList.Sum(p => p.Price); 
  27.     } 
  28.   
  29.     /// <summary> 
  30.     /// 获得所有商品的总的折扣 
  31.     /// </summary> 
  32.     /// <returns></returns> 
  33.     public decimal GetTotalDiscount() 
  34.     { 
  35.         decimal sum = 0
  36.         IDiscountStrategy idiscountStrategy; 
  37.         foreach (goods g in shoppingCar.GoodsList) 
  38.         { 
  39.             idiscountStrategy=strategies.SingleOrDefault(p => p.Key == g.Type).Value; 
  40.             if (idiscountStrategy != null) 
  41.             { 
  42.                 sum += idiscountStrategy.GetDiscount(g); 
  43.             } 
  44.         } 
  45.         return sum; 
  46.     } 

客户端代码

 
  1. class Client 
  2.     { 
  3.         static void Main(string[] args) 
  4.         { 
  5.             ShoppingCar sc = new ShoppingCar(); 
  6.             Dictionary<string, IDiscountStrategy> discountD = new Dictionary<string, IDiscountStrategy>(); 
  7.             //向购物车中加入商品 
  8.             sc.AddGoods(new goods {Name="NIKE鞋 ",Price=100,Type="运动鞋" }); 
  9.             sc.AddGoods(new goods { Name = "秋装"Price = 200Type = "秋装" }); 
  10.             sc.AddGoods(new goods { Name = "苹果"Price = 300Type = "水果" }); 
  11.             //配置折扣策略 
  12.             discountD.Add("运动鞋", new SportShoesDiscountStrategy()); 
  13.             discountD.Add("秋装", new AutumnDressDiscountStrategy()); 
  14.             CashierDesk cd = new CashierDesk(sc, discountD); 
  15.             //得到所有商品总价 
  16.             Console.WriteLine(cd.GetTotalPrice()); 
  17.             //得到所有商品折扣价 
  18.             Console.WriteLine(cd.GetTotalDiscount()); 
  19.   
  20.             Console.ReadLine(); 
  21.         } 
  22.     } 

策略模式优点与缺点:

优点

封装了算法不稳定性,易于以后业务策略的扩展。

缺点

每种策略对应于一个具体策略角色类,会增加系统需要维护的类的数量。

 

原文链接:http://www.cnblogs.com/ejiyuan/archive/2012/06/28/2567905.html

【编辑推荐】

 

 

责任编辑:彭凡 来源: 博客园
相关推荐

2014-12-29 10:39:16

JS

2013-11-26 16:09:34

Android设计模式

2020-11-04 08:54:54

状态模式

2021-06-09 08:53:34

设计模式策略模式工厂模式

2015-09-08 13:39:10

JavaScript设计模式

2017-07-07 10:55:14

数据库MongoDB设计模式

2024-01-29 12:22:07

设计模式策略模式

2021-04-18 21:07:32

门面模式设计

2021-04-19 21:25:48

设计模式到元

2010-01-21 09:08:53

.NET设计模式

2021-04-14 09:02:22

模式 设计建造者

2009-06-29 18:11:40

JSP设计模式

2011-07-14 14:46:46

设计模式

2022-01-12 13:33:25

工厂模式设计

2023-05-04 08:47:31

命令模式抽象接口

2021-02-01 10:01:58

设计模式 Java单例模式

2015-11-03 09:43:43

avascript设计模式工厂模式

2023-11-02 21:11:11

JavaScript设计模式

2023-04-10 09:20:13

设计模式访客模式

2020-08-21 07:23:50

工厂模式设计
点赞
收藏

51CTO技术栈公众号