请用java写一个简单的职责链模式的代码,这个职责链能自由组合
时间: 2023-03-13 08:24:33 浏览: 86
职责链模式模型代码
下面是一个使用Java实现职责链模式的示例代码:public class ChainPatternDemo {
private static AbstractLogger getChainOfLoggers(){ AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR);
AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG);
AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO); errorLogger.setNextLogger(fileLogger);
fileLogger.setNextLogger(consoleLogger); return errorLogger;
} public static void main(String[] args) {
AbstractLogger loggerChain = getChainOfLoggers(); loggerChain.logMessage(AbstractLogger.INFO,
"This is an information."); loggerChain.logMessage(AbstractLogger.DEBUG,
"This is an debug level information."); loggerChain.logMessage(AbstractLogger.ERROR,
"This is an error information.");
}
}
阅读全文