Skip to content

Commit 6383491

Browse files
committed
refactoring, add CoF example 1,2,3
1 parent 4ffa51b commit 6383491

23 files changed

+815
-11
lines changed

Diff for: .idea/uiDesigner.xml

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ Yet another collection of Java Design Patterns
44
This repository contains code samples from the related posts of my blog
55
I'm adding new samples from time to time.
66

7-
1. Chain Of Responsibility (COF) Pattern Java Examples
8-
2. Interpreter Pattern Java Examples
7+
1. Chain Of Responsibility (COF) Pattern Java Examples 1
8+
2. Chain Of Responsibility (COF) Pattern Java Examples 2
9+
3. Chain Of Responsibility (COF) Pattern Java Examples 3
10+
4. Interpreter Pattern Java Examples
911

1012
# Contribute
1113

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015 Massimo Caliman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
package io.github.mcaliman.patterns.chain.of.responsibility.example1;
25+
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
28+
29+
/**
30+
* @author Massimo Caliman
31+
*/
32+
public class AHandler implements Handler {
33+
34+
private static final Logger LOG = Logger.getLogger(AHandler.class.getName());
35+
36+
private Handler next;
37+
38+
@Override
39+
public void next(Handler handler) {
40+
next = handler;
41+
}
42+
43+
@Override
44+
public void handleRequest(Entry e) {
45+
if (!e.isPropertyA()) {
46+
next.handleRequest(e);
47+
} else {
48+
//handle request
49+
LOG.log(Level.INFO, "> {0} : call AHandler ", e.toString());
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015 Massimo Caliman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
package io.github.mcaliman.patterns.chain.of.responsibility.example1;
25+
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
28+
29+
/**
30+
* @author Massimo Caliman
31+
*/
32+
public class BHandler implements Handler {
33+
34+
private static final Logger LOG = Logger.getLogger(BHandler.class.getName());
35+
36+
private Handler next;
37+
38+
@Override
39+
public void next(Handler handler) {
40+
next = handler;
41+
}
42+
43+
@Override
44+
public void handleRequest(Entry e) {
45+
if (!e.isPropertyB()) {
46+
next.handleRequest(e);
47+
} else {
48+
//handle request
49+
LOG.log(Level.INFO, "> {0} : call BHandler ", e.toString());
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015 Massimo Caliman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
package io.github.mcaliman.patterns.chain.of.responsibility.example1;
25+
26+
/**
27+
* Se un handler giusto viene trovato ci si ferma nella catena e non si
28+
* prosegue.
29+
* <p>
30+
* Chain seq. stop iff match property.
31+
*
32+
* @author Massimo Caliman
33+
*/
34+
public class Client {
35+
36+
private EntryProcessor processor;
37+
38+
public Client() {
39+
processor = new EntryProcessor();
40+
processor.addHandler(new AHandler());
41+
processor.addHandler(new BHandler());
42+
}
43+
44+
public static void main(String[] args) {
45+
Entry e1 = new Entry();
46+
e1.setName("Object with property A and not B");
47+
e1.setPropertyA(true);
48+
e1.setPropertyB(false);
49+
50+
Entry e2 = new Entry();
51+
e2.setName("Object with property B and not A");
52+
e2.setPropertyA(false);
53+
e2.setPropertyB(true);
54+
55+
Client client = new Client();
56+
client.handle(e1);
57+
58+
client.handle(e2);
59+
60+
}
61+
62+
public void handle(Entry email) {
63+
processor.handleRequest(email);
64+
}
65+
}

0 commit comments

Comments
 (0)