浅学设计模式之策略<Strategy>模式及在android中的使用

策略模式(Strategy):它定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法的变化不会影响到使用算法的客户。(原文:The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.)





        应用场景:  1、 多个类只区别在表现行为不同,可以使用Strategy模式,在运行时动态选择具体要执行的行为。 

                                 2、 需要在不同情况下使用不同的策略(算法),或者策略还可能在未来用其它方式来实现。  

                                    3、 对客户隐藏具体策略(算法)的实现细节,彼此完全独立。

关于策略模式,还是来一个小例子吧,大家都知道超市或者网上商城里面经常有各种打折活动,如果我们写一个收费计算系统,关于这块代码怎么设计才好呢?因为打折活动各种情况,各种不同,如何写能够在打折活动修改的时候减少代码的修改,减少出差的概率呢?

       想想使用下策略模式:

先看下可以独立出系统的善变的打折接口:

[java]  view plain copy
  1. public interface IDiscount {  
  2.     double getPrice(double price, int quantity);  
  3. }  

下面是实现接口的两个打折方法:一、直接打折

[java]  view plain copy
  1. public class Rebate implements IDiscount {  
  2.     private double discount;  
  3.       
  4.     public void setDiscount(double discount){  
  5.         this.discount = discount;  
  6.     }  
  7.       
  8.     public double getDiscount(){  
  9.         return discount;  
  10.     }  
  11.   
  12.     @Override  
  13.     public double getPrice(double price, int quantity) {  
  14.         // TODO Auto-generated method stub  
  15.         return price*quantity*discount;  
  16.     }  
  17.   
  18. }  


 二、达到一定数额返现金

[java]  view plain copy
  1. public class CashReturn implements IDiscount {  
  2.     private int cashCondition;  
  3.     private int cashReturn;  
  4.       
  5.     public void setCondition(int cashCondition,int cashReturn){  
  6.         this.cashReturn = cashReturn;  
  7.         this.cashCondition = cashCondition;  
  8.     }  
  9.   
  10.     @Override  
  11.     public double getPrice(double price, int quantity) {  
  12.         double orignal = price * quantity;  
  13.         int n = (int) (orignal / cashCondition);  
  14.         return orignal - cashReturn * n;  
  15.   
  16.     }  
  17.   
  18. }  

收银机:

[java]  view plain copy
  1. public class CashRegister {  
  2.     private IDiscount iDiscount;  
  3.       
  4.     public void setDiscount(IDiscount iDiscount){  
  5.         this.iDiscount = iDiscount;  
  6.     }  
  7.       
  8.     public void getAccountReceivable(double price, int quantity){  
  9.         System.out.println("应收:"+iDiscount.getPrice(price,quantity));  
  10.     }  
  11. }  

测试:

[java]  view plain copy
  1. public static void main(String[] args){  
  2.         //两种折扣方式  
  3.         IDiscount rebate = new Rebate();  
  4.         IDiscount cashReturn = new CashReturn();  
  5.         CashRegister cashRegister = new CashRegister();  
  6.           
  7.           
  8.         //使用打折  
  9.         ((Rebate) rebate).setDiscount(0.8);  
  10.         cashRegister.setDiscount(rebate);  
  11.         cashRegister.getAccountReceivable(102.83);      
  12.           
  13.         //返现金  
  14.         ((CashReturn) cashReturn).setCondition(200,40);  
  15.         cashRegister.setDiscount(cashReturn);  
  16.         cashRegister.getAccountReceivable(102.83);  
  17.           
  18.           
  19.     }  

结果:

应收:246.72
应收:268.4




         在这里思考下,把善变的部分独立出来,使用委托进行解耦。我的实体部分完成固定功能。使用策略类来实现那些需要调整的功能。




优点:  

  1.                     提供了一种替代继承的方法,而且既保持了继承的优点(代码重用)还比继承更灵活(算法独立,可以任意扩展)。 
  2.                     避免程序中使用多重条件转移语句,使系统更灵活,并易于扩展。  
  3.                     遵守大部分GRASP原则和常用设计原则,高内聚、低偶合。  

缺点:  1、 因为每个具体策略类都会产生一个新类,所以会增加系统需要维护的类的数量。


在android中使用

          listView.setAdapter(),里面的Adapter ,一般都是自定义的Adapter ,继承自BaseAdapter ,这就是典型的策略模式,当ListView 的Item呈现不同形式,在getView方法中,就得不同的实现。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值