What Is a Chain of Responsibility?
The chain of responsibility pattern is a design pattern. In the chain of responsibility model, many objects are linked by each object's reference to its next home to form a chain. The request is passed on this chain until an object on the chain decides to process the request. The client making this request does not know which object on the chain will eventually process the request, which allows the system to dynamically reorganize and assign responsibilities without affecting the client.
- The chain of responsibility pattern is a kind of object-oriented programming
- The roles involved in the chain of responsibility model are as follows:
- Abstract Handler role: defines an interface for processing requests. If needed, the interface can define a method to set and return a reference to the next home. This role is usually implemented by a Java abstract class or Java interface. The aggregation relationship of the Handler class in the figure above gives a concrete subclass reference to the next home. The abstract method handleRequest () regulates the operation of the subclass to handle the request.
- Specific Handler (ConcreteHandler) role: After receiving the request, the specific handler can choose to process the request or pass the request to the next family. Because the specific processor holds a reference to the next home, the specific processor can visit the next home if needed. [2]
- The following logging example demonstrates this pattern. Each logging handler first decides whether it needs to do processing at this layer, and then passes control to the next logging handler. The output of the program is:
- Writing to debug output: Entering function y. Writing to debug output: Step1 completed. Sending via e-mail: Step1 completed. Writing to debug output: An error has occurred. Sending via e-mail: An error has occurred. Writing to stderr : An error has occurred.
- Note: This example is not the recommended implementation for logging classes.
- At the same time, it should be noted that in the implementation of the chain of responsibility model, if the logger has been processed at a certain level, the logger will not be passed on. In our example, the message will be passed to the lowest level regardless of whether it has been processed.
import java.util. *; 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 l) {next = l; return this;} public final void message (String msg, int priority) {if (priority <= mask) {writeMessage (msg); if (next! = null) {next .message (msg, priority);}}} protected abstract void writeMessage (String msg);} class StdoutLogger extends Logger {public StdoutLogger (int mask) {this.mask = mask;} protected void writeMessage (String msg) {System. out.println ("Writting to stdout:" + msg);}} class EmailLogger extends Logger {public EmailLog ger (int mask) {this.mask = mask;} protected void writeMessage (String msg) {System.out.println ("Sending via email:" + msg);)} class StderrLogger extends Logger {public StderrLogger (int mask) {this.mask = mask;} protected void writeMessage (String msg) {System.out.println ("Sending to stderr:" + msg);}} public class ChainOfResponsibilityExample {public static void main (String [] args) {/ / Build the chain of responsibility Logger l = new StdoutLogger (Logger.DEBUG) .setNext (new EmailLogger (Logger.NOTICE) .setNext (new StderrLogger (Logger.ERR))); // Handled by StdoutLogger l.message ("Entering function y. ", Logger.DEBUG); // Handled by StdoutLogger and EmailLogger l.message (" Step1 completed. ", Logger.NOTICE); // Handled by all three loggers l.message ("An error has occurred.", Logger.ERR);}}