Design Pattern, Software Development

Chain of Responsibility Design Pattern In Simple Words

The Chain of Responsibility is a behavioral design pattern that processes data using a sequence of loosely-coupled handlers. Each handler receives the data from a handler preceding it. Then, it processes the data. If there are problems with the data, the handler forwards it to the next handler for further processing. Otherwise, the handler throws an Exception or returns control to the calling codes.

Chain of Operations With Particular Responsibilities

The handlers in the Chain of Responsibility design pattern could be anything. They could be a mix of routers, computers, and even people who accept, process, and pass data.

Chain of Responsibility Design Pattern

For example, a student does his assignment. Then, his teacher checks the assignment. Next, the school generates a report card. The sequence of handlers could vary but still result in the same outcome.

Chain of Responsibility In Programming

In programming, each handler of the Chain of Responsibility design pattern is a set of codes (or a service) that perform a specific task. Moreover, it accepts, processes, and passes data to the next handler. Also, it could be the first or last handler in the chain. If it is the first handler in the chain, it accepts data directly from the calling codes. Meanwhile, the last handler completes the data processing and returns the control and data to the calling codes. Alternatively, it could produce an Exception error like the handlers in the middle of the chain.

The chaining of the handlers in the Chain of Responsibility pattern could be part of a configuration from which they derive their order. Moreover, the specific ordering could come from the nature of their operations. For example, a handler that reads a JSON file goes first. Then, another handler transforms a JSON string into XML data. Finally, another final handler transfers the XML to remote storage like an FTP server.

Chain of Responsibility Off The Gang of Four

Explaining the Chain of Responsibility in simple words is hard because we tend to oversimplify the design pattern. Therefore, we consult the technical reference GOF Design Patterns.

This design pattern intends to have a set of independent objects that we can easily configure as senders to or receivers from other objects. More importantly, the pattern avoids coupling the sender of a data (request) to its receiver by giving more than one object to handle the data. As a result, a handler in the chain does not know its data’s immediate sender and receiver.

In other words:

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.

Other Design Patterns

The Chain of Responsibility design pattern is only one of the behavioral design patterns we can use to create maintainable software applications. Check out the other design patterns on Design Patterns Are The Ultimate Toolkit For Programmers.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like