JAVA Design Pattern
JAVA Design Pattern
JAVA DP
If a problem occurs over and over again, a solution to that problem has been used
effectively. That solution is described as a pattern. The design patterns are
language-independent strategies for solving common object-oriented design
problems. When you make a design, you should know the names of some common
solutions. Learning design patterns is good for people to communicate each other
effectively. In fact, you may have been familiar with some design patterns, you
may not use well-known names to describe them. SUN suggests GOF (Gang Of
Four--four pioneer guys who wrote a book named "Design Patterns"- Elements of
Reusable Object-Oriented Software), so we use that book as our guide to describe
solutions. Please make you be familiar with these terms and learn how other people
solve the code problems.
If you want to be a professional Java developer, you should know at least some
popular solutions to coding problems. Such solutions have been proved efficient
and effective by the experienced developers. These solutions are described as so-
called design patterns. Learning design patterns speeds up your experience
accumulation in OOA/OOD. Once you grasped them, you would be benefit from
them for all your life and jump up yourselves to be a master of designing and
developing. Furthermore, you will be able to use these terms to communicate with
your fellows or assessors more effectively.
Many programmers with many years experience don't know design patterns, but as
an Object-Oriented programmer, you have to know them well, especially for new
Java programmers. Actually, when you solved a coding problem, you have used a
design pattern. You may not use a popular name to describe it or may not choose
an effective way to better intellectually control over what you built. Learning how
the experienced developers to solve the coding problems and trying to use them in
your project are a best way to earn your experience and certification.
Remember that learning the design patterns will really change how you design
your code; not only will you be smarter but will you sound a lot smarter, too.
Many. A site says at least 250 existing patterns are used in OO world, including
Spaghetti which refers to poor coding habits. The 23 design patterns by GOF are
well known, and more are to be discovered on the way.
Note that the design patterns are not idioms or algorithms or components.
Generally, to build a system, you may need many patterns to fit together. Different
designer may use different patterns to solve the same problem. Usually:
J2EE Patterns:--
1.MVC:
History
3
JAVA DP
Core issue
To achieve the MVC architecture in your project, you have to decouple View and
Model by Controller. Your GUI as View should be designed as a module. It can be
launched separately. Your data related classes as Model should be treated as a
module too. Your controller classes should response to any data change on the
GUI. Such design will increase reusability and flexibility.
Try to visualize that the user reacts with the GUI, a DataManager(Controller)
listens to the GUI's call. If the user needs to load data, such request is sent to the
DataManager, the DataManager starts loading, searching and extracting the
requested data from the server and sends it back to the GUI. GUI is responsible to
display data.
Here the server acts as Model, the DataManager acts as Controller and GUI acts as
View. The DataManager can be used for both remote and local modes (design two
constructors for both modes), the GUI can be replaced with any design and the data
related classes can be packaged together and put on local and server sides. All of
the three objects can be reused for other projects with little code alteration.
If you grasp such concept and skill, you will save a lot of time in designing and
developing your projects in the future. This is the so-called OOA/OOD.
2.Business Delegate:-
Definition
Example
Make a class deal with lookups and exception, acting as a representative of the
client components
3.Composite Entity:-
Definition
Transfer Object:
Example
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.
Session Façade
6
JAVA DP
Example:Provide a step by step process for a complex system. For example, let a
component or user access a complex system by exposing a simple or a unified
interface to hide complexity to reach a loose coupling and flexible interaction.
Definition
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.
7
JAVA DP
5.Front Controller
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
Related patterns include
Command combined with multiple requests.
Intercepting Filter both centralize control of certain types of request
processing.
Page Controller -- an alternative way.
6.Intercepting Filter
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
9
JAVA DP
}
public void destroy() {
//work on clean up
}
}
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/xxx.jsp</url-pattern>
</filter-mapping>
You may use filter mapping and servlet mapping in web.xml file to diable the
invoker servlet to apply the filter.
7.Service Locator
10
JAVA DP
Example: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.
8.Transfer Object
Example: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.
11
JAVA DP
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.
http://www.javacamp.org/designPattern/