- Kæde af ansvar mønster
- Fordel ved Chain Of Responsibility DP
- Brug af Chain Of Responsibility DP
- UML af Chain Of Responsibility DP
- Eksempel på Chain Of Responsibility DP
I kæde af ansvar sender afsender en anmodning til en kæde af objekter. Anmodningen kan håndteres af ethvert objekt i kæden.
A Chain of Responsibility Pattern siger det bare 'undgå at koble afsenderen af en anmodning til dens modtager ved at give flere objekter en chance for at håndtere anmodningen'. For eksempel bruger en pengeautomat designmønsteret Chain of Responsibility i pengegivningsprocessen.
Med andre ord kan vi sige, at hver modtager normalt indeholder reference til en anden modtager. Hvis et objekt ikke kan håndtere anmodningen, sender det det samme til den næste modtager og så videre.
Fordel ved Chain of Responsibility Pattern
- Det reducerer koblingen.
- Det tilføjer fleksibilitet, mens det tildeler ansvaret til objekter.
- Det tillader et sæt klasser at fungere som én; hændelser produceret i én klasse kan sendes til andre handlerklasser ved hjælp af sammensætning.
Brug af Chain of Responsibility-mønster:
Det bruges:
java-date nu
- Når mere end ét objekt kan håndtere en anmodning, og behandleren er ukendt.
- Når gruppen af objekter, der kan håndtere anmodningen, skal angives på en dynamisk måde.
Eksempel på Chain of Responsibility-mønster
Lad os forstå eksemplet med Chain of Responsibility Pattern ved ovenstående UML-diagram.
UML for Chain of Responsibility Pattern:
Implementering af ovenstående UML:
Trin 1
Lave en Logger abstrakt klasse.
hvor mange taster har tastaturer
public abstract class Logger { public static int OUTPUTINFO=1; public static int ERRORINFO=2; public static int DEBUGINFO=3; protected int levels; protected Logger nextLevelLogger; public void setNextLevelLogger(Logger nextLevelLogger) { this.nextLevelLogger = nextLevelLogger; } public void logMessage(int levels, String msg){ if(this.levels<=levels){ displayloginfo(msg); } if (nextlevellogger!="null)" { nextlevellogger.logmessage(levels, msg); protected abstract void displayloginfo(string < pre> <h4>Step 2</h4> <p> Create a <b>ConsoleBasedLogger</b> class.</p> File: ConsoleBasedLogger.java <pre> public class ConsoleBasedLogger extends Logger { public ConsoleBasedLogger(int levels) { this.levels=levels; } @Override protected void displayLogInfo(String msg) { System.out.println('CONSOLE LOGGER INFO: '+msg); } } </pre> <h4>Step 3</h4> <p>Create a <b>DebugBasedLogger</b> class.</p> File: DebugBasedLogger.java <pre> public class DebugBasedLogger extends Logger { public DebugBasedLogger(int levels) { this.levels=levels; } @Override protected void displayLogInfo(String msg) { System.out.println('DEBUG LOGGER INFO: '+msg); } }// End of the DebugBasedLogger class. </pre> <h4>Step 4</h4> <p>Create a <b>ErrorBasedLogger</b> class.</p> File: ErrorBasedLogger.java <pre> public class ErrorBasedLogger extends Logger { public ErrorBasedLogger(int levels) { this.levels=levels; } @Override protected void displayLogInfo(String msg) { System.out.println('ERROR LOGGER INFO: '+msg); } }// End of the ErrorBasedLogger class. </pre> <h4>Step 5</h4> <p>Create a <b>ChainOfResponsibilityClient</b> class.</p> File: ChainofResponsibilityClient.java <pre> public class ChainofResponsibilityClient { private static Logger doChaining(){ Logger consoleLogger = new ConsoleBasedLogger(Logger.OUTPUTINFO); Logger errorLogger = new ErrorBasedLogger(Logger.ERRORINFO); consoleLogger.setNextLevelLogger(errorLogger); Logger debugLogger = new DebugBasedLogger(Logger.DEBUGINFO); errorLogger.setNextLevelLogger(debugLogger); return consoleLogger; } public static void main(String args[]){ Logger chainLogger= doChaining(); chainLogger.logMessage(Logger.OUTPUTINFO, 'Enter the sequence of values '); chainLogger.logMessage(Logger.ERRORINFO, 'An error is occured now'); chainLogger.logMessage(Logger.DEBUGINFO, 'This was the error now debugging is compeled'); } } </pre> <hr> download this example <h4>Output</h4> <pre> bilityClient CONSOLE LOGGER INFO: Enter the sequence of values CONSOLE LOGGER INFO: An error is occured now ERROR LOGGER INFO: An error is occured now CONSOLE LOGGER INFO: This was the error now debugging is compeled ERROR LOGGER INFO: This was the error now debugging is compeled DEBUG LOGGER INFO: This was the error now debugging is compeled </pre></=levels){>
Trin 3
Lave en DebugBasedLogger klasse.
Fil: DebugBasedLogger.javapublic class DebugBasedLogger extends Logger { public DebugBasedLogger(int levels) { this.levels=levels; } @Override protected void displayLogInfo(String msg) { System.out.println('DEBUG LOGGER INFO: '+msg); } }// End of the DebugBasedLogger class.
Trin 4
Lave en ErrorBasedLogger klasse.
Fil: ErrorBasedLogger.javapublic class ErrorBasedLogger extends Logger { public ErrorBasedLogger(int levels) { this.levels=levels; } @Override protected void displayLogInfo(String msg) { System.out.println('ERROR LOGGER INFO: '+msg); } }// End of the ErrorBasedLogger class.
Trin 5
Lave en ChainOfResponsibilityClient klasse.
Fil: ChainofResponsibilityClient.javapublic class ChainofResponsibilityClient { private static Logger doChaining(){ Logger consoleLogger = new ConsoleBasedLogger(Logger.OUTPUTINFO); Logger errorLogger = new ErrorBasedLogger(Logger.ERRORINFO); consoleLogger.setNextLevelLogger(errorLogger); Logger debugLogger = new DebugBasedLogger(Logger.DEBUGINFO); errorLogger.setNextLevelLogger(debugLogger); return consoleLogger; } public static void main(String args[]){ Logger chainLogger= doChaining(); chainLogger.logMessage(Logger.OUTPUTINFO, 'Enter the sequence of values '); chainLogger.logMessage(Logger.ERRORINFO, 'An error is occured now'); chainLogger.logMessage(Logger.DEBUGINFO, 'This was the error now debugging is compeled'); } }
download dette eksempel
Produktion
bilityClient CONSOLE LOGGER INFO: Enter the sequence of values CONSOLE LOGGER INFO: An error is occured now ERROR LOGGER INFO: An error is occured now CONSOLE LOGGER INFO: This was the error now debugging is compeled ERROR LOGGER INFO: This was the error now debugging is compeled DEBUG LOGGER INFO: This was the error now debugging is compeled=levels){>