Android设计模式--策略模式

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、中心:

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

写了一个简单的策略使用

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

/**
 * 策略模式
 * 统一的算法接口
 * @author qubian
 * @data 2015年6月3日
 * @email [email protected]
 *
 */
public interface StrategyPattern {

	/**
	 * 计算注数
	 */
	public int calcLottery(int num);

}

其次,编写每个具体的实现

package com.example.demo;
/**
 * 策略模式
 * 具体的方法实现;
 * 比如说双色球
 * @author qubian
 * @data 2015年6月3日
 * @email [email protected]
 *
 */
public class StrategyPatternImp_SSQ implements StrategyPattern {

	@Override
	public int calcLottery(int num) {
		return 0;
	}

}
package com.example.demo;

/**
 * 策略模式
 * 具体的方法实现;
 * 比如说大乐透
 * @author qubian
 * @data 2015年6月3日
 * @email [email protected]
 *
 */
public class StrategyPatternImp_DLT implements StrategyPattern{

	@Override
	public int calcLottery(int num) {

		return 0;
	}

}

最后是策略的不同调用

package com.example.demo;

/**
 * 具体的使用
 * @author qubian
 * @data 2015年6月3日
 * @email [email protected]
 *
 */
public class LotteryCount {

	private StrategyPattern strategyPattern;

	public enum LotteryEnum {
	        SSQ, DLT, QLC;
	}

	public int  getLotteryCount(LotteryEnum e,int num)
	{
		switch (e) {
		case SSQ:
			strategyPattern =  new StrategyPatternImp_SSQ();
			break;
		case DLT:
			strategyPattern =  new StrategyPatternImp_DLT();
			break;
		default:
			break;
		}

		return strategyPattern.calcLottery(num);
	}

}

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

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

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

外部使用时也可以根据数据源,切换Adapter,这样的使用其实就是一种策略模式;

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

public interface Adapter {
    /**
     * How many items are in the data set represented by this Adapter.
     *
     * @return Count of items.
     */
    int getCount();   

    /**
     * Get the data item associated with the specified position in the data set.
     *
     * @param position Position of the item whose data we want within the adapter's
     * data set.
     * @return The data at the specified position.
     */
    Object getItem(int position);
    /**
     * Get a View that displays the data at the specified position in the data set. You can either
     * create a View manually or inflate it from an XML layout file. When the View is inflated, the
     * parent View (GridView, ListView...) will apply default layout parameters unless you use
     * {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}
     * to specify a root view and to prevent attachment to the root.
     *
     * @param position The position of the item within the adapter's data set of the item whose view
     *        we want.
     * @param convertView The old view to reuse, if possible. Note: You should check that this view
     *        is non-null and of an appropriate type before using. If it is not possible to convert
     *        this view to display the correct data, this method can create a new view.
     *        Heterogeneous lists can specify their number of view types, so that this View is
     *        always of the right type (see {@link #getViewTypeCount()} and
     *        {@link #getItemViewType(int)}).
     * @param parent The parent that this view will eventually be attached to
     * @return A View corresponding to the data at the specified position.
     */
    View getView(int position, View convertView, ViewGroup parent);

    static final int IGNORE_ITEM_VIEW_TYPE = AdapterView.ITEM_VIEW_TYPE_IGNORE;

    int getItemViewType(int position);
    int getViewTypeCount();
    static final int NO_SELECTION = Integer.MIN_VALUE;
     boolean isEmpty();
}

再到BaseAdapter的抽象

public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter {
    private final DataSetObservable mDataSetObservable = new DataSetObservable();

    public boolean hasStableIds() {
        return false;
    }
    /**
     * Notifies the attached observers that the underlying data has been changed
     * and any View reflecting the data set should refresh itself.
     */
    public void notifyDataSetChanged() {
        mDataSetObservable.notifyChanged();
    }

    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getView(position, convertView, parent);
    }

    public int getItemViewType(int position) {
        return 0;
    }

    public int getViewTypeCount() {
        return 1;
    }

    public boolean isEmpty() {
        return getCount() == 0;
    }
}
时间: 2024-08-24 09:43:58

Android设计模式--策略模式的相关文章

Android设计模式—策略模式

1.策略模式概念 定义一系列算法,把他们独立封装起来,并且这些算法之间可以相互替换.策略模式主要是管理一堆有共性的算法,客户端可以根据需要,很快切换这些算法,并且保持可扩展性. 策略模式的本质:分离算法,选择实现. 2.策略模式实现 下面针对策略模式说一个小型的实现例子,个人觉得学习设计模式,最好的方法是看看设计模式概念,然后先看看简单的模式实现的例子.策略模式实现起来,主要需要3部分:抽象接口.实现算法.上下文. //算法的接口 public interface PriceStrategy {

