责任链模式

顾名思义,责任链模式(Chain of Responsibility Pattern) 为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。

责任链(Chain Of Responsibility)

Class Diagram

Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Created with IntelliJ IDEA.
* User: 长歌
* Date: 2019/11/6
* Description: 抽象日志组件
*/
public abstract class AbstractLogger {
public static int INFO = 1;
public static int DEBUG = 2;
public static int ERROR = 3;

protected int level;

//责任链中的下一个元素
protected AbstractLogger nextLogger;

public void setNextLogger(AbstractLogger nextLogger) {
this.nextLogger = nextLogger;
}

public void logMessage(int level, String message) {
if (this.level <= level) {
write(message);
}
if (nextLogger != null) {
nextLogger.logMessage(level, message);
}
}

abstract protected void write(String message);
}


public class ConsoleLogger extends AbstractLogger {
public ConsoleLogger(int level){
this.level = level;
}

protected void write(String message) {
System.out.println("Standard Console::Logger: " + message);
}
}


public class ErrorLogger extends AbstractLogger {
public ErrorLogger(int level){
this.level = level;
}

protected void write(String message) {
System.out.println("Error Console::Logger: " + message);
}
}


public class FileLogger extends AbstractLogger {

public FileLogger(int level){
this.level = level;
}

protected void write(String message) {
System.out.println("File::Logger: " + message);
}
}

/**
* Created with IntelliJ IDEA.
* User: 长歌
* Date: 2019/11/6
* Description: 日志工厂类
*/
public class LoggerFactroy {

private LoggerFactroy(){}

public static AbstractLogger getLogger(){
AbstractLogger logger = new ErrorLogger(AbstractLogger.ERROR);
AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG);
AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO);

logger.setNextLogger(fileLogger);
fileLogger.setNextLogger(consoleLogger);
return logger;
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class ChainOfResponsibilityPatternTest {

@Test
public void test() throws Exception {
AbstractLogger logger = LoggerFactroy.getLogger();
logger.logMessage(AbstractLogger.INFO, "This is an information.");

logger.logMessage(AbstractLogger.DEBUG,
"This is a debug level information.");

logger.logMessage(AbstractLogger.ERROR,
"This is an error information.");
}
}

Example

  • [javax.servlet.Filter#doFilter()