责任链模式

责任链模式UML类图

责任链模式UML类图

责任链模式java实现

/**
* 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();
}

/**
* OrcCommander.
指挥官
*/
public class OrcCommander extends RequestHandler {

public OrcCommander(RequestHandler handler) {
super(handler);
}

@Override
public void handleRequest(Request req) {
//防御城堡
if (RequestType.DEFEND_CASTLE == req.getRequestType()) {
printHandling(req);
req.markHandled();
} else {
super.handleRequest(req);
}
}

@Override
public String toString() {
return "Orc commander";
}
}

/**
* OrcOfficer.

*/
public class OrcOfficer extends RequestHandler {

public OrcOfficer(RequestHandler handler) {
super(handler);
}

@Override
public void handleRequest(Request req) {
//刑讯犯
if (RequestType.TORTURE_PRISONER == req.getRequestType()) {
printHandling(req);
req.markHandled();
} else {
super.handleRequest(req);
}
}

@Override
public String toString() {
return "Orc officer";
}

}

/**
* OrcSoldier.
士兵
*/
public class OrcSoldier extends RequestHandler {

public OrcSoldier(RequestHandler handler) {
super(handler);
}

@Override
public void handleRequest(Request req) {
//征税
if (RequestType.COLLECT_TAX == req.getRequestType()) {
printHandling(req);
req.markHandled();
} else {
super.handleRequest(req);
}
}

@Override
public String toString() {
return "Orc soldier";
}
}

/**
* OrcKing makes requests that are handled by the chain.

*/
public class OrcKing {

private RequestHandler chain;

public OrcKing() {
buildChain();
}

private void buildChain() {
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
}

public void makeRequest(Request req) {
chain.handleRequest(req);
}

}

/**
* RequestType enumeration.
*/
public enum RequestType {

DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX

}

/**
* 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();
}

}

/**
* 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) {

var 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"));

}
}

极客时间

实际应用案例

  • Servlet Filter
  • Spring Interceptor
  • Dubbo Filter
  • Netty ChannelPipeline

项目实战

  • 敏感词过滤