From 96c16a8f3a904c17f47856219b245e52df33c000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Sat, 29 Aug 2020 11:29:30 +0300 Subject: [PATCH] Update README.md --- chain/README.md | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/chain/README.md b/chain/README.md index f11f0c59e..8b0508833 100644 --- a/chain/README.md +++ b/chain/README.md @@ -9,27 +9,32 @@ tags: --- ## Intent -Avoid coupling the sender of a request to its receiver by giving -more than one object a chance to handle the request. Chain the receiving -objects and pass the request along the chain until an object handles it. +Avoid coupling the sender of a request to its receiver by giving more than one object a chance to +handle the request. Chain the receiving objects and pass the request along the chain until an object +handles it. ## Explanation Real world example -> 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. +> 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. In plain words -> 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. +> It helps to build a chain of objects. A request enters from one end and keeps going from an object +> to another until it finds a suitable handler. Wikipedia says -> 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. +> 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. **Programmatic Example** -Translating our example with orcs from above. First we have the request class +Translating our example with the orcs from above. First we have the `Request` class: ```java public class Request { @@ -140,14 +145,16 @@ king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc so ``` ## Class diagram + ![alt text](./etc/chain.urm.png "Chain of Responsibility class diagram") ## Applicability + Use Chain of Responsibility when -* more than one object may handle a request, and the handler isn't known a priori. The handler should be ascertained automatically -* you want to issue a request to one of several objects without specifying the receiver explicitly -* the set of objects that can handle a request should be specified dynamically +* More than one object may handle a request, and the handler isn't known a priori. The handler should be ascertained automatically. +* You want to issue a request to one of several objects without specifying the receiver explicitly. +* The set of objects that can handle a request should be specified dynamically. ## Real world examples