Chain of Responsibility là gì? Trong trường hợp nào thì áp dụng pattern này?
Chain of Responsibility là gì? Trong trường hợp nào thì áp dụng pattern này?
Chain of Responsibility: chuyển yêu cầu dọc theo chuỗi handler; mỗi handler tự xử lý hoặc chuyển tiếp cho handler kế tiếp — người gửi không biết ai sẽ xử lý.
abstract class Handler {
private Handler next;
Handler setNext(Handler n) { this.next = n; return n; }
final void handle(Request r) {
if (!process(r) && next != null) next.handle(r);
}
protected abstract boolean process(Request r); // true = đã xử lý
}
auth.setNext(rateLimit).setNext(validation).setNext(controller);
auth.handle(request);
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.