chain of responsibility 职责链设计模式
是用一系列的classes处理一个请求,来一个请求,a先处理,如果没有处理,则b 处
理,如果没有处理,则由c处理,这样传递一个请求
示例代码如下:
方式一:=============
/**
* 多个请求
*
* @time 下午11:55:14
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public interface Handler {
// 方式一
// public void hanlderHelp();
// public void hanlderPrint();
// public void hanlderFormat();
}
/**
* 请求接口实现类
*
* @time 下午11:57:00
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class ConcreateHandler implements Handler {
private Handler successor;
public ConcreateHandler(Handler successor) {
this.successor = successor;
}
@Override
public void hanlderHelp() {
successor.hanlderHelp();
}
@Override
public void hanlderPrint() {
successor.hanlderPrint();
}
@Override
public void hanlderFormat() {
successor.hanlderFormat();
}
}
方式二:=====================
/**
*
* @time 下午11:54:25
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public interface HelpHandler {
public void hanlderHelp();
}
/**
*
* @time 下午11:54:25
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public interface FormatHandler {
public void hanlderFormat();
}
/**
*
* @time 下午11:54:25
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public interface PrintHandler {
public void hanlderPrint();
}
/**
* 请求接口实现类
*
* @time 下午11:57:00
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class ConcreateHandler2 implements PrintHandler, HelpHandler,
FormatHandler {
private HelpHandler helpHandlerSuccessor;
private PrintHandler printHandlerSuccessor;
private FormatHandler formatHandlerSuccessor;
public ConcreateHandler2(HelpHandler helpHandlerSuccessor,
PrintHandler printHandlerSuccessor, FormatHandler formatHandlerSuccessor)
{
this.helpHandlerSuccessor = helpHandlerSuccessor;
this.printHandlerSuccessor = printHandlerSuccessor;
this.formatHandlerSuccessor = formatHandlerSuccessor;
}
@Override
public void hanlderFormat() {
this.formatHandlerSuccessor.hanlderFormat();
}
@Override
public void hanlderHelp() {
this.helpHandlerSuccessor.hanlderHelp();
}
@Override
public void hanlderPrint() {
this.printHandlerSuccessor.hanlderPrint();
}
}
方式三:==============
/**
* 请求
*
* @time 上午12:09:00
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class Request {
private String type;
public String getType() {
return type;
}
public Request(String type) {
this.type = type;
}
}
/**
* 请求接口实现类
*
* @time 下午11:57:00
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class ConcreateHandler3 implements Handler {
private Handler successor;
public ConcreateHandler3(Handler successor) {
this.successor = successor;
}
@Override
public void handlerRequest(Request request) {
if (request.getType().equals("help")) {
} else if (request.getType().equals("print")) {
} else if (request.getType().equals("format")) {
}
}
}
是用一系列的classes处理一个请求,来一个请求,a先处理,如果没有处理,则b 处
理,如果没有处理,则由c处理,这样传递一个请求
示例代码如下:
方式一:=============
/**
* 多个请求
*
* @time 下午11:55:14
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public interface Handler {
// 方式一
// public void hanlderHelp();
// public void hanlderPrint();
// public void hanlderFormat();
}
/**
* 请求接口实现类
*
* @time 下午11:57:00
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class ConcreateHandler implements Handler {
private Handler successor;
public ConcreateHandler(Handler successor) {
this.successor = successor;
}
@Override
public void hanlderHelp() {
successor.hanlderHelp();
}
@Override
public void hanlderPrint() {
successor.hanlderPrint();
}
@Override
public void hanlderFormat() {
successor.hanlderFormat();
}
}
方式二:=====================
/**
*
* @time 下午11:54:25
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public interface HelpHandler {
public void hanlderHelp();
}
/**
*
* @time 下午11:54:25
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public interface FormatHandler {
public void hanlderFormat();
}
/**
*
* @time 下午11:54:25
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public interface PrintHandler {
public void hanlderPrint();
}
/**
* 请求接口实现类
*
* @time 下午11:57:00
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class ConcreateHandler2 implements PrintHandler, HelpHandler,
FormatHandler {
private HelpHandler helpHandlerSuccessor;
private PrintHandler printHandlerSuccessor;
private FormatHandler formatHandlerSuccessor;
public ConcreateHandler2(HelpHandler helpHandlerSuccessor,
PrintHandler printHandlerSuccessor, FormatHandler formatHandlerSuccessor)
{
this.helpHandlerSuccessor = helpHandlerSuccessor;
this.printHandlerSuccessor = printHandlerSuccessor;
this.formatHandlerSuccessor = formatHandlerSuccessor;
}
@Override
public void hanlderFormat() {
this.formatHandlerSuccessor.hanlderFormat();
}
@Override
public void hanlderHelp() {
this.helpHandlerSuccessor.hanlderHelp();
}
@Override
public void hanlderPrint() {
this.printHandlerSuccessor.hanlderPrint();
}
}
方式三:==============
/**
* 请求
*
* @time 上午12:09:00
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class Request {
private String type;
public String getType() {
return type;
}
public Request(String type) {
this.type = type;
}
}
/**
* 请求接口实现类
*
* @time 下午11:57:00
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class ConcreateHandler3 implements Handler {
private Handler successor;
public ConcreateHandler3(Handler successor) {
this.successor = successor;
}
@Override
public void handlerRequest(Request request) {
if (request.getType().equals("help")) {
} else if (request.getType().equals("print")) {
} else if (request.getType().equals("format")) {
}
}
}