J2EE DESIGN PATTERN @Diego Thanh Nguyen - 10/8/12.

39
J2EE DESIGN PATTERN @Diego Thanh Nguyen - http://free.smartbiz.vn 10/8/12 www.smartbiz.vn 1

Transcript of J2EE DESIGN PATTERN @Diego Thanh Nguyen - 10/8/12.

Page 1: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

J2EE DESIGN PATTERN@Diego Thanh Nguyen - http://free.smartbiz.vn

10/8/12 www.smartbiz.vn 1

Page 2: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

Table Of Content I. GoF Design Pattern

I.1. Creation PatternsI.2. Structural PatternsI.3. Behavioral Patterns

II. J2EE Presentation: III. J2EE Business: IV. J2EE Integration

10/8/12 www.smartbiz.vn 2

Page 3: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I. How Design Patterns Arise ?

10/8/12 www.smartbiz.vn 3

1.Problem

2. Context

3.Solution

Benefits

Related Patterns

Consequences

Forces

Pattern

Solution

Problem

Context

a design situation giving rise to a design problem

a form or rule that can be applied to resolve these forces

a set of forces occuring in that context

IF you find yourself in CONTEXT for example EXAMPLES, with PROBLEM, entailing FORCESTHEN for some REASONS, apply DESIGN FORM AND/OR RULE to construct SOLUTION leading to NEW CONTEXT & OTHER PATTERNS

Page 4: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I. Design-Pattern Catalog

10/8/12 www.smartbiz.vn 4

Defer object creation to another object

Describe ways to assemble objects

Describe algorithms and flow control

Page 5: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.1. Creation Patterns

Creational Patterns prescribe the way that objects are created. These patterns are used when a decision must be made at the time a class is instantiated.

Singleton: ensure a class has one Instance, and provide a global point of access to it.

Abstract Factory: provide an interface for creating families of related or dependent objects without specifying their concrete classes.

10/8/12 www.smartbiz.vn 5

Page 6: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.1.1 Abstract Factory Intent: create families of related objects without

specifying subclass names Applicability: when clients cannot anticipate

groups of classes to instantiate Concrete factories create groups of strategies

10/8/12 www.smartbiz.vn 6

Page 7: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.2. Structural Patterns

Structural Patterns prescribe the organization of classes and objects.

Adapter Convert the interface of a class into another

interface that clients expect Adapter lets classes work together that couldn’t

otherwise because of incompatible interfaces Decorator

Extend the functionality of the original class in a way that is transparent to the client class

10/8/12 www.smartbiz.vn 7

Page 8: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.2.1. Composite

Intenttreat individual objects & multiple, recursively-

composed objects uniformly Applicability

Objects must be composed recursively,and no distinction between individual & composed

elements,and objects in structure

can be treated uniformly

10/8/12 www.smartbiz.vn 8

Page 9: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.2.1. Composite

CORBA Naming Service example using CosNaming::BindingIterator (which is an example of the “Batch Iterator” pattern compound from POSA5)

10/8/12 www.smartbiz.vn 9

Composite Node Leaf Node

Page 10: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.3. Behavioral Patterns

Behavioral Patterns prescribe the way objects interact with each other. They help make complex behavior manageable by specifying the responsibilities of objects and the ways they communicate with each other.

State: allow an object to alter its behavior when its internal state changes

10/8/12 www.smartbiz.vn 10

Page 11: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.3.1. Strategy Intent

define a family of algorithms, encapsulate each one, & make them

interchangeable to let clients & algorithms vary independently

Applicability When object is configurable with one of many algorithms, and all algorithms can be encapsulated, and one interface covers all encapsulations

10/8/12 www.smartbiz.vn 11

Page 12: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.3.1. Strategy

Strategy applied in distributed (middleware)

10/8/12 www.smartbiz.vn 12

Hook for the concurrency strategy

Hook for the request demuxing strategy

Hook for marshaling strategy

Hook for the connection management strategy

Hook for the underlying transport strategy

Hook for the event demuxing strategy

Page 13: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

13

I.3.1. Oracle Fusion Component

EnterConnect Groupware API

Jakarta Slide API

Jakarta Commons HTTP Client

Exchange Connector

Jakarta Commons Logging Java Transaction API

XML HTTP request

XML HTTP response

JDOM (JSR 102)

OpenGroupware Connector

Lotus Domino Connector

MS Exchange 2003

Email

Calendar

Contact

Task

Uploaded Document

Web

DA

V S

erve

r

OracleFusion Groupware API: A unified interface for all connectors that access the groupware servers via WebDAV, applying Singleton, Façade and Factory design patterns

All connectors utilize the Jakarta Jackrabbit (client-side) API to send requests to and receive responses from a variety of WebDAV-enabled groupware servers

Jakarta Jackrabbit re-uses some of open source libraries such as the Jakarta Commons library (HTTP Client &Logging), JDOM (JSR 102), JTA, JMX, …

Page 14: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

10/8/12 www.smartbiz.vn 14

I.3.2. OF Design PatternSingleton: the Connector class must only have one instance because it’s wasteful &

useless to keep several instances on memory. Keeping so many connectors will difficult to manage lifecycle & impact the performance of application.

