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
The best example of this pattern is Spring MVC DispatcherServlet is a front controller who handles all the user request and processes the request as per there mapping. Let's discuss how Front Controller Design Pattern provides a centralized request handling mechanism so that all requests will be handled by a single handler.
It is hard to understand this pattern so my suggestion is to go to source code section and have a look at source code step by step gives you more clarity.
Table of contents |
---|
Problem |
Forces |
Solution |
Explanation |
Structure - Class Diagram, Sequence Diagram |
Participants and Responsibilities |
Implementation |
Consequences |
Applicability |
Real world examples |
References |
Problem
(Problem section describes the design issues faced by the developer)
You want a centralized access point for presentation-tier request handling.
Forces
(This section describes Lists the reasons and motivations that affect the problem and the solution. The list of forces highlights the reasons why one might choose to use the pattern and provides a justification for using the pattern)
- You want to avoid duplicate control logic.
- You want to apply common logic to multiple requests.
- You want to separate system processing logic from the view.
- You want to centralize controlled access points into your system.
Solution
(Here solution section describes the solution approach briefly and the solution elements in detail)
Use a Front Controller as the initial point of contact for handling all related requests. The Front Controller centralizes control logic that might otherwise be duplicated and manages the key request handling activities.
Explanation
In plain words
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.
Wikipedia says
The front controller software design pattern is listed in several pattern catalogs and related to the design of web applications. It is "a controller that handles all requests for a website", which is a useful structure for web application developers to achieve the flexibility and reuse without code redundancy.
Structure
Class Diagram
Sequence Diagram
Participants and Responsibilities
FrontController - The FrontController is the initial contact point for handling requests in the system. It delegates to an ApplicationController to perform action and view management.
ApplicationController - An ApplicationController is responsible for action and view management, including locating and routing to the specific actions that will service a request, and finding and dispatching to the appropriate view.
Command - A Command performs the action that handles the request.
View - A View represents the display returned to the client.
Implementation
Step 1 : Create Views- HomeView, StudentView, ErrorView, DashboardView java classes.
public class HomeView {
public void show() {
System.out.println("Displaying Home Page");
}
}
public class StudentView {
public void show() {
System.out.println("Displaying Student Page");
}
}
public class ErrorView {
public void show() {
System.out.println("Displaying Error Page");
}
}
public class DashboadView {
public void show() {
System.out.println("Displaying Dashboad Page");
}
}
Step 2: Create Dispatcher - Dispatcher.java
public class Dispatcher {
private StudentView studentView;
private HomeView homeView;
private DashboardView dashboardView;
private ErrorView errorView;
public Dispatcher() {
studentView = new StudentView();
homeView = new HomeView();
dashboardView = new DashboardView();
errorView = new ErrorView();
}
public void dispatch(String request) {
if (request.equalsIgnoreCase("STUDENT")) {
studentView.show();
}
if (request.equalsIgnoreCase("DASHBOARD")) {
dashboardView.show();
}
if (request.equalsIgnoreCase("HOME")) {
homeView.show();
} else {
errorView.show();
}
}
}
Step 3 : Create Front Controller - 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 each request
trackRequest(request);
//authenticate the user
if (isAuthenticUser()) {
dispatcher.dispatch(request);
}
}
}
Step 4: Use the Front Controller to demonstrate 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");
frontController.dispatchRequest("DASHBOARD");
frontController.dispatchRequest("ERROR");
}
}
The source code of Front Controller Design Pattern is available on GitHub
Consequences
- Centralizes control
- Improves manageability
- Improves reusability
- Improves role separation
Applicability
Use the Front Controller pattern when
- you want to encapsulate common request handling functionality in single place.
- you want to implement dynamic request handling i.e. change routing without modifying the code.
- make web server configuration portable, you only need to register the handler web server specific way.
Real world examples
References
- J2EE Design Patterns
- Core J2EE Patterns: Best Practices and Design Strategies, J2EE
- http://www.corej2eepatterns.com
Related Presentation Tier Posts
- Intercepting Filter Design Pattern in Java
- Front Controller Design Pattern in Java
- Application Controller Design Pattern in Java
- View Helper Design Pattern in Java
- Composite View Design Pattern in Java
- Context Object Design Pattern in Java
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course