Front Controller Pattern
The Front Controller Pattern is used to provide a centralized request-handling mechanism, where all requests are handled by a single handler. This handler can perform authentication/authorization/logging, or track requests, and then pass the requests to the appropriate handlers. The following are the entities of this design pattern.
- Front Controller - A single handler that handles all types of requests for the 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 the appropriate concrete handlers.
- View - A View is an object created for a request.
Summary
Intent
Use a central controller (or handler) to forward client requests to the appropriate handlers.
Main problem solved
- Solve the problem of scattered request handling in web applications and provide a unified entry point for request handling.
Use Cases
- When requests in a web application need to be managed and distributed uniformly.
Implementation
- Front Controller: acts as the single entry point for requests, responsible for receiving and forwarding requests.
- Views: used to present the processing results.
- Handler: The component that actually performs request processing.
Key code
- Front Controller: Contains the logic to decide which handler to forward the request to.
- Handler Mapping: Maps requests to the appropriate handlers.
Application Example
- Web framework: Such as DispatcherServlet in Spring MVC, acting as the front controller.
Advantages
- Centralized request handling: Simplifies the request handling flow, making it easy to manage and maintain.
- Reduce code duplication: Reduces duplicate code in views and handlers by reusing controllers.
- Easy to extend: When adding new request handling logic, simply add a new handler.
Disadvantages
- May become a performance bottleneck: All requests pass through the front controller, which may affect performance.
Usage recommendation
- When building a web application with a clear request handling flow, consider using the Front Controller pattern.
Notes
- Ensure the front controller does not become a performance bottleneck due to centrally handling all requests.
Main roles included
Front Controller:
- Acts as the single entry point for requests, responsible for receiving requests and deciding how to handle them.
Handler (Handler):
- The component that actually performs request processing.
View:
- Used to render the response generated by the handler.
Handler Mapping (Handler Mapping) (optional):
- Maps requests to the appropriate handlers.
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.
Create a class that implements
We will create FrontController、Dispatcher Treated as the front controller and the dispatcher, respectively.HomeView and StudentView Represents the various views created for requests received by the front controller.
FrontControllerPatternDemo, and our demo class uses the FrontController To demonstrate the Front Controller design pattern.
Step 1
Create the 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 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
)
{
//
Log every request
trackRequest
(
request
)
;
//
Authenticate the user
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
Run the program, output result:
Page requested: HOME
User is authenticated successfully.
Displaying Home Page
Page requested: STUDENT
User is authenticated successfully.
Displaying Student Page