Front Controller Pattern
Front Controller Pattern
The Front Controller Pattern is used to provide a centralized request handling mechanism. All requests will be handled by a single handler. This handler can perform authentication/authorization/logging, or track requests, and then pass the request to the appropriate 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 can be a web-based application or a desktop-based application.
- Dispatcher - The Front Controller may use a Dispatcher object to dispatch requests to specific concrete handlers.
- View - A View is an object created for the request.
Summary
Intent
Use a central controller (or handler) to forward client requests to the appropriate handler.
Problem It Solves
- Solves the problem of scattered request handling in web applications by providing a unified entry point for request processing.
Use Cases
- When there is a need to centrally manage and dispatch requests in a web application.
Implementation
- Front Controller: Acts as the single entry point for requests, responsible for receiving and forwarding requests.
- View: Used to present the processing result.
- Handler: The component that actually executes the request processing.
Key Code
- Front Controller: Contains logic to decide which handler to forward the request to.
- Handler Mapping: Maps requests to the corresponding handler.
Application Example
- Web Frameworks: Such as the DispatcherServlet in Spring MVC, which acts as the Front Controller.
Advantages
- Centralized Request Handling: Simplifies the request processing flow, making it easy to manage and maintain.
- Reduces Code Duplication: Reduces duplicate code in views and handlers by reusing the controller.
- Easy to Extend: When adding new request processing 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.
Recommendations
- Consider using the Front Controller pattern when building a web application with a clear request processing flow.
Considerations
- Ensure the Front Controller does not become a performance bottleneck due to centrally handling all requests.
Key Roles Involved
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 the request processing.
View:
- Used to present the response generated by the handler.
Handler Mapping (Optional):
- Maps requests to the corresponding handler.
Client (Optional):
- The web browser or API client that issues the request.
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 create FrontController and Dispatcher to act as the front 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.
Step 1
Create views.
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.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 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 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 the result:
Page requested: HOME
User is authenticated successfully.
Displaying Home Page
Page requested: STUDENT
User is authenticated successfully.
Displaying Student Page
YouTip