1、定义:

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.

定义了一系列的算法(这些算法实现了同样的工作。仅仅是实现不同),它能够已同样的方式调用全部的算法。降低算法类与算法之间的耦合。

2、目的:

将详细的算法抽象出来,把每一个算法独立出来,形成一系列的算法组,这个算法组里面的算法能够依据实际情况进行相互替换。

3、中心:

策略模式的中心,不在于怎样实现算法,而在于怎样组织和调用这些算法,即:解耦合。形成独立模块。增强程序拓展性。

写了一个简单的策略使用

首先,编写一个统一的算法接口

  1. /**
  2. * 策略模式
  3. * 统一的算法接口
  4. * @author qubian
  5. * @data 2015年6月3日
  6. * @email naibbian@163.com
  7. *
  8. */
  9. public interface StrategyPattern {
  10.  
  11. /**
  12. * 计算注数
  13. */
  14. public int calcLottery(int num);
  15.  
  16. }

其次,编写每一个详细的实现

  1. package com.example.demo;
  2. /**
  3. * 策略模式
  4. * 详细的方法实现;
  5. * 比方说双色球
  6. * @author qubian
  7. * @data 2015年6月3日
  8. * @email naibbian@163.com
  9. *
  10. */
  11. public class StrategyPatternImp_SSQ implements StrategyPattern {
  12.  
  13. @Override
  14. public int calcLottery(int num) {
  15. return 0;
  16. }
  17.  
  18. }
  1. package com.example.demo;
  2.  
  3. /**
  4. * 策略模式
  5. * 详细的方法实现;
  6. * 比方说大乐透
  7. * @author qubian
  8. * @data 2015年6月3日
  9. * @email naibbian@163.com
  10. *
  11. */
  12. public class StrategyPatternImp_DLT implements StrategyPattern{
  13.  
  14. @Override
  15. public int calcLottery(int num) {
  16.  
  17. return 0;
  18. }
  19.  
  20. }

最后是策略的不同调用

  1. package com.example.demo;
  2.  
  3. /**
  4. * 详细的使用
  5. * @author qubian
  6. * @data 2015年6月3日
  7. * @email naibbian@163.com
  8. *
  9. */
  10. public class LotteryCount {
  11.  
  12. private StrategyPattern strategyPattern;
  13.  
  14. public enum LotteryEnum {
  15. SSQ, DLT, QLC;
  16. }
  17.  
  18. public int getLotteryCount(LotteryEnum e,int num)
  19. {
  20. switch (e) {
  21. case SSQ:
  22. strategyPattern = new StrategyPatternImp_SSQ();
  23. break;
  24. case DLT:
  25. strategyPattern = new StrategyPatternImp_DLT();
  26. break;
  27. default:
  28. break;
  29. }
  30.  
  31. return strategyPattern.calcLottery(num);
  32. }
  33.  
  34. }

策略模式 在Android Framework 中运用广泛。

比方说,我们常常使用的 BaseAdapter 实际也是策略模式;

我们编写的适配器继承自BaseAdapter,通过getview中实现不同的算法。实现不同的view的返回,

外部使用时也能够依据数据源。切换Adapter,这种使用事实上就是一种策略模式;

Adapter 就是一个最顶层的策略接口

  1. public interface Adapter {
  2. /**
  3. * How many items are in the data set represented by this Adapter.
  4. *
  5. * @return Count of items.
  6. */
  7. int getCount();
  8.  
  9. /**
  10. * Get the data item associated with the specified position in the data set.
  11. *
  12. * @param position Position of the item whose data we want within the adapter's
  13. * data set.
  14. * @return The data at the specified position.
  15. */
  16. Object getItem(int position);
  1. /**
  2. * Get a View that displays the data at the specified position in the data set. You can either
  3. * create a View manually or inflate it from an XML layout file. When the View is inflated, the
  4. * parent View (GridView, ListView...) will apply default layout parameters unless you use
  5. * {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}
  6. * to specify a root view and to prevent attachment to the root.
  7. *
  8. * @param position The position of the item within the adapter's data set of the item whose view
  9. * we want.
  10. * @param convertView The old view to reuse, if possible. Note: You should check that this view
  11. * is non-null and of an appropriate type before using. If it is not possible to convert
  12. * this view to display the correct data, this method can create a new view.
  13. * Heterogeneous lists can specify their number of view types, so that this View is
  14. * always of the right type (see {@link #getViewTypeCount()} and
  15. * {@link #getItemViewType(int)}).
  16. * @param parent The parent that this view will eventually be attached to
  17. * @return A View corresponding to the data at the specified position.
  18. */
  19. View getView(int position, View convertView, ViewGroup parent);
  20.  
  21. static final int IGNORE_ITEM_VIEW_TYPE = AdapterView.ITEM_VIEW_TYPE_IGNORE;
  22.  
  23. int getItemViewType(int position);
  24. int getViewTypeCount();
  25. static final int NO_SELECTION = Integer.MIN_VALUE;
  26. boolean isEmpty();
  27. }

