Chain of Responsibility Pattern - Design Patterns

What is Chain of Responsibility Pattern?

As the name suggests, the chain of responsibility pattern creates a chain of receiver objects for a request. This pattern decouples sender and receiver of a ask for based on type of relate for. This pattern comes under behavioural patterns.

In this instance, in general each sender contains orientation to another recipient. If one object cannot handle the request then it passes the same to the next sender and so on.

Implementation

We have formed an abstract class Abstract Logger with a level of classification. Then we have formed three types of loggers extending the Abstract Logger. Each logger checks the level of message to its level and print accordingly otherwise does not issue and pass the communication to its next logger.
chain_pattern_uml_diagram

Step 1

Create an abstract logger class.

AbstractLogger.java

Step 2

Create real classes extending the logger.

ConsoleLogger.java

ErrorLogger.java

FileLogger.java

Step 3

Construct different types of loggers. Allocate them error levels and set next logger in each logger. Next logger in each logger represents the part of the sequence.

ChainPatternDemo.java

Step 4

Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Design Patterns Topics