Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.time.*;
- /*
- * Client -> request handler 1 -> request handler 2 -> request handler 3
- * Withraw 2000rs from atm -> 2000 rs handler -> 500 rs handler -> 100 rs handler and so on
- *
- * chain of responsibility : validate username -> check atm pin -> then check atm expiry date-> check user balance -> then give money ...
- *
- */
- /*
- * basically means ki agar info logging ka call aae and info logger na mile to
- * error logging kardo
- *
- */
- abstract class LogProcessor{
- // steps he in responsibility
- LogProcessor nextLogProcessor;
- LogProcessor(LogProcessor logger){
- this.nextLogProcessor = logger;
- }
- public void log(String logLevel, String message){
- if(nextLogProcessor != null){
- nextLogProcessor.log(logLevel, message);
- }
- }
- }
- class InfoLogProcessor extends LogProcessor{
- InfoLogProcessor(LogProcessor logProcessor){
- // creating next log processor
- super(logProcessor);
- }
- public void log(String logLevel, String message){
- if(logLevel == "INFO"){
- System.out.println("INFO:" + message);
- }else{
- super.log(logLevel, message);
- }
- }
- }
- class ErrorLogProcessor extends LogProcessor{
- ErrorLogProcessor(LogProcessor logProcessor){
- super(logProcessor);
- }
- public void log(String logLevel, String message){
- System.out.println("ERROR:" + message);
- }
- }
- public class Main{
- public static void main(String args[]) throws Exception{
- LogProcessor logProcessor = new InfoLogProcessor(new ErrorLogProcessor(null));
- logProcessor.log("INFO", "stops at info logging");
- logProcessor.log("something", "nothing matches");
- // INFO:stops at info logging
- // ERROR:nothing matches
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement