J2EE DESIGN PATTERN

streakconvertingΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 7 μήνες)

77 εμφανίσεις

J2EE DESIGN PATTERN

@Diego Thanh Nguyen
-

http://
free.smartbiz.vn

10/8/12

www.smartbiz.vn

1

Table Of Content


I.
GoF

Design Pattern


I.1. Creation
Patterns


I.2. Structural
Patterns


I.3. Behavioral Patterns


II. J2EE Presentation
:


III. J2EE Business:


IV. J2EE Integration

10/8/12

www.smartbiz.vn

2

I. How Design Patterns Arise ?


10/8/12

www.smartbiz.vn

3


1.

Problem


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
FORCES

THEN

for
some
REASONS
,


apply
DESIGN FORM AND/OR RULE


to
construct
SOLUTION



leading
to
NEW CONTEXT
&
OTHER PATTERNS

I. Design
-
Pattern Catalog


10/8/12

www.smartbiz.vn

4



P
u
r
p
o
s
e

C
r
e
a
t
i
o
n
a
l

S
t
r
u
c
t
u
r
a
l

B
e
h
a
v
i
o
r
a
l

S
c
o
p
e

C
l
a
s
s



F
a
c
t
o
r
y

M
e
t
h
o
d



A
d
a
p
t
e
r



I
n
t
e
r
p
e
r
t
e
r

O
b
j
e
c
t



S
i
n
g
l
e
t
o
n



F
a
c
t
o
r
y



A
b
s
t
r
a
c
t

F
a
c
t
o
r
y



B
u
i
l
d
e
r



P
r
o
t
o
t
y
p
e



O
b
j
e
c
t

P
o
o
l



F
a
ç
a
d
e



D
e
c
o
r
a
t
o
r



C
o
m
p
o
s
i
t
e



A
d
a
p
t
e
r



B
r
i
d
g
e



F
l
y
w
e
i
g
h
t



P
r
o
x
y



O
b
s
e
r
v
e
r



C
o
m
m
a
n
d



I
t
e
r
a
t
o
r



C
h
a
i
n

o
f

R
e
s
p
o
n
s
i
b
i
l
i
t
y



M
e
d
i
a
t
o
r



M
o
m
e
n
t
o



S
t
a
t
e



S
t
r
a
t
e
g
y



V
i
s
i
t
o
r



Defer object creation to

another object

Describe ways to

assemble objects

Describe algorithms and

flow control

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

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

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

I.2.1. Composite


Intent


treat 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

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

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

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

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

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
WebDAV Server
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, …

10/8/12

www.smartbiz.vn

14

I.
3.2. OF
Design Pattern


Singleton
:

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
.

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

I.4.1.
Observer
Pattern


Easier to keep a consistent and maintainable
view of the data


Half as many connections between actions, views


Views 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

I.4
.2. Implementing
an Observer

1.
Assign the subject to the

observers (MainForm_Load)



IssueSubject




10/8/12

www.smartbiz.vn

17

I.4
.3. Implementing
an Observer

m_subject = new IssueSubject()

paneA.Subject = m_subject

paneB.Subject = m_subject ...


1.
Assign the subject to the

observers (MainForm_Load)



IssueSubject




a

b

c

d

10/8/12

www.smartbiz.vn

18

I.4
.4. Implementing
an Observer

m_subject = new IssueSubject()

paneA.Subject = m_subject

paneB.Subject = m_subject ...

1.
Assign the subject to the

observers (MainForm_Load)



2.
Observers bind and save

changes to Subject.DataSet

IssueSubject




a

b

c

d

10/8/12

www.smartbiz.vn

19

I.4
.5. Implementing
an Observer

m_subject = new IssueSubject()
paneA.Subject = m_subject

paneB.Subject = m_subject ...


1.
Assign the subject to the

observers (MainForm_Load)



2.
Observers bind and save

changes to Subject.DataSet

3.
When data changes, subject

raises DataChanged event

a

b

c

d

IssueSubject




10/8/12

www.smartbiz.vn

20

I.4
.6. Implementing
an Observer

m_subject = new IssueSubject()

paneA.Subject = m_subject

paneB.Subject = m_subject ...

1.
Assign the subject to the

observers (MainForm_Load)



4.
Observers handle subject

events to rebind data, if
they care


m_subject.DataChanged += …

‘ rebind

2.
Observers bind and save

changes to Subject.DataSet

3.
When data changes, subject

raises DataChanged event




IssueSubject




a

b

c

d

10/8/12

www.smartbiz.vn

21

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

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 command

offlineCommand.IsEnabled = false

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

II. J2EE
Application Design Patterns


10/8/12

www.smartbiz.vn

25

EJB Container

EJB Container

EJB Container

Presentation

Business Logic &

Messaging Fabric

Data Integration

& Persistence

Action
Servlet

Action
Object

Action
Form

JSF, JSP
(
Taglibs
)

Value
Object

XML

Action to
Form &
Forward
Mappings

Business
Service
Proxy

Data
Access
Proxy

Business

Delegate

Adapter

Business

Delegate

Factory

Business
Service
Facade

Value
Object
Assembler

Data
Access
Proxy

Value
Object

(Stateless)
Session
Bean

Serialized
Value
Object

Data
Connection
Factory

Connection
Pool

Resources

Database

Directory

JMS

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 State

3. Validate

16. Forward

<<uses>>

<<uses>>

<<uses>>

<<supplies>>

5. Get Delegate

6. Execute

7a.

Execute

Biz Method

7b. CRUD Data

17. State?

15. Fill State

18. Response

8.

Execute

Biz Method

9.

Assemble

Data

10b.

Get/Set

Remove

10a.

Invoke

13.

CRUD

Data

14.

Query

<<uses>>

<<uses>>

<<creates>>

<<de
-
serialize>>

Key

M

V

C

P

Other

Pattern

Model

Pattern

View

Pattern

Controller

Pattern

To DAO

Response

Web Server &

Servlet Engine

JMS

RMI

JMS

RMI

<<uses>>

<<uses>>

11.

CRUD

Data

Entity
Bean

12.

Get/Set

Remove

II.
Model
-
View
-
Controller


Passive


10/8/12

www.smartbiz.vn

26

:Controller

:Model

:View

handleEvent

service

update

getData

II.
Model
-
View
-
Controller


Active


10/8/12

www.smartbiz.vn

27

Model

Controller

View

<<interface>>

Observer

+update()

:Model

:View

update

getData

data

handleEvent

notify

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

II.2.
Intercepting Filter


Problems:


Verify authentication


Check type of client


Is 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

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

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

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

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

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

Client
Session
Bean
Entity Bean
EJB Tier
IV. Integration


Data Access Object


Service Activator


User Workflow
Integrator

10/8/12

www.smartbiz.vn

37

IV.1
. Data Access Object


10/8/12

www.smartbiz.vn

38

Business
Object
DataAcces
sObject
accesses
Oracle
DAO
DB2DAO
Sybase
DAO
Oracle
Database
DB2
Database
Sybase
Database
adapts
adapts
adapts
inherits
inherits
inherits


10/8/12

www.smartbiz.vn

39