Front Controller Pattern

Front Controller Pattern is used to provide a centralized request handling mechanism, where all requests are handled by a single handler. The handler can perform authentication/authorization/logging, or track requests, and then pass the request to the corresponding handler. The following are the entities of this design pattern.

  • Front Controller- A single handler that processes all types of requests for an application, which may be a web-based application or a desktop-based application.
  • Dispatcher- The front controller may use a dispatcher object to dispatch requests to the corresponding concrete handlers.
  • View- Views are objects created for requests.

summary

intent

Uses a central controller (or handler) to forward client requests to the appropriate handlers.

Main Problems Solved

  • Solves the problem of scattered request handling in web applications, providing a unified request processing entry point.

Use Case

  • When requests need to be centrally managed and dispatched in a web application.

Implementation Approach

  • Front Controller: Acts as the single entry point for requests, responsible for receiving and forwarding requests.
  • view: Used to render the processing results.
  • Handler: The component that actually executes request handling.

Key code

  • Front Controller: Contains logic to decide which handler to forward the request to.
  • Handler Mapping: Maps requests to the corresponding handlers.

Application example

  • Web FrameworkAn example is the DispatcherServlet in Spring MVC, which acts as the front controller.

Advantages

  1. Centralized Request Handling: Simplifies the request processing flow, making it easy to manage and maintain.
  2. Reduce Code Duplication: Reduces duplicate code in views and handlers by reusing controllers.
  3. Easy to Extend: When adding new request handling logic, only a new handler needs to be added.

Disadvantages

  • May become a performance bottleneck.: All requests pass through the front controller, which may affect performance.

Usage suggestions

  • When building a web application that requires a clear request processing flow, consider using the Front Controller Pattern.

Notes

  • Ensure that the front controller does not become a performance bottleneck due to centralized handling of all requests.

Contains several main roles.

Front Controller:

  • : Acts as the single entry point for requests, responsible for receiving requests and deciding how to handle them.

Handler:

  • The component that actually executes request handling.

View:

  • Used to render the response generated by the handler.

Handler Mapping (optional):

  • Maps requests to the corresponding handlers.

Client (optional):

  • : The web browser or API client that issues requests.

The Front Controller Pattern helps build web applications that are easy to maintain and extend by providing a centralized request handling mechanism.


Implementation

We will createFrontController、DispatcherTreated as the front controller and dispatcher respectively.HomeViewandStudentViewRepresents various views created for requests received by the front controller.

FrontControllerPatternDemo, our demonstration class usesFrontControllerto demonstrate the front controller design pattern.

前端控制器模式的 UML 图

Step 1

Create 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 a 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 a 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){ //Log every request trackRequest(request); //Authenticate users if(isAuthenticUser()){ dispatcher.dispatch(request); } } }

Step 4

UsageFrontControllerto 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

Execute the program, output results:

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