职责链模式(Chain of Responsibility Pattern)
2017-09-19 14:49
323 查看
所有一切链式的、分步完成的、"传递性"的都可以想到用职责链模式。其实现方式的关键类似于链表的结构。职责链由一些职责结点组成,每个结点能够将任务传递给下一个结点。最后每个结点都遍历且执行了该结点规定执行的任务。举个例子:
类图:
关键是RequestHandler的实现。
代码:
/**
*
* RequestHandler
*
*/
public abstract class RequestHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(RequestHandler.class);
private RequestHandler next;
public RequestHandler(RequestHandler next) {
this.next = next;
}
/**
* Request handler
*/
public void handleRequest(Request req) {
if (next != null) {
next.handleRequest(req);
}
}
protected void printHandling(Request req) {
LOGGER.info("{} handling request \"{}\"", this, req);
}
@Override
public abstract String toString();
}
类图:
关键是RequestHandler的实现。
代码:
/**
*
* RequestHandler
*
*/
public abstract class RequestHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(RequestHandler.class);
private RequestHandler next;
public RequestHandler(RequestHandler next) {
this.next = next;
}
/**
* Request handler
*/
public void handleRequest(Request req) {
if (next != null) {
next.handleRequest(req);
}
}
protected void printHandling(Request req) {
LOGGER.info("{} handling request \"{}\"", this, req);
}
@Override
public abstract String toString();
}
/** * * OrcOfficer * */ public class OrcOfficer extends RequestHandler { public OrcOfficer(RequestHandler handler) { super(handler); } @Override public void handleRequest(Request req) { if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) { printHandling(req); req.markHandled(); } else { super.handleRequest(req); } } @Override public String toString() { return "Orc officer"; } }
/** * * OrcCommander * */ public class OrcCommander extends RequestHandler { public OrcCommander(RequestHandler handler) { super(handler); } @Override public void handleRequest(Request req) { if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) { printHandling(req); req.markHandled(); } else { super.handleRequest(req); } } @Override public String toString() { return "Orc commander"; } }
/** * * OrcSoldier * */ public class OrcSoldier extends RequestHandler { public OrcSoldier(RequestHandler handler) { super(handler); } @Override public void handleRequest(Request req) { if (req.getRequestType().equals(RequestType.COLLECT_TAX)) { printHandling(req); req.markHandled(); } else { super.handleRequest(req); } } @Override public String toString() { return "Orc soldier"; } }
/** * Request */ public class Request { /** * The type of this request, used by each item in the chain to see if they should or can handle * this particular request */ private final RequestType requestType; /** * A description of the request */ private final String requestDescription; /** * Indicates if the request is handled or not. A request can only switch state from unhandled to * handled, there's no way to 'unhandle' a request */ private boolean handled; /** * Create a new request of the given type and accompanied description. * * @param requestType The type of request * @param requestDescription The description of the request */ public Request(final RequestType requestType, final String requestDescription) { this.requestType = Objects.requireNonNull(requestType); this.requestDescription = Objects.requireNonNull(requestDescription); } /** * Get a description of the request * * @return A human readable description of the request */ public String getRequestDescription() { return requestDescription; } /** * Get the type of this request, used by each person in the chain of command to see if they should * or can handle this particular request * * @return The request type */ public RequestType getRequestType() { return requestType; } /** * Mark the request as handled */ public void markHandled() { this.handled = true; } /** * Indicates if this request is handled or not * * @return <tt>true</tt> when the request is handled, <tt>false</tt> if not */ public boolean isHandled() { return this.handled; } @Override public String toString() { return getRequestDescription(); } }
/** * * RequestType enumeration * */ public enum RequestType { DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX }
/** * * OrcKing makes requests that are handled by the chain. * */ public class OrcKing { RequestHandler chain; public OrcKing() { buildChain(); } private void buildChain() { chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null))); } public void makeRequest(Request req) { chain.handleRequest(req); } }
/** * * The Chain of Responsibility pattern is a design pattern consisting of command objects and a * series of processing objects. Each processing object contains logic that defines the types of * command objects that it can handle; the rest are passed to the next processing object in the * chain. A mechanism also exists for adding new processing objects to the end of this chain. * <p> * In this example we organize the request handlers ({@link RequestHandler}) into a chain where each * handler has a chance to act on the request on its turn. Here the king ({@link OrcKing}) makes * requests and the military orcs ({@link OrcCommander}, {@link OrcOfficer}, {@link OrcSoldier}) * form the handler chain. * */ public class App { /** * Program entry point * * @param args command line args */ public static void main(String[] args) { OrcKing king = new OrcKing(); king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner")); king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); } }
相关文章推荐
- 设计模式14:Chain of Responsibility Pattern (职责链模式)
- .NET设计模式(23): 职责链模式(Chain of Responsibility Pattern)
- JAVA设计模式之 职责链模式【Chain of Responsibility Pattern】
- 职责链模式(Chain of Responsibility Pattern)
- 设计模式 职责链模式(Chain of Responsibility Pattern)
- 职责链模式(Chain of Responsibility Pattern)
- C#设计模式之二十职责链模式(Chain of Responsibility Pattern)【行为型】
- 深入浅出设计模式——职责链模式(Chain of Responsibility Pattern)
- 设计模式 -- 职责链模式(Chain of Responsibility Pattern)
- C#设计模式——职责链模式(Chain Of Responsibility Pattern)
- 十一个行为型模式1:职责链模式-Chain of Responsibility Pattern【学习难度:★★★☆☆,使用频率:★★☆☆☆】
- 设计模式(行为型)之职责链模式(Chain of Responsibility Pattern)
- "围观"设计模式(22)--行为型之职责链模式(Chain Of Responsibility Pattern)
- 设计模式笔记18:职责链模式(Chain of Responsibility Pattern)
- 职责链模式(Chain of Responsibility Pattern)
- 设计模式笔记18:职责链模式(Chain of Responsibility Pattern)
- 职责链模式(Chain of Responsibility Pattern) 转
- 职责链模式(Chain of Responsibility Pattern)
- 职责链模式(Chain of Responsibility Pattern)-餐馆的故事
- 设计模式职责链模式(ChainofResponsibilityPattern)