Skip to content

Commit 0236658

Browse files
committed
Added Chain of Responsibility pattern and tests.
1 parent bdcb230 commit 0236658

2 files changed

Lines changed: 142 additions & 1 deletion

File tree

‎Java/src/designpatterns/ChainOfResponsibility.java‎

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,82 @@
99
*
1010
* @author jtherrell
1111
*/
12-
class ChainOfResponsibility {
12+
abstract class HelpHandler {
13+
protected static final int NO_HELP_TOPIC = -1;
14+
public static Log log;
15+
16+
protected HelpHandler successor;
17+
protected int helpTopicID;
1318

19+
public HelpHandler(HelpHandler successor, int helpTopicID) {
20+
this.successor = successor;
21+
this.helpTopicID = helpTopicID;
22+
}
23+
24+
public boolean hasHelpFor(int helpTopicID) {
25+
return this.helpTopicID == helpTopicID;
26+
}
27+
28+
public void handleHelp(int helpTopicID) {
29+
log.append("In HelpHandler...");
30+
if (successor != null)
31+
successor.handleHelp(helpTopicID);
32+
}
33+
}
34+
35+
class HelpWidget extends HelpHandler {
36+
private HelpWidget parent;
37+
38+
public HelpWidget(HelpWidget widget, int helpTopicID) {
39+
super(widget, helpTopicID);
40+
parent = widget;
41+
}
42+
}
43+
44+
class HelpButton extends HelpWidget {
45+
public HelpButton(HelpWidget widget, int helpTopicID) {
46+
super(widget, helpTopicID);
47+
}
48+
49+
@Override
50+
public void handleHelp(int helpTopicID) {
51+
log.append("In HelpButton...");
52+
if (hasHelpFor(helpTopicID)) {
53+
log.append("Button helped!");
54+
} else {
55+
super.handleHelp(helpTopicID);
56+
}
57+
}
58+
}
59+
60+
class HelpDialog extends HelpWidget {
61+
public HelpDialog(HelpWidget widget, int helpTopicID) {
62+
super(widget, helpTopicID);
63+
}
64+
65+
@Override
66+
public void handleHelp(int helpTopicID) {
67+
log.append("In HelpDialog...");
68+
if (hasHelpFor(helpTopicID)) {
69+
log.append("Dialog helped!");
70+
} else {
71+
super.handleHelp(helpTopicID);
72+
}
73+
}
74+
}
75+
76+
class HelpApplication extends HelpWidget {
77+
public HelpApplication(int helpTopicID) {
78+
super(null, helpTopicID);
79+
}
80+
81+
@Override
82+
public void handleHelp(int helpTopicID) {
83+
log.append("In HelpApplication...");
84+
if (hasHelpFor(helpTopicID)) {
85+
log.append("Application helped!");
86+
} else {
87+
super.handleHelp(helpTopicID);
88+
}
89+
}
1490
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
6+
package designpatterns;
7+
8+
import org.junit.AfterClass;
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
*
15+
* @author jtherrell
16+
*/
17+
public class ChainOfResponsibilityTest {
18+
final int PRINT_TOPIC = 1;
19+
final int PAPER_ORIENTATION_TOPIC = 2;
20+
final int APPLICATION_TOPIC = 3;
21+
22+
private HelpApplication application;
23+
private HelpDialog dialog;
24+
private HelpButton button;
25+
26+
private Log log;
27+
28+
public ChainOfResponsibilityTest() {
29+
log = new Log();
30+
HelpHandler.log = log;
31+
application = new HelpApplication(APPLICATION_TOPIC);
32+
dialog = new HelpDialog(application, PRINT_TOPIC);
33+
button = new HelpButton(dialog, PAPER_ORIENTATION_TOPIC);
34+
}
35+
36+
// TODO add test methods here.
37+
// The methods must be annotated with annotation @Test. For example:
38+
//
39+
@Test
40+
public void testExpectedButtonHandle() {
41+
button.handleHelp(PAPER_ORIENTATION_TOPIC);
42+
String expResult = "In HelpButton...Button helped!";
43+
String result = log.toString();
44+
assertEquals(expResult, result);
45+
}
46+
47+
@Test
48+
public void testExpectedDialogHandle() {
49+
button.handleHelp(PRINT_TOPIC);
50+
String expResult = "In HelpButton...In HelpHandler..." +
51+
"In HelpDialog...Dialog helped!";
52+
String result = log.toString();
53+
assertEquals(expResult, result);
54+
}
55+
56+
@Test
57+
public void testExpectedApplicationHandle() {
58+
button.handleHelp(APPLICATION_TOPIC);
59+
String expResult = "In HelpButton...In HelpHandler..." +
60+
"In HelpDialog...In HelpHandler..." +
61+
"In HelpApplication...Application helped!";
62+
String result = log.toString();
63+
assertEquals(expResult, result);
64+
}
65+
}

0 commit comments

Comments
 (0)