Front Controller Design pattern tutorials with examples in Java


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

Front controller are mostly used in MVC applications and following are implemented in frameworks

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

Why Front controller pattern required?

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

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

Here there is 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 Java Web application, Servlets act as a controller sitting in front of other resources.

In this pattern, all the request that send by different resources like 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 act Single central entry point for the request sent by the browser. Fore 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 an simple example for understanding how mobile and desktop request are processed via single controller.

Let’s define java classes

DesktopClient is an clients sending request from Desktop

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

MobileClient is an clients sending request from Mobile

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

In Real time, These are request from Android app for MobileClient, browser request for DesktopClient

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

if request is from mobile, Delegate 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 the request and logic for authorization and authentication.

And also main method contains sending 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 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
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.