Chain of Responsibility Pattern
Chain of responsibility pattern consist source of command objects and chains of processing objects. Here all individual processing objects will be containing the logic that defines what types of command objects it could handle and after filtering out commands rest will be passed to next processing object in series. In the last mechanism will be applied to add new processing objects.
In deviation of standard chain of responsibility pattern model some of the handlers could act as dispatchers and capable to send commands in variety of directions which forms tree of responsibility. In some of the scenario it could occurs recursively with the processing objects calls higher processing objects using commands which will attempt to solve smaller part of problem and this case recursion will be continue until the last command has processed or in another words until entire tree been explored. Best example is XML interpreter which works in this manner. It promotes idea of loose coupling which is known as very good programming practice.
Example:
Below code showing pattern with example of logging class. Here log handler decides if any action should take at this logger level and once done pass message to next logging handler.
Also please take note here that when you do actual implementation of chain of responsibility pattern logger will not pass responsibility further down to chain after handling message. Our example message will be passing down to chain whether it handled or not.
- Create abstract class Logger:
public abstract class Logger { public static int ERR = 3; public static int NOTICE = 5; public static int DEBUG = 7; protected int mask; // The next element in the chain of responsibility protected Logger next; public Logger setNext(Logger log) { next = log; return log; } public void message(String msg, int priority) { if (priority <= mask) { writeMessage(msg); } if (next != null) { next.message(msg, priority); } } abstract protected void writeMessage(String msg); }
- Create class StdoutLogger and extends Logger
public class StdoutLogger extends Logger { public StdoutLogger(int mask) { this.mask = mask; } protected void writeMessage(String msg) { System.out.println("Writing to stdout: " + msg); } }
- Create class EmailLogger and extends Logger
public class EmailLogger extends Logger { public EmailLogger(int mask) { this.mask = mask; } protected void writeMessage(String msg) { System.out.println("Sending via email: " + msg); } }
- Create class StderrLoggerand extends Logger
public class StderrLogger extends Logger { public StderrLogger(int mask) { this.mask = mask; } protected void writeMessage(String msg) { System.err.println("Sending to stderr: " + msg); } }
- Create test class chainOfResponsibilty
public class ChainOfResponsibilityExample { public static void main(String[] args) { // Build the chain of responsibility Logger logger, logger1, logger2; logger = new StdoutLogger(Logger.DEBUG); logger1 = logger.setNext(new EmailLogger(Logger.NOTICE)); logger2 = logger1.setNext(new StderrLogger(Logger.ERR)); // Handled by StdoutLogger logger.message("Entering function y.", Logger.DEBUG); // Handled by StdoutLogger and EmailLogger logger.message("Step1 completed.", Logger.NOTICE); // Handled by all three loggers logger.message("An error has occurred.", Logger.ERR); } }
- That’s it. Below is list of all design patterns link:
Creational Patterns:
Structural Patterns:
Behavioral Patterns:
- Chain of Responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template Method
- Visitor