Skip to content

Commit ed98e7a

Browse files
committed
Update chain of responsibility
1 parent 0f9f3b9 commit ed98e7a

15 files changed

+24
-15
lines changed

chain/README.md renamed to chain-of-responsibility/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
---
22
layout: pattern
33
title: Chain of responsibility
4-
folder: chain
5-
permalink: /patterns/chain/
4+
folder: chain-of-responsibility
5+
permalink: /patterns/chain-of-responsibility/
66
categories: Behavioral
77
language: en
88
tags:
99
- Gang of Four
1010
---
1111

1212
## Intent
13+
1314
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to
1415
handle the request. Chain the receiving objects and pass the request along the chain until an object
1516
handles it.
1617

1718
## Explanation
1819

19-
Real world example
20+
Real-world example
2021

2122
> The Orc King gives loud orders to his army. The closest one to react is the commander, then
22-
> officer and then soldier. The commander, officer and soldier here form a chain of responsibility.
23+
> an officer, and then a soldier. The commander, officer, and soldier form a chain of responsibility.
2324
2425
In plain words
2526

@@ -35,7 +36,7 @@ Wikipedia says
3536
3637
**Programmatic Example**
3738

38-
Translating our example with the orcs from above. First we have the `Request` class:
39+
Translating our example with the orcs from above. First, we have the `Request` class:
3940

4041
```java
4142
public class Request {
@@ -66,7 +67,7 @@ public enum RequestType {
6667
}
6768
```
6869

69-
Then the request handler hierarchy
70+
Next, we show the request handler hierarchy.
7071

7172
```java
7273
@Slf4j
@@ -116,7 +117,7 @@ public class OrcCommander extends RequestHandler {
116117

117118
```
118119

119-
Then we have the Orc King who gives the orders and forms the chain
120+
Th Orc King gives the orders and forms the chain.
120121

121122
```java
122123
public class OrcKing {
@@ -136,18 +137,26 @@ public class OrcKing {
136137
}
137138
```
138139

139-
Then it is used as follows
140+
The chain of responsibility in action.
140141

141142
```java
142143
var king = new OrcKing();
143-
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); // Orc commander handling request "defend castle"
144-
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner")); // Orc officer handling request "torture prisoner"
145-
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc soldier handling request "collect tax"
144+
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
145+
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
146+
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
147+
```
148+
149+
The console output.
150+
151+
```
152+
Orc commander handling request "defend castle"
153+
Orc officer handling request "torture prisoner"
154+
Orc soldier handling request "collect tax"
146155
```
147156

148157
## Class diagram
149158

150-
![alt text](./etc/chain.urm.png "Chain of Responsibility class diagram")
159+
![alt text](./etc/chain-of-responsibility.urm.png "Chain of Responsibility class diagram")
151160

152161
## Applicability
153162

@@ -157,7 +166,7 @@ Use Chain of Responsibility when
157166
* You want to issue a request to one of several objects without specifying the receiver explicitly.
158167
* The set of objects that can handle a request should be specified dynamically.
159168

160-
## Real world examples
169+
## Real-world examples
161170

162171
* [java.util.logging.Logger#log()](http://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html#log%28java.util.logging.Level,%20java.lang.String%29)
163172
* [Apache Commons Chain](https://commons.apache.org/proper/commons-chain/index.html)

chain/pom.xml renamed to chain-of-responsibility/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<artifactId>java-design-patterns</artifactId>
3232
<version>1.25.0-SNAPSHOT</version>
3333
</parent>
34-
<artifactId>chain</artifactId>
34+
<artifactId>chain-of-responsibility</artifactId>
3535
<dependencies>
3636
<dependency>
3737
<groupId>org.junit.jupiter</groupId>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
<module>facade</module>
106106
<module>flyweight</module>
107107
<module>proxy</module>
108-
<module>chain</module>
108+
<module>chain-of-responsibility</module>
109109
<module>command</module>
110110
<module>interpreter</module>
111111
<module>iterator</module>

0 commit comments

Comments
 (0)