Front Controller Design Pattern – Core J2EE Patterns

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.

Front Controller Design Pattern

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.

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.

Spring 5 Design Pattern Book

You could purchase my Spring 5 book that is with title name “Spring 5 Design Patterns“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.
Spring-5-Design-Pattern

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.

Structure of the Front Controller Design Pattern

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.

Front Controller Design Pattern

Front Controller

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.

Dispatcher

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.

View

The view is responsible for displaying information to the client.

Example of the Front Controller Pattern

There are following classes based the Front Controller pattern.

  • DispatcherServlet in the Spring Framework
  • FilterDispatcher in the Struts 2 Framework

Sample Implementation for Front Controller

Let’s see the following sample example for this pattern’s implementation.

Step 1 : Creating number of views.
CompanyView.java

/**
 * 
 */
package com.doj.patterns.j2ee.frontcontroller;

/**
 * @author Dinesh.Rajput
 *
 */
public class CompanyView {
	
	public void view(){
		System.out.println("Rendering Company Home Page!!!");
	}
}

EmployeeView.java

/**
 * 
 */
package com.doj.patterns.j2ee.frontcontroller;

/**
 * @author Dinesh.Rajput
 *
 */
public class EmployeeView {
	
	public void view(){
		System.out.println("Rendering Employee Detail Page!!!");
	}
}

Step 2: Let’s create Dispatcher class.
Dispatcher.java

/**
 * 
 */
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();
		}	
	}
}

Step 3: After creating Dispatcher class, lets create front controller class whose have dispatcher object.
FrontController.java

/**
 * 
 */
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);
	}
}


Step 4: Let’s create a demo class to use the FrontController to demonstrate Front Controller Design Pattern.
FrontControllerPatternDemo.java

/**
 * 
 */
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");
	}

}

Step 5: Let’s run the above demo class and verify the output.

Page requested: COMPANY
Rendering Company Home Page!!!
Page requested: EMPLOYEE
Rendering Employee Detail Page!!!

Happy J2EE design Pattern learning.

Previous
Next