publicclassChainPatternDemo { privatestatic AbstractLogger getChainOfLoggers(){ AbstractLoggererrorLogger=newErrorLogger(AbstractLogger.ERROR); AbstractLoggerfileLogger=newFileLogger(AbstractLogger.DEBUG); AbstractLoggerconsoleLogger=newConsoleLogger(AbstractLogger.INFO); errorLogger.setNextLogger(fileLogger); fileLogger.setNextLogger(consoleLogger); return errorLogger; } publicstaticvoidmain(String[] args) { AbstractLoggerloggerChain= getChainOfLoggers(); loggerChain.logMessage(AbstractLogger.INFO, "This is an information."); loggerChain.logMessage(AbstractLogger.DEBUG, "This is a debug level information."); loggerChain.logMessage(AbstractLogger.ERROR, "This is an error information."); } }
步骤 4
执行程序,输出结果:
1 2 3 4 5 6
Standard Console::Logger: This is an information. File::Logger: This is a debug level information. Standard Console::Logger: This is a debug level information. Error Console::Logger: This is an error information. File::Logger: This is an error information. Standard Console::Logger: This is an error information.