在软件的设计/实现过程中,我的同事经常会遇到这样一个场景:需要在一个可重用的、大范围的方法中调用若干个不确定的方法或一系列不确定的操作。在这种情况下,我经常建议他们使用“回调”这种技巧去解决问题。
回调模式和接口、抽象这两个概念是紧密相关的,在这里简要的说明一下。
接口(Interface):说明类该做什么而不指定如何去做。抽象(abstract)的道理也大致如此。
回调,这个模式一般是这么定义的:在回调模式中你可以指定当一个特定时间发生时回调对象上的方法。这句话是不是很不好理解?那么举些例子就清楚了,例:java swing中ActionListener的接口监听机制,类似的API还有java.swing.JOptionPane、java.swing.Timer、java.awt.Tookit……有点明白了吧,简单的说:所谓回调模式,一般就是方法回调——定义个抽象的(先不实现的)方法先行调用,然后在具体的使用中对此抽象方法进行实现,以供前面“回头调用”,此之所谓回调,如是然也。
有个实际的例子,这是在工作中我设计的一个基于回调模式的小工具,它可以根据指定的批量数(batchSize)来处理一个较大的泛型数组(Object[]),这可以用在广域网络计算、Web服务的场景中,因为如果一次让远程网络服务端处理大量的数据,必然会存在效率低下、响应不及的情况,如果分批次处理再传输的话,那样效果就会好的多,响应效率也能让人接受。
package com.tibco.plugin.salesforce.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/** *//**
* Batch Operation Tool
*
* @author <a href=" huqimailto:huqiyes@gmail.com">huqi</a>
* @serialData 2007
*/
public abstract class BatchOperation ...{
* Batch Operation Tool
*
* @author <a href=" huqimailto:huqiyes@gmail.com">huqi</a>
* @serialData 2007
*/
public abstract class BatchOperation ...{
/** *//**
* Sign means no batchSize, all do once.
*/
public final static int SIGN_NO_BATCH_SIZE = -1;
* Sign means no batchSize, all do once.
*/
public final static int SIGN_NO_BATCH_SIZE = -1;
private final Object[] ALL;
private final int SUM;
/** *//**
* Call back method, define your operation by imple it.
*
* @param params
* @return
* @throws Exception
*/
protected abstract Object[] operate(Object[] params) throws Exception;
* Call back method, define your operation by imple it.
*
* @param params
* @return
* @throws Exception
*/
protected abstract Object[] operate(Object[] params) throws Exception;
/** *//**
*
* @param all
* all data
*/
public BatchOperation(Object[] all) ...{
if (all != null && all.length > 0) ...{
this.ALL = all;
this.SUM = all.length;
} else ...{
this.ALL = null;
this.SUM = 0;
}
}
*
* @param all
* all data
*/
public BatchOperation(Object[] all) ...{
if (all != null && all.length > 0) ...{
this.ALL = all;
this.SUM = all.length;
} else ...{
this.ALL = null;
this.SUM = 0;
}
}
/** *//**
* You can use it to do batch operation.
*
* @param batchSize
* batch operation size
* @return operation results
* @throws Exception
*/
public Object[] doBatch(int batchSize) throws Exception ...{
if (SUM == 0)
return null;
Object[] results = null;
if (batchSize == SIGN_NO_BATCH_SIZE) ...{
// no batch
results = operate(ALL);
} else ...{
// has batch
final List<Object> LIST_ALL = Collections.unmodifiableList(Arrays
.asList(ALL));
int remain = SUM;
int times = 0;
List<Object> resultsList = new ArrayList<Object>(SUM);
while (remain > 0) ...{
int theBatch = remain > batchSize ? batchSize : remain;
Object[] batch = LIST_ALL.subList(batchSize * times,
batchSize * times + theBatch).toArray(
new Object[theBatch]);
Collections.addAll(resultsList, operate(batch));
times++;
remain = remain - batchSize;
}
results = resultsList.toArray(new Object[SUM]);
}
return results;
}
}
* You can use it to do batch operation.
*
* @param batchSize
* batch operation size
* @return operation results
* @throws Exception
*/
public Object[] doBatch(int batchSize) throws Exception ...{
if (SUM == 0)
return null;
Object[] results = null;
if (batchSize == SIGN_NO_BATCH_SIZE) ...{
// no batch
results = operate(ALL);
} else ...{
// has batch
final List<Object> LIST_ALL = Collections.unmodifiableList(Arrays
.asList(ALL));
int remain = SUM;
int times = 0;
List<Object> resultsList = new ArrayList<Object>(SUM);
while (remain > 0) ...{
int theBatch = remain > batchSize ? batchSize : remain;
Object[] batch = LIST_ALL.subList(batchSize * times,
batchSize * times + theBatch).toArray(
new Object[theBatch]);
Collections.addAll(resultsList, operate(batch));
times++;
remain = remain - batchSize;
}
results = resultsList.toArray(new Object[SUM]);
}
return results;
}
}
如果看了以上的内容还是不明白回调模式或这个工具如何使用,那么我们可以讨论……
转载于:https://blog.51cto.com/huqicto/280904