Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Front Controller Pattern

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.


Summary

Intent

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

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

  1. Centralized request handling: Simplifies the request handling 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, simply add a new handler.

Disadvantages

Usage recommendation

Notes

Main roles included

Front Controller:

Handler (Handler):

View:

Handler Mapping (Handler Mapping) (optional):

Client (optional):

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