Front Controller Design Pattern

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

  • Front Controller - A single handler that handles all types of requests for an application, either web-based or desktop-based.
  • Dispatcher - The front controller may use a scheduler object to dispatch requests to the appropriate handler.
  • View - A view is an object created for a request.


We will create FrontController and Dispatcher as front-end controllers and schedulers, 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.

UML diagram of front controller mode

Step 1

Create a view.

public class HomeView { public void show(){ System.out.println("Displaying Home Page"); } }

public class StudentView { public void show(){ System.out.println("Displaying Student Page"); } }

Step 2

Create a scheduler Dispatcher.

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")){; }else{; } } }

step 3

Create a front controller FrontController。

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

Step 4

Use FrontController to demonstrate the front controller design pattern.

public class FrontControllerPatternDemo { public static void main(String[] args) { FrontController frontController = new FrontController(); frontController.dispatchRequest("HOME"); frontController.dispatchRequest("STUDENT"); } }

Step 5

Executing the program, outputting results:

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

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition