Design Patterns - Front Controller

design-patterns

// Design Patterns - Structural - Front Controller:

The front controller pattern is where you have a single entrance point for your 
web application (e.g. index.php) that handles all of the requests. This code is 
responsible for loading all of the dependencies, processing the request and 
sending the response to the browser. The front controller pattern can be 
beneficial because it encourages modular code and gives you a central place 
to hook in code that should be run for every request (such as input 
sanitization).

The front controller design pattern is used to provide a centralized request 
handling mechanism so that all requests will be handled by a single handler. 
This handler can do the authentication/ authorization/ logging or tracking of 
request and then pass the requests to corresponding handlers. Following are the 
entities of this type of design pattern.

1. Front Controller - Single handler for all kinds of requests coming to the 
   application (either web based/ desktop based).
2. Dispatcher - Front Controller may use a dispatcher object which can dispatch 
   the request to corresponding specific handler.
3. View - Views are the object for which the requests are made.

We are going to create a FrontController and Dispatcher to act as Front 
Controller and Dispatcher correspondingly. HomeView and StudentView represent 
various views for which requests can come to front controller. 

// Step 1: Create Views.
public class HomeView {
   public void show(){
      System.out.println("Displaying Home Page");
   }
}

public class StudentView {
   public void show(){
      System.out.println("Displaying Student Page");
   }
}

// Step 2: Create Dispatcher
public class Dispatcher {
   private StudentView studentView;
   private HomeView homeView;

   public Dispatcher(){
      studentView = new StudentView();
      homeView = new HomeView();
   }

   public void dispatch(String request){
      if(request.equalsIgnoreCase("STUDENT")){
         studentView.show();
      } else{
         homeView.show();
      }    
   }
}

// Step 3: Create FrontController
public class FrontController {

   private Dispatcher dispatcher;

   public FrontController(){
      dispatcher = new Dispatcher();
   }

   private boolean isAuthenticUser(){
      System.out.println("User is authenticated successfully.");
      return true;
   }

   private void trackRequest(String request){
      System.out.println("Page requested: " + request);
   }

   public void dispatchRequest(String request){
      //log each request
      trackRequest(request);

      //authenticate the user
      if(isAuthenticUser()){
         dispatcher.dispatch(request);
      }    
   }
}

// Step 4: Use the FrontController to demonstrate Front Controller Design Pattern.
FrontController frontController = new FrontController();
frontController.dispatchRequest("HOME");
frontController.dispatchRequest("STUDENT");
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License