责任链情势[Java编程]
本文“责任链情势[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
package pattern.chainofresponsibility;
/**
* 责任链情势:Chain of Responsibility
* @version 2009-5-9
* @author Winty(wintys@gmail.com)
*/
public class ChainOfResponsibilityTest{
public static void main(String[] args){
Handler handler1 = new ConcreteHandler1();
Handler handler2 = new ConcreteHandler2();
Handler handler3 = new ConcreteHandler3();
//设置责任链
handler3.setSuccessor(handler2);
handler2.setSuccessor(handler1);
//发送号令
handler3.handleRequest();
}
}
/**
*抽象处理者
*/
abstract class Handler{
protected Handler successor;
public Handler getSuccessor(){
return successor;
}
public void setSuccessor(Handler successor){
this.successor = successor;
}
public abstract void handleRequest();
}
/**
*具体处理者
*/
class ConcreteHandler1 extends Handler{
public void handleRequest(){
if(getSuccessor() != null){
System.out.print("Request passed:from class Concrete1");
System.out.println(" to class" + getSuccessor().getClass().getName());
getSuccessor().handleRequest();
}
else{
System.out.println("Request handled in ConcreteHandler1");
}
}
}
/**
*具体处理者
*/
class ConcreteHandler2 extends Handler{
public void handleRequest(){
if(getSuccessor() != null){
System.out.print("Request passed:from class Concrete2");
System.out.println(" to class " + getSuccessor().getClass().getName());
getSuccessor().handleRequest();
}
else{
System.out.println("Request handled in ConcreteHandler2");
}
}
}
/**
*具体处理者
*/
class ConcreteHandler3 extends Handler{
public void handleRequest(){
if(getSuccessor() != null){
System.out.print("Request passed:from class Concrete3");
System.out.println(" to class " + getSuccessor().getClass().getName());
getSuccessor().handleRequest();
}
else{
System.out.println("Request handled in ConcreteHandler3");
}
}
}
以上是“责任链情势[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
- ·上一篇文章:<b>自定义分页标签</b>
- ·下一篇文章:优化Hibernate性能的几点倡议
- ·中查找“责任链情势”更多相关内容
- ·中查找“责任链情势”更多相关内容