Chain of Responsibility
The wiki definition is "this pattern consists of a source of command objects and a series of processing objects. The processing objects contain logic that defines the types of command objects that it can handle while deferring the rest to the next processing object in the chain. This also provides the mechanism for adding new processing objects to the end of the chain".
Example:
We can think of series of objects with certain processing ability. If the processing ability (or power) of that object is not enough to handle the task, then the request is deferred to another processing object in the chain.
Following picture illustrates the concept:
Client module--------> Processing module_1---->Processing module_2(successor)-->.....->Processing module_n(successor).
If there is no successor of a processing module (sometimes called as a handler), then this module should handle that case.
As a real world example, we can think of the decision making ability of employees and design the processing module as the hierarchy of the employees. If an employee is unable to make the decision, then s/he defers that to his/her senior and so forth.
The related source code from wiki is available in GitHub.
References:
No comments:
Post a Comment