Front Controller Design Pattern in Java

The front controller design pattern is used to provide a centralized request handling mechanism so that all requests will be handled by a single handler. This handler can do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers
The best example of this pattern is Spring MVC DispatcherServlet is a front controller who handles all the user request and processes the request as per there mapping. Let's discuss how Front Controller Design Pattern provides a centralized request handling mechanism so that all requests will be handled by a single handler.
It is hard to understand this pattern so my suggestion is to go to source code section and have a look at source code step by step gives you more clarity.
Table of contents
Problem
Forces
Solution
Explanation
Structure - Class Diagram, Sequence Diagram
Participants and Responsibilities
Implementation
Consequences
Applicability
Real world examples
References

Problem

(Problem section describes the design issues faced by the developer)
You want a centralized access point for presentation-tier request handling.

Forces

(This section describes Lists the reasons and motivations that affect the problem and the solution. The list of forces highlights the reasons why one might choose to use the pattern and provides a justification for using the pattern)
  • You want to avoid duplicate control logic.
  • You want to apply common logic to multiple requests.
  • You want to separate system processing logic from the view.
  • You want to centralize controlled access points into your system.

Solution

(Here solution section describes the solution approach briefly and the solution elements in detail)
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.

Explanation

In plain words
The front controller design pattern is used to provide a centralized request handling mechanism so that all requests will be handled by a single handler. This handler can do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers. Following are the entities of this type of design pattern.
Wikipedia says
The front controller software design pattern is listed in several pattern catalogs and related to the design of web applications. It is "a controller that handles all requests for a website", which is a useful structure for web application developers to achieve the flexibility and reuse without code redundancy.

Structure

Class Diagram


Sequence Diagram


Participants and Responsibilities

FrontController - The FrontController is the initial contact point for handling requests in the system. It delegates to an ApplicationController to perform action and view management.
ApplicationController - An ApplicationController is responsible for action and view management, including locating and routing to the specific actions that will service a request, and finding and dispatching to the appropriate view.
Command - A Command performs the action that handles the request.
View - A View represents the display returned to the client.

Implementation

Step 1 : Create Views- HomeView, StudentView, ErrorView, DashboardView java classes.
public class HomeView {
    public void show() {
        System.out.println("Displaying Home Page");
    }
}

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

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

public class DashboadView {
    public void show() {
        System.out.println("Displaying Dashboad Page");
    }
}
Step 2: Create Dispatcher - Dispatcher.java
public class Dispatcher {
    private StudentView studentView;
    private HomeView homeView;
    private DashboardView dashboardView;
    private ErrorView errorView;

    public Dispatcher() {
        studentView = new StudentView();
        homeView = new HomeView();
        dashboardView = new DashboardView();
        errorView = new ErrorView();
    }

    public void dispatch(String request) {
        if (request.equalsIgnoreCase("STUDENT")) {
            studentView.show();
        }
        if (request.equalsIgnoreCase("DASHBOARD")) {
            dashboardView.show();
        }
        if (request.equalsIgnoreCase("HOME")) {
            homeView.show();
        } else {
            errorView.show();
        }
    }
}
Step 3 : Create Front Controller - FrontController.java
public class FrontController {

    private Dispatcher dispatcher;

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

    private boolean isAuthenticUser() {
        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);
        }
    }
}
Step 4: Use the Front Controller to demonstrate Front Controller Design Pattern - FrontControllerPatternDemo.java
public class FrontControllerPatternDemo {
   public static void main(String[] args) {
   
      FrontController frontController = new FrontController();
      frontController.dispatchRequest("HOME");
      frontController.dispatchRequest("STUDENT");
      frontController.dispatchRequest("DASHBOARD");
      frontController.dispatchRequest("ERROR");
   }
}
The source code of Front Controller Design Pattern is available on GitHub

Consequences

  • Centralizes control
  • Improves manageability
  • Improves reusability
  • Improves role separation

Applicability

Use the Front Controller pattern when
  • you want to encapsulate common request handling functionality in single place.
  • you want to implement dynamic request handling i.e. change routing without modifying the code.
  • make web server configuration portable, you only need to register the handler web server specific way.

Real world examples

References

Related Presentation Tier Posts

