|
9 | 9 | ---
|
10 | 10 |
|
11 | 11 | ## Intent
|
12 |
| -Avoid coupling the sender of a request to its receiver by giving |
13 |
| -more than one object a chance to handle the request. Chain the receiving |
14 |
| -objects and pass the request along the chain until an object handles it. |
| 12 | +Avoid coupling the sender of a request to its receiver by giving more than one object a chance to |
| 13 | +handle the request. Chain the receiving objects and pass the request along the chain until an object |
| 14 | +handles it. |
15 | 15 |
|
16 | 16 | ## Explanation
|
17 | 17 |
|
18 | 18 | Real world example
|
19 | 19 |
|
20 |
| -> The Orc King gives loud orders to his army. The closest one to react is the commander, then officer and then soldier. The commander, officer and soldier here form a chain of responsibility. |
| 20 | +> The Orc King gives loud orders to his army. The closest one to react is the commander, then |
| 21 | +> officer and then soldier. The commander, officer and soldier here form a chain of responsibility. |
21 | 22 |
|
22 | 23 | In plain words
|
23 | 24 |
|
24 |
| -> It helps building a chain of objects. Request enters from one end and keeps going from object to object till it finds the suitable handler. |
| 25 | +> It helps to build a chain of objects. A request enters from one end and keeps going from an object |
| 26 | +> to another until it finds a suitable handler. |
25 | 27 |
|
26 | 28 | Wikipedia says
|
27 | 29 |
|
28 |
| -> In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. |
| 30 | +> In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of |
| 31 | +> a source of command objects and a series of processing objects. Each processing object contains |
| 32 | +> logic that defines the types of command objects that it can handle; the rest are passed to the |
| 33 | +> next processing object in the chain. |
29 | 34 |
|
30 | 35 | **Programmatic Example**
|
31 | 36 |
|
32 |
| -Translating our example with orcs from above. First we have the request class |
| 37 | +Translating our example with the orcs from above. First we have the `Request` class: |
33 | 38 |
|
34 | 39 | ```java
|
35 | 40 | public class Request {
|
@@ -140,14 +145,16 @@ king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc so
|
140 | 145 | ```
|
141 | 146 |
|
142 | 147 | ## Class diagram
|
| 148 | + |
143 | 149 | 
|
144 | 150 |
|
145 | 151 | ## Applicability
|
| 152 | + |
146 | 153 | Use Chain of Responsibility when
|
147 | 154 |
|
148 |
| -* more than one object may handle a request, and the handler isn't known a priori. The handler should be ascertained automatically |
149 |
| -* you want to issue a request to one of several objects without specifying the receiver explicitly |
150 |
| -* the set of objects that can handle a request should be specified dynamically |
| 155 | +* More than one object may handle a request, and the handler isn't known a priori. The handler should be ascertained automatically. |
| 156 | +* You want to issue a request to one of several objects without specifying the receiver explicitly. |
| 157 | +* The set of objects that can handle a request should be specified dynamically. |
151 | 158 |
|
152 | 159 | ## Real world examples
|
153 | 160 |
|
|
0 commit comments