再到BaseAdapter的抽象

  1. public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter {
  2. private final DataSetObservable mDataSetObservable = new DataSetObservable();
  3.  
  4. public boolean hasStableIds() {
  5. return false;
  6. }
  7. /**
  8. * Notifies the attached observers that the underlying data has been changed
  9. * and any View reflecting the data set should refresh itself.
  10. */
  11. public void notifyDataSetChanged() {
  12. mDataSetObservable.notifyChanged();
  13. }
  14.  
  15. public View getDropDownView(int position, View convertView, ViewGroup parent) {
  16. return getView(position, convertView, parent);
  17. }
  18.  
  19. public int getItemViewType(int position) {
  20. return 0;
  21. }
  22.  
  23. public int getViewTypeCount() {
  24. return 1;
  25. }
  26.  
  27. public boolean isEmpty() {
  28. return getCount() == 0;
  29. }
  30. }

Android设计模式(二)--策略模式的更多相关文章

  1. Android设计模式之策略模式

    今天介绍下策略模式,直接先上UML图 策略模式的概念 The Strategy Pattern defines a family of algorithms,encapsulates each one ...

  2. PHP设计模式之策略模式

    前提: 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成该功能.如查 找.排序等,一种常用的方法是硬编码(Hard Cod ...

  3. Android 设计模式之MVC模式

    说到Android设计模式的MVC模式,估计很多人都是比较熟悉了,这里深入了解一下MVC到底是怎么回事,以ListView为例子讲解. 一.深入理解MVC概念 MVC即Model-View-Contr ...

  4. [design-patterns]设计模式之一策略模式

    设计模式 从今天开始开启设计模式专栏,我会系统的分析和总结每一个设计模式以及应用场景.那么首先,什么是设计模式呢,作为一个软件开发人员,程序人人都会写,但是写出一款逻辑清晰,扩展性强,可维护的程序就不 ...

  5. 设计模式:策略模式(Strategy)

    定   义:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化, 不会影响到使用算法的客户. 示例:商场收银系统,实现正常收费.满300返100.打8折.......等不同收费 ...

  6. JavaScript设计模式之策略模式(学习笔记)

    在网上搜索“为什么MVC不是一种设计模式呢?”其中有解答:MVC其实是三个经典设计模式的演变:观察者模式(Observer).策略模式(Strategy).组合模式(Composite).所以我今天选 ...

  7. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

  8. JavaScript设计模式之策略模式

    所谓"条条道路通罗马",在现实中,为达到某种目的往往不是只有一种方法.比如挣钱养家:可以做点小生意,可以打分工,甚至还可以是偷.抢.赌等等各种手段.在程序语言设计中,也会遇到这种类 ...

  9. 【设计模式】【应用】使用模板方法设计模式、策略模式 处理DAO中的增删改查

    原文:使用模板方法设计模式.策略模式 处理DAO中的增删改查 关于模板模式和策略模式参考前面的文章. 分析 在dao中,我们经常要做增删改查操作,如果每个对每个业务对象的操作都写一遍,代码量非常庞大. ...

  10. 设计模式入门,策略模式,c++代码实现

    // test01.cpp : Defines the entry point for the console application.////第一章,设计模式入门,策略模式#include &quo ...

随机推荐

  1. linux shell脚本:在脚本中实现读取键盘输入,根据输入判断下一步的分支

    echo please input “runbip” to run bip. variableName="null" while [ $variableName != " ...

  2. ShareSDK第三方登陆 (IOS)

    1.http://www.mob.com/ 注册申请 2.http://www.mob.com/#/download SDK下载  (简洁版:http://www.mob.com/#/download ...

  3. 新发现的Cyberduck(映射网盘)和zsuncloud(硬件产品很新潮),群辉nas的确好用(购买链接)

    https://cyberduck.io/?l=en http://www.zsuncloud.com/ 群辉nas的确好用啊在哪里可以买到?官网 淘宝也可以自己做黑群晖 先用xpenoboot is ...

  4. axure制作项目符号列表样式

    1. 拖动文本面板到页面编辑区域 2. 点击工具栏的[项目符合列表] 来自:非原型不设计

  5. mui如何增加自定义字体icon图标

    http://ask.dcloud.net.cn/article/128 字体地址:http://www.iconfont.cn/

  6. codility上的问题(18) Rho 2012

    从正整数1开始,产生一个数列,数列中的每个数是之前出现过的任意两个数的和(可以相等),问产生正整数A,需要的数列长度至少是多少?返回这样一个最短的序列. 例如A=42 可以这样[1, 2, 3, 6, ...

  7. View实现涂鸦、撤销以及重做功能

    import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  8. DELPHI XE7 新的并行库

    DELPHI XE7 的新功能列表里面增加了并行库System.Threading, System.SyncObjs. 为什么要增加新的并行库? 还是为了跨平台.以前要并行编程只能从TThread类继 ...

  9. innerXml,outerXml,innerText的不同

    原文:innerXml,outerXml,innerText的不同 昨天看到咱们园子里有一个仁兄写的关于xml的有关操作,在读的过程中,由于是初学者有不明白的地方就查资料,发现自己多innerXml, ...

  10. jquery ajax局部加载方法介绍

    [导读] 在jquery中实现ajax加载的方法有很多种,不像以前的js的ajax只有那一种,下面我们介绍jquery ajax实现局部加载方法总结,有需要了解的朋友可参考.例 代码如下复制代码 $ ...