Front Controller Design Pattern

16 Jul, 2020
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.
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 successful.");        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 successful.
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.
This article is contributed by Saket Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Akanksha_Rai
nidhi_biet
Design Pattern
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.