Title: Pattern
1Pattern Framework
2??
- More about Pattern
- Framework
- Comparison
3??
- More about Pattern
- Framework
- Comparison
4More about Pattern
- Patterns of Patterns
- Anti-pattern
- J2EE patterns
5More about Pattern
- Patterns of Patterns
- Anti-pattern
- J2EE patterns
6Working together
- Patterns are often used together and combined
with the same design solution. - A compound pattern combines two or more patterns
into a solution that solves a recurring or
general problem.
7Example?????????
- ????
- ????
- ???
- ??????
- ??????(look-and-feel)??
- ????????
- ????
- ????????
8??????
- Composite?????????????
- Strategy??????????
- Decorator??????
- Abstract Factory???????
- Bridge????????
- Command????????
- Iterator?????????
- Visitor?????????????????????????
9Duck Example
- Start with a bunch of Quackables..
- A goose came along and wanted to act like a
Quakable too. - Then, the Quackologists decided they wanted to
count quacks. - But the Quackologists were worried theyd forget
to add the QuackCounter decorator. - We had management problems keeping track of all
those ducks and geese and quackables. - The Quackologists also wanted to be notified when
any quackable quacked.
Adapter
Decorator
Abstract factory
Composite
Iterator
Observer
10Compound Patterns
- MVC
- a song about MVC by James Dempsey (lyrics)
11MVC
- MVC is a compound pattern consisting of the
Observer (model), Strategy (controller) and
Composite (view) patterns. - The Adapter pattern can be used to adapt a new
model to an existing view and controller.
12Pattern Definition Review
- A Pattern is a solution to a problem in a
context. - Context the recurring situation in which the
pattern applies - Problem the goal you are trying to achieve in
this context and any constrains that occur in the
context. - Solution a general design that anyone can apply
which resolves the goal and set of constraints
13Pattern Categories I -- Goal
- Creational Patterns involve object instantiation
and all provide a way to decouple a client from
the objects it needs to instantiate. - Structural Patterns compose classes or objects
into larger structures. - Behavioral Patterns concerned with how classes
and objects interact and distribute responsibility
14Pattern Categories II -- Scope
- Class Patterns
- Describe how relationships between classes are
defined via inheritance. - Relationships in class patterns are established
at compile time.
15Pattern Categories II -- Scope
- Object Patterns
- Describe relationships between objects and are
primarily defined by composition. - Relationships in object patterns are typically
created at runtime and are more dynamic and
flexible
16More about Pattern
- Patterns of Patterns
- Anti-pattern
- J2EE patterns
17Anti-Patterns
- Andrew Koenig 1995, Michael Akroyd 1996
- An anti-pattern (????) tells you how to go from a
problem to a BAD solution. - Tell you why a bad solution is attractive.
- Tell you why that solution in the long term is
bad. - Suggest other patterns that are applicable which
may provide good solutions.
Wiki
18??????
- ?????????
- ?????????
- ?????????
- ?????????
- ?????????
- ???????????
- ???????
- ?????????
- ??????
- ????????
19Example
- ?????
- ???????????????,??????????????????????????????????
,?????????????? - ?????
- ??????????????
- ??????????????????????
- ??????????????????????
- ?????????,???????????????????
- ??????????,????????
- ?????????????????????
- ????????????????????
20Example
- ???? (Refactor)
- ?????????????????????,??????????????
- ????????????????,???????????????
- ??????????????????,?????????
- ?????????????????????
- ??,??????????
- ??,????????????????,???????????????,??????????????
?,??????????
21More about Pattern
- Patterns of Patterns
- Anti-pattern
- J2EE patterns
22J2EE Patterns
- ??
- ????????,????J2EE?????Java????????
- ??????????,?J2EE??????????????????????????
- ????????,??????????????,?????
- ???????
23J2EE Patterns Classification
- ??????SJC?????TheServerSide???????
- J2EE JSP, Servlet, EJB, JMS, JDBC, JNDI
- SJC (Sun Java Center)????
- Web???JSP,Servlet
- Intercepting Filter, Front Controller, View
Helper - ?????EJB
- Business Delegate, Value Object, Session Façade
- ?????JMS,JDBC
- Data Access Object, Service Activator
24J2EE Patterns Classification
- TheServerSide????
- EJB???????
- Session Façade, Message Façade, EJB Command
- ????????
- Data Transfer Object, Data Transfer HashMap
- ????????
- Version Number, JDBC for Reading
- ???EJB????
- EJB Home Factory, Business Delegate
25Example
- EJB??????????
- ??EJB ??EJBHome em JNDIServer.getRemoteHome(EJ
B-JNDI-NAME) EJBObject myEJB em.create() - em.create()??Afactory.create()
- EJBObject ???
26Example
- ?? Bean???Bean Façade??
- ????Bean???????Bean
- ???Bean???Façade?/Manager?
- ??Façade ??Bean??
- ????,???????????Bean?????
- ????,???????
27Example
- DTO(Data Transfer Object)??
- DTO?????????????JavaBeans,?J2EE?????????
- DTO????,???????Message
- ?JavaBeans?????????Model
28Example
- MVC??
- MVC???J2EE Web??????
29Example
- Proxy??
- ???????????????????????????????
- ???????????????(Access Proxy)????????????
- ??Proxy??
- ??????Java???(Reflect)??,?????????????????????????
????????? - ???????????????????????????????,??????????????????
?
30J2EE Anti-Pattern
- ?EJB??J2EE
- ????
- ???????
- ????????Session Bean
- ????
- ??Servlet????JSP
31NowOO Design Toolbox
- OO Basics
- OO Principles
- OO Patterns
- Your patterns here!
32??
- More about Pattern
- Framework why, what, how
- Comparison
33Forms of design-level reuse
- Sharing of consistency programming and scripting
language - Sharing concrete solution fragments libraries
- Sharing contracts interfaces
- Sharing individual architecture patterns
- Sharing subsystem architectures frameworks
- Sharing overall structure system architectures.
34Framework
- GoF A framework is a set of cooperating
classes, some of which may be abstract, that make
up a reusable design for a specific class of
software. - A software framework is an abstraction in which
common code providing generic functionality can
be selectively overridden or specialized by user
code, thus providing specific functionality. - Frameworks are a special case of software
libraries in that they are reusable abstractions
of code wrapped in a well-defined API, yet they
contain some key distinguishing features that
separate them from normal libraries.
35Framework
- ??,????????????,??????,???????????????????????????
,????????,????????,???????? - ????????????(?J2EE)??????????????
36Framework
????
??????
????
???????
?????
????
37Framework
- First commercial application framework MacApp by
Apple Computer for Macintosh - Well-known frameworks
- MFC
- Struts, Spring, Avalon, PicoContainer
- Hibernate
- Jdon
-
38Framework ??
- Inversion of control - The overall program's
flow of control is not dictated by the caller,
but by the framework. - Default behavior - A framework has a default
behavior. This default behavior must actually be
some useful behavior and not a series of no-ops. - Extensibility - A framework can be extended by
the user usually by selective overriding or
specialized by user code providing specific
functionality. - Non-modifiable framework code - The framework
code, in general, is not allowed to be modified.
Users can extend the framework, but not modify
its code.
39How to design a good framework?
- ?????? (Domain Driven Design)
- ????IoC (Inversion of Control) or ????DI
(Dependence Injection) - AOP (Aspect Oriented Programming)
40Domain Driven Design
- ??DDD http//domaindrivendesign.org/
- The premise of domain-driven design is two-fold
- For most software projects, the primary focus
should be on the domain and domain logic and - Complex domain designs should be based on a
model.
41DDD Background
- ????????Java??Web????,????????
- Ruby on Rails (RoR)?Web???????Java??????????????
???????????? - ?Java??????DDD??(RoR)??,????Java??????,?????????,
????IoC/AOP??????
42DDD
- Domain-driven design is not a technology or a
methodology. It is a way of thinking and a set of
priorities, aimed at accelerating software
projects that have to deal with complicated
domains.
43???DDD
44DDD
- ????
- ??????(Domain Model)??,?????????????,??????????
,?????????????? - ??
- ????????,????Delphi/VB?????????????
45DDD??
- ??????????(ubiquitous )???,????????????????????,?
????????????????? - ??????????????????????,??????????????????
- ???????????????????,?????? ?????????
46??????????
Persistence
Domain Service
Domain Model
GUI
Logging
47?????????
- ????MVC?????,???????????????Controller?,?Struts?Ac
tion?????Action????? - ????SOA???,????????????????
- ????????,?????setter/getter????????????????????,??
???????? - ???????????????,?????????????????????,???????
48?????????????
- ?????OrderItemId, OrderId, ProductId ? Qty?
- ???? MinDeliveryQty ? PendingQty??,??????????
- ????????????,???????????????????
- ?????OO??,????????????????????
- DDD????????????????? ?
49????????????
- ?????????????,??????????????
- ???????????????,????????????,???????
- ??????????,????,?ER????,???????,???domain
model???????,???????
50DDD advantage
51?Jdon Framework (JF)??
- http//www.jdon.com/jdonframework/
- Domain Model?JF?????????????
- ??Model??????????????
- ?Domain Model????????????
?Domain???
52??JF?DDD????
53Model Configuration
- ????????MVC??Controller??
54IoC
- ???? Inversion of Control
- Review
- Design Principles DIP (dependence inversion)
- Hollywood Principle Dont call us, well call
you. - ???? Dependence Injection (by Martin Fowler)
- http//www.martinfowler.com/articles/injection.htm
l
55IoC Motivation
- First
- We knew from basic Java tutorial, that all
objects should be created first before use. - Then
- We knew how to create objects by usage of factory
patterns. - Now
- We will study how to get an object from an IoC
container and use it without creating it by
ourselves.
abc new ABC()
abc Factory.createInstanceOfABC()
56Example
http//today.java.net/pub/a/today/2004/02/10/ioc.h
tml
public class JDBCDataManger public void
accessData() DataSource dataSource new
DataSource() //access data ...
public class JDBCDataManger public void
accessData() DataSource dataSource
ApplciationResources.getDataSource() //access
data ...
In either case, the JDBCDataManager has to fetch
the DataSource itself!
57IoC Motivation
- ??????,?????????,?????????????????,???????????,???
??????,????????????,?????,????????????????????,???
?????
58IoC Approach I
- Interface Injection / Contextualized Dependency
Lookup (Type 1) - components implement specific interfaces provided
by their containers in order to be configured
Avalon
import org.apache.avalon.framework. public
class JDBCDataManger implements Serviceable
DataSource dataSource public void service
(ServiceManager sm) throws ServiceException
dataSource (DataSource)sm.lookup("dataSource
") public void getData()
//use dataSource for something
The requirement to implement specific interfaces
can give code a "bloated" feel, while at the same
time coupling your application code to the
underlying framework.
59IoC Approach II
- Setter Injection (Type 2)
- some external metadata is used to resolve
dependencies.
Spring
public class JDBCDataManger private
DataSource dataSource public void
setDataManager(DataSource dataSource
this.dataSource dataSource public void
getData() //use dataSource for something
Looks like a normal bean!
60IoC Approach II
- Metadata takes the form of an XML configuration
file in Spring.
ltbean id"myDataSource" class"org.apache.commo
ns.dbcp.BasicDataSource" gt ltproperty
name"driverClassName"gt ltvaluegtcom.mydb.jdbc.D
riverlt/valuegt lt/propertygt ltproperty
name"url"gt ltvaluegtjdbcmydb//serverport/myd
blt/valuegt lt/propertygt ltproperty
name"username"gt ltvaluegtrootlt/valuegt
lt/propertygt lt/beangt
Metadata can be reused by multiple components!
61IoC Approach II
- define an instance of our manager and pass in a
reference to the data source
ltbean id"dataManager" class"example.JDBCDataM
anger"gt ltproperty name"dataSource"gt ltref
bean"myDataSource"/gt lt/propertygt lt/beangt
Make unit test easier! Application code is not
tied to the container.
Potential downsides Component relationship
unclear Encapsulation broken
62IoC Approach III
- Constructor Injection (Type 3)
- Based on the principle of Good Citizen
- Register an object with the framework, specify
the parameters to use (which can in turn be
created by the framework itself) and then just
request an instance. - The components being registered just have to
implement a constructor, which can be used to
inject the dependencies.
63IoC Approach III
public class JDBCDataManger private
DataSource dataSource public
JDBCDataManger(DataSource dataSource)
this.dataSource dataSource public
void getData() //use dataSource for
something
64IoC Approach III
PicoContainer
- //create a datasource bean
- BasicDataSource dataSource new
BasicDataSource() - dataSource.setDriverClassName("com.mydb.jdbc.Drive
r") - dataSource.setUrl("jdbcmysql//localhost3306/myd
b") - dataSource.setUsername("Bob")
- //create the container
- MutablePicoContainer pico new
DefaultPicoContainer() - //register components with container
- ConstantParameter dataSourceParam new
ConstantParameter(dataSource) - String key "DataManager"
- Parameter params dataSourceParam
- // Now each request for a DataManager will
instantiate a JDBCDataManager object with our
defined - // dataSource object
- pico.registerComponentImplementation (key,
JDBCDataManger.class,params)
Use old-fashioned Java code to glue everything
65IoC Approach III
- To get instances of the JDBCDataManager object,
we just have to reference the class by its key
JDBCDataManger dm (JDBCDataManger)pico.getCo
mponentInstance(key)
Application code is independent of the framework
itself only a constructor is needed.
Potential downsides using constructors to
maintain the dependencies can become more complex
when using inheritance
66Comparison
Type 1 ?JNDI?ServiceManager??????? 1. EJB/J2EE2. Avalon
Type 2 ??JavaBeans?setter?? 1. Spring Framework,2. WebWork/XWork
Type 3 ?????????? 1. PicoContainer,2. HiveMind
???????IoC??????
67IoC Timeline
68Spring vs. PicoContainer
- Different work required to set up your IoC
bindings - Spring can be configured either by an XML
configuration file or directly in Java - PicoContainer requires a Java binding
- Both are fairly lightweight framework
- PicoContainer is smaller
- Spring is not just an IoC framework it also
provides web application and AOP frameworks, as
well as some general support classes
69Aspect-Oriented Programming
- Separation of Concerns
- Crosscutting Concerns
- Aspects
- Weaving
70Aspect-Oriented Programming
- Complexity of enterprise software systems
- JBoss 4.0????AOP?????????????Authentication ??
Caching ??Context passing ???? Error handling
????Lazy loading ??? Debugging ??logging,
tracing, profiling and monitoring ???? ??
??Performance optimization ????Persistence
??? Resource pooling ???Synchronization
?? Transactions ??
71??
- More about Pattern
- Framework why, what, how
- Comparison
72Comparison
- ???????
- ???????????????????
- ???????????,?????????????????
- ?????
- ???????
73Comparison
- ??????????
- ????????????,??????
- ????????,??????????
- ?????????????????
- ??????????????????,????
- ????????????
- ???????????????
- ???OO???????????,???OO????????????????
74Comparison
- ?????,????????????,?????????
75More Comparison
- ?? vs. ????
- ?? vs. ???,??
- ?? vs. ??????
76??(????????)
- ????(container)???,?????????,??????????
- ??????,??Spring??????IoC??
- Ref
- ????Presentation????????email??,OO????/????/??????
?