Keep a distance from the noise and trade with dreams on an equal footing

Avoid coupling the sender and receiver of the request, make it possible for multiple objects to receive the request, connect these objects into a chain, and pass the request along the chain until an object processes it

The handler in the responsibility chain is responsible for processing the request. The customer only needs to send the request to the responsibility chain, and does not need to care about the processing details of the request and the transmission of the request. Therefore, the responsibility chain decouples the sender of the request from the handler of the request

Reducing coupling simplifies objects. Objects do not need to know the structure of the chain to enhance the flexibility of assigning responsibilities to objects. By changing the members in the chain or transferring their order, it is very convenient to dynamically add or delete responsibilities and add new request processing classes

It is not guaranteed that the request will be accepted, and the system performance will be affected to some extent, which may cause circular call, and it may be difficult to observe the runtime characteristics, which hinders debugging

We create an abstract class abstractlogger with a detailed logging level. Then we create three types of recorders, all of which extend abstractlogger. Whether the message level of each recorder belongs to its own level. If so, it will be printed accordingly. Otherwise, it will not be printed and the message will be transmitted to the next recorder.

AbstractLogger. java

@Override protected void write(String message) { System.out.println("Standard Console::Logger: " + message); } } < pre class="java">public class ErrorLogger extends AbstractLogger {

@Override protected void write(String message) { System.out.println("Error Console::Logger: " + message); } } < pre class="java">public class FileLogger extends AbstractLogger {

@Override protected void write(String message) { System.out.println("File::Logger: " + message); } } < H4 id = "demo use" > demo use

Create different types of recorders. Give them different error levels and set the next recorder in each recorder. The next recorder in each recorder represents part of the chain.

ChainPatternDemo.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.");

}
}

Standard Console::Logger: This is an information.
File::Logger: This is an debug level information.
Standard Console::Logger: This is an 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.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>