docs: update chain

This commit is contained in:
Ilkka Seppälä
2024-05-24 18:57:17 +03:00
parent 8162dc19fb
commit ccad122750
+19 -12
View File
@@ -3,8 +3,10 @@ title: Chain of responsibility
category: Behavioral
language: en
tag:
- Gang of Four
- Decoupling
- Event-driven
- Gang of Four
- Messaging
---
## Also known as
@@ -21,7 +23,7 @@ Avoid coupling the sender of a request to its receiver by giving more than one o
Real-world example
> The Orc King gives loud orders to his army. The closest one to react is the commander, then an officer, and then a soldier. The commander, officer, and soldier form a chain of responsibility.
> An analogous real-world example of the Chain of Responsibility pattern is a technical support call center. When a customer calls in with an issue, the call is first received by a front-line support representative. If the issue is simple, the representative handles it directly. If the issue is more complex, the representative forwards the call to a second-level support technician. This process continues, with the call being escalated through multiple levels of support until it reaches a specialist who can resolve the problem. Each level of support represents a handler in the chain, and the call is passed along the chain until it finds an appropriate handler, thereby decoupling the request from the specific receiver.
In plain words
@@ -33,7 +35,9 @@ Wikipedia says
**Programmatic Example**
Translating our example with the orcs from above. First, we have the `Request` class:
The Orc King gives loud orders to his army. The closest one to react is the commander, then an officer, and then a soldier. The commander, officer, and soldier form a chain of responsibility.
First, we have the `Request` class:
```java
import lombok.Getter;
@@ -103,7 +107,7 @@ public class OrcCommander implements RequestHandler {
}
}
// OrcOfficer and OrcSoldier are defined similarly as OrcCommander
// OrcOfficer and OrcSoldier are defined similarly as OrcCommander ...
```
@@ -136,13 +140,16 @@ public class OrcKing {
The chain of responsibility in action.
```java
var king=new OrcKing();
king.makeRequest(new Request(RequestType.DEFEND_CASTLE,"defend castle"));
king.makeRequest(new Request(RequestType.TORTURE_PRISONER,"torture prisoner"));
king.makeRequest(new Request(RequestType.COLLECT_TAX,"collect tax"));
public static void main(String[] args) {
var king = new OrcKing();
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
}
```
The console output.
The console output:
```
Orc commander handling request "defend castle"
@@ -152,7 +159,7 @@ Orc soldier handling request "collect tax"
## Class diagram
![alt text](./etc/chain-of-responsibility.urm.png "Chain of Responsibility class diagram")
![Chain of Responsibility](./etc/chain-of-responsibility.urm.png "Chain of Responsibility class diagram")
## Applicability
@@ -193,8 +200,8 @@ Trade-Offs:
## Credits
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/49NGldq)
* [Pattern-Oriented Software Architecture, Volume 1: A System of Patterns](https://amzn.to/3PAJUg5)
* [Refactoring to Patterns](https://amzn.to/3VOO4F5)
* [Pattern languages of program design 3](https://amzn.to/4a4NxTH)