Showing posts with label Front Controller. Show all posts
Showing posts with label Front Controller. Show all posts

Friday, 30 August 2013

Front Controller Design Pattern - Implementation

🚀 Master Java Architecture!

Join our community for clear, practical coding tutorials that get you hired.

SUBSCRIBE TO RAM N JAVA

Implementing the Front Controller Pattern

In software design, the Front Controller Pattern is a powerful way to manage complex web applications. By providing a single entry point for all requests, you can handle common tasks—like security and logging—in one place instead of repeating code across dozens of different files.

1. The Core Idea

Think of the Front Controller as a Receptionist in a large office building. Instead of guests wandering around trying to find the right room, they all talk to the receptionist first. The receptionist checks their ID (Security) and then directs them to the correct office (Dispatching).

2. Key Best Practices

To implement this pattern effectively, keep these three points in mind:

  • Keep it Lightweight: The controller should delegate the "heavy lifting" to other specialized classes.
  • Centralize Common Logic: Tasks like user authentication or tracking page views should happen here.
  • Use a Dispatcher: The controller should use a separate 'Dispatcher' object to figure out which view or data to return.

3. Why Developers Love It

For beginners, this pattern might seem like extra work at first, but it makes your application much easier to scale. When you need to add a new security feature, you only have to change it in one file rather than fifty!

Check Out More From Ram N Java

If you're ready to dive deeper into Java and software development, check out these related videos:



Click the below Image to Enlarge
Front Controller Design Pattern - Implementation - Class Diagram























UserView.java

public class UserView
{
public void show()
{
System.out.println("Displaying User Page");
}
}


SalaryView.java

public class SalaryView
{
public void show()
{
System.out.println("Displaying Salary Page");
}
}


AccountView.java

public class AccountView
{
public void show()
{
System.out.println("Displaying Account Page");
}
}

Dispatcher.java

public class Dispatcher
{
private SalaryView  salaryView;
private UserView    userView;
private AccountView accountView;

public Dispatcher()
{
salaryView = new SalaryView();
userView = new UserView();
accountView = new AccountView();
}

public void dispatch( String request )
{
if( request.equalsIgnoreCase("USER") )
{
userView.show();
}
else if( request.equalsIgnoreCase("ACCOUNT") )
{
accountView.show();
}
else
{
salaryView.show();
}
}
}


FrontController.java

public class FrontController
{
private Dispatcher dispatcher;

public FrontController()
{
dispatcher = new Dispatcher();
}

private boolean isAuthenticUser()
{
//here you have to write Authentication logic
System.out.println("User is authenticated successfully.");
return true;
}

private void trackRequest( String request )
{
System.out.println("Page requested: " + request);
}

public void dispatchRequest( String request )
{
// log each request
trackRequest(request);
// authenticate the user
if( isAuthenticUser() )
{
dispatcher.dispatch(request);
}
}
}

FrontControllerPatternDemo.java

public class FrontControllerPatternDemo
{
public static void main( String[] args )
{
FrontController frontController = new FrontController();
frontController.dispatchRequest("USER");
System.out.println();
frontController.dispatchRequest("ACCOUNT");
System.out.println();
frontController.dispatchRequest("SALARY");
}
}

Output

Page requested: USER
User is authenticated successfully.
Displaying User Page

Page requested: ACCOUNT
User is authenticated successfully.
Displaying Account Page

Page requested: SALARY
User is authenticated successfully.
Displaying Salary Page


See also:

  • Front Controller Design Pattern - Introduction
  • Front Controller Design Pattern - Class and Sequence Diagram
  • Front Controller Design Pattern - Key Points
  • All Design Patterns Links



  • Tutorials