Pattern - PowerPoint PPT Presentation

About This Presentation
Title:

Pattern

Description:

Pattern & Framework & * Institute of Computer Software Nanjing University Comparison * Institute of Computer Software Nanjing University * Type 1 JNDI ... – PowerPoint PPT presentation

Number of Views:128
Avg rating:3.0/5.0
Slides: 77
Provided by: zf1
Category:

less

Transcript and Presenter's Notes

Title: Pattern


1
Pattern Framework
  • ????

2
??
  • More about Pattern
  • Framework
  • Comparison

3
??
  • More about Pattern
  • Framework
  • Comparison

4
More about Pattern
  • Patterns of Patterns
  • Anti-pattern
  • J2EE patterns

5
More about Pattern
  • Patterns of Patterns
  • Anti-pattern
  • J2EE patterns

6
Working 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.

7
Example?????????
  • ????
  • ????
  • ???
  • ??????
  • ??????(look-and-feel)??
  • ????????
  • ????
  • ????????

8
??????
  • Composite?????????????
  • Strategy??????????
  • Decorator??????
  • Abstract Factory???????
  • Bridge????????
  • Command????????
  • Iterator?????????
  • Visitor?????????????????????????

9
Duck 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
10
Compound Patterns
  • MVC
  • a song about MVC by James Dempsey (lyrics)

11
MVC
  • 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.

12
Pattern 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

13
Pattern 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

14
Pattern Categories II -- Scope
  • Class Patterns
  • Describe how relationships between classes are
    defined via inheritance.
  • Relationships in class patterns are established
    at compile time.

15
Pattern 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

16
More about Pattern
  • Patterns of Patterns
  • Anti-pattern
  • J2EE patterns

17
Anti-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
??????
  • ?????????
  • ?????????
  • ?????????
  • ?????????
  • ?????????
  • ???????????
  • ???????
  • ?????????
  • ??????
  • ????????

19
Example
  • ?????
  • ???????????????,??????????????????????????????????
    ,??????????????
  • ?????
  • ??????????????
  • ??????????????????????
  • ??????????????????????
  • ?????????,???????????????????
  • ??????????,????????
  • ?????????????????????
  • ????????????????????

20
Example
  • ???? (Refactor)
  • ?????????????????????,??????????????
  • ????????????????,???????????????
  • ??????????????????,?????????
  • ?????????????????????
  • ??,??????????
  • ??,????????????????,???????????????,??????????????
    ?,??????????

21
More about Pattern
  • Patterns of Patterns
  • Anti-pattern
  • J2EE patterns

22
J2EE Patterns
  • ??
  • ????????,????J2EE?????Java????????
  • ??????????,?J2EE??????????????????????????
  • ????????,??????????????,?????
  • ???????

23
J2EE 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

24
J2EE 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

25
Example
  • EJB??????????
  • ??EJB ??EJBHome em JNDIServer.getRemoteHome(EJ
    B-JNDI-NAME) EJBObject myEJB em.create()
  • em.create()??Afactory.create()
  • EJBObject ???

26
Example
  • ?? Bean???Bean Façade??
  • ????Bean???????Bean
  • ???Bean???Façade?/Manager?
  • ??Façade ??Bean??
  • ????,???????????Bean?????
  • ????,???????

27
Example
  • DTO(Data Transfer Object)??
  • DTO?????????????JavaBeans,?J2EE?????????
  • DTO????,???????Message
  • ?JavaBeans?????????Model

28
Example
  • MVC??
  • MVC???J2EE Web??????

29
Example
  • Proxy??
  • ???????????????????????????????
  • ???????????????(Access Proxy)????????????
  • ??Proxy??
  • ??????Java???(Reflect)??,?????????????????????????
    ?????????
  • ???????????????????????????????,??????????????????
    ?

30
J2EE Anti-Pattern
  • ?EJB??J2EE
  • ????
  • ???????
  • ????????Session Bean
  • ????
  • ??Servlet????JSP

31
NowOO Design Toolbox
  • OO Basics
  • OO Principles
  • OO Patterns
  • Your patterns here!

32
??
  • More about Pattern
  • Framework why, what, how
  • Comparison

33
Forms 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.

34
Framework
  • 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.

35
Framework
  • ??,????????????,??????,???????????????????????????
    ,????????,????????,????????
  • ????????????(?J2EE)??????????????

36
Framework
????
??????
????
???????
?????
????
37
Framework
  • First commercial application framework MacApp by
    Apple Computer for Macintosh
  • Well-known frameworks
  • MFC
  • Struts, Spring, Avalon, PicoContainer
  • Hibernate
  • Jdon

38
Framework ??
  • 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.

39
How to design a good framework?
  • ?????? (Domain Driven Design)
  • ????IoC (Inversion of Control) or ????DI
    (Dependence Injection)
  • AOP (Aspect Oriented Programming)

40
Domain 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.

41
DDD Background
  • ????????Java??Web????,????????
  • Ruby on Rails (RoR)?Web???????Java??????????????
    ????????????
  • ?Java??????DDD??(RoR)??,????Java??????,?????????,
    ????IoC/AOP??????

42
DDD
  • 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
44
DDD
  • ????
  • ??????(Domain Model)??,?????????????,??????????
    ,??????????????
  • ??
  • ????????,????Delphi/VB?????????????

45
DDD??
  • ??????????(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???????,???????

50
DDD advantage
  • ?????? ???Model??????

51
?Jdon Framework (JF)??
  • http//www.jdon.com/jdonframework/
  • Domain Model?JF?????????????
  • ??Model??????????????
  • ?Domain Model????????????

?Domain???
52
??JF?DDD????
53
Model Configuration
  • ????????MVC??Controller??

54
IoC
  • ???? 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

55
IoC 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()
56
Example
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!
57
IoC Motivation
  • ??????,?????????,?????????????????,???????????,???
    ??????,????????????,?????,????????????????????,???
    ?????

58
IoC 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.
59
IoC 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!
60
IoC 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!
61
IoC 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
62
IoC 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.

63
IoC Approach III
  • Need a constructor

public class JDBCDataManger private
DataSource dataSource public
JDBCDataManger(DataSource dataSource)
this.dataSource dataSource public
void getData() //use dataSource for
something
64
IoC 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
65
IoC 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
66
Comparison
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??????
67
IoC Timeline
68
Spring 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

69
Aspect-Oriented Programming
  • Separation of Concerns
  • Crosscutting Concerns
  • Aspects
  • Weaving

70
Aspect-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

72
Comparison
  • ???????
  • ???????????????????
  • ???????????,?????????????????
  • ?????
  • ???????

73
Comparison
  • ??????????
  • ????????????,??????
  • ????????,??????????
  • ?????????????????
  • ??????????????????,????
  • ????????????
  • ???????????????
  • ???OO???????????,???OO????????????????

74
Comparison
  • ?????,????????????,?????????

75
More Comparison
  • ?? vs. ????
  • ?? vs. ???,??
  • ?? vs. ??????

76
??(????????)
  • ????(container)???,?????????,??????????
  • ??????,??Spring??????IoC??
  • Ref
  • ????Presentation????????email??,OO????/????/??????
    ?
Write a Comment
User Comments (0)
About PowerShow.com