diff --git a/notes/设计模式.md b/notes/设计模式.md index 4f561df4..536072c2 100644 --- a/notes/设计模式.md +++ b/notes/设计模式.md @@ -639,22 +639,27 @@ abc ```java public abstract class Handler { + protected Handler successor; + public Handler(Handler successor) { this.successor = successor; } + protected abstract void handleRequest(Request request); } ``` ```java public class ConcreteHandler1 extends Handler { + public ConcreteHandler1(Handler successor) { super(successor); } + @Override protected void handleRequest(Request request) { if (request.getType() == RequestType.TYPE1) { @@ -669,11 +674,13 @@ public class ConcreteHandler1 extends Handler { ``` ```java -public class ConcreteHandler2 extends Handler{ +public class ConcreteHandler2 extends Handler { + public ConcreteHandler2(Handler successor) { super(successor); } + @Override protected void handleRequest(Request request) { if (request.getType() == RequestType.TYPE2) { @@ -689,22 +696,27 @@ public class ConcreteHandler2 extends Handler{ ```java public class Request { + private RequestType type; private String name; + public Request(RequestType type, String name) { this.type = type; this.name = name; } + public RequestType getType() { return type; } + public String getName() { return name; } } + ``` ```java @@ -715,11 +727,15 @@ public enum RequestType { ```java public class Client { + public static void main(String[] args) { + Handler handler1 = new ConcreteHandler1(null); Handler handler2 = new ConcreteHandler2(handler1); + Request request1 = new Request(RequestType.TYPE1, "request1"); handler2.handleRequest(request1); + Request request2 = new Request(RequestType.TYPE2, "request2"); handler2.handleRequest(request2); }