Can the handler process the request in the Responsibility Step?

I will not mention here the definitions of the chain of responsibility (CoR). However, I want to know that if there is a series of handlers in the chain, and if the request is processed by all handlers, and the last handlers will not process the request, if the initial handlers are not passed, is it against the pattern?

I'll explain with an example. Let's say an online application allows users to search for books. When a user submits a request, the system performs a search and displays the results. Let's assume that this functionality is implemented in the CoR template.

The first handler takes request parameters (book category, price, author, etc.) and adds additional parameters (such as user country, region, language selected from the request) and performs certain checks.
The second handler takes the output of the first handler and searches the database and creates a list of results. These results are carried over to the third processor. The third handler will order results, filter, change the language. If any of the previous handlers are faulty, the next handlers will not do their job.

Is the use of CoR against its concepts? or any other good sample for this?

+3


source to share


3 answers


The chain of responsibility usually has only one handler to take care of the request.

Great analogy from 1970 Classic Tootsie Roll commercial - "How many licks" , where the boy (request) tries to answer the question: "How many licks does it take to get to the Topi Pop center?"

He goes to the first animal, which does not know the answer, but sends it to the next animal, etc. Finally, the "request" is processed and the boy gets a response.



Only the boy knows about the beginning of the chain (all other references to animals are given sequentially).

In your problem, it seems you have a well-defined (static) processing order. If you don't need to dynamically change steps and dynamically or reuse steps in other projects, there is nothing wrong with a simple modular scheme: it's called Keep it simple dupid - KISS.

+3


source


If a filter is functionally dependent on another (that is, it expects something to happen before it is executed), then the two functions must be executed within the same filter. The main logic behind COR is its modularity, in other words, the fact that you can add / remove filters as you wish without affecting the data flow in the pipe.



+2


source


The idea behind CoR is that each chain handler is unaware of the chain (only its successor delegates the request if it can't handle it), and since you base your chain elements on each other, you are breaking the core concept of CoR in my eyes - I would rather use Decorator to decorate your original object with additional data using the original object. In your example, you will have a query parameter object, and then a result object that will decorate the query parameter object with the results of that query, and then a third object that decorates the results by ordering, filtering and / or changing the language.

+1


source







All Articles