-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathMainChainResponsibility.java
More file actions
54 lines (45 loc) · 2.33 KB
/
Copy pathMainChainResponsibility.java
File metadata and controls
54 lines (45 loc) · 2.33 KB
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
package ru.akhcheck.patterns.chainresponsibility;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import ru.akhcheck.patterns.chainresponsibility.handlers.BaseHandler;
import ru.akhcheck.patterns.chainresponsibility.handlers.DivisionZeroHandler;
import ru.akhcheck.patterns.chainresponsibility.handlers.FileHandler;
import ru.akhcheck.patterns.chainresponsibility.handlers.Log;
import ru.akhcheck.patterns.chainresponsibility.handlers.LogLevel;
import ru.akhcheck.patterns.chainresponsibility.handlers.StderrHandler;
/** Точка входа примера цепочки обязанностей. */
public class MainChainResponsibility {
public static void main(String[] args) throws IOException {
Path logPath = Path.of("chain.log");
Files.deleteIfExists(logPath);
// Цепочка собирается вызовами setNext, каждый возвращает следующее звено.
BaseHandler root = new BaseHandler(LogLevel.all());
root.setErrorFunction(MainChainResponsibility::reportError);
DivisionZeroHandler broken = new DivisionZeroHandler(LogLevel.all());
broken.setErrorFunction(MainChainResponsibility::reportError);
root.setNext(broken)
.setNext(new FileHandler(logPath, LogLevel.all()))
.setNext(new StderrHandler(LogLevel.problems()));
List<Log> records = List.of(
new Log(LogLevel.DEBUG, "cache warmed up"),
new Log(LogLevel.INFO, "request handled"),
new Log(LogLevel.ERROR, "database is unreachable")
);
for (Log record : records) {
System.out.println("--- отправляем: " + record);
root.handle(record);
}
System.out.println();
System.out.println("в файл попало всё, потому что у него уровни all:");
System.out.println(Files.readString(logPath, StandardCharsets.UTF_8).strip());
Files.deleteIfExists(logPath);
}
private static void reportError(Exception error, Log log) {
System.out.println(
"звено упало на записи " + log + ", но цепочка продолжила: " + error.getMessage()
);
}
}