设计模式 - 策略模式(Strategy Pattern) 具体解释

策略模式(Strategy Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26577879 本文版权全部, 禁止转载, 如有须要, 请站内联系. 策略模式: 定义了算法族, 分别封装起来, 让它们之间能够相互替换, 此模式让算法的变化独立于使用算法的客户. 对于父类的子类族须要常常扩展新的功能, 为了使用父类比較灵活的加入子类, 把父类的行为写成接口(interface)的形式; 使用set()方法

设计模式 - 策略模式(Strategy Pattern) 详解

策略模式(Strategy Pattern) 详解 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26577879 本文版权所有, 禁止转载, 如有需要, 请站内联系. 策略模式: 定义了算法族, 分别封装起来, 让它们之间可以相互替换, 此模式让算法的变化独立于使用算法的客户. 对于父类的子类族需要经常扩展新的功能, 为了使用父类比较灵活的添加子类, 把父类的行为写成接口(interface)的形式; 使用set()方法,

2.大话设计模式-策略模式

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DesignModel 8 { 9 /// <summary> 10 /// 策略模式 11 /// </summary> 12 public class TacticsModel 13 { 14 //对于

设计模式---策略模式Strategy(对象行为型)

1. 概述 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化. 策略模式是对算法的封装,它把算法的责任和算法本身分割开,委派给不同的对象管理. 2. 应用场景 (1)多个类只区别在表现行为不同,在运行时动态选择具体要执行的行为. (2)需要在不同情况下使用不同的策略(算法),或者策略还可能在未来用其它方式来实现. (3)对客户隐藏具体策略(算法)的实现细节,彼此完全独立. 3. 示例 出行旅游:我们可以有几个策略可以考虑:可

设计模式—策略模式

什么是策略模式? 策略模式定义了算法家族,分别封装起来,让它们之间能够相互替换,此模式让算法的变化不会影响到使用算法 的客户. 策略模式是一种定义一系列算法的方法,从概念上看全部这些算法完毕的都是同样的工作,仅仅是实现不同,它可 以以同样的方式调用全部的算法,降低了各种算法类与使用算法之间的耦合. 策略模式的长处? (1)策略模式的Strategy类层为Context类定义了一系列的可供重用的算法和行为.继承有助于析取出这些算法 的公共功能. (2)简化了单元測试(每一个算法都有自己的类,能够通

说说设计模式~策略模式(Strategy)

策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.而对于客户端(UI)来说,可以通过IOC再配合工厂模块,实现动态策略的切换,策略模块通常于一个抽象策略对象(interface or abstract class),多个具体策略对象(implement class )和一个调用策略的入口组成. 何时能用到它? 对于UI来说,可能一个功能会有多种实现方式,而且实现方式可能还会有扩展,例如,一个ATM机,它目前支持工行,建行,家行,以后可能又出现了占占银行,而这时,ATM

head first 设计模式 策略模式

HEAD FIRST:策略模式定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 设计模式:定义一系列的算法,把它们一个个封装起来,并且使它们可以相互替换.本模式使得算法可独立于它的客户而变化. 大话设计模式:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户. 使用这个模式的原因: 用许多的算法对一类进行计算.那么如果把这些算法强行编码到这个类当中.其实是不可取的.因为很多时候下不同的情况下使用不同的算

15. 星际争霸之php设计模式--策略模式

题记==============================================================================本php设计模式专辑来源于博客(jymoz.com),现在已经访问不了了,这一系列文章是我找了很久才找到完整的,感谢作者jymoz的辛苦付出哦! 本文地址:http://www.cnblogs.com/davidhhuan/p/4248199.html============================================

Android开发中无处不在的设计模式——策略模式

这个系列停更了好久了,差不多可以重新拿起来更一篇了,这篇文章主要介绍策略模式.在这之前,先温习一下前面介绍的4种模式. 设计模式很重要! 设计模式很重要! 设计模式很重要! 重要的事说三遍!!! Android开发中无处不在的设计模式--单例模式 Android开发中无处不在的设计模式--Builder模式 Android开发中无处不在的设计模式--观察者模式 Android开发中无处不在的设计模式--原型模式 接着看下策略模式的定义 策略模式定义了一些列的算法,并将每一个算法封装起来,而且使它