auto commit

This commit is contained in:
CyC2018 2018-06-05 10:40:38 +08:00
parent a3dc8be2fa
commit 4e57c4e034
2 changed files with 103 additions and 1 deletions

View File

@ -584,7 +584,109 @@ abc
### 意图
避免将请求的发送者附加到其接收者,从而使其它对象也可以处理请求;将请求以对象的方式发送到链上直到请求被处理完毕。
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
### 类图
- Handler定义处理请求的接口并且实现后继链successor
<div align="center"> <img src="../pics//691f11eb-31a7-46be-9de1-61f433c4b3c7.png"/> </div><br>
### 实现
```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) {
System.out.println(request.getName() + " is handle by ConcreteHandler1");
return;
}
if (successor != null) {
successor.handleRequest(request);
}
}
}
```
```java
public class ConcreteHandler2 extends Handler{
public ConcreteHandler2(Handler successor) {
super(successor);
}
@Override
protected void handleRequest(Request request) {
if (request.getType() == RequestType.type2) {
System.out.println(request.getName() + " is handle by ConcreteHandler2");
return;
}
if (successor != null) {
successor.handleRequest(request);
}
}
}
```
```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
public enum RequestType {
type1, type2
}
```
```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);
}
}
```
```html
request1 is handle by ConcreteHandler1
request2 is handle by ConcreteHandler2
```
### JDK

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB