首页 > 学院 > 开发设计 > 正文

责任链模式

2019-11-06 09:55:34
字体:
来源:转载
供稿:网友

学习笔记

对责任链模式概念的解释来自:http://www.cnblogs.com/java-my-life/archive/2012/05/28/2516865.html

责任链模式:责任链模式是一种对象的行为模式。在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。发出这个请求的客户端并不知道链上的哪一个对象最终处理这个请求,这使得系统可以在不影响客户端的情况下动态地重新组织和分配责任。

运用场景:android中的事件处理机制、有序广播

项目目录:

AbstractRequest.java类(抽象请求类)

package com.example.resposbility;public abstract class AbstractRequest {	PRivate Object object;		public AbstractRequest(Object object)	{		this.object=object;	}	/**	 * 具体的内容对象	 * @return	 */	public Object getContent()	{		return object;	}	/**	 * 获取请求级别	 */	public abstract int getRequestLevel();}

Handler.java类(抽象处理类)

package com.example.resposbility;public abstract class Handler {	public Handler nextHandler;	public  void handleRequest(AbstractRequest abstractRequest)	{		if(getHandleLevel()==abstractRequest.getRequestLevel())		{			handle(abstractRequest);		}else {			if(nextHandler!=null)			{				nextHandler.handleRequest(abstractRequest);			}else {				System.out.println("---->  所有的处理对象都不能处理它");			}					}	}	/**	 * 每个处理者的对象的具体处理方式	 * @param abstractRequest	 */	public abstract void handle(AbstractRequest abstractRequest);	/**	 * 每个处着对象处理的级别	 * @return	 */	public abstract int getHandleLevel();}定义三个具体的请求类:

Request1.java类

package com.example.resposbility;public class Request1 extends AbstractRequest{	public Request1(Object object) {		super(object);					}	@Override	public int getRequestLevel() {		return 1;	}}

Request2.java类

package com.example.resposbility;public class Request2  extends AbstractRequest{	public Request2(Object object) {		super(object);	}	@Override	public int getRequestLevel() {		return 2;	}}

Request3.java类

package com.example.resposbility;public class Request3  extends AbstractRequest{	public Request3(Object object) {		super(object);	}	@Override	public int getRequestLevel() {		return 3;	}}

定义三个具体的请求类

Handler1.java类

package com.example.resposbility;public class Handler1 extends Handler{	@Override	public void handle(AbstractRequest abstractRequest) {		System.out.println("----handle1  处理请求: "+abstractRequest.getRequestLevel());	}	@Override	public int getHandleLevel() {		return 1;	}  }Handler2.java类

package com.example.resposbility;public class Handler2 extends Handler {	@Override	public void handle(AbstractRequest abstractRequest) {		System.out.println("----handle2  处理请求: "+abstractRequest.getRequestLevel());	}	@Override	public int getHandleLevel() {		return 2;	}}Handler3.java类

package com.example.resposbility;public class Handler3   extends Handler {	@Override	public void handle(AbstractRequest abstractRequest) {		System.out.println("----handle3  处理请求: "+abstractRequest.getRequestLevel());	}	@Override	public int getHandleLevel() {		return 3;	}}

测试:

Client.java类

package com.example;import com.example.resposbility.AbstractRequest;import com.example.resposbility.Handler;import com.example.resposbility.Handler1;import com.example.resposbility.Handler2;import com.example.resposbility.Handler3;import com.example.resposbility.Request3;public class Client {	public static void main(String[] args) {		//确定链式关系		Handler handler1=new Handler1();		Handler handler2=new Handler2();		Handler handler3=new Handler3();		handler1.nextHandler=handler2;		handler2.nextHandler=handler3;				AbstractRequest request3=new Request3("请求1");				handler1.handleRequest(request3);	}}


上一篇:swift中的枚举

下一篇:html5Mode

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表