diff --git a/front-controller/README.md b/front-controller/README.md index 6be08d55f00c..bb5997469116 100644 --- a/front-controller/README.md +++ b/front-controller/README.md @@ -1,29 +1,117 @@ --- title: Front Controller -category: Structural +category: Architectural language: en tag: - - Decoupling + - Architecture + - Decoupling + - Enterprise patterns + - Layered architecture + - Web development --- +## Also known as + +* Centralized Request Handling + ## Intent -Introduce a common handler for all requests for a web site. This -way we can encapsulate common functionality such as security, -internationalization, routing and logging in a single place. + +The Front Controller design pattern aims to provide a centralized entry point for handling all incoming web requests, ensuring that request handling is managed consistently and efficiently across an application. + +## Explanation + +Real-world example + +> Imagine a busy hotel where all guest requests and queries are first directed to a central reception desk. This desk acts as the "front controller" of the hotel, responsible for receiving all inquiries, from room service orders to maintenance requests. The receptionist assesses each request and routes it to the appropriate department—housekeeping, the kitchen, or maintenance. This system centralizes request handling, ensuring that guest needs are addressed efficiently and consistently, similar to how a Front Controller in a software application manages all incoming requests and delegates them to specific handlers. + +In plain words + +> The Front Controller design pattern centralizes incoming web requests into a single handling point, allowing consistent processing and delegation across an application. + +Wikipedia says + +> The front controller software design pattern is listed in several pattern catalogs and is 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 flexibility and reuse without code redundancy. + +**Programmatic example** + +The Front Controller design pattern is a pattern that provides a centralized entry point for handling all requests in a web application. It ensures that request handling is managed consistently and efficiently across an application. + +In the provided code, we can see an example of the Front Controller pattern in the `App` and `FrontController` classes. + +The `App` class is the entry point of the application. It creates an instance of `FrontController` and uses it to handle various requests. + +```java +public class App { + + public static void main(String[] args) { + var controller = new FrontController(); + controller.handleRequest("Archer"); + controller.handleRequest("Catapult"); + controller.handleRequest("foobar"); + } +} +``` + +The `FrontController` class is the front controller in this example. It handles all requests and routes them to the appropriate handlers. + +```java +public class FrontController { + + public void handleRequest(String request) { + Command command; + + switch (request) { + case "Archer": + command = new ArcherCommand(); + break; + case "Catapult": + command = new CatapultCommand(); + break; + default: + command = new UnknownCommand(); + } + + command.process(); + } +} +``` + +In this example, when a request is received, the `FrontController` creates a command object based on the request and calls its `process` method. The command object is responsible for handling the request and rendering the appropriate view. + +This is a basic example of the Front Controller pattern, where all requests are handled by a single controller, ensuring consistent and efficient request handling. ## Class diagram -![alt text](./etc/front-controller.png "Front Controller") + +![Front Controller](./etc/front-controller.png "Front Controller") ## Applicability -Use the Front Controller pattern when -* you want to encapsulate common request handling functionality in single place -* you want to implements dynamic request handling i.e. change routing without modifying code -* make web server configuration portable, you only need to register the handler web server specific way +* Web applications requiring a centralized mechanism for request handling. +* Systems that need a common processing point for all requests to perform tasks such as authentication, logging, and routing. -## Real world examples +## Known uses * [Apache Struts](https://struts.apache.org/) +* Java web frameworks like Spring MVC and JavaServer Faces (JSF) implement the Front Controller pattern through their central dispatcher servlet, which manages web requests and delegates responsibilities. + +## Consequences + +Benefits: + +* Centralizes request handling, which simplifies maintenance and promotes consistency. +* Eases the integration of services like security and user session management. +* Facilitates common behavior like routing, logging, and authentication across requests. + +Trade-offs: + +* Can become a bottleneck if not properly managed. +* Increases complexity in the dispatcher controller, requiring careful design to avoid tight coupling. + +## Related Patterns + +* [Page Controller](https://java-design-patterns.com/patterns/page-controller/): Front Controller can delegate requests to Page Controllers, which handle specific page requests. This division supports the Single Responsibility Principle. +* [Model-View-Controller (MVC)](https://java-design-patterns.com/patterns/model-view-controller/): Front Controller acts as the controller, managing the flow between model and view. +* [Command](https://java-design-patterns.com/patterns/command/): Can be used to encapsulate a request as an object, which the Front Controller can manipulate and delegate. ## Credits diff --git a/front-controller/src/main/java/com/iluwatar/front/controller/App.java b/front-controller/src/main/java/com/iluwatar/front/controller/App.java index 5bc2b0422814..06922251f1b3 100644 --- a/front-controller/src/main/java/com/iluwatar/front/controller/App.java +++ b/front-controller/src/main/java/com/iluwatar/front/controller/App.java @@ -26,7 +26,7 @@ /** * The Front Controller is a presentation tier pattern. Essentially it defines a controller that - * handles all requests for a web site. + * handles all requests for a website. * *

The Front Controller pattern consolidates request handling through a single handler object ( * {@link FrontController}). This object can carry out the common the behavior such as diff --git a/front-controller/src/main/java/com/iluwatar/front/controller/ApplicationException.java b/front-controller/src/main/java/com/iluwatar/front/controller/ApplicationException.java index 04a9744061ff..6ba13a46ff78 100644 --- a/front-controller/src/main/java/com/iluwatar/front/controller/ApplicationException.java +++ b/front-controller/src/main/java/com/iluwatar/front/controller/ApplicationException.java @@ -24,11 +24,14 @@ */ package com.iluwatar.front.controller; +import java.io.Serial; + /** * Custom exception type. */ public class ApplicationException extends RuntimeException { + @Serial private static final long serialVersionUID = 1L; public ApplicationException(Throwable cause) {