0

Why implementing Front Controller?

Posted by Guillermo GarcĂ­a on 4:42 PM in , ,
Simple: This design pattern allows centralizing the start point of your web application. With this, you can add controls for every request, avoiding re-write code in each controller you may develop. This definitively enhances the reusability of your code.

Now, this is not all. Front controller pattern could be implemented in many ways, even using a JSP as the controller (look at J2EE core patterns if you don't believe me). So, for me the real question is: HOW TO implement Front Controller?.

First of all, the big picture: This pattern only says: "Every request must pass through the Front Controller". After that the pattern says something like: "Do something and Good luck!". And we need it indeed. We need a "strategy", as our Sun Microsystem fellows call it.


The answer is subjective, but here I will leave my opinion. You must implement the "Command and Controller Strategy". In fact, this strategy is a bundle of design patterns, like: Factory Method (Gang of Four), View Helper (J2EE Core), Business Delegate (J2EE core), and many others. It is a complex solution, but when well implemented, the result is very nice and stable.

This implementation, to me has a "Front Controller" which handles the request and transforms the input data for the "View Helper". This helpers are "Command"s with a generic interface (mostly, they are an input and output HashMaps). The controller gets the helper command with a "Factory Method" and then executes the command and gets a response, that the "View Dispatcher" sends to the client.

NOTE: Most of the time, the dispatcher is so simple, that it is implemented as part of the Front Controller.

The details are the tricky part: How can I handle the Factory? Error Handling? How can I treat different kinds of requests (servlet, jsp, downloading, reports in pdf or xls)? etc.

We will discuss many of these details ... in other post.

0 Comments

Copyright © 2009 ggarciao.com
- Cup of Java -
All rights reserved.