iluwatar / java-design-patterns

Design patterns implemented in Java
https://java-design-patterns.com
Other
88.23k stars 26.18k forks source link

Refactor Front Controller #2979

Closed iluwatar closed 3 weeks ago

iluwatar commented 4 weeks ago

Description

The Front Controller design pattern typically consists of the following key parts:

  1. Front Controller: A single handler that handles all the requests. It's responsible for processing application-level requests, performing common operations like authentication, authorization, logging, or tracking, and then delegating the business-level processing to a suitable handler.
  2. Dispatcher: The dispatcher is responsible for view management and navigation, dispatching the request to the appropriate handler or view.
  3. View: The view represents the presentation layer of the application. It's responsible for displaying the user interface and the data.
  4. Command: The command encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Looking at the provided code from the front-controller module, it seems to have the Front Controller (FrontController), Command (Command), and View (View, ArcherView, CatapultView, ErrorView) components. However, it appears to be missing a distinct Dispatcher component. In the current implementation, the Front Controller (FrontController) is directly handling the dispatching of requests to the appropriate views. While this might work for a simple application, for a more complex application with a larger number of views, having a separate Dispatcher can help keep the Front Controller from becoming overly complex and hard to maintain.

Acceptance Criteria

Mayankchoudhary294 commented 3 weeks ago

Hello @iluwatar

I was thinking, should we have both the command and view parts in our solution? This way, commands can deal with the business side of things, while views can handle how things look to the user. So, when a request comes in through the FrontController, it would go to the dispatcher. There, it would get sorted out for both the business side and how it looks on the screen. Does that sound good to you? If it does, just let me know and I'll get started on it.

iluwatar commented 3 weeks ago

Yes, I think there already are the Command and View hierarchies. At the moment FrontController is handling the Dispatcher responsibilities as well and this should be fixed.

commands can deal with the business side of things

Yes, and just to get on the same page, command should encapsulate the request.

I'll assign to you @Mayankchoudhary294

Mayankchoudhary294 commented 3 weeks ago

Hello @iluwatar The PR is ready for review. Could you please take a look on it? Thanks