Front controller mode


Front Controller Pattern is used to provide a centralized request processing mechanism, and all requests will be processed by a single handler. The handler can do authentication/authorization/logging, or track the request, and then pass the request to the appropriate handler. The following are the entities of this design pattern.

  • Front Controller - A single handler that handles all types of requests for an application. The application can be a web-based application or a Desktop based application.

  • Scheduler (Dispatcher) - The front-end controller may use a dispatcher object to dispatch requests to the corresponding specific handler.

  • View (View) - A view is an object created for a request.

Implementation

We will create FrontController and Dispatcher as front-end controller and dispatcher respectively. HomeView and StudentView represent various views created for requests received by the front controller.

FrontControllerPatternDemo, our demo class uses FrontController to demonstrate the front controller design pattern.

frontcontroller_pattern_uml_diagram.jpg

Step 1

Create a view.

HomeView.java

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

StudentView.java

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

Step 2

Create the dispatcher Dispatcher .

Dispatcher.java

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 the front-end controller FrontController.

Context.java

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){
      //记录每一个请求
      trackRequest(request);
      //对用户进行身份验证
      if(isAuthenticUser()){
         dispatcher.dispatch(request);
      }	
   }
}

Step 4

Use FrontController to demonstrate the front controller design pattern.

FrontControllerPatternDemo.java

public class FrontControllerPatternDemo {
   public static void main(String[] args) {
      FrontController frontController = new FrontController();
      frontController.dispatchRequest("HOME");
      frontController.dispatchRequest("STUDENT");
   }
}

Step 5

Verify the output.

Page requested: HOME
User is authenticated successfully.
Displaying Home Page
Page requested: STUDENT
User is authenticated successfully.
Displaying Student Page