+1 vote
in Design Patterns by
What is FrontController design pattern in Java ?

1 Answer

0 votes
by
The front controller design pattern means that all requests that come for a resource in an application will be handled by a single handler and then dispatched to the appropriate handler for that type of request. The front controller may use other helpers to achieve the dispatching mechanism.

UML Diagram Front Controller Design Pattern

 

Design components

Controller : The controller is the initial contact point for handling all requests in the system. The controller may delegate to a helper to complete authentication and authorization of a user or to initiate contact retrieval.

View: A view represents and displays information to the client. The view retrieves information from a model. Helpers support views by encapsulating and adapting the underlying data model for use in the display.

Dispatcher: A dispatcher is responsible for view management and navigation, managing the choice of the next view to present to the user, and providing the mechanism for vectoring control to this resource.

Helper : A helper is responsible for helping a view or controller complete its processing. Thus, helpers have numerous responsibilities, including gathering data required by the view and storing this intermediate model, in which case the helper is sometimes referred to as a value bean.

Let’s see an example of Front Controller Design Pattern.

filter_none

edit

play_arrow

brightness_4

class TeacherView  

{

    public void display()

    {

        System.out.println("Teacher View");

    }

}

  

class StudentView  

{

    public void display()

    {

        System.out.println("Student View");

    }

}

  

class Dispatching  

{

    private StudentView studentView;

    private TeacherView teacherView;

     

    public Dispatching()

    {

        studentView = new StudentView();

        teacherView = new TeacherView();

    }

  

    public void dispatch(String request)

    {

        if(request.equalsIgnoreCase("Student"))

        {

            studentView.display();

        }

        else

        {

            teacherView.display();

        }     

    }

}

  

class FrontController  

{

    private Dispatching Dispatching;

  

    public FrontController()

    {

        Dispatching = new Dispatching();

    }

  

    private boolean isAuthenticUser()

    {

        System.out.println("Authentication successfull.");

        return true;

    }

  

    private void trackRequest(String request)

    {

        System.out.println("Requested View: " + request);

    }

  

    public void dispatchRequest(String request)

    {

        trackRequest(request);

        

        if(isAuthenticUser())

        {

            Dispatching.dispatch(request);

        }     

    }

}

  

class FrontControllerPattern

{

    public static void main(String[] args)  

    {

        FrontController frontController = new FrontController();

        frontController.dispatchRequest("Teacher");

        frontController.dispatchRequest("Student");

    }

}

Output:

Requested View: Teacher

Authentication successfull.

Teacher View

Requested View: Student

Authentication successful.

Student View

Advantages :

Centralized control : Front controller handles all the requests to the Web application. This implementation of centralized control that avoids using multiple controllers is desirable for enforcing application-wide policies such as users tracking and security.

Thread-safety : A new command object arises when receiving a new request and the command objects are not meant to be thread-safe. Thus, it will be safe in the command classes. Though safety is not guaranteed when threading issues are gathered, codes that act with the command are still thread safe.

Disadvantages :

It is not possible to scale an application using a front controller.

Performance is better if you deal with a single request uniquely.

Related questions

+1 vote
asked Oct 17, 2019 in Design Patterns by Robin
0 votes
asked Jul 24, 2023 in Design Patterns by SakshiSharma
...