auto commit

This commit is contained in:
CyC2018 2018-09-07 11:16:10 +08:00
parent e69d324b09
commit 0fbafacb9b

View File

@ -639,22 +639,27 @@ abc
```java ```java
public abstract class Handler { public abstract class Handler {
protected Handler successor; protected Handler successor;
public Handler(Handler successor) { public Handler(Handler successor) {
this.successor = successor; this.successor = successor;
} }
protected abstract void handleRequest(Request request); protected abstract void handleRequest(Request request);
} }
``` ```
```java ```java
public class ConcreteHandler1 extends Handler { public class ConcreteHandler1 extends Handler {
public ConcreteHandler1(Handler successor) { public ConcreteHandler1(Handler successor) {
super(successor); super(successor);
} }
@Override @Override
protected void handleRequest(Request request) { protected void handleRequest(Request request) {
if (request.getType() == RequestType.TYPE1) { if (request.getType() == RequestType.TYPE1) {
@ -669,11 +674,13 @@ public class ConcreteHandler1 extends Handler {
``` ```
```java ```java
public class ConcreteHandler2 extends Handler{ public class ConcreteHandler2 extends Handler {
public ConcreteHandler2(Handler successor) { public ConcreteHandler2(Handler successor) {
super(successor); super(successor);
} }
@Override @Override
protected void handleRequest(Request request) { protected void handleRequest(Request request) {
if (request.getType() == RequestType.TYPE2) { if (request.getType() == RequestType.TYPE2) {
@ -689,22 +696,27 @@ public class ConcreteHandler2 extends Handler{
```java ```java
public class Request { public class Request {
private RequestType type; private RequestType type;
private String name; private String name;
public Request(RequestType type, String name) { public Request(RequestType type, String name) {
this.type = type; this.type = type;
this.name = name; this.name = name;
} }
public RequestType getType() { public RequestType getType() {
return type; return type;
} }
public String getName() { public String getName() {
return name; return name;
} }
} }
``` ```
```java ```java
@ -715,11 +727,15 @@ public enum RequestType {
```java ```java
public class Client { public class Client {
public static void main(String[] args) { public static void main(String[] args) {
Handler handler1 = new ConcreteHandler1(null); Handler handler1 = new ConcreteHandler1(null);
Handler handler2 = new ConcreteHandler2(handler1); Handler handler2 = new ConcreteHandler2(handler1);
Request request1 = new Request(RequestType.TYPE1, "request1"); Request request1 = new Request(RequestType.TYPE1, "request1");
handler2.handleRequest(request1); handler2.handleRequest(request1);
Request request2 = new Request(RequestType.TYPE2, "request2"); Request request2 = new Request(RequestType.TYPE2, "request2");
handler2.handleRequest(request2); handler2.handleRequest(request2);
} }