✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.
Quel est le pattern correspondant au code suivant ?
abstract class Logger {
protected int level;
protected Logger next;
public static final int ERR = 0;
public static final int NOTICE = 1;
public static final int DEBUG = 2;
public Logger setNext(Logger next) {
this.next = next;
return this.next;
}
public void message(String msg, int priority) {
if (priority <= this.level)
writeMessage(msg);
else if (this.next != null)
this.next.message(msg, priority);
}
public abstract void writeMessage(String msg);
}
class DebugLogger extends Logger {
public DebugLogger(int level) {
this.level = level;
this.next = null;
}
public void writeMessage(String msg) {
System.out.println("Debug message: " + msg);
}
}
class EmailLogger extends Logger {
public EmailLogger(int level) {
this.level = level;
this.next = null;
}
public void writeMessage(String msg) {
System.out.println("Email notification: " + msg);
}
}
class ErrorLogger extends Logger {
public ErrorLogger(int level) {
this.level = level;
this.next = null;
}
public void writeMessage(String msg) {
System.err.println("Error: " + msg);
}
}
public class Main {
public static void main(String[] args) {
ErrorLogger logger = new ErrorLogger(Logger.ERR);
EmailLogger logger2 = new EmailLogger(Logger.NOTICE);
DebugLogger logger3 = new DebugLogger(Logger.DEBUG);
logger.setNext(logger2);
logger2.setNext(logger3);
logger.message("An error has occurred.", Logger.ERR);
logger.message("Step1 completed.", Logger.NOTICE);
logger.message("Entering function y.", Logger.DEBUG);
}
}
Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!