-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChainOfResponsibilityTest.java
More file actions
136 lines (111 loc) · 2.81 KB
/
ChainOfResponsibilityTest.java
File metadata and controls
136 lines (111 loc) · 2.81 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
A Behavioral Pattern
The Chain of Responsibility is intended to promote loose coupling
between the sender of a request and its receiver by giving more than
one object an opportunity to handle the request. The receiving objects
are chained and pass the request along the chain until one of the objects handles it.
*/
public class ChainOfResponsibilityTest {
public static void main(String[] args){
System.out.println("-------------- CHAIN_OF_RESPONSIBILITY ---------------");
Handler h1 = new TrimHandler();
Handler h4 = new PerfectCaseHandler();
Handler h2 = new UpperCaseHandler();
Handler h3 = new LowerCaseHandler();
h1.setSuccessor(h2);
h2.setSuccessor(h3);
h3.setSuccessor(h4);
Request req = new Request("ParaDise");
h1.handle(req);
System.out.println(req.get());
}
}
// Request
class Request {
String str = null;
Request(String str){
this.str = str;
}
public String get(){
return str;
}
public void set(String str){
this.str = str;
}
}
// Chain
abstract class Handler {
Handler successor;
public abstract void handle(Request req);
public Handler getSuccessor(){
return successor;
}
public void setSuccessor(Handler handler){
successor = handler;
}
}
// Concrete Chain 1
class TrimHandler extends Handler {
public void handle(Request req){
String str = req.get();
if(str.startsWith(" ") || str.endsWith(" ")){
str = str.trim();
req.set(str);
System.out.println("TrimHandler handled...");
}
else{
if(successor != null){
successor.handle(req);
}
}
}
}
// Concrete Chain 2
class PerfectCaseHandler extends Handler {
public void handle(Request req){
String str = req.get();
if(!str.substring(0, 1).equals(str.toUpperCase().substring(0, 1))
|| !str.substring(1).equals(str.toLowerCase().substring(1)) ){
str = str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
req.set(str);
System.out.println("PerfectCaseHandler handled...");
}
else{
if(successor != null){
successor.handle(req);
}
}
}
}
// Concrete Chain 3
class UpperCaseHandler extends Handler {
public void handle(Request req){
String str = req.get();
if(!str.equals(str.toUpperCase())){
str = str.toUpperCase();
req.set(str);
System.out.println("UpperCaseHandler handled...");
}
else{
if(successor != null){
successor.handle(req);
}
}
}
}
// Concrete Chain 4
class LowerCaseHandler extends Handler {
public void handle(Request req){
String str = req.get();
if(!str.equals(str.toLowerCase())){
str = str.toLowerCase();
req.set(str);
System.out.println("LowerCaseHandler handled...");
}
else{
if(successor != null){
successor.handle(req);
}
}
}
}