Professional Documents
Culture Documents
Behavioral Patterns part-II Introduction Mediator Memento Observer State Strategy Template Method Visitor Reference
Behavioral Patterns part-II Introduction Mediator Memento Observer State Strategy Template Method Visitor Reference
Behavioral Patterns part-II Introduction Mediator Memento Observer State Strategy Template Method Visitor Reference
No 1 2 3 4 5 6 7 8 9
TOPIC
PPT Slides
L1 L2 L3 L4 L5 L6 L7 L8 L9 UNIT-VII 1 24 5 12 13 32 33 54 55 62 63 73 74 82 83 85 86 86
Behavioral patterns part-II introduction Mediator Memento Observer State Strategy Template Method Visitor Reference
L1
L1
UNIT-VII
L1
L2
Using a Mediator
L2
UNIT-VII
L2
UNIT-VII
Mediator as a 2ro)er
L2
UNIT-VII
Mediator 2eha"ior
L2
UNIT-VII
L2
L2
0ummar(
2ro)er patterns use a 2ro)er class to acilitate the interaction #et!een a 'lient and a 0upplier& The *a+ade pattern uses a #ro)er :the a+ade; to pro"ide a simpli ied inter ace to a comple% su#-s(stem& The Mediator pattern uses a #ro)er to encapsulate and control a comple% interaction among se"eral suppliers&
UNIT-VII 12
L2
L3
Memento Pattern
UNIT-VII
13
L3
<e erences
do*actor(&com Marc 'li ton>s 2log
6 http-==!!!&marccli ton&com=ta#id=33=7e ault&asp%
6 http-==!!!&do actor(&com=Patterns=PatternMemento&asp%
6 http-==seð9&ch=teaching=ss2445=4454=slides=.4Bso tarch
UNIT-VII
14
Intent
'apture and e%ternali9e an o#$ect>s state !ithout "iolating encapsulation& <estore the o#$ect>s state at some later time&
6 Use ul !hen implementing chec)points and undo mechanisms that let users #ac) out o tentati"e operations or reco"er rom errors& 6 ?ntrusts other o#$ects !ith the in ormation it needs to re"ert to a pre"ious state !ithout e%posing its internal structure and representations&
UNIT-VII 15
L3
L3
*orces
,pplication needs to capture states at certain times or at user discretion& Ma( #e used or6 Undue = redo 6 Cog errors or e"ents 6 2ac)trac)ing
D#$ect o!ning state ma( not )no! !hen to ta)e state snapshot&
UNIT-VII 1.
L3
Moti"ation
Man( technical processes in"ol"e the e%ploration o some comple% data structure& D ten !e need to #ac)trac) !hen a particular path pro"es unproducti"e&
6 ?%amples are graph algorithms8 searching )no!ledge #ases8 and te%t na"igation&
UNIT-VII 1/
L3
Moti"ation
Memento stores a snapshot o another o#$ect>s internal state8 e%posure o !hich !ould "iolate encapsulation and compromise the application>s relia#ilit( and e%tensi#ilit(& , graphical editor ma( encapsulate the connecti"it( relationships #et!een o#$ects in a class8 !hose pu#lic inter ace might #e insu icient to allo! precise re"ersal o a move operation&
$ndo Move
UNIT-VII 11
L3
Moti"ation
Memento pattern sol"es this pro#lem as ollo!s The editor reEuests a memento rom the o#$ect #e ore e%ecuting move operation& Driginator creates and returns a memento& 7uring undo operation8 the editor gi"es the memento #ac) to the originator& 2ased on the in ormation in the memento8 the originator restores itsel to its pre"ious state&
UNIT-VII 13
L3
,pplica#ilit(
Use the Memento pattern !hen6 , snapshot o an o#$ect>s state must #e sa"ed so that it can #e restored later8 and 6 direct access to the state !ould e%pose implementation details and #rea) encapsulation&
UNIT-VII
24
L3
0tructure
Driginator
,ttri#utestate Dperation0etMemento:Memento m; 'reateMemento: ;
Memento
,ttri#utestate DperationFet0tate: ; 0et0tate: ;
careta)er
state G m-HFet0tate: ;
UNIT-VII
21
L3
Participants
Memento
6 0tores internal state o the Driginator o#$ect& Driginator decides ho! much& 6 Protects against access #( o#$ects other than the originator& 6 Mementos ha"e t!o inter aces 'areta)er sees a narro! inter ace& Driginator sees a !ide inter ace&
UNIT-VII 22
L3
Participants :continued;
Driginator
6 'reates a memento containing a snapshot o its current internal state& 6 Uses the memento to restore its internal state&
UNIT-VII
23
L3
'areta)er
Is responsi#le or the memento>s sa e)eeping& Ne"er operates on or e%amines the contents o a memento&
UNIT-VII
24
L3
?"ent Trace
aCareta er anOriginator a!e"ento
0et0tate: ;
0etMemento:aMemento; Fet0tate: ;
UNIT-VII
25
L3
'olla#orations
, careta)er reEuests a memento rom an originator8 holds it or a time8 and passes it #ac) to the originator& Mementos are passi"e& Dnl( the originator that created a memento !ill assign or retrie"e its state&
UNIT-VII 2.
L3
'onseEuences
Memento has se"eral conseEuences6 Memento a"oids e%posing in ormation that onl( an originator should manage8 #ut or simplicit( should #e stored outside the originator& 6 @a"ing clients manage the state the( as) or simpli ies the originator&
UNIT-VII
2/
L3
'onseEuences :continued;
Using mementos ma( #e e%pensi"e8 due to cop(ing o large amounts o state or reEuent creation o mementos& , careta)er is responsi#le or deleting the mementos it cares or& , careta)er ma( incur large storage costs !hen it stores mementos&
UNIT-VII 21
L3
Implementation
5hen mementos get created and passed #ac) to their originator in a predicta#le seEuence8 then Memento can sa"e $ust incremental changes to originator>s state&
UNIT-VII
23
L3
Ino!n Uses
Memento is a 2444 ilm a#out Ceonard 0hel#( and his Euest to re"enge the #rutal murder o his !i e& Though Ceonard is hampered !ith short-term memor( loss8 he uses notes and tatoos to compile the in ormation into a suspect&
UNIT-VII
34
L3
UNIT-VII
31
L3
<elated Patterns
'ommand 'ommands can use mementos to maintain state or undo mechanisms& Iterator
Mementos can #e used or iteration&
UNIT-VII
32
L4
D#ser"er Pattern
7e ine a one-to-man( dependenc(8 all the dependents are noti ied and updated automaticall( The interaction is )no!n as #$%lis&' s$%s(ri%e or s$%s(ri%e'noti)* ,"oiding o#ser"er-speci ic update protocol- #$ll "odel "s& #$s& "odel Dther conseEuences and open issues
UNIT-VII 33
L4
D#ser"er Pattern
Intent6 7e ine a one-to-man( dependenc( #et!een o#$ects so that !hen one o#$ect changes state8 all its dependents are noti ied and updated automaticall(
Ie( orces6 There ma( #e man( o#ser"ers 6 ?ach o#ser"er ma( react di erentl( to the same noti ication 6 The su#$ect should #e as decoupled as possi#le rom the o#ser"ers to allo! o#ser"ers to change independentl( o the su#$ect
UNIT-VII 34
L4
D#ser"er
Man(-to-one dependenc( #et!een o#$ects Use !hen there are t!o or more "ie!s on the same JdataK a)a JPu#lish and su#scri#eK mechanism 'hoice o JpushK or JpullK noti ication st(les
Subject
attach:D#ser"er; detach:D#ser"er; noti (:;
Observer
orall o in o#ser"ers o&update:;
update()
'oncrete0u#$ect
get0tate:;
'oncreteD#ser"er
update:;
UNIT-VII
stateGsu#$ect&get0tate:;L 35
L4
D#ser"er'lass 7iagram
2ubject subscribe)* unsubscribe)* notify)* 5 subscribers 6 Observer update)*
L4
L4
D#ser"er'onseEuences
'onse(uences& Decouples 2ubject! which maintains state! from 0bservers! who ma-e use of the state. Can result in many spurious broadcasts when the state of 2ubject changes.
UNIT-VII
31
L4
set0tate:;
noti (:;
update:;
get0tate:; update:;
get0tate:;
UNIT-VII
33
L4
'hange in one "ie! automaticall( re lected in other "ie!s& ,lso8 change in the application data is re lected in all "ie!s&
7e ines one-to-man( dependenc( amongst o#$ects so that !hen one o#$ect changes its state8 all its dependents are noti ied&
UNIT-VII
44
L4
A ' C
Application data
41
L4
Observer
@pdate)*
'oncrete Sub)ect
$et2tate)* 2et2tate)* subject2tate UNIT-VII
@pdate)* observer2tate
L4
L4
protectedo#ser"er :;L
RL
L4
pu#lic"irtual P0u#$ectL
"irtual "oid ,ttach :o#ser"erQ;L "irtual "oid 7etach :o#ser"erQ; L "irtual "oid Noti (:;L
protected0u#$ect :;L
RL UNIT-VII 45
L4
UNIT-VII
4.
L4
UNIT-VII
4/
L4
Noti (:;L
R
UNIT-VII
41
L4
#$%li(,
+igitalClo( 1Clo( Ti"er234 /irt$al 5+igitalClo( 134 /irt$al /oid :#date1S$%9e(t234 /irt$al /oid +ra6134
#ri/ate,
Clo( Ti"er2 8s$%9e(t4
7
UNIT-VII
43
L4
+igitalClo( ,,5+igitalClo( 13 0
8s$%9e(t'=+eta(&1t&is34
7
UNIT-VII
54
L4
/oid +igitalClo( ,,+ra6 13 0 int &o$r ; 8s$%9e(t'=>et?o$r134 int "in$te ; 8s$%9e(t'=>e!in$te134 @@ et(. @@ Code )or dra6ing t&e digital (lo( .
7
UNIT-VII
51
L4
UNIT-VII
52
L4
Unexpected updates- D#ser"ers need not #e concerned a#out !hen then updates are to occur& The( are not concerned a#out each other>s presence& In some cases this ma( lead to un!anted updates&
UNIT-VII 53
L4
L5
0T,T? Pattern
2( <agha"endar Vapala
UNIT-VII
55
L5
Feneral 7escription
, t(pe o 2eha"ioral pattern& ,llo!s an o#$ect to alter its #eha"ior !hen its internal state changes& The o#$ect !ill appear to change its class& Uses Pol(morphism to de ine di erent #eha"iors or di erent states o an o#$ect&
UNIT-VII 5.
L5
UNIT-VII
L5
?%ample I
!ater state "aria#le
increaseTemp:; decreaseTemp:;
0tateD 5ater
increaseTemp:; decreaseTemp:;
5aterVapor 'lient
increaseTemp:; increaseTemp:; decreaseTemp:;
CiEuid5ater
increaseTemp:; decreaseTemp:;
Ice
increaseTemp:; decreaseTemp:;
UNIT-VII
51
L5
L5
?%ample II
M(Mood state "aria#le
do0omething:;
Mood0tate
mad 'lient
do0omething:;
angr(
happ(
do0omething:;
do0omething:;
do0omething:;
UNIT-VII
.4
L5
L5
*ood or thoughtW
To &a/e a "onolit&i( single (lass or "an* s$%(lasses C Increases the num#er o classes and is less compact& ,"oids large conditional statements& -&ere to de)ine t&e state transitions C I criteria is i%ed8 transition can #e de ined in the conte%t& More le%i#le i transition is speci ied in the 0tate su#class& Introduces dependencies #et!een su#classes& -&et&er to (reate State o%9e(ts as and 6&en reD$ired or to (reate't&e"'on(e'and'$se'"an*'ti"es C
*irst is desira#le i the conte%t changes state in reEuentl(& Cater is desira#le i the conte%t changes state reEuentl(&
UNIT-VII
.2
L6
Pattern- 0trateg(
objects that hold alternate algorithms to solve a problem
UNIT-VII
.3
L6
0trateg( pattern
pulling an algorithm out rom the o#$ect that contains it8 and encapsulating the algorithm :the Xstrateg(X; as an o#$ect each strateg( implements one #eha"ior8 one implementation o ho! to sol"e the same pro#lem
6 ho! is this di erent rom Co""and patternU
separates algorithm or #eha"ior rom o#$ect that !ants to act allo!s changing an o#$ectYs #eha"ior d(namicall( !ithout e%tending = changing the o#$ect itsel eBa"#les6 ile sa"ing=compression 6 la(out managers on FUI containers 6 ,I algorithms or computer game pla(ers UNIT-VII
.4
L6
S!artStrategy());
// uses strategy
.5
0trateg(?ncapsulating ,lgorithms
%ame& 2trategy design pattern Problem description& Decouple a policy1deciding class from a set of mechanisms! so that different mechanisms can be changed transparently. ./ample& A mobile computer can be used with a wireless networ-! or connected to an thernet! with dynamic switching between networ-s based on location and networ- costs.
UNIT-VII ..
L6
0trateg(?ncapsulating ,lgorithms
Solution& A Client accesses services provided by a Conte+t. 3he Conte+t services are reali"ed using one of several mechanisms! as decided by a (olicy object. 3he abstract class 2trategy describes the interface that is common to all mechanisms that Conte+t can use. (olicy class creates a Concrete2trategy object and configures Conte+t to use it.
L6
UNIT-VII
./
L6
UNIT-VII
0trateg('lass 7iagram
Client Conte+t conte+t#nterface)* (olicy
L6
Strategy algorithm#nterface)*
Concrete2trategy5
Concrete2trategy%
UNIT-VII
.3
0trateg('onseEuences
'onse(uences& Concrete2trategies can be substituted transparently from Conte+t. (olicy decides which 2trategy is best! given the current circumstances. ?ew policy algorithms can be added without modifying Conte+t or Client.
L6
UNIT-VII
/4
L6
0trateg(
Zou !ant to
6 use di erent algorithms depending upon the conte%t 6 a"oid ha"ing to change the conte%t or client
Strategy
6 decouples inter ace rom implementation 6 shields client rom implementations 6 'onte%t is not a!are !hich strateg( is #eing usedL 'lient con igures the 'onte%t 6 strategies can #e su#stituted at runtime 6 e%ample- inter ace to !ired and !ireless net!or)s
UNIT-VII /1
L6
0trateg(
Ma)e algorithms interchangea#le---Kchanging the gutsK ,lternati"e to su#classing 'hoice o implementation at run-time Increases run-time comple%it(
'onte%t
"ontext!nterface()
Strategy
Operation()
'oncrete0trateg(1 'oncrete0trateg(2
Dperation:; Dperation:;
UNIT-VII
/2
L7
Method
L7
L7
Introduction
3he D'AnimationApplet illustrates the use of an abstract class that serves as a template for classes with shared functionality. An abstract class contains behavior that is common to all its subclasses. 3his behavior is encapsulated in nonabstract methods! which may even be declared final to prevent any modification. 3his action ensures that all subclasses will inherit the same common behavior and its implementation. 3he abstract methods in such templates ensure the interface of the subclasses and require that conte+t specific behavior be implemented for each concrete subclass.
UNIT-VII /5
L7
UNIT-VII
/.
L7
UNIT-VII
//
L7
UNIT-VII
/1
L7
UNIT-VII
/3
L7
L7
Structure of the Template Method 2esign Pattern 'on-t" 1eneric'lass )e.g.! D'AnimationApplet*! which defines abstract hoo- methods )e.g.! paintArame)** that concrete subclasses )e.g.! 'ouncing1'all%* override to implement steps of an algorithm and implements a template method )e.g.! update)** that defines the s-eleton of an algorithm by calling the hoo- methodsC 'oncrete'lass )e.g.! 'ouncing1'all%* which implements the hoo- methods )e.g.! paintArame)** to carry out subclass specific steps of the algorithm defined in the template method.
UNIT-VII 11
L7
Structure of the Template Method 2esign Pattern 'on-t" #n the 3emplate ,ethod design pattern! hook methods do not have to be abstract. 3he generic class may provide default implementations for the hoo- methods. 3hus the subclasses have the option of overriding the hoomethods or using the default implementation. 3he initAnimator)* method in D'AnimationApplet is a nonabstract hoo- method with a default implementation. 3he init)* method is UNIT-VII another template method.
12
L8
UNIT-VII
13
L8
Pattern @atching
Visitor pattern
"isited&accept:this;L
Visitor
"&"isit:this;L
Visited instance
UNIT-VII
14
L8
Pattern @atching
Visitor Pattern
Class Eisitor 0 #$%li(, Eisitor134 /oid /isit1File234 /oid /isit1+ire(tor*234 /oid /isit 1Lin 234 74 /oid Eisitor,,/isit 1+ire(tor*2 d3 0(err GG Hno #rinto$t )or a dire(tor*I47 /oid Eisitor,,/isit 1Lin 2 l3 0l'=getS$%9e(t13'=a((e#t12t&is347 /oid File,,a((e#t 1EisitorJ /3 0/./isit1t&is347 /oid +ire(tor*,,a((e#t 1EisitorJ /3 0/./isit1t&is347 /oid Eisitor,,/isit 1File2 )3 0)'=strea"O$t1(o$t347
L9
<e erences
Va"a ,PI pages
6 6 6 6 6 http-==$a"a&sun&com=$2se=1&4&2=docs=api=$a"a=util='ollection&html http-==$a"a&sun&com=$2se=1&4&2=docs=api=$a"a=util=Iterator&html http-==$a"a&sun&com=$2se=1&4&2=docs=api=$a"a=a!t='ontainer&html http-==$a"a&sun&com=$2se=1&4&2=docs=api=$a"a=a!t=Ca(outManager&html http-==$a"a&sun&com=$2se=1&4&2=docs=api=$a"a%=s!ing=V0crollPane&html