职责链模式(Chain of Responsibility Pattern)
发布时间
阅读量:
阅读量
所有顺序式且分步完成并具有传递特性的操作都可以考虑采用职责链模式。其核心实现方式类似于基于链表结构的设计。职责链由多个节点构成,并通过一系列连接关系形成完整的处理流程。这些节点能够依次接收和转移任务直至最终完成整个操作序列。例如,在一个复杂的系统架构中,默认情况下会采用这种组织形式以确保各环节都能按照其责任范围完成相应的功能模块
类图:
关键是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"));
}
}
全部评论 (0)
还没有任何评论哟~
