The Front Controller Design Pattern is one of the J2EE software design patterns. Several pattern catalogs have it listed in them. It is related to and used in the design of web applications. The front controller is responsible for handling all the requests for a website. For web application developers, it can be a very useful structure, as it allows the developers the flexibility and the ability to reuse the code without having to add again and again.
In web applications, the front controllers are used to implement the workflows. It is not necessarily required in the process, but it helps in controlling when the user navigates through a number of related pages.
Use a Front Controller as the initial point of contact for handling all related requests. The Front Controller centralizes control logic that might otherwise be duplicated, and manages the key request handling activities.
also read:
For example, navigating through a number of pages that are used in an online shopping website while purchasing. It is difficult to control the navigation when every page is individually responsible for navigation. The front controller can be implemented as an object in Java. It can also be implemented in a scripting language as a script, for example, Python, Ruby or PHP. For every single request of a web session, this script is called.
For example, index.php is a script, which will be responsible for handling all the tasks that are common to the framework or an application. These tasks might include, caching, input filtering and handling. The front controller is able to instantiate further objects or to handle any particular tasks, it would call methods- but these actions depend on the specific requests.
In an alternative to the front controller, there are certain individual scripts, such as order.php or login.php. These can be used to satisfy the specific requests. Every single script would be required to duplicate the objects or codes which are common to all tasks. But, every single script might have the flexibility to perform the specific tasks required.
The structure of front controller design pattern is such that there is a controller, dispatcher, helper, and view. All of these objects have different tasks to perform and different responsibilities to cater.
The controller is more like a gateway for the users in order to handle the requests within the system. It can play multiple roles like for instance, it can be a delegating helper or it can initiate contact retrieval.
The dispatchers cater management of the view output and the navigation. A helper is responsible for helping the user to view or control the process.
The view is responsible for displaying information to the client.
There are following classes based the Front Controller pattern.
Let’s see the following sample example for this pattern’s implementation.
/** * */ package com.doj.patterns.j2ee.frontcontroller; /** * @author Dinesh.Rajput * */ public class CompanyView { public void view(){ System.out.println("Rendering Company Home Page!!!"); } }
/** * */ package com.doj.patterns.j2ee.frontcontroller; /** * @author Dinesh.Rajput * */ public class EmployeeView { public void view(){ System.out.println("Rendering Employee Detail Page!!!"); } }
/** * */ package com.doj.patterns.j2ee.frontcontroller; /** * @author Dinesh.Rajput * */ public class Dispatcher { private EmployeeView employeeView; private CompanyView companyView; public Dispatcher(){ employeeView = new EmployeeView(); companyView = new CompanyView(); } public void dispatch(String request){ if(request.equalsIgnoreCase("EMPLOYEE")){ employeeView.view(); }else{ companyView.view(); } } }
/** * */ package com.doj.patterns.j2ee.frontcontroller; /** * @author Dinesh.Rajput * */ public class FrontController { private Dispatcher dispatcher; public FrontController(){ dispatcher = new Dispatcher(); } public void dispatchRequest(String request){ System.out.println("Page requested: " + request); dispatcher.dispatch(request); } }
/** * */ package com.doj.patterns.j2ee.frontcontroller; /** * @author Dinesh.Rajput * */ public class FrontControllerPatternDemo { /** * @param args */ public static void main(String[] args) { FrontController frontController = new FrontController(); frontController.dispatchRequest("COMPANY"); frontController.dispatchRequest("EMPLOYEE"); } }
Page requested: COMPANY Rendering Company Home Page!!! Page requested: EMPLOYEE Rendering Employee Detail Page!!!
Happy J2EE design Pattern learning.
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…