publicclassChainPatternDemo{privatestaticAbstractLoggergetChainOfLoggers(){AbstractLoggererrorLogger = newErrorLogger(AbstractLogger.ERROR); AbstractLoggerfileLogger = newFileLogger(AbstractLogger.DEBUG); AbstractLoggerconsoleLogger = newConsoleLogger(AbstractLogger.INFO); errorLogger.setNextLogger(fileLogger); fileLogger.setNextLogger(consoleLogger); returnerrorLogger; }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
执行程序,输出结果:
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.