Factory Method: because of we assume our API can be used against multiple servers so that there must be a Connector for each server (i.e. ECExchangeConnector, ECLotusDominoConnector, ECOpenGroupwareConnector). Each connector is responsible for manufacturing its own managers (MailManager, CalendarManager, TasksManager) and delegate the jobs to them. The connector will only be determined by the client depending on the configuration.For example, if a client (portlet) need to send a mail by Exchange server then it will

call the ECExchangeConnector; this connector then will use its localized manager ExchangeMailManager to make life easier.

Abstract Factory: ECGroupwareFactory is the abstract factory class for all connectors because it will expose a unified interface for connectors to client.

Facade: the unified interface will be defined in the Façade pattern. As stated before, a specific Connector will delegate the appropriate jobs to its subsystems (managers). The façade pattern also decouples the managers from the client and other managers, thereby promoting independence and portability.

Page 15: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

15

I.3.1. OF Design Pattern

<<Abstract Factory>>

ECGroupwareFactory

ECLotusDominoConnector ECOpenGroupwareConnector

<<Singleton>>

<<Façade>>

ECExchangeConnector

<<Factory Method>>

«interface»MailManager

ExchangMailManager

ExchangeCalendarManager

ExchangeTasksManager

«interface»CalendarManager

«interface»TasksManager

Apache Slide Class

10/8/12 www.smartbiz.vn

Page 16: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.4.1. Observer Pattern

Easier to keep a consistent and maintainable view of the dataHalf as many connections between actions, viewsViews are independent and unaware of each other

10/8/12 www.smartbiz.vn 16

Action Action Action Action Action Action Action Action

Data

View View & Action View Messages about the data

Data notifies observers via events when the state of the data changes

Page 17: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.4.2. Implementing an Observer

1. Assign the subject to theobservers (MainForm_Load)

IssueSubject

10/8/12 www.smartbiz.vn 17

Page 18: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.4.3. Implementing an Observer

m_subject = new IssueSubject()paneA.Subject = m_subjectpaneB.Subject = m_subject ...

1. Assign the subject to theobservers (MainForm_Load)

IssueSubject

a

bc d

10/8/12 www.smartbiz.vn 18

Page 19: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.4.4. Implementing an Observer

m_subject = new IssueSubject()paneA.Subject = m_subjectpaneB.Subject = m_subject ...

1. Assign the subject to theobservers (MainForm_Load)

2. Observers bind and savechanges to Subject.DataSet

IssueSubject

a

bc d

10/8/12 www.smartbiz.vn 19

Page 20: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.4.5. Implementing an Observer

m_subject = new IssueSubject() paneA.Subject = m_subjectpaneB.Subject = m_subject ...

1. Assign the subject to theobservers (MainForm_Load)

2. Observers bind and savechanges to Subject.DataSet

3. When data changes, subjectraises DataChanged event

a

bc d

IssueSubject

10/8/12 www.smartbiz.vn 20

Page 21: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.4.6. Implementing an Observer

m_subject = new IssueSubject()paneA.Subject = m_subjectpaneB.Subject = m_subject ...

1. Assign the subject to theobservers (MainForm_Load)

4. Observers handle subjectevents to rebind data, if they carem_subject.DataChanged += …‘ rebind

2. Observers bind and savechanges to Subject.DataSet

3. When data changes, subjectraises DataChanged event

IssueSubject

a

bc d

10/8/12 www.smartbiz.vn 21

Page 22: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.5.1. Coordinate Command State

Related menu and toolbar widgets are not automatically handled together

Command Pattern Command objects unify the state and action for related

UI widgets

Example:“Save” action

Menu item Toolbar button Context menu

10/8/12 www.smartbiz.vn 22

Page 23: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.5.2. Implementing a Command

10/8/12 www.smartbiz.vn 23

action = new Command.Action(this.WorkOffline_Action)offlineCommand = new Command(action)

1. Create a Command for each action in MainForm_Load()

MenuItemCommander.Connect(menuWorkOffline, offlineCommand )ToolBarButtonCommander.Connect(tlbOffline, offlineCommand )

2. Wire menu items and toolbar buttons to the Command using Commander objects

3. Control button and menu item state through the Command

' Disable all UI widgets connected to this commandofflineCommand.IsEnabled = false

Page 24: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

I.6.1. Accelerated J2EE Development

Architect Service-Oriented Components Design for Flexible, Agile Applications and

Iterative Development

Apply/Extend Pre-Built COTS Components Infrastructure and Common Business Components

Generate/Extend Custom Components Rapidly Specify and Provision New Components

Leverage Industry Development Standards Frameworks (STRUTS, Spring / Weld, Hibernate) Patterns (MVC) XML, Best Practices

10/8/12 www.smartbiz.vn 24

Page 25: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

II. J2EE Application Design Patterns

10/8/12 www.smartbiz.vn 25

EJB Container

EJB Container EJB Container

PresentationBusiness Logic &Messaging Fabric

Data Integration& Persistence

Action Servlet

Action Object

Action Form

JSF, JSP (Taglibs)

Value Object

XMLAction to Form & Forward

