Chain-of-responsibility pattern: Difference between revisions

Content deleted Content added
fixed spelling of responsibility
Line 9:
=== Java ===
The following Java code illustrates the pattern with the example of a logging class. Each logging handler decides if any action is to be taken at this log level and then passes the message on to the next logging handler. The output is:
Writing to debug output: Entering function xy.
Writing to debug output: Step1 completed.
Writing to stderr: Step1 completed.
Line 19:
import java.util.*;
'''abstract class Logger''' {
public static int ERR = 3;
public static int NOTICE = 5;
Line 31:
}
'''class DebugLogger ''extends Logger''''' {
public DebugLogger(int mask) { this.mask = mask; }
Line 40:
}
'''class EMailLogger ''extends Logger''''' {
public EMailLogger(int mask) { this.mask = mask; }
Line 49:
}
'''class StderrLogger ''extends Logger''''' {
public StderrLogger(int mask) { this.mask = mask; }
Line 58:
}
'''class ChainOfResponsibilityExample''' {
public static void main(String[] args) {
// building the chain of responsibility
Line 65:
new StderrLogger(Logger.NOTICE) ) );
l.message("Entering function xy.", Logger.DEBUG); ''// handled by DebugLogger''
l.message("Step1 completed.", Logger.NOTICE); ''// handled by Debug- and StderrLogger''
l.message("An error has occurred.", Logger.ERR); ''// handled by all three Logger''
}
}
 
 
 
==See also==