1. J2EE Pattern - 5
By,
D. B. Naga Muruga,
Dept of Mechanical Engineering,
Sriram Engineering College
2. MVC PATTERN
Define MVC pattern
The Model/View/Controller(MVC) is an architecture design pattern. Model
means data, View means representation and Controller works on data and
representation. MVC focuses on decouple the triad relationships among data,
representation and controller.
Where to use & benefits
Application architecture design.
Any data related design, including non-visual application.
Decouple complex object to improve maintainability.
Increase object reusability.
Achieve design flexibility.
3. RELATED PATTERNS INCLUDE
Almost all patterns can be used with MVC
Example of MVC pattern
Struts framework is the best example of MVC pattern
4. BUSINESS DELEGATE PATTERN
Define business delegate pattern
An intermediate class decouples between presentation-tier clients and
business services.
Where to use & benefits
Simplify the complicated relationship.
Reduce coupling.
Cache results and references to remote business services.
Cut potentially costly round trips
Hide the underlying implementation details of business service.
6. COMPOSITE ENTITY
PATTERN Define composite entity pattern
Use a coarse-grained interface to manage interactions between fine-grained or coarse-grained and
dependent objects internally. The Composite Entity is a coarse-grained entity bean. It may be the
coarse-grained object or hold a reference to the coarse-grained object. Also known as Aggregate Entity.
Where to use & benefits
Combine coarse-grained object and its related dependent objects into a single entity bean.
Multiple clients share persistent objects.
Model a network of related business entities.
In both local and distributed environment, use remote entity beans to model dependent business
objects or fine-grained objects.
Improve performance by eliminating the parameter and return value serialization and data transmission
costs.
Eliminate inter-entity relationships
Improve manageability by reducing entity beans.
Improve network performance
Reduce database schema dependency
Increase object granularity
Facilitate composite transer object creation.
Overhead of multi-level dependent object graphs.
7. RELATED PATTERNS INCLUDE
Transfer Object used to return to client and also used to serialize
the coarse-grained and dependent objects tree, or part of the
tree, as required.
Session Facade used to manage the inter-entity-bean
relationships.
8. EXAMPLE OF COMPOSITE ENTITY PATTERN
Maybe any of the reader can help me with this
9. DATA ACCESS OBJECT PATTERN
Define data access object pattern
Adapt a uniform interface to access multiple databases like relational,
unrelational, object-oriented, etc.
Where to use & benefits
Need to access multiple data sources like legacy systems, B2B, LDAP, and so
forth.
Lack of uniform APIs to address the requirements to access disparate systems.
Persistent storage APIs vary depending on the product vendor.
Adapt and encapsulate all access to the data source.
Hide the data source implementation details from its clients.
More portable and less code dependencies in components.
Solve differences in the APIs which is used to access different persistent storage
mechanisms.
Not useful for container-managed persistence.
10. RELATED PATTERNS
INCLUDE factory method -- used to deal with different data sources.
abstract factory -- used to make an abstract layer of access to data
sources.
transfer object -- used to transport data to and from its clients.
EXAMPLE OF DATA ACCESS OBJECT PATTERN
See sun's code sample
11. FRONT CONTROLLER
Define Front controller pattern
Using a single component to process application requests.
Where to use & benefits
JSP or Servlet.
Design request handling component.
Channel all requests through a single controller.
Centralize request processing and view selection.
Reduce business logic in a view
Improve manageability of security
Promote code reuse across requests
Avoid code duplication
12. RELATED PATTERNS INCLUDE
Command combined with multiple requests.
Intercepting Filter both centralize control of certain types of request processing.
Page Controller -- an alternative way.
EXAMPLE FRONT CONTROLLER PATTERN
Design a servlet to deal with all the requests. (Just like struts
controller)
13. INTERCEPTING FILTER PATTERN
Define Intercepting Filter Pattern
A pluggable component design to intercept incomming requests and outgoing responses,
provide common services in a standard manner (independently) without changing core
processing code.
Where to use & benefits
Logging and authentication.
Enhance security.
Add additional function to existing web application.
Decorate main process.
Debug.
Pre-processing or post-processing for specific clients.
Uncompress incoming request.
Convert input encoding schema.
Being added or removed transparently or declaratively and triggered automatically
Improve reusability
Deployment-time composability
Each filter is loosely coupled
Inefficient for information sharing.
14. EXAMPLE INTERCEPTING FILTER PATTERN
To create a basic filter, you need to:
implement Filter interface
implement doFilter method
call doFilter from FilterChain object
register the filter with the appropriate servlets and JSP pages
Mapping the filter to specific pages
disable the invoker servlet
General skeleton program
Front Control better suited to handling core processing.
Template good for template filter strategy
Decorator providing for dynamically pluggable wrappers.
Related patterns include
15. import javax.servlet.*;
import javax.servlet.http.*;
public class MyFilter implements Filter {
public void doFilter(ServletRequest request,
ServletResponse resonse,
FilterChain chain)
throws ServletException, IOException {
//work on request and response
chain.doFilter(request, response);
}
public void init(FilterConfig config) throws ServletException {
//work on config
}
public void destroy() {
//work on clean up
}
}
Register and filter mapping
//in web.xml file
<web-app>
...
Before the servlet description
<filter>
<filter-name>MyFilter</filter-name>
<display-name>MyCoolFilter</display-name>
<description>This is my cool filter</description>
<filter-class>somePackage.MyFilter</filter-class>
<init-param>
<param-name>yyy</param-name>
<param-value>/xxx/zzz</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/xxx.jsp</url-pattern>
</filter-mapping>
<!-- also apply to another servlet -->
<filter-mapping>
<filter-name>MyFilter</filter-name>
<servlet-name>xxx</servlet-name>
</filter-mapping> ...
</web-app>
You may use filter mapping and servlet mapping in web.xml file to diable the invoker servlet to apply the filter.
16. SERVICE LOCATOR
Define Service Locator
Centralizing distributed service object lookups, providing a centralized
point of control, acting as a cache that eliminates redundant lookups.
Where to use & benefits
Lookup object in JNDI, RMI, JMS, etc.
Encapsulate any vendor-specific features of lookup process
Simplify the lookup process
Improve the performance
17. RELATED PATTERNS
INCLUDE Singlton combined with service locator to make sure only one
lookup object exists.
Example Service Locator
Use a container as cache to hold the lookup object. One application only
lookups same object once. Doing so will dramatically improve performance.
Make sure the container used is thread-safe.
18. TRANSFER OBJECT PATTERN
Define transfer Object Pattern
Using a serializable class to act as data carrier, grouping related
attributes, forming a composite value and working as a return
type from remote business method. Also known as Value object.
Where to use & benefits
Get related values from remote business method.
Fetch multiple value in one trip.
Decrease network traffic.
Minimize latency and server resource usage.
19. EXAMPLE OF TRANSFER OBJECT PATTERN
In the J2EE server, the client tier may make several calls to retrieve data from
the enterprise bean. Even in the same machine, the every call from the client
tier to the server tier is a remote method call. Think about use Transfer Object
design pattern to retrieve related attributes and return a single object instead
of each call just for retrieving a single attribute value. The transfer object is
passed by value to the client. All calls to the transfer object instance are local
calls to the client, so such design saves a lot of network traffic.
Let's say that you have a remote banking system. If the user has five requests
one time, you should design your system to give response once, not five
times. You may need to group all return values from these five requests to an
object carrier and then return to client just once. Once the client program
receives the instance of this object, it invokes the accessors and gets value to
display. The total network traffic for one user just once.