责任链模式

概念:将多个任务单元组合成一条任务链,通过对链条的操作实现递归调用任务单元,并实现任务单元间的嵌入操作

要点:

1、开发任务单元,封装业务逻辑

2、将多个任务单元组合成任务链

3、开发链执行单元

4、递归调用

实例:我首先定义了责任链模式的各种接口和一些实现类,然后打成jar包,后面多个项目就可以在此jar包基础之上实现责任链模式

1、首先定义链条(接口+抽象类),实现类可以有多种,可采用List实现,也可采用数组实现

1)chain接口

/**
 * 业务执行链接口
 * 
 * @author ypqiao
 *
 * @param <T>
 */
public interface Chain {
	
	/**
	 * 获取下一个业务单元
	 * @param index
	 * @return
	 * @throws Exception
	 */
	public BusinessUnit getBusinessExecuteUnit( int index ) throws Exception;
	
	/**
	 * 增加一个业务单元
	 * @param businessExecuteUnit
	 * @throws Exception
	 */
	public void addBusinessExecuteUnit( BusinessUnit  businessExecuteUnit) throws Exception;
	
	/**
	 * 移除一个业务单元
	 * @param businessExecuteUnit
	 * @throws Exception
	 */
	public void removeBusinessExecuteUnit( BusinessUnit  businessExecuteUnit) throws Exception;
	
	/**
	 * 获取练的长度
	 * @return
	 * @throws Exception
	 */
	public int getSize() throws Exception;
	
	/**
	 * 获取链 指针的位置
	 * @return
	 * @throws Exception
	 */
	public int getIndex() throws Exception;
	
	/**
	 * 判断链指针是否到达末尾
	 * @return
	 * @throws Exception
	 */
	public boolean isEnd() throws Exception;
	
}

2)chain抽象类实现

/**
 * 链抽象实现
 * 定义了链指针及其相关操作方法实现
 * 
 * @author ypqiao
 *
 * @param <T>
 */
public abstract class AbstractChainImpl implements Chain{
	
	protected int index;	// 链条指针
	
	@Override
	public int getIndex() throws Exception{
		// 获取完毕后,自动向下移动一位,所以index++
		return index++;
	}

	@Override
	public boolean isEnd() throws Exception {
		
		if( index == getSize() )
			return true;
		
		return false;
	}
}

3)List Chain具体实现

/**
 * 链条 List实现
 * 继承了AbstractChainImpl
 * 
 * @author ypqiao
 *
 * @param <T>
 */
public class ListChainImpl extends AbstractChainImpl {
	
	private List<BusinessUnit> listChain = new ArrayList<BusinessUnit>();

	@Override
	public int getSize() {
		return listChain.size();
	}

	@Override
	public void addBusinessExecuteUnit(BusinessUnit businessExecuteUnit)
			throws Exception {
		listChain.add(businessExecuteUnit);
	}

	@Override
	public void removeBusinessExecuteUnit(
			BusinessUnit businessExecuteUnit) throws Exception {
		listChain.remove(businessExecuteUnit);
	}

	@Override
	public BusinessUnit getBusinessExecuteUnit(int index) throws Exception {
		return listChain.get(index);
	}

}

2、定义业务执行单元接口

/**
 * 业务单元接口
 * 
 * @author ypqiao
 *
 * @param <T>
 * @param <E>
 */
public interface BusinessUnit extends Unit {
	
	/**
	 * 业务方法
	 * @param inputData
	 * @param chainExecuteUnit
	 * @return
	 * @throws Exception
	 */
	public Object executeBusiness( Object  inputData, 
			ChainUnit chainExecuteUnit ) throws Exception;

}

3、定义链执行单元接口

/**
 * 链执行单元接口
 * @author ypqiao
 *
 * @param <T>
 * @param <E>
 */
public interface ChainUnit extends Unit {
	
	/**
	 * 执行链
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public Object executeChain( Object data) throws Exception;

}

4、定义链执行接口默认实现

/**
 * 链执行单元默认实现
 * 
 * @author ypqiao
 *
 * @param <T>
 * @param <E>
 */
public class DefaultChainUnitImpl implements ChainUnit {
	
	protected Chain chain ;
	
	public DefaultChainUnitImpl(  Chain chain ){
		this.chain = chain;
	}
	
	
	
	@Override
	public Object executeChain(Object data) throws Exception {
		if( chain.isEnd() ){
			return data;
		}
		
		int index = chain.getIndex();
		BusinessUnit executeUnit = chain.getBusinessExecuteUnit(index);
		return executeUnit.executeBusiness(data, this);
	}

}

5、定义单元接口

/**
 * 单元统一接口
 * 
 * @author ypqiao
 *
 * @param <T>
 * @param <E>
 */
public interface Unit {

}
6、将这些类和接口打成jar包,命名为chain-pattern.jar

7、 新建一个项目,并把chain-pattern.jar构建进classpath

8、开发单独的业务单元

public class SayHello1BusinessUnitt implements BusinessUnit {

	@Override
	public Object executeBusiness(Object inputData, ChainUnit chainExecuteUnit)
			throws Exception {
		int value = ((Integer)inputData).intValue();
		if( value == 1 ){
			return " sayHello1";
		}
		else {
			return chainExecuteUnit.executeChain(inputData);
		}
		
		
	}


}

public class SayHello2BusinessUnitt implements BusinessUnit {

	@Override
	public Object executeBusiness(Object inputData, ChainUnit chainExecuteUnit)
			throws Exception {
		int value = ((Integer)inputData).intValue();
		if( value == 2 ){
			return " sayHello2";
		}
		else {
			return chainExecuteUnit.executeChain(inputData);
		}
	}


}

public class SayHello3BusinessUnitt implements BusinessUnit {

	@Override
	public Object executeBusiness(Object inputData, ChainUnit chainExecuteUnit)
			throws Exception {
		int value = ((Integer)inputData).intValue();
		if( value == 3 ){
			return " sayHello3";
		}
		else {
			return chainExecuteUnit.executeChain(inputData);
		}
	}


}

9、创建测试例子

public class ChainTest {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		
		BusinessUnit sayHello1 = new SayHello1BusinessUnitt();
		BusinessUnit sayHello2 = new SayHello2BusinessUnitt();
		BusinessUnit sayHello3 = new SayHello3BusinessUnitt();
		
		Chain listChain = new ListChainImpl();
		listChain.addBusinessExecuteUnit(sayHello1);
		listChain.addBusinessExecuteUnit(sayHello2);
		listChain.addBusinessExecuteUnit(sayHello3);
		
		ChainUnit chainUtil = new DefaultChainUnitImpl(listChain);
		Object obj = chainUtil.executeChain(2);
		String res = (String)obj;
		System.out.println(res);

	}

}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值