Design Pattern

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“.

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

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
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago