You are on page 1of 36

J2EE Design Pattern: Part I

(Basic OOPS Concept & Introduction to Patterns)


Sunny Ratra
http://sunnyratra.me/
Course rerequlslLes & Cb[ecuves
rerequlslLes
8aslc knowledge of CCS concepL
8aslc knowledge of !2LL framework
Cb[ecuves
1o provlde an overvlew of CCS ConcepL & lnLroducuon Lo !2LL
ueslgn auern
http://sunnyratra.me/
Sesslon CuLllne
ueslgn Coals
8aslc Cb[ecL-CrlenLed ueslgn ConcepLs
Cb[ecL-CrlenLed ueslgn rlnclples
!2LL and ueslgn auerns
Mulu-uered !2LL appllcauons
ArchlLecLural auerns : ueLalls of MvC ueslgn auern
!2LL 1echnologles ln 1lers
!2LL auern CaLalog
resenLauon 1ler auerns
lnLegrauon 1ler auerns
8uslness 1ler auerns
http://sunnyratra.me/
ueslgn Coals
1he deslgn prlnclples and pauerns are Lools LhaL help us Lo achleve Lhe
followlng deslgn goals :
AdapLablllLy
LxLenslblllLy
MalnLalnablllLy
8eusablllLy
erformance
ScalablllLy
8ellablllLy
SecurlLy

http://sunnyratra.me/
8aslc Cb[ecL-CrlenLed ueslgn ConcepLs
Lecuve use of Lhe fundamenLal ob[ecL-orlenLed concepLs can lmprove an
appllcauons deslgn. 1hese fundamenLal concepLs are Lhe foundauon LhaL
ob[ecL-orlenLed deslgn pauerns and !2LL pauerns bulld upon. 1he followlng
ob[ecL-orlenLed concepLs are presenLed:
Lncapsulauon
olymorphlsm
lmplemenLauon lnherlLance
lnLerface lnherlLance
Coheslon
Coupllng
Composluon
http://sunnyratra.me/
lnLerface lnherlLance
lnLerface lnherlLance ls Lhe separauon of an lnLerface denluon from lLs
lmplemenLauon. Whlle lmplemenLauon lnherlLance can cause ughL coupllng,
lnLerface lmplemenLauon can help malnLaln loose coupllng.
When a class lmplemenLs a !ava lnLerface, Lhe class does noL lnherlL any
lmplemenLauon, only Lhe lnLerface. 1hus, Lhere ls a separauon of Lhe
lnLerface and Lhe lmplemenLauon.


http://sunnyratra.me/
Coheslon
Coheslon ls Lhe degree Lo whlch Lhe meLhods and aurlbuLes of a class focus
on only one purpose ln Lhe sysLem. 1he followlng descrlbes Lhe Lwo ends of
Lhe coheslon conunuum: four dlerenL levels of coupllng are shown:

Low cohes|on - Many unrelaLed funcuons wlLhln Lhe same componenL 8lg
classes wlLh many unrelaLed funcuons are usually hard Lo malnLaln.

n|gh cohes|on - Cnly relaLed funcuons wlLhln Lhe same componenL Small
classes wlLh fewer, hlghly relaLed funcuons are usually easler Lo malnLaln.

http://sunnyratra.me/
AdvanLages for malnLalnlng hlgh Coheslon
MalnLalnlng hlgh coheslon has Lhe followlng advanLages:
Avolds slde aecLs of changlng unrelaLed code wlLhln Lhe same class.
lmproves code readablllLy by clarlfylng Lhe role of a class.
laclllLaLes Lhe creauon of small reusable componenLs.


http://sunnyratra.me/
Coupllng
Coupllng ls a measure of how dependenL classes are on oLher classes. 1he ughLer
1he coupllng beLween Lwo classes, Lhe more llkely LhaL a change ln one class wlll
requlre a change ln Lhe second class.
four dlerenL levels of coupllng are shown:
1|ght coup||ng - 1he CllenL class ls a subclass of Lhe Servlce class. Subclasses
are exposed Lo Lhe lnLernal lmplemenLauon of Lhelr superclass.
Looser coup||ng - A CllenL class dlrecLly references Lhe Servlce class and ls
exposed Lo all of Lhe Servlce classes publlc meLhods and aurlbuLes.
Looser abstract coup||ng - 1he CllenL class references Lhe Servlcelmpl class
Lhrough an lnLerface. lL ls only exposed Lo Lhe parLs of Lhe Servlcelmpl class
exposed Lo Lhe lnLerface.
No coup||ng - 1he CllenL class has no reference Lo Lhe Servlce class aL all.

http://sunnyratra.me/
Coupllng
http://sunnyratra.me/
Coupllng
1he followlng meLhods can help us Lo reduce coupllng beLween classes:

Plde Lhe lmplemenLauon of Lhe classes.
Couple Lhe rsL class Lo only Lhe absLracL lnLerface of Lhe second class.
8educe Lhe number of meLhods ln Lhe lnLerface of Lhe class.

Someumes, lL ls more worLhwhlle Lo look aL Lhe coupllng of Lhe enure sysLem,
Lhan beLween lndlvldual classes. lL ls lmposslble Lo ellmlnaLe coupllng and
reLaln a useful sysLem. WlLhouL coupllng, classes cannoL communlcaLe wlLh
oLher classes. 1herefore, Lhe goal ls Lo keep reasonably loose coupllng
beLween ob[ecLs, whlle noL llmlung Lhe funcuonallLy of Lhe sysLem.

http://sunnyratra.me/
Composluon
Composluon ls a loosely coupled relauonshlp beLween classes based on
delegauon raLher Lhan rellance on lmplemenLauon deLalls.
1he composluon aggregauon relauonshlp ls [usL anoLher form of Lhe
aggregauon relauonshlp, buL Lhe chlld class's lnsLance llfecycle ls dependenL
on Lhe parenL class's lnsLance llfecycle.



A company class lnsLance wlll always have aL leasL one ueparLmenL class
lnsLance. 8ecause Lhe relauonshlp ls a composluon relauonshlp, when Lhe
Company lnsLance ls removed/desLroyed, Lhe ueparLmenL lnsLance ls
auLomaucally removed/desLroyed as well.

http://sunnyratra.me/
ulerence beLween Aggregauon,
Composluon and uelegauon
Aggreganon: When ob[ecL conslsLs of oLher ob[ecLs whlch can llve even
aer ob[ecL ls desLroyed. 1hls Lype of relauonshlp applles Lo deslgns
where Lhe "parL" ob[ecL can exlsL ln oLher places ln Lhe sysLem and does
not enurely depend upon a parucular "whole" ob[ecL for lLs exlsLence.
Compos|non: When ob[ecL conslsLs of oLher ob[ecLs whlch ln Lurn cannoL
exlsL aer my ob[ecL ls desLroyed-garbage collecLed. 1hls Lype of
relauonshlp applles Lo deslgns where Lhe "parL" ob[ecL cannot exlsL ln
oLher places ln Lhe sysLem and |s enurely dependenL upon a parucular
"whole" ob[ecL for lLs exlsLence.

De|eganon: When ob[ecL uses anoLher ob[ecL's funcuonallLy as ls wlLhouL
changlng lL.



http://sunnyratra.me/
Cb[ecL-CrlenLed ueslgn rlnclples
1he followlng Col prlnclples can help you achleve Lhe !2LL plauorm deslgn
goals. 1hese deslgn prlnclples bulld upon Lhe fundamenLal ob[ecL-orlenLed
concepLs and provlde a foundauon for Lhe Cang of lour deslgn pauerns:

lavorlng composluon
rogrammlng Lo an lnLerface
ueslgnlng for change




http://sunnyratra.me/
lavorlng Composluon
lavor ob[ecL composluon over [lmplemenLauon] lnherlLance.

Imp|ementanon |nher|tance |s wh|te-box reuse : ln oLher words, Lhe
subclass ls Lxposed Lo Lhe lnLernal lmplemenLauon of Lhe superclass
Lhrough lnherlLance.

Compos|non |s b|ack-box reuse : ln oLher words, Lhe cllenL class ls
Lxposed only Lo Lhe lnLerface of Lhe class LhaL lL uses. 1hus Lhe coupllng
beLween Lhe Classes LhaL use composluon ls looser.

http://sunnyratra.me/
lavorlng Composluon
http://sunnyratra.me/
rogrammlng Lo an lnLerface
rogram Lo an lnLerface, noL an lmplemenLauon.
1hls prlnclple ls more formally sLaLed as the Dependency Invers|on
r|nc|p|e. 1hls prlnclple says LhaL classes should depend upon absLracuons,
noL on lmplemenLauons.


http://sunnyratra.me/
ueslgnlng for Change
1hls prlnclple ls more formally sLaLed as Lhe Cpen-C|osed r|nc|p|e. lL sLaLes
1haL soware enuues, such as classes, should be open for exLenslon, buL
closed for Modlcauon.

1he requlremenLs for appllcauons always change, you should creaLe
sysLems LhaL are adapLable and supporL new and changlng requlremenLs,
wlLhouL havlng ma[or redeslgns or havlng changes rlpple Lhrough large parLs
of Lhe sysLem.

http://sunnyratra.me/
!2LL and ueslgn auerns
!2LL: An CL8A1lnC S?S1LM lC8 1PL WL8 LnLerprlse web appllcauons,
whlch llve on neLworks and are accesslble Lhrough browsers, are
redenlng LnLerprlse Web Soware. 1hls ls Lhe nexL wave of compuung.
ueslgn auerns capLure soluuons LhaL have developed and evolved over
ume . 1hey reecL unLold redeslgn and recodlng as developers have
sLruggled for greaLer reuse and exlblllLy ln Lhelr soware.- CCl ln
ueslgn auerns - LlemenLs of 8eusable Cb[ecL CrlenLed Soware.
1he !2LL archlLecLure ls bullL Lo enable componenL developers Lo use a
Mode| V|ew Contro||er (MVC) Des|gn auern.
http://sunnyratra.me/
J2EE Application I J2EE Application II
Client tier
Web tier
Business tier
EIS tier
Client
machine
J2EE
Server
machine
DataBase
Server
machine
Application
Client
Dynamic HTML
pages
JSP/Servlet
Enterprise
Beans
Database Database
Enerprise
Beans
Mulu-uered !2LL appllcauons
http://sunnyratra.me/
Arch|tectura| auerns : Deta||s of MVC Des|gn auern
1he Model vlew ConLroller (MvC) pauern ls an archlLecLural pauern.
Name (essence of Lhe pauern)
Model vlew ConLroller MvC

Context (where does Lhls problem occur)
MvC ls an archlLecLural pauern LhaL ls used when developlng
lnLeracuve appllcauon such as a shopplng carL on Lhe
lnLerneL.

rob|em (denluon of Lhe reoccurrlng dlmculLy)
user lnLerfaces change oen, especlally on Lhe lnLerneL
where look-and-feel ls a compeuuve lssue. Also, Lhe same
lnformauon ls presenLed ln dlerenL ways. 1he core
buslness loglc and daLa ls sLable.
http://sunnyratra.me/
MvC conunued
So|unon (how do you solve Lhe problem)
use Lhe soware englneerlng prlnclple of separauon of
concerns Lo dlvlde Lhe appllcauon lnLo Lhree areas:
Model encapsulates the core data and
functionality
View encapsulates the presentation of the data
there can be many views of the common data
Controller accepts input from the user and
makes request from the model for the data to
produce a new view.
http://sunnyratra.me/
MvC SLrucLure for !2LL
http://sunnyratra.me/
MvC SLrucLure for !2LL
http://sunnyratra.me/
!2LL 1echnologles ln 1lers
http://sunnyratra.me/
1he separauon of Lhe soware sysLem lnLo ve ma[or uers:

C||ent - rovldes user lnLeracuon wlLh Lhe sysLem.1he CllenL uer
encompasses all of Lhe componenLs used by Lhe user Lo lnLeracL wlLh Lhe
sysLem

resentanon - rovldes Lhe Web pages and forms LhaL are senL Lo Lhe
Web browser and processes Lhe users requesLs.1he resenLauon uer
provldes a buer beLween a Lhln cllenL and Lhe 8uslness uer.

8us|ness - rovldes Lhe buslness servlces and enuues.1he 8uslness uer
provldes Lhe componenLs LhaL manage Lhe buslness loglc, rules, and
enuues of Lhe appllcauon.
!2LL 1echnologles ln 1lers
http://sunnyratra.me/

Integranon - rovldes componenLs Lo lnLegraLe Lhe 8uslness
uer wlLh Lhe 8esources uer.1he lnLegrauon uer provldes
componenLs LhaL perform Lhe fundamenLal creaLe, reLrleve,
updaLe, and deleLe (C8uu) operauons for a glven buslness enuLy.

kesource - ConLalns all back-end resources, such as a uaLa8ase
ManagemenL SysLem (u8MS) or LnLerprlse lnformauon SysLem
(LlS).1he 8esource uer lncludes all exLernal sysLems LhaL record
buslness daLa.
!2LL 1echnologles ln 1lers
http://sunnyratra.me/
!2LL auern CaLalog
1he !2LL pauerns caLalog ls grouped lnLo uers.
1hls paruuonlng ls loglcal raLher Lhan physlcal. 1he Lhree uers
are:
resenLauon uer
8uslness uer
lnLegrauon uer


http://sunnyratra.me/
resenLauon 1ler auerns
1he presenLauon uer manages Lhe user lnLerface Lo Lhe
appllcauon. 1hls uer ls prlmarlly lmplemenLed wlLh servleLs,
!S pages, helper beans, and cusLom Lags. resenLauon 1ler
pauerns focus on achlevlng Lhese goals:

rovlde guldance for deslgnlng lndlvldual presenLauon uer
componenLs by role.
rovlde mechanlsms Lo reduce copy-and-pasLe reuse.
Lnforce role separauon beLween loglc and presenLauon
componenLs.
http://sunnyratra.me/
resenLauon 1ler auerns
http://sunnyratra.me/
Model 2 ArchlLecLure
http://sunnyratra.me/
lnLegrauon 1ler auerns
lnLegrauon uer pauerns are one seL of !2LL pauerns provlded by Lhe
!ava CenLer program.

lnLegrauon uer pauerns encapsulaLe access Lo Lhe daLa sLores used by
a !2LL plauorm appllcauon. 1hls encapsulauon focuses on managlng
Lhe
deLalls needed Lo load and sLore daLa, frequenLly relauonal daLa, on
behalf of enLerprlse classes. 1he lnLegrauon uer also malnLalns
responslblllLy for provldlng access Lo auxlllary servlces such as
messaglng Lo Lhe buslness uer. llnally, lnLegrauon uer pauerns deal
wlLh lnLegraung web servlces wlLh buslness uer componenLs!
http://sunnyratra.me/
lnLegrauon 1ler auerns
http://sunnyratra.me/
8uslness uer pauerns have a Lwofold role, sLemmlng from Lhelr posluon
beLween Lhe lnLegrauon and presenLauon uers. lL provlde Lhe loglc LhaL
operaLes on sLored daLa and shapes lL Lo reecL a buslness purpose.

8uslness uer pauerns:

Crganlze buslness loglc so LhaL lL ls slmple and unlform.
8educe coupllng among componenLs ln dlerenL uers.
8educe neLwork overhead.

8uslness 1ler auerns
http://sunnyratra.me/
8uslness 1ler auerns
http://sunnyratra.me/



1hank ?ou !!
Pave A nlce uay
http://sunnyratra.me/

You might also like