Mappings

Business Service Proxy

Data Access Proxy

BusinessDelegateAdapter

BusinessDelegateFactory

Business Service Facade

Value Object

AssemblerData

Access Proxy

Value Object

(Stateless) Session

Bean

Serialized Value Object

Data Connection

Factory

Connection Pool

Resources

Database

DirectoryJMS

Other Infrastructure

Event Interceptor Framework

Security Management Framework

Event Notification Framework

Messaging Command

Factory

Filter Interceptors

Service Activation

Message Q& Channel

Locator

Data Access Object

Request

1. Request

4. Dispatch 2. Fill State3. Validate

16. Forward

<<uses>>

<<uses>>

<<uses>>

<<supplies>>

5. Get Delegate

6. Execute

7a.ExecuteBiz Method

7b. CRUD Data

17. State?

15. Fill State

18. Response

8.ExecuteBiz Method

9.AssembleData

10b.Get/SetRemove

10a.Invoke

13.CRUDData

14.Query

<<uses>>

<<uses>>

<<creates>>

<<de-serialize>>

Key

M

V

C

P OtherPattern

ModelPattern

ViewPattern

ControllerPattern

To DAO

Response

Web Server &Servlet Engine

JMS

RMI

JMS

RMI

<<uses>>

<<uses>>

11.CRUDData

Entity Bean

12.Get/SetRemove

Page 26: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

II. Model-View-Controller – Passive

10/8/12 www.smartbiz.vn 26

:Controller :Model :View

handleEvent

service

update

getData

Page 27: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

II. Model-View-Controller – Active

10/8/12 www.smartbiz.vn 27

Model

Controller

View

<<interface>>Observer

+update()

:Model :View

update

getData

data

handleEventnotify

Page 28: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

II.1. Presentation Tier Patterns

Intercepting Filter Front Controller: Use of a Controller to

handle all web requests Dispatcher View: The controller works with

a Dispatcher that handles navigation Composite View View Helper Service to Worker

10/8/12 www.smartbiz.vn 28

Page 29: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

II.2. Intercepting Filter

Problems:Verify authenticationCheck type of clientIs the session valid?

Solution: implement one or more Intercepting filters.

Sample: Filter-servlet - can transparently pre and post process all requests in an unlimited length filter chain.

10/8/12 www.smartbiz.vn 29

Page 30: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

II.3. Front Controller & Dispatcher

Front Controller - Problems:Multiple views.Navigation logic embedded in views.Difficult to maintain view logic as application

grows.Want to reuse view navigation within multiple

clients.

10/8/12 www.smartbiz.vn 30

Page 31: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

II.3. Front Controller - Dispatcher

10/8/12 www.smartbiz.vn 31

Client FrontController Dispatcher View Helper

1: Send Request

1.1: Delegate Request

1.1.1: Forward Request

2: Send Request

2.1: Forward Request

3: Send Request

3.1: Process Request

Page 32: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

III.4. View Helper

Problem:Many common operations in each view.Often view output is dependant on business

data.Create more cohesive look and feel.Like to hand off UI to web design group

Solution:Basically anything that helps you display your

model data in a reusable fashion.Implemented as JavaBeans, Tags or XML/XSLT

See the JSTL, JSF and Struts.

10/8/12 www.smartbiz.vn 32

Page 33: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

II.5. Composite View

Problem:Many common & recurring components in a view. Fragments may display differently given user type

(i.e. faculty, student).Look and feel in flux needs to be easily

updateable. Differs from View Helper as the Composite

View decides which sub-views to include while View Helper generally decides how to display a given sub-view.

10/8/12 www.smartbiz.vn 33

Page 34: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

III. Business Tier Patterns

Business Delegate: use of a Business Delegate to hide Implementation Detail.

Session Façade Service Locator Value Object Composite Entity Value Object Assembler Value List Handler

10/8/12 www.smartbiz.vn 34

Page 35: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

III.1. Business Delegate

10/8/12 www.smartbiz.vn 35

Client

BusinessDelegate

Handle

BusinessService

1: Create with ID

1.1: Convert ID to Handle

1.1.1: Create

2: Invoke

2.1: Get Business Service

2.1.1: Connect

2.1.2: Return Business Service

2.2: Invoke

Page 36: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

III.2. Session Façade Pattern Problem:

Need very loose coupling between client and ever changing business tier.

Data tier changing; need to improve reuse. Session Bean

provides unified approach Façade hides

Entity Bean Details

10/8/12 www.smartbiz.vn 36

ClientSession

BeanEntity Bean

EJB Tier

Page 37: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

IV. Integration

Data Access Object Service Activator User Workflow Integrator

10/8/12 www.smartbiz.vn 37

Page 38: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

IV.1. Data Access Object

10/8/12 www.smartbiz.vn 38

BusinessObject

DataAccessObject

accesses

OracleDAO

DB2DAOSybase

DAO

OracleDatabase

DB2Database

SybaseDatabase

adapts adapts adapts

inherits inherits inherits

Page 39: J2EE DESIGN PATTERN @Diego Thanh Nguyen -  10/8/12.

10/8/12 www.smartbiz.vn 39