Front Controller Design pattern tutorials with examples in Java

Front Controller is one of the most important design patterns used in web applications. It is a single palace controller to handle all your request to an application.

Front controllers are mostly used in MVC applications and the following are implemented in frameworks.

  • Legacy Java web apps
  • Struts
  • Spring boot and MVC
  • Angular, React, and Vuejs framework
  • Microsoft .net and Sharepoint

Why Front controller pattern required?

Let’s see the reason for the front controller and what issues are solved?

In a web application, whenever clients send a request to the server, the server process the request, if possible makes a database call, gets the data from the Database or other third-party systems and processes the data, and sends the response.

Here there are different components involved.

  • business data validation,
  • holding the database data(model)
  • view process.

Because of this process, there is a lack of a single point of contact for the request.

Front Control design pattern is introduced. In the Java Web application, Servlets act as a controller sitting in front of other resources.

In this pattern, all the requests that send by different resources like the browser, the mobile client, as well as Standalone apps, are handled by this servlet and delegate the request to appropriate web resources.

This Servlet is an act Single central entry point for the request sent by the browser. For example. ActionServlet is the front controller for Struts-based applications. DispatchServlet is the front controller for spring web MVC-based applications.

Front Controller Design pattern example

This is a simple example of understanding how mobile and desktop requests are processed via a single controller.

Let’s define java classes.

DesktopClient is a client sending a request from Desktop.

public class DesktopClient {
    public void render() {
        System.out.println("Desktop UI rendered");
    }
}

MobileClient is a client sending a request from Mobile.

public class MobileClient {
    public void render() {
        System.out.println("Mobile UI rendered");
    }
}

In Real-time, These are requests from the Android app for MobileClient, a browser request for DesktopClient

Write a simple DispatchHandler class which is a delegation of requests based on the request type.

if the request is from mobile, Delegate the request to mobile rendering logic else Delegate request to desktop rendering logic.

public class DispatchHandler {
    private final MobileClient mobile;
    private final DesktopClient desktop;

    public DispatchHandler() {
        mobile = new MobileClient();
        desktop = new DesktopClient();
    }

    public void dispatchRequest(String request) {
        if (request.equalsIgnoreCase("mobile")) {
            mobile.render();
        } else {
            desktop.render();
        }
    }
}

FrontController.java contains central place logic for calling handler An optional logic to add filters to the request and logic for authorization and authentication.

And also the main method contains sending a request from mobile and desktop.

public class FrontController {

    private final DispatchHandler handler;

    public FrontController() {
        handler = new DispatchHandler();
    }


    public static void main(String[] args) {
        FrontController fc = new FrontController();
        fc.handler.dispatchRequest("mobile"); // request is from mobile
        fc.handler.dispatchRequest("desktop"); //request is from desktop or tablet

    }
}

Output

Movile UI rendered
Desktop UI rendered

Advantages

  • Single place of code logic for the controller
  • Loose coupling with different components as validations and delegations are separated
  • Easy to write a logic to focus
  • Thread safety can be achieved by writing logic