Friday, 22 April 2016

Chain of Responsibilities design pattern

Chain of Responsibilities design pattern

In this design pattern a set of handlers are chained together to handle a client request.
Except for the 1st & the last handler, all the handlers have a predecessor and a successor.
If a handler is not able to handle the request, it passes it to its successor. This goes on until either the request is handled or the chain is exhausted. Caller doesn’t know when and where the request will be handled.

E.g.: A Validation Handler.
Let suppose an immigration officer has to validate a traveller’s documents before letting the traveller to board the plane. The order of validations is:
1.     Does traveller carries a valid plane ticket
2.     Does traveller has a valid passport
3.     Does traveller has a valid visa

public abstract class ValidationHandler {
     
      protected ValidationHandler successor;
     
      public void setSuccessor(ValidationHandler vh) {
            this.successor = vh;
      }
      public abstract void validate(TravellingRequest request) throws Exception;

}

public class TicketValidationHandler extends ValidationHandler {

      @Override
      public void validate(TravellingRequest request) throws Exception {
            if(request.isValidTicket()) {
                  this.successor.validate(request);
            } else {
                  throw new Exception("You don't have valid ticket, can't travel");
            }

      }

}

public class PassportValidationHandler extends ValidationHandler {

      @Override
      public void validate(TravellingRequest request) throws Exception {
            if(request.isValidPassport()) {
                  this.successor.validate(request);
            } else {
                  throw new Exception("You don't have valid passport, can't travel");
            }

      }

}

public class VisaValidationHandler extends ValidationHandler {

      @Override
      public void validate(TravellingRequest request) throws Exception {
            if(request.isValidVisa()) {
                  System.out.println("Congratulations !! You can travel");
            } else {
                  throw new Exception("You don't have valid visa, can't travel");
            }

      }

}

public class TravellingRequest {
     
      private boolean validTicket;
      private boolean validPassport;
      private boolean validVisa;
     
      public TravellingRequest(boolean validTicket, boolean validPassport,
                  boolean validVisa) {
            super();
            this.validTicket = validTicket;
            this.validPassport = validPassport;
            this.validVisa = validVisa;
      }

      public boolean isValidTicket() {
            return validTicket;
      }

      public boolean isValidPassport() {
            return validPassport;
      }

      public boolean isValidVisa() {
            return validVisa;
      }

}

public class MainClass {

      public static void main(String[] args) {
            ValidationHandler ticket = new TicketValidationHandler();
            ValidationHandler passport = new PassportValidationHandler();
            ValidationHandler visa = new VisaValidationHandler();
            ticket.setSuccessor(passport);
            passport.setSuccessor(visa);
            TravellingRequest traveller1 = new TravellingRequest(false, false, false);
            System.out.println("Traveller 1");
            try {
                  ticket.validate(traveller1);
            } catch (Exception e) {
                  System.out.println(e.getMessage());
            }
            System.out.println("-----------");
            TravellingRequest traveller2 = new TravellingRequest(true, false, false);
            System.out.println("Traveller 2");
            try {
                  ticket.validate(traveller2);
            } catch (Exception e) {
                  System.out.println(e.getMessage());
            }
            System.out.println("-----------");
            TravellingRequest traveller3 = new TravellingRequest(true, true, false);
            System.out.println("Traveller 3");
            try {
                  ticket.validate(traveller3);
            } catch (Exception e) {
                  System.out.println(e.getMessage());
            }
            System.out.println("-----------");
            TravellingRequest traveller4 = new TravellingRequest(true, true, true);
            System.out.println("Traveller 4");
            try {
                  ticket.validate(traveller4);
            } catch (Exception e) {
                  System.out.println(e.getMessage());
            }
            System.out.println("-----------");
      }


}

Result :


Traveller 1
You don't have valid ticket, can't travel
-----------
Traveller 2
You don't have valid passport, can't travel
-----------
Traveller 3
You don't have valid visa, can't travel
-----------
Traveller 4
Congratulations !! You can travel

-----------


Code link : https://github.com/sksumit1/DesignPatterns

No comments:

Post a Comment