-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandler.java
More file actions
82 lines (62 loc) · 1.95 KB
/
Handler.java
File metadata and controls
82 lines (62 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package handler.demo;
/**
* @author cuishifeng
* @Title: Handler
* @ProjectName handler.demo
* @date 2018-11-14
*/
public abstract class Handler {
protected Handler handler;
public void setHandler(Handler handler) {
this.handler = handler;
}
/**
* 传递处理请求
*
* @param message
*/
protected abstract void handlerRequest(String message);
// -------------------- 子类 ------------------------
static class ConcreteHandlerA extends Handler {
@Override
protected void handlerRequest(String message) {
if (message.equals("A")) {
System.out.println("ConcreteHandlerA 处理请求");
} else {
System.out.println("A转发请求");
this.handler.handlerRequest(message);
}
}
}
static class ConcreteHandlerB extends Handler {
@Override
protected void handlerRequest(String message) {
if (message.equals("B")) {
System.out.println("ConcreteHandlerB 处理请求");
} else {
System.out.println("B转发请求");
this.handler.handlerRequest(message);
}
}
}
static class ConcreteHandlerC extends Handler {
@Override
protected void handlerRequest(String message) {
if (message.equals("C")) {
System.out.println("ConcreteHandlerC 处理请求");
} else {
System.out.println("C转发请求");
this.handler.handlerRequest(message);
}
}
}
public static void main(String[] args) {
Handler handlerA, handlerB, handlerC;
handlerA = new ConcreteHandlerA();
handlerB = new ConcreteHandlerB();
handlerC = new ConcreteHandlerC();
handlerA.setHandler(handlerB);
handlerB.setHandler(handlerC);
handlerA.handlerRequest("C");
}
}