(十四)责任链模式

基于面向对象的思想,想想业务中哪些流程是可以组装为一个链条的
就是说会有一些相似的业务,组成的功能链条可能只是顺序会改变,或会增减链条节点,就可以用责任链模式复用代码
每一个Handler都持有下一个Handler的引用,他如果判断还有下一个Handler,就会继续往下执行

public abstract class Handler {
	protected Handler successor;
	public Handler(Handler successor) {
		this.successor = successor;
	}
	public abstract void execute()
}

public class HanderOne extends Handler {
	public HandlerOne(Handler successor) {
		super(successor);
	}
	public void execute() {
		System.out.println("执行功能一");
		if (successor != null) {
			successor.execute();
		}
	}
}
public class HanderTwo implements Handler {
	public HandlerTwo(Handler successor) {
		super(successor);
	}
	public void execute() {
		System.out.println("执行功能二");
		if (successor != null) {
			successor.execute();
		}
	}
}
public class HanderThree implements Handler {
	public HandlerThree(Handler successor) {
		super(successor);
	}
	public void execute() {
		System.out.println("执行功能三");
		if (successor != null) {
			successor.execute();
		}
	}
}

使用时,可以随意组装顺序

Handler handler3 = new HandlerThree(null);
Handler handler2 = new HandlerTwo(handler3);
Handler handler1 = new HandlerOne(handler2);
handler1.execute();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值