Front Controller Design Pattern

Front Controller Design Pattern

It handles incoming requests centrally in a handler and then dispatches the corresponding view. The requests are handled centrally because every request might need to be checked against authorization or be logged.

The URL dispatcher in Django is crafted using the front controller pattern, which confers significant advantages, especially ensuring that every request passes through the SecurityMiddleware irrespective of its origin or type. This design pattern is especially recommended when there is a substantial number of common operations to be performed on requests, as it helps to prevent code duplication. Secondly, it provides thread safety and control.

+------------------+         +-------------------+
|  FrontController |         |     Dispatcher    |
+------------------+         +-------------------+
| -dispatcher      |<>------>| -requestHandlerMap|
+------------------+         +-------------------+
| +handleRequest() |         | +dispatchRequest()|
+------------------+         +-------------------+
       ^                             ^
       |                             |
+------+--------+           +--------+--------+
|   Application |           |  RequestHandler |
+---------------+           +-----------------+
| +main()       |           | +handleRequest()|
+---------------+           +-----------------+
                                  ^
                                  |
                   +--------------+------------------+
                   |                                 |
           +-------+---------+               +-------+---------+
           |   ViewHandler   |               | CommandHandler  |
           +-----------------+               +---------=--=----+
           | +handleRequest()|               | +handleRequest()|
           +-----------------+               +-----------------+

Resources:
Design Pattern – Front Controller Pattern