User:Biosunbj/sandbox

= Front controller = From Wikipedia, the free encyclopedia

The front controller software design pattern is listed in several pattern catalogs and relates to the design of web applications. It "a controller that handles all requests for a Web site." [1], which is a useful structure for web application developers to achieve the flexibility and reuse without code redundancy.

Contents
hide]
 * 1Instruction
 * 2Examples
 * 3Implementation
 * 3.1Participants and Responsibilities
 * 3.2Demo implementation in Java
 * 4Benefits and Liabilities
 * 5Relationship with MVC pattern
 * 6Comparison
 * 7See also
 * 8References
 * 9Notes
 * 10External links

Instruction[edit | edit source]
A typical front controller structure.

Front controllers are often used in web applications to implement workflows. While not strictly required, it is much easier to control navigation across a set of related pages (for instance, multiple pages used in an online purchase) from a front controller than it is to make the individual pages responsible for navigation.

The front controller may be implemented as a Java object, or as a script in a script language like PHP, Python or Ruby that is called on every request of a web session. This script, for example an index.php, would handle all tasks that are common to the application or the framework, such as session handling, caching, and input filtering. Based on the specific request, it would then instantiate further objects and call methods to handle the particular task(s) required.

The alternative to a front controller would be individual scripts like login.php and order.php that would each then satisfy the type of request. Each script would have to duplicate code or objects that are common to all tasks. However, each script might also have more flexibility to implement the particular task required.

Examples[edit | edit source]
Several web-tier application frameworks implement the front controller pattern, among them:
 * MVC frameworks written in PHP. For example, Yii, CakePHP, Laravel, Symfony, CodeIgniter and Zend Framework
 * Drupal
 * ASP.NET MVC.
 * Spring Framework[2]
 * Cairngorm framework in Adobe Flex.
 * Yesod web application framework written in Haskell.
 * Apache Struts.

Implementation[edit | edit source]
To better understand front controller pattern, there is an example to implement front controller in Java.[3] It can be define in 3 components:
 * 1) XML Mapping: files which map requests to the class will handle the request processing.
 * 2) Request Processor: used for dealing with the request processing (and modifying or retrieving the appropriate model).
 * 3) Flow Manager: first get the request and the output of the processing, then determine what will show on the next page.

Demo implementation in Java[edit | edit source]
Here is part of a demo code to implement front controller[4]. private void doProcess(HttpServletRequest request,                       HttpServletResponse response) throws IOException, ServletException { ...   try { getRequestProcessor.processRequest(request); getScreenFlowManager.forwardToNextScreen(request, response); } catch (Throwable ex) { String className = ex.getClass.getName; nextScreen = getScreenFlowManager.getExceptionScreen(ex); // put the exception in the request request.setAttribute("javax.servlet.jsp.jspException", ex); if (nextScreen == null) { // send to general error screen ex.printStackTrace; throw new ServletException("MainServlet: unknown exception: " +            className); }   }

Benefits and Liabilities[edit | edit source]
There are several benefits for using front controller pattern.[5] In terms of liability, front controllers that determine following activities by searching the database or XML documents, performance might be decreased badly. And implementation of front controller to existed systems always involving replacing the current ones, which makes it harder for beginners to start with.
 * Centralized control. Front controller process all the requests to the web application. This kind of centralized control avoid using multiple controllers which is good for enforce application-wide policies such as using tracking and security.
 * Thread-safety. A new command object arise when receiving a new request and the command objects are not meant to be thread safe. Thus, you will keep safe in the command classes. Though it does not guarantee safety when threading issues are gathered, code that act with command is still thread safe.
 * Configurability. Since just one front controller is need in web application. This will simplify the configuration of implement web applications. The handler does the rest of dispatching so that we will not required to changing anything to add new commands with dynamic commands.

Relationship with MVC pattern[edit | edit source]

 * 1) In order to improve system reliability and maintainability, duplicated codes should be avoided and centralized when they are of the same common logic through the system.
 * 2) The data for the application is better to be handled in one location, thus there will be no need to duplicate database retrieval code.
 * 3) Different roles in the MVC pattern should be separated to increase testability, which is also true for controller part in the MVC pattern.

Comparison[edit | edit source]
Page controller is an alternative to front controller in MVC model.

See also[edit | edit source]

 * Design pattern (computer science).
 * Mediator pattern *Note: the front controller pattern is a specialized kind of mediator pattern.

References[edit | edit source]

 * 1) Jump up^
 * 2) Jump up^
 * 3) Jump up^
 * 4) Jump up^
 * 5) Jump up^

External links[edit | edit source]

 * Bear Bibeault's Front Man™, A lightweight Java implementation.