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 naibbian@163.com
*
*/
public interface StrategyPattern { /**
* 计算注数
*/
public int calcLottery(int num); }
其次,编写每一个详细的实现
package com.example.demo;
/**
* 策略模式
* 详细的方法实现;
* 比方说双色球
* @author qubian
* @data 2015年6月3日
* @email naibbian@163.com
*
*/
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 naibbian@163.com
*
*/
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 naibbian@163.com
*
*/
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;
}
}
Android设计模式(二)--策略模式的更多相关文章
- Android设计模式之策略模式
今天介绍下策略模式,直接先上UML图 策略模式的概念 The Strategy Pattern defines a family of algorithms,encapsulates each one ...
- PHP设计模式之策略模式
前提: 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成该功能.如查 找.排序等,一种常用的方法是硬编码(Hard Cod ...
- Android 设计模式之MVC模式
说到Android设计模式的MVC模式,估计很多人都是比较熟悉了,这里深入了解一下MVC到底是怎么回事,以ListView为例子讲解. 一.深入理解MVC概念 MVC即Model-View-Contr ...
- [design-patterns]设计模式之一策略模式
设计模式 从今天开始开启设计模式专栏,我会系统的分析和总结每一个设计模式以及应用场景.那么首先,什么是设计模式呢,作为一个软件开发人员,程序人人都会写,但是写出一款逻辑清晰,扩展性强,可维护的程序就不 ...
- 设计模式:策略模式(Strategy)
定 义:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化, 不会影响到使用算法的客户. 示例:商场收银系统,实现正常收费.满300返100.打8折.......等不同收费 ...
- JavaScript设计模式之策略模式(学习笔记)
在网上搜索“为什么MVC不是一种设计模式呢?”其中有解答:MVC其实是三个经典设计模式的演变:观察者模式(Observer).策略模式(Strategy).组合模式(Composite).所以我今天选 ...
- 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)
原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...
- JavaScript设计模式之策略模式
所谓"条条道路通罗马",在现实中,为达到某种目的往往不是只有一种方法.比如挣钱养家:可以做点小生意,可以打分工,甚至还可以是偷.抢.赌等等各种手段.在程序语言设计中,也会遇到这种类 ...
- 【设计模式】【应用】使用模板方法设计模式、策略模式 处理DAO中的增删改查
原文:使用模板方法设计模式.策略模式 处理DAO中的增删改查 关于模板模式和策略模式参考前面的文章. 分析 在dao中,我们经常要做增删改查操作,如果每个对每个业务对象的操作都写一遍,代码量非常庞大. ...
- 设计模式入门,策略模式,c++代码实现
// test01.cpp : Defines the entry point for the console application.////第一章,设计模式入门,策略模式#include &quo ...
随机推荐
- Swift - 给图片添加图片水印(图片上绘制另一张图,并可设透明度)
我前面写了篇文章讲解如何给图片添加文字水印,而如果想要添加图片类型的水印也很简单,只要把原来代码里添加文字的部分改成图片即可. 1,效果图如下: (在图片左上角添加了一个半透明的logo图片) 2,为 ...
- Eclipse用法和技巧二:自动生成Main方法1
刚开始编写java小程序,基本都要用到main方法.后期开发大一点的程序,也可以用main方法进行单元测试.总是编写main方法,感觉太无聊了,幸好Eclipse可以帮我们自动生成main方法.见图: ...
- <1> perl概述
[root@wx03 1]# cat a1.pl $arr=[1,2,3,4,5,6]; print $arr->[4]."\n"; $hash={a=>1,b=> ...
- ASP.NET - 一般处理程序获取session值
1.要在一般处理程序中获取其他页面的session值,需要引用名空间: using System.Web.SessionState; 2.然后继承一个接口:IRequiresSessionState, ...
- ASP.NET、WinForm - 判断整个页面文本框是否为空
foreach(Control ctrl in Page.Controls) { foreach(Control childc in ctrl.Controls) { switch(childc.Ge ...
- Windows 8 动手实验系列教程 实验5:进程生命周期管理
动手实验 实验5:进程生命周期管理 2012年9月 简介 进程生命周期管理对构建Windows应用商店应用的开发者来说是需要理解的最重要的概念之一.不同于传统的Windows应用(它们即使在后台仍然继 ...
- 遇到Audio/Speech相关问题,如何抓取log
[DESCRIPTION] 遇到Audio/Speech相关问题时,经常需要抓取相关log信息,总结抓取方法如下 [SOLUTION] 1. 通话声音相关的问题: Case 1: 通话中某一 ...
- NYOJ 623 A*B ProblemII
A*B Problem II 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描写叙述 ACM的C++同学有好多作业要做,最头痛莫过于线性代数了.由于每次做到矩阵相乘的时候,大 ...
- Cocos2d-x CCProgressTimer
CCProgressTimer,创建使用这个节点可以大致实现两个作用的效果: 其一:在游戏中几乎大部分的游戏启动界面都是游戏加载画面,那么用到的一般是进度条提示加载进度,其使用的就是CCProgres ...
- setStyleSheet来设定窗口部件的样式
使用setStyleSheet来设置图形界面的外观:QT Style Sheets是一个很有利的工具,允许定制窗口的外观,此外还可以用子类QStyle来完成,他的语法很大比重来源于html的CSS,但 ...