Java 设计模式 之 责任链模式
http://www.verejava.com/?id=16999052620463
package com.chain.theory;
public class TestHandler
{
public static void main(String[] args)
{
Handler hrHandler=new HrHandler(null);
Handler managerHandler=new ManagerHandler(hrHandler);
Handler financeHandler=new FinanceHandler(managerHandler);
Handler frontHandler=new FrontHandler(financeHandler);
frontHandler.handleRequest("离职");
}
}
package com.chain.theory;
public class ManagerHandler implements Handler
{
private Handler nextHandler;
public ManagerHandler(Handler nextHandler)
{
this.nextHandler=nextHandler;
}
@Override
public void handleRequest(String request)
{
if("审批".equals(request))
{
System.out.println("经理审核完毕");
if(nextHandler!=null)
nextHandler.handleRequest("批准");
}
}
}
package com.chain.theory;
public class HrHandler implements Handler
{
private Handler nextHandler;
public HrHandler(Handler nextHandler)
{
this.nextHandler=nextHandler;
}
@Override
public void handleRequest(String request)
{
if("批准".equals(request))
{
System.out.println("人事批准");
if(nextHandler!=null)
nextHandler.handleRequest("完毕");
}
}
}
package com.chain.theory;
public interface Handler
{
public void handleRequest(String request);
}
package com.chain.theory;
public class FrontHandler implements Handler
{
private Handler nextHandler;
public FrontHandler(Handler nextHandler)
{
this.nextHandler=nextHandler;
}
@Override
public void handleRequest(String request)
{
if("离职".equals(request))
{
System.out.println("离职申请");
if(nextHandler!=null)
nextHandler.handleRequest("审核");
}
}
}
package com.chain.theory;
public class FinanceHandler implements Handler
{
private Handler nextHandler;
public FinanceHandler(Handler nextHandler)
{
this.nextHandler=nextHandler;
}
@Override
public void handleRequest(String request)
{
if("审核".equals(request))
{
System.out.println("财务审核完毕");
if(nextHandler!=null)
nextHandler.handleRequest("审批");
}
}
}