责任链模式

2016-10-23  本文已影响0人  狐尼克朱迪

定义

当你想要让一个以上的对象有机会能够处理某个请求的时候,就是用责任链模式。

使用场景

例子

兽族部落里的国王(King)负责发号施令,指挥官(Commander)负责保卫城堡,官员(officer)负责审判罪犯,士兵(solider)负责收税。
命令对象是这样的:

public class Request {
  private final RequestType requestType;

  private boolean handled;

  public Request(final RequestType requestType, final String requestDescription) {
    this.requestType = Objects.requireNonNull(requestType);
    this.requestDescription = Objects.requireNonNull(requestDescription);
  }
  public void markHandled() {
    this.handled = true;
  }
}

三种角色分别处理自己对应的国王命令:

public abstract class RequestHandler {
  private RequestHandler next;
  public RequestHandler(RequestHandler next) {
    this.next = next;
  }
  public void handleRequest(Request req) {
    if (next != null) {
      next.handleRequest(req);
    }
  }
  protected void printHandling(Request req) {
    System.out.println(this + " handling request \"" + req + "\"");
  }
}

// 指挥官 负责保卫城堡(DEFEND_CASTLE)
public class OrcCommander extends RequestHandler {
  public OrcCommander(RequestHandler handler) {
    super(handler);
  }

  public void handleRequest(Request req) {
    if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
      printHandling(req);
      req.markHandled();
    } else {
      super.handleRequest(req);
    }
  }
}

// 官员 负责审判犯人(TORTURE_PRISONER)
public class OrcOfficer extends RequestHandler {
  public OrcOfficer(RequestHandler handler) {
    super(handler);
  }

  public void handleRequest(Request req) {
    if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
      printHandling(req);
      req.markHandled();
    } else {
      super.handleRequest(req);
    }
  }
}

// 士兵 负责收税(COLLECT_TAX)
public class OrcSoldier extends RequestHandler {
  public OrcSoldier(RequestHandler handler) {
    super(handler);
  }

  public void handleRequest(Request req) {
    if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
      printHandling(req);
      req.markHandled();
    } else {
      super.handleRequest(req);
    }
  }
}

国王构建一个职责链,请求会经过这个链条进行请求处理:

// 国王 发号施令。  命令会经过一个有指挥官、官员、士兵组成的职责链处理
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);
  }
}

发个命令试试:

// 搞个命令
public class App {
  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"));
  }
}

分析

职责链的优点:

职责链的用途和缺点:

参考

iluwatar/java-design-patterns

上一篇下一篇

猜你喜欢

热点阅读