Showing posts with label Design Pattern. Show all posts
Showing posts with label Design Pattern. 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

12.12.11

Musing on mis-usings: 'Powerful use, Damaging misuse' | Javalobby


  • there are three types of lies: "lies, damn lies and statistics".
  • Object orientated lanuages introduced the notion of inheritance, a clever idea to promote code reuse. However, inheritance - when misused - can easily lead to complex hierarchies and can make it difficult to change objects.

Every Single One of GOF Design Patterns Has Been Implemented in JDK


You can find an overview of a lot design patterns in Wikipedia. It also mentions which patterns are mentioned by GoF. I'll sum them up here and try to assign as much as possible pattern implementations found in both the Java SE and Java EE API's.

Creational patterns

Abstract factory (recognizeable by creational methods returning an abstract/interface type)

Builder (recognizeable by creational methods returning the instance itself)

Factory method (recognizeable by creational methods returning a concrete type)

Prototype (recognizeable by creational methods returning a different instance of itself with the same properties)

Singleton (recognizeable by creational methods returning the same instance (usually of itself) everytime)


Structural patterns

Adapter (recognizeable by creational methods taking an instance of different abstract/interface type and returning an implementation of own/another abstract/interface type which decorates/overrides the given instance)

Bridge (recognizeable by creational methods taking an instance of different abstract/interface type and returning an implementation of own abstract/interface type which delegates/uses the given instance)

  • None comes to mind yet. A fictive example would be new LinkedHashMap(LinkedHashSet<K>, List<V>) which returns an unmodifiable linked map which doesn't clone the items, but uses them. Thejava.util.Collections#newSetFromMap() and singletonXXX() methods however comes close.

Composite (recognizeable by behavioral methods taking an instance of same abstract/interface type)

Decorator (recognizeable by creational methods taking an instance of same abstract/interface type)

Facade (recognizeable by behavioral methods which internally uses instances of different independent abstract/interface types)

Flyweight (recognizeable by creational methods returning a cached instance, a bit the "multiton" idea)

Proxy (recognizeable by creational methods which returns an implementation of given abstract/interface type which in turn delegates/uses adifferent implementation of given abstract/interface type)

The Wikipedia example is IMHO a bit poor, lazy loading has actually completely nothing to do with the proxy pattern at all.

Behavioral patterns

Chain of responsibility (recognizeable by behavioral methods which (indirectly) invokes the same method in another implementation of same abstract/interface type in a queue)

Command (recognizeable by behavioral methods in an abstract/interface type which invokes a method in an implementation of a differentabstract/interface type which has been encapsulated by the command implementation during its creation)

Interpreter (recognizeable by behavioral methods returning a structurally different instance/type of the given instance/type; note that parsing/formatting is not part of the pattern, determining the pattern and how to apply it is)

Iterator (recognizeable by behavioral methods sequentially returning instances of a different type from a queue)

Mediator (recognizeable by behavioral methods taking an instance of different abstract/interface type (usually using the command pattern) which delegates/uses the given instance)

Memento (recognizeable by behavioral methods which internally changes the state of the whole instance)

Observer (or Publish/Subscribe) (recognizeable by behavioral methods which invokes a method on an instance of anotherabstract/interface type, depending on own state)

State (recognizeable by behavioral methods which changes its behaviour depending on the instance's state which can be controlled externally)

Strategy (recognizeable by behavioral methods in an abstract/interface type which invokes a method in an implementation of a differentabstract/interface type which has been passed-in as method argument into the strategy implementation)

Template method (recognizeable by behavioral methods which already have a "default" behaviour definied by an abstract type)

Visitor (recognizeable by two different abstract/interface types which has methods definied which takes each the other abstract/interface type; the one actually calls the method of the other and the other executes the desired strategy on it)