Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Front-end controller mode


May 27, 2021 Design mode


Table of contents


Front-end controller mode

Front Controller Pattern is used to provide a centralized request processing mechanism, and all requests are handled by a single handler. T he handler can do authentication/authorization/log, 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, between a web-based application and a desktop-based application.
  • Dispatcher - The front-end controller may use a scheduler object to dispatch requests to the appropriate specific handler.
  • View - A view is an object created for a request.

Realize

We'll create FrontController and Dispatcher as front-end controllers and schedulers, respectively. HomeView and StudentView represent a variety of views created for requests received by the front-end controller.

FrontController Pattern Demo, our demo class uses FrontController to demonstrate front-end controller design patterns.

Front-end controller mode

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

FrontController.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-end 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