Comments

  1. Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
    web designing classes in chennai | web designing training institute in chennai
    web designing and development course in chennai | web designing courses in Chennai
    best institute for web designing in chennai | web designing course with placement in chennai

    ReplyDelete
  2. Artificial Intelligence is the latest technology that transforms the way the E-commerce store performs. Thanks for sharing an informative blog.
    Dedicated Wordpress Developer
    Hire Dedicated Php Developer
    Hire Dedicated Wordpress Developer
    Hire Dedicated Opencart Developers
    Smarty Developers

    ReplyDelete
  3. The article was up to the point and described the information very effectively. Thanks to blog author for wonderful and informative post.
    website designing

    ReplyDelete
  4. Thanks to blog author for wonderful and informative post.
    gutter cleaning port st lucie

    ReplyDelete
  5. Thanks for sharing this valuable information.and I gathered some information from this blog...Apply online for Indian Postal Recruitment for Postman/Mail Guard, Postal Assistant, Sorting Assistant, MTS-Multi Tasking Staff, etc. Indian Postal Recruitment 2020 provides challenging Platform for Graduates, Freshers and experienced candidates...

    ReplyDelete
  6. Thanks a lot for sharing this interesting information with us.
    global asset management

    ReplyDelete
  7. מעולה. תודה על הכתיבה היצירתית.
    פרסום דיגיטלי לעסקים

    ReplyDelete
  8. I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter. Here is deep description about the article matter which helped me more.
    Data Science Course

    ReplyDelete
  9. Fantastic Site with relevant information and content Shared was knowledgeable thank you.
    Data Science Course in Hyderabad

    ReplyDelete
  10. i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
    best data science courses in bangalore

    ReplyDelete
  11. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
    data analytics course in bangalore

    ReplyDelete
  12. I Want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging endeavors.
    business analytics course in bangalore

    ReplyDelete
  13. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
    data science course fees in bangalore

    ReplyDelete
  14. i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
    cyber security training in bangalore

    ReplyDelete
  15. I Want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging endeavors.
    data science course in bangalore with placement

    ReplyDelete
  16. Stupendous blog huge applause to the blogger and hoping you to come up with such an extraordinary content in future. Surely, this post will inspire many aspirants who are very keen in gaining the knowledge. Expecting many more contents with lot more curiosity further.

    data science in bangalore

    ReplyDelete
  17. I Want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging endeavors.
    data science certification in banagalore

    ReplyDelete
  18. Tremendous blog quite easy to grasp the subject since the content is very simple to understand. Obviously, this helps the participants to engage themselves in to the subject without much difficulty. Hope you further educate the readers in the same manner and keep sharing the content as always you do.

    data analytics courses in bangalore with placement

    ReplyDelete
  19. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    Data Science Training in Bangalore

    ReplyDelete
  20. I need to thank you for this very good read and i have bookmarked to check out new things from your post. Thank you very much for sharing such a useful article and will definitely saved and revisit your site.
    Data Science Course

    ReplyDelete
  21. Your site is truly cool and this is an extraordinary moving article and If it's not too much trouble share more like that. Thank You..
    Digital Marketing Institute in Bangalore

    ReplyDelete
  22. Wow, happy to see this awesome post. I hope this think help any newbie for their awesome work and by the way thanks for share this awesomeness, i thought this was a pretty interesting read when it comes to this topic. Thank you..
    Artificial Intelligence Course

    ReplyDelete
  23. Thank a lot. You have done excellent job. I enjoyed your blog . Nice efforts
    Cyber Security Course in Bangalore

    ReplyDelete
  24. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
    Data Science Course in Bangalore

    ReplyDelete
  25. I want to say thanks to you. I have bookmarked your site for future updates.
    digital marketing courses in hyderabad with placement

    ReplyDelete
  26. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
    Data Science Course in Bangalore

    ReplyDelete
  27. i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
    cyber security training in bangalore

    ReplyDelete
  28. I want to say thanks to you. I have bookmarked your site for future updates.

    best data science institute in hyderabad

    ReplyDelete
  29. Thanks for posting the best information and the blog is very helpful.artificial intelligence course in hyderabad

    ReplyDelete
  30. i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
    cyber security training in bangalore

    ReplyDelete
  31. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
    data analytics courses in bangalore

    ReplyDelete
  32. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
    data analytics course in bangalore

    ReplyDelete
  33. This comment has been removed by the author.

    ReplyDelete
  34. Excellent post,keep sharing such type of informative post..

    full stack claases in bangalore

    ReplyDelete
  35. If your car is critically malfunctioned somewhere in or in the region of Tallaght, you will need the Tow Truck Tallaght to tow your automobile safely. We offer car towing at an affordable cost all-around Tallaght. Visit Vehicle Towing Tallaght

    ReplyDelete
  36. Your site is truly cool and this is an extraordinary moving article and If it's not too much trouble share more like that. Thank You..
    Digital Marketing Course in Hyderabad

    ReplyDelete
  37. Thank a lot. You have done excellent job. I enjoyed your blog . Nice efforts
    Data Science Certification in Hyderabad

    ReplyDelete
  38. Wow, happy to see this awesome post. I hope this think help any newbie for their awesome work and by the way thanks for share this awesomeness, i thought this was a pretty interesting read when it comes to this topic. Thank you..
    Artificial Intelligence Course

    ReplyDelete
  39. I need to thank you for this very good read and i have bookmarked to check out new things from your post. Thank you very much for sharing such a useful article and will definitely saved and revisit your site.
    Data Science Course

    ReplyDelete
  40. I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
    Data Science Training in Chennai

    ReplyDelete

Post a Comment