Showing posts with label JavaEE. Show all posts
Showing posts with label JavaEE. Show all posts

1.1.12

Application Controller

P of EAA: Application Controller

Core J2EE Patterns


Problem Domain

Core J2EE Patterns

  • You want to centralize and modularize action and view management
    • You want to reuse action and view-management code.
    • You want to improve request-handling extensibility, such as adding use case functionality to an application incrementally.
    • You want to improve code modularity and maintainability, making it easier to extend the application and easier to test discrete parts of your request-handling code independent of a web container.
Pro Spring Patterns
  • Remove action and view management functionality from the front controller.
  • Deploy pluggable action and view handlers to provide support for different types of page controllers and views
  • Improve the reusability, cohesion, and modularity of the application code.
  • The front controller should be generic and as lightweight as possible.
  • Promote test-driven devleopment by making it possible to run unit tests outside the web container.
My understanding

To put it simple, Front Controller is a very first step of creating a good web application by centralize the controller into one access point, so that we can standardize the web application process flow, create access points to inject reusable components/filters.

However, Front Controller is too simple to most web applications. We need to do more. So we need to think about how to generalize the other part of the system, to make it "better".

Thanks to Front Controller, we can now have a more general MVC model. We can make sure that the centralized Front Controller, such as Spring Dispatcher Servlet, decouple the web request and many web specific technologies out of the scope of the design, so that we can think in a more generic way, an application way.

After Front Controller, it is not difficult to put the rest of process into two pars: Action Handling and View Handling. The application works through the request and does everything according to the request on the server side, such update database, create session information, update some distributed services, etc. This is the action part. After that, the application needs to response the client with appropriate data with appropriate format. Usually it would be HTML page, but sometimes, it could be something else.

When considering the action part, it could be very complex. But most of the cases could be defined as a chain of execution. Then we can see that the HandlerMapping interface of Spring makes sense:
  • It uses some request properties to locate an execution chain.
Even though it won't cover all cases, but at least, it would help most of the case. This design is good enough.



Front Controller

Core J2EE Patterns - Front Controller
Design Patterns: Front Controller
Core J2EE Patterns
Front Controller pattern - Wikipedia, the free encyclopedia
P of EAA: Front Controller
Front Controller
Front Controller [Web Application Component Toolkit]

Front Controller pattern - Wikipedia, the free encyclopedia

  • The Front Controller Pattern is a software design pattern listed in several pattern catalogs.
  • The pattern related to the design of web application.
  • It "provides a centralized entry point for handling requests."
My understanding

The most straightforward to implement a web application feature:
  • Create a start point page, or use a result of another request.
  • Have the client to fill in some data if necessary.
  • Have the client to submit the data to send out a request.
  • Process the request and response with some new information/page.
To fulfill this logic, you just need a form and point out to a response JSP page which is subject to process the request and response with with information.

Centralized dispatcher mechanism:
  • As a software system, web applications have a log of reusable items, including:
    • Authentication management.
    • Consistent page styles.
    • Shared business logic/component.
    • Auditing.
    • etc.
To apply for re-usability, you need to make all the request/response processes working in a similar way and make some places for the reusable components to fit in.

Luckily, we have Spring DispatcherServlet or other framework to help us with this requirements. There are no reasons that we should not stick to it.

Basically, this is a first step to develop a good web application.

What's in the book:


  • Use Spring Dispatcher Servlet as the standard access point of most of the web request, instead of having a JSP to handle form requests.
  • Use servlet mapping to map URL patterns to Dispatcher Servlet:
    • Decouple Spring framework.
    • Make the system extensible (You can add as many urls as you want into the system)
  • Use Spring bean container to define the mapping between URL path with a Controller implementation.
    • A controller is a pure Java class with the knowledge to handling internal process of the requests and the view for the responses.
    • This controller handle the business logic in a single purpose class.

Design theories:
  • Decouple the underlying support system. Here is Web server.
    • Comparing to CGI, or bad JSP implementation
  • Make the implementation extensible
    • URL pattern mapping to automatically map things into the dispatcher.
    • You just need to add a new URL with a certain patterns into your system, without changing the dispatcher's configuration. (Many extensible framework haves this theory, such as OSGi.

Dispatcher View

Core J2EE Patterns - Dispatcher View
Core J2EE Patterns
Dispatcher - Wikipedia, the free encyclopedia
Pro JavaTM EE Spring Patterns: Best ... - Dhrubojyoti Kayal - Google Books
DispatcherServlet (Spring Framework)

Problem domain:


Core J2EE Patterns


  • Problem:
    • You want a view to handle a request and generate a response, while managing limited amounts of business processing.
  • Forces:
    • You have static views
    • You have views generated from an existing presentation model.
    • You have views which are independent of any business service response.
    • You have limited business processing.
Pro Java EE Spring Patterns
  • Basically the same.
Solution:





Pro Spring Patterns


What are the differences?

  • Logically they are the same.
  • Simple business processes will be configured into the controller using IoC/AOP container, so that business process will be out of the scope of the main process flow. This is the power of declarable service injection and decoupling.
  • By decoupling business processes, the whole process flow is a simple mapping url dispatching flow
    • Find the corresponding URL
    • Create the view
    • Render it.
Pre-assumption
  • The business processes should be simple enough to inject into the the controller
    • Simple input/output parameter
    • Simple work flow