Está en la página 1de 1072

I l@ve RuBoard

Table of Contents

Design Patterns Explained: A New Perspective on Object-Oriented Design


By Alan Shalloway, James R. Trott

Publ sher

! "esley

Add son

Pub #ate ISB(

! July $%, &$$' ! *'+%,)+ $)&$')

Pa-es

! ./0

1...I would e23e4t that readers w th a bas 4 understand n- of ob5e4t)or ented 3ro-ramm nand des -n would f nd th s boo6 useful, before a33roa4h n- des -n 3atterns 4om3letely. Design Patterns Explained 4om3lements the e2 st n- des -n 3atterns te2ts and may 3erform a very useful role, f tt n- between ntrodu4tory te2ts su4h as 789 # st lled and the more advan4ed 3atterns boo6s.1 -James Noble Design Patterns Explained: A New Perspective on Object-Oriented Design draws to-ether the 3r n4 3les of ob5e4t)or ented 3ro-ramm n- w th the 3ower of des -n 3atterns to 4reate an env ronment for robust and rel able software develo3ment. Pa46ed w th 3ra4t 4al and a33l 4able e2am3les, th s boo6 tea4hes you to solve 4ommon 3ro-ramm n- 3roblems w th 3atterns))and e23la ns the advanta-es of 3atterns for modern software des -n.

Be- nn n- w th a 4om3lete overv ew of the fundamentals of 3atterns, Design Patterns Explained stresses the m3ortan4e of analys s and des -n. The authors 4learly demonstrate how 3atterns 4an fa4 l tate the overall develo3ment 3ro4ess. Throu-hout the boo6, 6ey ob5e4t)or ented des -n 3r n4 3les are e23la ned, alon- w th the 4on4e3ts and benef ts beh nd s3e4 f 4 3atterns. " th llustrat ve e2am3les n C:: and Java, the boo6 demyst f es the 1whys,1 1why nots,1 and 1hows1 of 3atterns and e23la ns 3attern m3lementat on. ;ey to3 4s 4overed n4lude!

(ew 3ers3e4t ves on ob5e4ts, en4a3sulat on, and nher tan4e The dea of des -n 3atterns, the r or - ns, and how they a33ly n the d s4 3l ne of software des -n

Pattern)based, ob5e4t)or ented software develo3ment us n- the 7n f ed 8odel n9an-ua-e <789=

>ow to m3lement 4r t 4al 3atterns))Strate-y, ?bserver, Br d-e, #e4orator, and many more

Commonal ty@Aar ab l ty Analys s and des -n 3atterns, and how they a d n understand n- abstra4t 4lasses

Brom analys s to m3lementat on, #es -n Patterns C23la ned allows you to unleash the true 3otent al of 3atterns and 3aves the 3ath for m3rov n- your overall des -ns. Th s boo6 3rov des new4omers w th a -enu nely a44urate and hel3ful ntrodu4t on to ob5e4t)or ented des -n 3atterns.

I l@ve RuBoard

I l@ve RuBoard

Table of Contents

Design Patterns Explained: A New Perspective on Object-Oriented Design


By Alan Shalloway, James R. Trott

Publ sher

! "esley

Add son

Pub #ate ISB(

! July $%, &$$' ! *'+%,)+ $)&$')

Pa-es

! ./0

Co3yr -ht Prefa4e Brom ?b5e4t ?r entat on to Patterns to True ?b5e4t ?r entat on Brom Art f 4 al Intell -en4e to Patterns to True ?b5e4t ?r entat on A (ote About Convent ons 7sed n Th s Boo6 Beedba46 A46nowled-ments

Part I! An Introdu4t on to ?b5e4t)?r ented Software #evelo3ment Cha3ter '. The ?b5e4t)?r ented Parad -m ?verv ew Before The ?b5e4t)?r ented Parad -m! Bun4t onal #e4om3os t on The Problem of ReDu rements #eal n- w th Chan-es! 7s n- Bun4t onal #e4om3os t on #eal n- w th Chan- n- ReDu rements The ?b5e4t)?r ented Parad -m

?b5e4t)?r ented Pro-ramm n- n A4t on S3e4 al ?b5e4t 8ethods Summary

Cha3ter &. The 789EThe 7n f ed 8odel n- 9an-ua-e ?verv ew "hat Is the 789F "hy 7se the 789F The Class # a-ram Intera4t on # a-rams Summary

Part II! The 9 m tat ons of Trad t onal ?b5e4t)?r ented #es -n Cha3ter .. A Problem That Cr es ?ut for Ble2 ble Code ?verv ew C2tra4t n- Informat on from a CA#@CA8 System 7nderstand the Ao4abulary #es4r be the Problem The Cssent al Challen-es and A33roa4hes Summary

Cha3ter ,. A Standard ?b5e4t)?r ented Solut on ?verv ew Solv n- w th S3e4 al Cases Summary Su33lement! C:: Code C2am3les

Part III! #es -n Patterns Cha3ter +. An Introdu4t on to #es -n Patterns ?verv ew #es -n Patterns Arose from Ar4h te4ture and Anthro3olo-y 8ov n- from Ar4h te4tural to Software #es -n Patterns "hy Study #es -n PatternsF ?ther Advanta-es to Study n- #es -n Patterns Summary

Cha3ter /. The Ba4ade Pattern ?verv ew Introdu4 n- the Ba4ade Pattern 9earn n- the Ba4ade Pattern B eld (otes! The Ba4ade Pattern Relat n- the Ba4ade Pattern to the CA#@CA8 Problem

Summary

Cha3ter *. The Ada3ter Pattern ?verv ew Introdu4 n- the Ada3ter Pattern 9earn n- the Ada3ter Pattern B eld (otes! The Ada3ter Pattern Relat n- the Ada3ter Pattern to the CA#@CA8 Problem Summary Su33lement! C:: Code C2am3le

Cha3ter 0. C23and n- ?ur >or Gons ?verv ew ?b5e4ts! the Trad t onal A ew and the (ew A ew Cn4a3sulat on! the Trad t onal A ew and the (ew A ew B nd "hat Is Aary n- and Cn4a3sulate It Commonal ty@Aar ab l ty and Abstra4t Classes Summary

Cha3ter %. The Br d-e Pattern ?verv ew Introdu4 n- the Br d-e Pattern 9earn n- the Br d-e Pattern! An C2am3le An ?bservat on About 7s n- #es -n Patterns 9earn n- the Br d-e Pattern! #er v n- It The Br d-e Pattern n Retros3e4t B eld (otes! 7s n- the Br d-e Pattern Summary Su33lement! C:: Code C2am3les

Cha3ter '$. The Abstra4t Ba4tory Pattern ?verv ew Introdu4 n- the Abstra4t Ba4tory Pattern 9earn n- the Abstra4t Ba4tory Pattern! An C2am3le 9earn n- the Abstra4t Ba4tory Pattern! Im3lement n- It B eld (otes! The Abstra4t Ba4tory Pattern Relat n- the Abstra4t Ba4tory Pattern to the CA#@CA8 Problem Summary Su33lement! C:: Code C2am3les

Part IA! Putt n- It All To-ether! Th n6 n- n Patterns Cha3ter ''. H>ow #o C23erts #es -nF ?verv ew

Bu ld n- by Add n- # st n4t ons Summary

Cha3ter '&. Solv n- the CA#@CA8 Problem w th Patterns ?verv ew Rev ew of the CA#@CA8 Problem Th n6 n- n Patterns Th n6 n- n Patterns! Ste3 ' Th n6 n- n Patterns! Ste3 &a Th n6 n- n Patterns! Ste3 &b Th n6 n- n Patterns! Ste3 &4 Th n6 n- n Patterns! Ste3 &d <Ba4ade= Th n6 n- n Patterns! Ste3 &d <Ada3ter= Th n6 n- n Patterns! Ste3 &d <Abstra4t Ba4tory= Th n6 n- n Patterns! Ste3 . Com3ar son w th the Prev ous Solut on Summary

Cha3ter '.. The Pr n4 3les and Strate- es of #es -n Patterns ?verv ew The ?3en)Closed Pr n4 3le The Pr n4 3le of #es -n n- from Conte2t The Pr n4 3le of Cn4a3sulat n- Aar at on Summary

Part A! >andl n- Aar at ons w th #es -n Patterns Cha3ter ',. The Strate-y Pattern ?verv ew An A33roa4h to >andl n- (ew ReDu rements In t al ReDu rements of the Case Study >andl n- (ew ReDu rements The Strate-y Pattern B eld (otes! 7s n- the Strate-y Pattern Summary

Cha3ter '+. The #e4orator Pattern ?verv ew A 9 ttle 8ore #eta l The #e4orator Pattern A33ly n- the #e4orator Pattern to the Case Study Another C2am3le! In3ut@?ut3ut B eld (otes! 7s n- the #e4orator Pattern Summary

Su33lement! C:: Code C2am3les

Cha3ter '/. The S n-leton Pattern and the #ouble)Che46ed 9o46 n- Pattern ?verv ew Introdu4 n- the S n-leton Pattern A33ly n- the S n-leton Pattern to the Case Study A Aar ant! The #ouble)Che46ed 9o46 n- Pattern B eld (otes! 7s n- the S n-leton and #ouble)Che46ed 9o46 n- Patterns Summary Su33lement! C:: Code C2am3les

Cha3ter '*. The ?bserver Pattern ?verv ew Cate-or es of Patterns 8ore ReDu rements for the Case Study The ?bserver Pattern A33ly n- the ?bserver to the Case Study B eld (otes! 7s n- the ?bserver Pattern Summary Su33lement! C:: Code C2am3le

Cha3ter '0. The Tem3late 8ethod Pattern ?verv ew 8ore ReDu rements for the Case Study The Tem3late 8ethod Pattern A33ly n- the Tem3late 8ethod to the Case Study B eld (otes! 7s n- the Tem3late 8ethod Pattern Summary

Cha3ter '%. The Ba4tory 8ethod Pattern ?verv ew 8ore ReDu rements for the Case Study The Ba4tory 8ethod Pattern B eld (otes! 7s n- the Ba4tory 8ethod Pattern Summary

Cha3ter &$. The Analys s 8atr 2 ?verv ew In the Real "orld! Aar at ons Case Study n Aar at on! An Internat onal C)Ta l System B eld (otes Summary

Part AI! Cnd n-s and Be- nn n-s Cha3ter &'. #es -n Patterns Rev ewed from the (ew Pers3e4t ve of ?b5e4t)?r ented #es -n ?verv ew A Summary of ?b5e4t)?r ented Pr n4 3les >ow #es -n Patterns Cn4a3sulate Im3lementat ons Commonal ty@Aar ab l ty Analys s and #es -n Patterns #e4om3os n- a Problem #oma n nto Res3ons b l t es Relat onsh 3s " th n a Pattern Patterns and Conte2tual #es -n B eld (otes Summary

Cha3ter &&. B bl o-ra3hy Design Patterns Explained: The "eb S te Com3an on Re4ommended Read n- on #es -n Patterns and ?b5e4t ?r entat on Re4ommended Read n- for Java Pro-rammers Re4ommended Read n- for C:: Pro-rammers Re4ommended Read n- for C?B?9 Pro-rammers Re4ommended Read n- on eItreme Pro-ramm nRe4ommended Read n- on Jeneral Pro-ramm nPersonal Bavor tes

I l@ve RuBoard

I l@ve RuBoard

Copyright
8any of the des -nat ons used by manufa4turers and sellers to d st n-u sh the r 3rodu4ts are 4la med as trademar6s. "here those des -nat ons a33ear n th s boo6, and Add son "esley 9on-man In4., was aware of a trademar6 4la m, the des -nat ons have been 3r nted w th n t al 4a3 tal letters or n all 4a3 tals. The authors and 3ubl sher have ta6en 4are n the 3re3arat on of th s boo6, but ma6e no e23ressed or m3l ed warranty of any 6 nd and assume no res3ons b l ty for errors or om ss ons. (o l ab l ty s assumed for n4 dental or 4onseDuent al dama-es n 4onne4t on w th or ar s n- out of the use of the nformat on or 3ro-rams 4onta ned here n. The 3ubl sher offers d s4ounts on th s boo6 when ordered n Duant ty for s3e4 al sales. Bor

more nformat on, 3lease 4onta4t! Pearson Cdu4at on Cor3orate Sales # v s on &$' ". '$.rd Street Ind ana3ol s, I( ,/&%$ <0$$= ,&0)+..' 4o3rsales@3earsoned.4om A s t A"9 on the "eb! www.awl.4om@4sen-@ Library of Congress Cataloging-in-P blication Data Shalloway, Alan. #es -n 3atterns e23la ned ! a new 3ers3e4t ve on ob5e4t)or ented des -n @ Alan Shalloway, James Trott. 3. 4m. In4ludes b bl o-ra3h 4al referen4es and nde2. ISB( $)&$')*'+%,)+ '. ?b5e4t)or ented methods <Com3uter s4 en4e= &. Com3uter softwareE#evelo3ment. I. Trott, James II. T tle. KA*/.%.?.+ S+& &$$' $$+.IL'*))d4&' &$$'$.'/,+ Co3yr -ht M &$$& by Add son)"esley All r -hts reserved. (o 3art of th s 3ubl 4at on may be re3rodu4ed, stored n a retr eval system, or transm tted, n any form, or by any means, ele4tron 4, me4han 4al,

3hoto4o3y n-, re4ord n-, or otherw se, w thout the 3r or 4onsent of the 3ubl sher. Pr nted n the 7n ted States of Amer 4a. Publ shed s multaneously n Canada. Te2t 3r nted on re4y4led 3a3er & . , + / * 0 % '$E8AE$+$,$.$& Se4ond 3r nt n-, January &$$&

Dedication
!o Leig"# $ryan# Lisa# %ic"ael# and &teven for t"eir love# s pport# enco rage'ent# and sacrifice( EAlan Shalloway

Dedication
!o )ill# Eri*a# Lorien# %i*aela# and +eneva# t"e roses in t"e garden of 'y life( sola gloria Dei EJames R. Trott

I l@ve RuBoard

I l@ve RuBoard

Preface
#es -n 3atterns and ob5e4t)or ented 3ro-ramm n-. They hold su4h 3rom se to ma6e your l fe as a software des -ner and develo3er eas er. The r term nolo-y s band ed about every day n the te4hn 4al and even the 3o3ular 3ress. But t 4an be hard to learn them, to be4ome 3rof 4 ent w th them, to understand what s really -o n- on. Perha3s you have been us n- an ob5e4t)or ented or ob5e4t)based lan-ua-e for years. >ave you learned that the true 3ower of ob5e4ts s not nher tan4e but s n 1en4a3sulat nbehav ors1F Perha3s you are 4ur ous about des -n 3atterns and have found the l terature a

b t too esoter 4 and h -h)falut n. If so, th s boo6 s for you. It s based on years of tea4h n- th s mater al to software develo3ers, both e23er en4ed and new to ob5e4t or entat on. It s based u3on the bel efEand our e23er en4eEthat on4e you understand the bas 4 3r n4 3les and mot vat ons that underl e these 4on4e3ts, why they are do n- what they do, your learn n- 4urve w ll be n4red bly shorter. And n our d s4uss on of des -n 3atterns, you w ll understand the true m ndset of ob5e4t or entat on, wh 4h s a ne4ess ty before you 4an be4ome 3rof 4 ent. As you read th s boo6, you w ll -a n a sol d understand n- of the ten most essent al des -n 3atterns. Nou w ll learn that des -n 3atterns do not e2 st on the r own, but are su33osed to wor6 n 4on4ert w th other des -n 3atterns to hel3 you 4reate more robust a33l 4at ons. Nou w ll -a n enou-h of a foundat on that you w ll be able to read the des -n 3attern l terature, f you want to, and 3oss bly d s4over 3atterns on your own. 8ost m3ortantly, you w ll be better eDu 33ed to 4reate fle2 ble and 4om3lete software that s eas er to ma nta n.
I l@ve RuBoard

I l@ve RuBoard

ro! Object Orientation to Patterns to "r#e Object Orientation


In many ways, th s boo6 s a retell n- of my 3ersonal e23er en4e learn n- des -n 3atterns. Pr or to study n- des -n 3atterns, I 4ons dered myself to be reasonably e23ert n ob5e4t) or ented analys s and des -n. 8y tra46 re4ord had n4luded several fa rly m3ress ve des -ns and m3lementat ons n many ndustr es. I 6new C:: and was be- nn n- to learn Java. The ob5e4ts n my 4ode were well)formed and t -htly en4a3sulated. I 4ould des -n e24ellent data abstra4t ons for nher tan4e h erar4h es. I thou-ht I 6new ob5e4t)or entat on. (ow, loo6 n- ba46, I see that I really d d not understand the full 4a3ab l t es of ob5e4t) or ented des -n, even thou-h I was do n- th n-s the way the e23erts adv sed. It wasnLt unt l I be-an to learn des -n 3atterns that my ob5e4t)or ented des -n ab l t es e23anded and dee3ened. ;now n- des -n 3atterns has made me a better des -ner, even when I donLt use these 3atterns d re4tly. I be-an study n- des -n 3atterns n '%%/. I was a C::@ob5e4tor ented des -n mentor at a

lar-e aeros3a4e 4om3any n the northwest. Several 3eo3le as6ed me to lead a des -n 3attern study -rou3. ThatLs where I met my 4o)author, J m Trott. In the study -rou3, several nterest n- th n-s ha33ened. B rst, I -rew fas4 nated w th des -n 3atterns. I loved be n- able to 4om3are my des -ns w th the des -ns of others who had more e23er en4e than I had. I d s4overed that I was not ta6 n- full advanta-e of des -n n- to nterfa4es and that I d dnLt always 4on4ern myself w th see n- f I 4ould have an ob5e4t use another ob5e4t w thout 6now n- the used ob5e4tLs ty3e. I not 4ed that be- nners to ob5e4t)or ented des -n Ethose who would normally be deemed as learn n- des -n 3atterns too earlyEwere benef t n- as mu4h from the study -rou3 as the e23erts were. The 3atterns 3resented e2am3les of e24ellent ob5e4t)or ented des -ns and llustrated bas 4 ob5e4t)or ented 3r n4 3les, wh 4h hel3ed to mature the r des -ns more Du 46ly. By the end of the study sess ons, I was 4onv n4ed that des -n 3atterns were the -reatest th n- to ha33en to software des -n s n4e the nvent on of ob5e4t)or ented des -n. >owever, when I loo6ed at my wor6 at the t me, I saw that I was not n4or3orat n- any des -n 3atterns nto my 4ode. I 5ust f -ured I d dnLt 6now enou-h des -n 3atterns yet and needed to learn more. At the t me, I only 6new about s 2 of them. Then I had what 4ould be 4alled an e3 3hany. I was wor6 n- on a 3ro5e4t as a mentor n ob5e4t)or ented des -n and was as6ed to 4reate a h -h)level des -n for the 3ro5e4t. The leader of the 3ro5e4t was e2tremely shar3, but was fa rly new to ob5e4t)or ented des -n. The 3roblem tself wasnLt that d ff 4ult, but t reDu red a -reat deal of attent on to ma6e sure the 4ode was -o n- to be easy to ma nta n. 9 terally, after about two m nutes of loo6 n- at the 3roblem, I had develo3ed a des -n based on my normal a33roa4h of data abstra4t on. 7nfortunately, t was very 4lear th s was not -o n- to be a -ood des -n. #ata abstra4t on alone had fa led me. I had to f nd someth n- better. Two hours later, after a33ly n- every des -n te4hn Due I 6new, I was no better off. 8y des -n was essent ally the same. "hat was most frustrat n- was that I 6new there was a better des -n. I 5ust 4ouldnLt see t. Iron 4ally, I also 6new of four des -n 3atterns that 1l ved1 n my 3roblem but I 4ouldnLt see how to use them. >ere I wasEa su33osed e23ert n ob5e4t)or ented des -nEbaffled by a s m3le 3roblemO

Beel n- very frustrated, I too6 a brea6 and started wal6 n- down the hall to 4lear my head, tell n- myself I would not th n6 of the 3roblem for at least '$ m nutes. "ell, .$ se4onds later, I was th n6 n- about t a-a nO But I had -otten an ns -ht that 4han-ed my v ew of des -n 3atterns! rather than us n- 3atterns as nd v dual tems, I should use the des -n 3atterns to-ether. Patterns are s pposed to be sewn toget"er to solve a proble'( I had heard th s before, but hadnLt really understood t. Be4ause 3atterns n software have been ntrodu4ed as design 3atterns, I had always labored under the assum3t on that they had mostly to do w th des -n. 8y thou-hts were that n the des -n world, the 3atterns 4ame as 3retty mu4h well)formed relat onsh 3s between 4lasses. Then, I read Chr sto3her Ale2anderLs amaG n- boo6, !"e !i'eless ,ay of $ ilding. I learned that 3atterns e2 sted at all levelsEanalys s, des -n, and m3lementat on. Ale2ander d s4usses us n- 3atterns to hel3 n the understand n- of the 3roblem doma n <even n des4r b n- t=, not 5ust us nthem to 4reate the des -n after the 3roblem doma n s understood. 8y m sta6e had been n try n- to 4reate the 4lasses n my 3roblem doma n and then st t4h them to-ether to ma6e a f nal system, a 3ro4ess wh 4h Ale2ander 4alls a 3art 4ularly bad dea. I had never as6ed f I had the r -ht 4lasses be4ause they 5ust seemed so r -ht, so obv ousP they were the 4lasses that mmed ately 4ame to m nd as I started my analys s, the 1nouns1 n the des4r 3t on of the system that we had been tau-ht to loo6 for. But I had stru--led try n- to 3 e4e them to-ether. "hen I ste33ed ba46 and used des -n 3atterns and Ale2anderLs a33roa4h to -u de me n the 4reat on of my 4lasses, a far su3er or solut on unfolded n only a matter of m nutes. It was a -ood des -n and we 3ut t nto 3rodu4t on. I was e24 tedEe24 ted to have des -ned a -ood solut on and e24 ted about the 3ower of des -n 3atterns. It was then that I started n4or3orat n- des -n 3atterns nto my develo3ment wor6 and my tea4h n-. I be-an to d s4over that 3ro-rammers who were new to ob5e4t)or ented des -n 4ould learn des -n 3atterns, and n do n- so, develo3 a bas 4 set of ob5e4t)or ented des -n s6 lls. It was true for me and t was true for the students that I was tea4h n-. Ima- ne my sur3r seO The des -n 3attern boo6s I had been read n- and the des -n 3attern

e23erts I had been tal6 n- to were say n- that you really needed to have a -ood -round nn ob5e4t)or ented des -n before embar6 n- on a study of des -n 3atterns. (evertheless, I saw, w th my own eyes, that students who learned ob5e4t)or ented des -n 4on4urrently w th des -n 3atterns learned ob5e4t)or ented des -n faster than those 5ust study n- ob5e4t) or ented des -n. They even seemed to learn des -n 3atterns at almost the same rate as e23er en4ed ob5e4t)or ented 3ra4t t oners. I be-an to use des -n 3atterns as a bas s for my tea4h n-. I be-an to 4all my 4lasses Pattern Oriented Design: Design Patterns fro' Analysis to -'ple'entation( I wanted my students to understand these 3atterns and be-an to d s4over that us n- an e23loratory a33roa4h was the best way to foster th s understand n-. Bor nstan4e, I found that t was better to 3resent the Br d-e 3attern by 3resent n- a 3roblem and then have my students try to des -n a solut on to the 3roblem us n- a few -u d n- 3r n4 3les and strate- es that I had found were 3resent n most of the 3atterns. In the r e23lorat on, the students d s4overed the solut onE4alled the Br d-e 3atternEand remembered t. In any event, I found that these -u d n- 3r n4 3les and strate- es 4ould be used to 1der ve1 several of the des -n 3atterns. By 1der ve a des -n 3attern,1 I mean that f I loo6ed at a 3roblem that I 6new 4ould be solved by a des -n 3attern, I 4ould use the -u d n- 3r n4 3les and strate- es to 4ome u3 w th the solut on that s e23ressed n the 3attern. I made t 4lear to my students that we werenLt really 4om n- u3 w th des -n 3atterns th s way. Instead, I was 5ust llustrat n- one 3oss ble thou-ht 3ro4ess that the 3eo3le who 4ame u3 w th the or - nal solut ons, those that were eventually 4lass f ed as des -n 3atterns, m -ht have used.

A slight digression$
The -u d n- 3r n4 3les and strate- es seem very 4lear to me now. Certa nly, they are stated n the 1Jan- of BourLs1 des -n 3atterns boo6. But t too6 me a lont me to understand them be4ause of l m tat ons n my own understand n- of the ob5e4t)or ented 3arad -m. It was only after nte-rat n- n my own m nd the wor6 of the Jan- of Bour w th Ale2anderLs wor6, J m Co3l enLs wor6 on 4ommonal ty and var ab l ty analys s, and 8art n BowlerLs wor6 n methodolo- es and analys s 3atterns that these 3r n4 3les be4ame 4lear enou-h to me to that I was able to

tal6 about them to others. It hel3ed that I was ma6 n- my l vel hood e23la n nth n-s to others so I 4ouldnLt -et away w th ma6 n- assum3t ons as eas ly as I 4ould when I was 5ust do n- th n-s for myself.

8y ab l t es to e23la n these few, but 3owerful, 3r n4 3les and strate- es m3roved. As they d d, I found that t be4ame more useful to e23la n an n4reas n- number of the Jan- of Bour 3atterns. In fa4t, I use these 3r n4 3les and strate- es to e23la n '& of the ', 3atterns I d s4uss n my des -n 3atterns 4ourse. I found that I was us n- these 3r n4 3les n my own des -ns both w th and w thout 3atterns. Th s d dnLt sur3r se me. If us n- these strate- es resulted n a des -n eDu valent to a des -n 3attern when I 6new the 3attern was 3resent, that meant they were - v n- me a way to der ve e24ellent des -ns <s n4e 3atterns are e24ellent des -ns by def n t on=. "hy would I -et any 3oorer des -ns from these te4hn Dues 5ust be4ause I d dnLt 6now the name of the 3attern that m -ht or m -ht not be 3resent anywayF These ns -hts hel3ed hone my tra n n- 3ro4ess <and now my wr t n- 3ro4ess=. I had already been tea4h n- my 4ourses on several levels. I was tea4h n- the fundamentals of ob5e4t)or ented analys s and des -n. I d d that by tea4h n- des -n 3atterns and us n- them to llustrate -ood e2am3les of ob5e4t)or ented analys s and des -n. In add t on, by us nthe 3atterns to tea4h the 4on4e3ts of ob5e4t or entat on, my students were also better able to understand the 3r n4 3les of ob5e4t or entat on. And by tea4h n- the -u d n- 3r n4 3les and strate- es, my students were able to 4reate des -ns of 4om3arable Dual ty to the 3atterns themselves. I relate th s story be4ause th s boo6 follows mu4h the same 3attern as my 4ourse <3un ntended=. In fa4t, from Cha3ter . on, th s boo6 s very mu4h the f rst day of my two)day 4ourse! Pattern Oriented Design: Design Patterns fro' Analysis to -'ple'entation( As you read th s boo6, you w ll learn the 3atterns. But even more m3ortantly, you w ll learn why they wor6 and how they 4an wor6 to-ether, and the 3r n4 3les and strate- es u3on wh 4h they rely. It w ll be useful to draw on your own e23er en4es. "hen I 3resent a 3roblem n the te2t, t s hel3ful f you ma- ne a s m lar 3roblem that you have 4ome a4ross. Th s boo6 snLt about new b ts of nformat on or new 3atterns to a33ly, but rather a new way of loo6 n- at ob5e4t)or ented software develo3ment. I ho3e that your own

e23er en4es, 4onne4ted w th the 3r n4 3les of des -n 3atterns, w ll 3rove to be a 3owerful ally n your learn n-. Alan &"alloway Dece'ber# .///

I l@ve RuBoard

I l@ve RuBoard

ro! Artificial %ntelligence to Patterns to "r#e Object Orientation


8y 5ourney nto des -n 3atterns had a d fferent start n- 3o nt than AlanLs but we have rea4hed the same 4on4lus ons!

Pattern)based analyses ma6e you a more effe4t ve and eff 4 ent analyst be4ause they let you deal w th your models more abstra4tly and be4ause they re3resent the 4olle4ted e23er en4es of many other analysts.

Patterns hel3 3eo3le to learn 3r n4 3les of ob5e4t or entat on. The 3atterns hel3 to e23la n why we do what we do w th ob5e4ts.

I started my 4areer n art f 4 al ntell -en4e <AI= 4reat n- rule)based e23ert systems. Th s nvolves l sten n- to e23erts and 4reat n- models of the r de4 s on)ma6 n- 3ro4esses and then 4od n- these models nto rules n a 6nowled-e)based system. As I bu lt these systems, I be-an to see re3eat n- themes! n 4ommon ty3es of 3roblems, e23erts tended to wor6 n s m lar ways. Bor e2am3le, e23erts who d a-nose 3roblems w th eDu 3ment tend to loo6 for s m3le, Du 46 f 2es f rst, then they -et more systemat 4, brea6 n- the 3roblem nto 4om3onent 3artsP but n the r systemat 4 d a-nos s, they tend to try f rst ne23ens ve tests or tests that w ll el m nate broad 4lasses of 3roblems before other 6 nds of tests. Th s was true whether we were d a-nos n- 3roblems n a 4om3uter or a 3 e4e of o l f eld eDu 3ment. Today, I would 4all these re4urr n- themes 3atterns. Intu t vely, I be-an to loo6 for these re4urr n- themes as I was des -n n- new e23ert systems. 8y m nd was o3en and fr endly to the dea of 3atterns, even thou-h I d d not 6now what they were.

Then, n '%%,, I d s4overed that resear4hers n Curo3e had 4od f ed these 3atterns of e23ert behav or and 3ut them nto a 3a46a-e that they 4alled ;nowled-e Analys s and #es -n Su33ort, or ;A#S. #r. ;aren Jardner, a most - fted analyst, modeler, mentor, and human be n-, be-an to a33ly ;A#S to her wor6 n the 7n ted States. She e2tended the Curo3eanLs wor6 to a33ly ;A#S to ob5e4t)or ented systems. She o3ened my eyes to an ent re world of 3attern)based analys s and des -n that was form n- n the software world, n lar-e 3art due to Chr sto3her Ale2anderLs wor6. >er boo6, Cognitive Patterns <Cambr d-e 7n vers ty Press, '%%0= des4r bes th s wor6. Suddenly, I had a stru4ture for model n- e23ert behav ors w thout -ett n- tra33ed by the 4om3le2 t es and e24e3t ons too early. I was able to 4om3lete my ne2t three 3ro5e4ts n less t me, w th less rewor6, and w th -reater sat sfa4t on by end)users, be4ause!

I 4ould des -n models more Du 46ly be4ause the 3atterns 3red 4ted for me what ou-ht to be there. They told me what the essent al ob5e4ts were and what to 3ay s3e4 al attent on to.

I was able to 4ommun 4ate mu4h more effe4t vely w th e23erts be4ause we had a more stru4tured way to deal w th the deta ls and e24e3t ons.

The 3atterns allowed me to develo3 better end)user tra n n- for my system be4ause the 3atterns 3red 4ted the most m3ortant features of the system.

Th s last 3o nt s s -n f 4ant. Patterns hel3 end)users understand systems be4ause they 3rov de the 4onte2t for the system, why we are do n- th n-s n a 4erta n way. "e 4an use 3atterns to des4r be the -u d n- 3r n4 3les and strate- es of the system. And we 4an use 3atterns to develo3 the best e2am3les to hel3 end)users understand the system. I was hoo6ed. So, when a des -n 3atterns study -rou3 started at my 3la4e of em3loyment, I was ea-er to -o. Th s s where I met Alan who had rea4hed a s m lar 3o nt n h s wor6 as an ob5e4t) or ented des -ner and mentor. The result s th s boo6. I ho3e that the 3r n4 3les n th s boo6 hel3 you n your own 5ourney to be4ome a more effe4t ve and eff 4 ent analyst.

)a'es 0( !rott Dece'ber# .///


I l@ve RuBoard

I l@ve RuBoard

A Note Abo#t Conventions &sed in "his 'oo(


In the wr t n- of th s boo6, we had to ma6e several 4ho 4es about style and 4onvent on. Some of our 4ho 4es have sur3r sed our readers. So, t s worth a few 4omments about why we have 4hosen to do what we have done.

Approach B rst vo 4e

Rationale

3erson Th s boo6 s a 4ollaborat ve effort between two authors. "e debated and ref ned our deas to f nd the best ways to e23la n these 4on4e3ts. Alan tr ed them out n h s 4ourses and we ref ned some more. "e 4hose to use the f rst 3erson s n-ular n the body of th s boo6 be4ause t allows us to tell the story n what we ho3e s a more en-a- n- and natural style.

S4ann n- te2t "e have tr ed to ma6e th s boo6 easy to s4an so that you 4an -et the ma n 3o nts even f you do not read the body, or so that you 4an Du 46ly f nd the nformat on you need. "e ma6e s -n f 4ant use of tables and bulleted l sts. "e 3rov de te2t n the outs de mar- n that summar Ges 3ara-ra3hs. " th the d s4uss on of ea4h 3attern, we 3rov de a summary table of the 6ey features of the 3attern. ?ur ho3e s that these w ll ma6e the boo6 that mu4h more a44ess ble. Code fra-ments Th s boo6 s about analys s and des -n more than m3lementat on. ?ur ntent s to hel3 you th n6 about 4raft n-ood des -ns based on the ns -hts and best 3ra4t 4es of the ob5e4t)or ented 4ommun ty, as e23ressed n des -n 3atterns. ?ne of the 4hallen-es for all of us 3ro-rammers s to avo d -o n- to the m3lementat on too early, do n- before th n6 n-. ;now n- th s, we have 3ur3osefully tr ed to stay away from too mu4h d s4uss on on m3lementat on. ?ur 4ode e2am3les may seem a b t l -htwe -ht and fra-mentary. S3e4 f 4ally, we never 3rov de error 4he46 n- n the 4ode. Th s s be4ause we are try n- to use the 4ode to llustrate 4on4e3ts.

Strate- es ?urs s an ntrodu4tory boo6. It w ll hel3 you be able to -et u3 and 3r n4 3les to s3eed Du 46ly w th des -n 3atterns. Nou w ll understand the

3r n4 3les and strate- es that mot vate des -n 3atterns. After read n- th s boo6, you 4an -o on to a more s4holarly or referen4e boo6. The last 4ha3ter w ll 3o nt you to many of the referen4es that we have found useful. Show breadth "e are try n- - ve you a taste for des -n 3atterns, to e23ose you to the and - ve a breadth of the 3attern world but not -o nto de3th n any of them <see taste
the 3rev ous 3o nt=.

?ur thou-ht was th s! If you brou-ht someone to the 7SA for a two wee6 v s t, what would you show themF 8aybe a few s tes to hel3 them -et fam l ar w th ar4h te4tures, 4ommun t es, the feel of 4 t es and the vast s3a4es that se3arate them, freeways, and 4offee sho3s. But you would not be able to show them everyth n-. To f ll n the r 6nowled-e, you m -ht 4hoose to show them sl de shows of many other s tes and 4 t es to - ve them a taste of the 4ountry. Then, they 4ould ma6e 3lans for future v s ts. "e are show n- you the ma5or s tes n des -n 3atterns and then - v nyou tastes of other areas so that you 4an 3lan your own 5ourney nto 3atterns.

I l@ve RuBoard

I l@ve RuBoard

eedbac(
#es -n 3atterns are a wor6 n 3ro-ress, a 4onversat on amon-st 3ra4t t oners who d s4over best 3ra4t 4es, who d s4over fundamental 3r n4 3les n ob5e4t or entat on. "e 4ovet your feedba46 on th s boo6!

"hat d d we do well or 3oorlyF Are there errors that need to be 4orre4tedF

"as there someth n- that was 4onfus n-ly wr ttenF

Please v s t us at the "eb s te for th s boo6. The 7R9 s htt3!@@www.netob5e4t ves.4om@d3e23la ned. At th s s te, you w ll f nd a form that you 4an

use to send us your 4omments and Duest ons. Nou w ll also f nd our latest resear4h.

I l@ve RuBoard

I l@ve RuBoard

Ac(nowledg!ents
Almost every 3refa4e ends w th a l st of a46nowled-ments of those who hel3ed n the develo3ment of the boo6. "e never fully a33re4 ated how true th s was unt l do n- a boo6 of our own. Su4h an effort s truly a wor6 of a 4ommun ty. The l st of 3eo3le to whom we are n debt s lon-. The follow n- 3eo3le are es3e4 ally s -n f 4ant to us!

#ebb e 9afferty from Add son)"esley, who never -rew t red of en4oura- n- us and 6ee3 n- us on tra46.

S4ott Ba n, our 4ollea-ue who 3at ently rev ewed th s wor6 and -ave us ns -hts.

And es3e4 ally 9e -h and J ll, our 3at ent w ves, who 3ut u3 w th us and en4oura-ed us n our dream of th s boo6.

S3e4 al than6s from Alan!

Several of my students early on had an m3a4t they 3robably never 6new. 8any t mes dur n- my 4ourses I hes tated to 3ro5e4t new deas, feel n- I should st 46 w th the tr ed and true. >owever, the r enthus asm n my new 4on4e3ts when I f rst started my 4ourses en4oura-ed me to 3ro5e4t more and more of my own deas nto the 4urr 4ulum I was 3utt n- to-ether. Than6s to 9an4e Noun-, Peter Sh rley, John Terrell, and ;aren Allen. They serve as a 4onstant rem nder to me how en4oura-ement 4an -o a lon- way.

Than6s to John Al ss des for h s thou-htful 4omments and tou-h Duest ons.

S3e4 al than6s from J m!

#r. ;aren Jardner, a mentor and w se tea4her n 3atterns of human thou-ht. #r. 8arel (orwood and Arthur 8ur3hy, my n t al 4ollaborators n ;A#S and

3attern)based analys s.

Brad AanBee6 who -ave me the s3a4e to -row n th s d s4 3l ne.

Ale2 S dey who 4oa4hed me n the d s4 3l ne and myster es of te4hn 4al wr t n-.

I l@ve RuBoard

I l@ve RuBoard

Part %: An %ntrod#ction to Object-Oriented )oftware Develop!ent


Part Overview Th s 3art ntrodu4es you to a method for develo3 n- ob5e4t)or ented software that s based on patternsEthe ns -hts and best 3ra4t 4es learned by des -ners and users over the years Eand on the model n- lan-ua-e <789= that su33orts t. Th s w ll not follow the ob5e4t)or ented 3arad -m of the '%0$s, where develo3ers were s m3ly told to f nd the nouns n the reDu rement statements and ma6e them nto ob5e4ts. In that 3arad -m, en4a3sulat on was def ned as data)h d n- and ob5e4ts were def ned as th n-s w th data and methods used to a44ess that data. Th s s a l m ted v ew, 4onstra ned as t s by a fo4us on how to m3lement ob5e4ts. It s n4om3lete. Th s 3art d s4usses a vers on of the ob5e4t)or ented 3arad -m that s based on an e23anded def n t on of these 4on4e3ts. These e23anded def n t ons are the result of strate- es and 3r n4 3les that ar se from the des -n and m3lementat on of des -n 3atterns. It refle4ts a more 4om3lete m ndset of ob5e4t or entat on.

Chapter ' &

Discusses These Topics An ntrodu4t on to the latest understand n- of ob5e4ts. The 7n f ed 8odel n- 9an-ua-e <789= w ll then be 3resented. The 789 - ves us the tools to des4r be ob5e4t) or ented des -ns n a -ra3h 4al, more read ly understood manner.

I l@ve RuBoard

I l@ve RuBoard

Chapter *$ "he Object-Oriented Paradig!


?verv ew Before The ?b5e4t)?r ented Parad -m! Bun4t onal #e4om3os t on The Problem of ReDu rements #eal n- w th Chan-es! 7s n- Bun4t onal #e4om3os t on #eal n- w th Chan- n- ReDu rements The ?b5e4t)?r ented Parad -m ?b5e4t)?r ented Pro-ramm n- n A4t on S3e4 al ?b5e4t 8ethods Summary
I l@ve RuBoard

I l@ve RuBoard

Overview
Th s 4ha3ter ntrodu4es you to the ob5e4t)or ented 3arad -m by 4om3ar n- and 4ontrast nt w th someth n- fam l ar! standard stru4tured 3ro-ramm n-. The ob5e4t)or ented 3arad -m -rew out of a need to meet the 4hallen-es of 3ast 3ra4t 4es us n- standard stru4tured 3ro-ramm n-. By be n- 4lear about these 4hallen-es, we 4an better see the advanta-es of ob5e4t)or ented 3ro-ramm n-, as well as -a n a better understand n- of th s me4han sm. Th s 4ha3ter w ll not ma6e you an e23ert on ob5e4t)or ented methods. It w ll not even ntrodu4e you to all of the bas 4 ob5e4t)or ented 4on4e3ts. It w ll, however, 3re3are you for the rest of th s boo6, wh 4h w ll e23la n the 3ro3er use of ob5e4t)or ented des -n methods as 3ra4t 4ed by the e23erts.

In th s 4ha3ter,

I d s4uss a 4ommon method of analys s, 4alled fun4t onal de4om3os t on. I address the 3roblem of reDu rements and the need to deal w th 4han-e <the s4our-e of 3ro-ramm n-O=.

I des4r be the ob5e4t)or ented 3arad -m and show ts use n a4t on.

I 3o nt out s3e4 al ob5e4t methods.

I 3rov de a table of m3ortant ob5e4t term nolo-y used n th s 4ha3ter on 3a-e &'.

I l@ve RuBoard

I l@ve RuBoard

'efore "he Object-Oriented Paradig!: #nctional Deco!position


9etLs start out by e2am n n- a 4ommon a33roa4h to software develo3ment. If I were to - ve you the tas6 of wr t n- 4ode to a44ess a des4r 3t on of sha3es that were stored n a database and then d s3lay them, t would be natural to th n6 n terms of the ste3s reDu red. Bor e2am3le, you m -ht th n6 that you would solve the 3roblem by do n- the follow n-! '. &. .. ,. 9o4ate the l st of sha3es n the database. ?3en u3 the l st of sha3es. Sort the l st a44ord n- to some rules. # s3lay the nd v dual sha3es on the mon tor.

Nou 4ould ta6e any one of these ste3s and further brea6 down the ste3s reDu red to m3lement t. Bor e2am3le, you 4ould brea6 down Ste3 , as follows! Bor ea4h sha3e n the l st, do the follow n-! ,a. Ident fy ty3e of sha3e. ,b. Jet lo4at on of sha3e.

,4. Call a33ro3r ate fun4t on that w ll d s3lay sha3e, - v n- t the sha3eLs lo4at on. Th s s 4alled f nctional deco'position be4ause the analyst brea6s down <de4om3oses= the 3roblem nto the fun4t onal ste3s that 4om3ose t. Nou and I do th s be4ause t s eas er to deal w th smaller 3 e4es than t s to deal w th the 3roblem n ts ent rety. It s the same a33roa4h I m -ht use to wr te a re4 3e for ma6 n- lasa-na, or nstru4t ons to assemble a b 4y4le. "e use th s a33roa4h so often and so naturally that we seldom Duest on t or as6 f there are other alternat ves. The 3roblem w th fun4t onal de4om3os t on s that t does not hel3 us 3re3are the 4ode for 3oss ble 4han-es n the future, for a -ra4eful evolut on. "hen 4han-e s reDu red, t s often be4ause I want to add a new var at on to an e2 st n- theme. Bor e2am3le, I m -ht have to deal w th new sha3es or new ways to d s3lay sha3es. If I have 3ut all of the lo- 4 that m3lements the ste3s nto one lar-e fun4t on or module, then v rtually any 4han-e to the ste3s w ll reDu re 4han-es to that fun4t on or module. And 4han-e 4reates o33ortun t es for m sta6es and un ntended 4onseDuen4es. ?r, as I l 6e to say, %any b gs originate wit" c"anges to code( Aer fy th s assert on for yourself. Th n6 of a t me when you wanted to ma6e a 4han-e to your 4ode, but were afra d to 3ut t n be4ause you 6new that mod fy n- the 4ode n one 3la4e 4ould brea6 t somewhere else. "hy m -ht th s ha33enF 8ust the 4ode 3ay attent on to all of ts fun4t ons and how they m -ht be usedF >ow m -ht the fun4t ons ntera4t w th one anotherF "ere there too many deta ls for the fun4t on to 3ay attent on to, su4h as the lo- 4 t was try n- to m3lement, the th n-s w th wh 4h t was ntera4t n-, the data t was us n-F As t s w th 3eo3le, try n- to fo4us on too many th n-s at on4e be-s for errors when anyth n- 4han-es. And no matter how hard you try, no matter how well you do your analys s, you 4an never -et all of the reDu rements from the user. Too mu4h s un6nown about the future. Th n-s 4han-e. They always do Q And not"ing yo can do will stop c"ange( $ t yo do not "ave to be overco'e by it(
I l@ve RuBoard

I l@ve RuBoard

"he Proble! of +e,#ire!ents


As6 software develo3ers what they 6now to be true about the reDu rements they -et from users. They w ll often say!

ReDu rements are n4om3lete. ReDu rements are usually wron-.

ReDu rements <and users= are m slead n-.

ReDu rements do not tell the whole story.

?ne th n- you w ll never hear s, 1not only were our reDu rements 4om3lete, 4lear, and understandable, but they la d out all of the fun4t onal ty we were -o n- to need for the ne2t f ve yearsO1 In my th rty years of e23er en4e wr t n- software, the ma n th n- I have learned about reDu rements s that Q 0e1 ire'ents always c"ange( I have also learned that most develo3ers th n6 th s s a bad th n-. But few of them wr te the r 4ode to handle 4han- n- reDu rements well. ReDu rements 4han-e for a very s m3le set of reasons!

The usersL v ew of the r needs 4han-e as a result of the r d s4uss ons w th develo3ers and from see n- new 3oss b l t es for the software.

The develo3ersL v ew of the usersL 3roblem doma n 4han-es as they develo3 software to automate t and thus be4ome more fam l ar w th t.

The env ronment n wh 4h the software s be n- develo3ed 4han-es. <"ho ant 4 3ated, f ve years a-o, "eb develo3ment as t s todayF=

Th s does not mean you and I 4an - ve u3 on -ather n- -ood reDu rements. It does mean that we must wr te our 4ode to a44ommodate 4han-e. It also means we should sto3

beat n- ourselves u3 <or our 4ustomers, for that matter= for th n-s that w ll naturally o44ur.

Change happens- Deal with it$

In all but the s m3lest 4ases, reDu rements w ll always 4han-e, no matter how well we do the n t al analys sO

Rather than 4om3la n n- about 4han- n- reDu rements, we should 4han-e the develo3ment 3ro4ess so that we 4an address 4han-e more effe4t vely.

I l@ve RuBoard

I l@ve RuBoard

Dealing with Changes: &sing #nctional Deco!position


9oo6 a l ttle 4loser at the 3roblem of d s3lay n- sha3es. >ow 4an I wr te the 4ode so that t s eas er to handle sh ft n- reDu rementsF Rather than wr t n- one lar-e fun4t on, I 4ould ma6e t more modular. Bor e2am3le, n Ste3 ,4 on 3a-e ,, where I 2Call appropriate f nction t"at will display s"ape# giving it t"e s"ape3s location#2 I 4ould wr te a module l 6e that shown n C2am3le ') '. Example 1-1 sin! "o#ularit$ to Contain %ariation

function: display shape input: type of shape, description of shape action: switch (type of shape) case square: put display function for square here case circle: put display function for circle here
Then, when I re4e ve a reDu rement to be able to d s3lay a new ty3e of sha3eEa tr an-le, for nstan4eEI only need to 4han-e th s module <ho3efullyO=. There are some 3roblems w th th s a33roa4h, however. Bor e2am3le, I sa d that the n3uts to the module were the ty3e of sha3e and a des4r 3t on of the sha3e. #e3end n- u3on how

I am stor n- sha3es, t may or may not be 3oss ble to have a 4ons stent des4r 3t on of sha3es that w ll wor6 well for all sha3es. "hat f the des4r 3t on of the sha3e s somet mes stored as an array of 3o ntsF "ould that st ll wor6F 8odular ty def n tely hel3s to ma6e the 4ode more understandable, and understandab l ty ma6es the 4ode eas er to ma nta n. But modular ty does not always hel3 4ode deal w th all of the var at on t m -ht en4ounter. " th the a33roa4h that I have used so far, I f nd that I have two s -n f 4ant 3roblems, wh 4h -o by the terms low co"esion and tig"t co pling( In h s boo6 Code Co'plete, Steve 84Connell - ves an e24ellent des4r 3t on of both 4ohes on and 4ou3l n-. >e says,

Co"esion refers to how 14losely the o3erat ons n a rout ne are related.1

R'S

R'S

84Connell, S., Code Co'plete: A Practical 4andboo* of &oftware Constr ction , Redmond! 8 4rosoft Press, '%%., 3. 0'.

<(ote! 84Connell d d not nvent these terms, we 5ust ha33en to l 6e h s def n t ons of them best.=

I have heard other 3eo3le refer to 4ohes on as clarity be4ause the more that o3erat ons are related n a rout ne <or a 4lass=, the eas er t s to understand th n-s.

Co pling refers to 1the stren-th of a 4onne4t on between two rout nes. Cou3l n- s a 4om3lement to 4ohes on. Cohes on des4r bes how stron-ly the nternal 4ontents of a rout ne are related to ea4h other. Cou3l n- des4r bes how stron-ly a rout ne s related to other rout nes. The -oal s to 4reate rout nes w th nternal nte-r ty <stron- 4ohes on= and small, d re4t, v s ble, and fle2 ble relat ons to other rout nes <loose 4ou3l n-=.1
R&S

R&S

b d, 3. 0*.

8ost 3ro-rammers have had the e23er en4e of ma6 n- a 4han-e to a fun4t on or 3 e4e of data n one area of the 4ode that then has an une23e4ted m3a4t on other 3 e4es of 4ode. Th s ty3e of bu- s 4alled an 1unwanted s de effe4t.1 That s be4ause wh le we -et the m3a4t we want <the 4han-e=, we also -et other m3a4ts we donLt wantEbu-sO "hat s worse, these bu-s are often d ff 4ult to f nd be4ause we usually donLt not 4e the relat onsh 3 that 4aused the s de effe4ts n the f rst 3la4e < f we had, we wouldnLt have 4han-ed t the way we d d=.

In fa4t, bu-s of th s ty3e lead me to a rather startl n- observat on! ,e really do not spend ' c" ti'e fixing b gs( I th n6 f 2 n- bu-s ta6es a short 3er od of t me n the ma ntenan4e and debu-- n- 3ro4ess. The overwhelm n- amount of t me s3ent n ma ntenan4e and debu-- n- s on finding bu-s and ta6 n- the t me to avo d unwanted s de effe4ts. The a4tual f 2 s relat vely shortO S n4e unwanted s de effe4ts are often the hardest bu-s to f nd, hav n- a fun4t on that tou4hes many d fferent 3 e4es of data ma6es t more l 6ely that a 4han-e n reDu rements w ll result n a 3roblem.

"he devil is in the side effects$



A fo4us on fun4t ons s l 6ely to 4ause s de effe4ts that are d ff 4ult to f nd.

8ost of the t me s3ent n ma ntenan4e and debu-- n- s not s3ent on f 2 n- bu-s, but n finding them and see n- how to avo d unwanted s de effe4ts from the f 2.

" th fun4t onal de4om3os t on, 4han- n- reDu rements 4auses my software develo3ment and ma ntenan4e efforts to thrash. I am fo4used 3r mar ly on the fun4t ons. Chan-es to one set of fun4t ons or data m3a4t other sets of fun4t ons and other sets of data, wh 4h n turn m3a4t other fun4t ons that must be 4han-ed. 9 6e a snowball that 3 46s u3 snow as t rolls downh ll, a fo4us on fun4t ons leads to a 4as4ade of 4han-es from wh 4h t s d ff 4ult to es4a3e.

I l@ve RuBoard

I l@ve RuBoard

Dealing with Changing +e,#ire!ents


To f -ure out a way around the 3roblem of 4han- n- reDu rements and to see f there s an alternat ve to fun4t onal de4om3os t on, letLs loo6 at how 3eo3le do th n-s. 9etLs say that you were an nstru4tor at a 4onferen4e. Peo3le n your 4lass had another 4lass to attend follow n- yours, but d dnLt 6now where t was lo4ated. ?ne of your res3ons b l t es s to

ma6e sure everyone 6nows how to -et to the r ne2t 4lass. If you were to follow a stru4tured 3ro-ramm n- a33roa4h, you m -ht do the follow n-! '. &. Jet l st of 3eo3le n the 4lass. Bor ea4h 3erson on th s l st! a. b. 4. d. B nd the ne2t 4lass they are ta6 n-. B nd the lo4at on of that 4lass. B nd the way to -et from your 4lassroom to the 3ersonLs ne2t 4lass. Tell the 3erson how to -et to the r ne2t 4lass.

To do th s would reDu re the follow n- 3ro4edures! '. &. .. A way of -ett n- the l st of 3eo3le n the 4lass A way of -ett n- the s4hedule for ea4h 3erson n the 4lass A 3ro-ram that - ves someone d re4t ons from your 4lassroom to any other 4lassroom ,. A 4ontrol 3ro-ram that wor6s for ea4h 3erson n the 4lass and does the reDu red ste3s for ea4h 3erson I doubt that you would a4tually follow th s a33roa4h. Instead, you would 3robably 3ost d re4t ons to -o from th s 4lassroom to the other 4lassrooms and then tell everyone n the 4lass, 1I have 3osted the lo4at ons of the 4lasses follow n- th s n the ba46 of the room, as well as the lo4at ons of the other 4lassrooms. Please use them to -o to your ne2t 4lassroom.1 Nou would e23e4t that everyone would 6now what the r ne2t 4lass was, that they 4ould f nd the 4lassroom they were to -o to from the l st, and 4ould then follow the d re4t ons for -o n- to the 4lassrooms themselves. "hat s the d fferen4e between these a33roa4hesF

In the f rst oneE- v n- e23l 4 t d re4t ons to everyoneEyou have to 3ay 4lose attent on to a lot of deta ls. (o one other than you s res3ons ble for anyth n-. Nou

w ll -o 4raGyO

In the se4ond 4ase, you - ve -eneral nstru4t ons and then e23e4t that ea4h 3erson w ll f -ure out how to do the tas6 h mself or herself.

The b --est d fferen4e s th s shi&t o& responsibilit$' In the f rst 4ase, you are res3ons ble for everyth n-P n the se4ond 4ase, students are res3ons ble for the r own behav or. In both 4ases, the same th n-s must be m3lemented, but the or-an Gat on s very d fferent. ,"at is t"e i'pact of t"is5 To see the effe4t of th s reor-an Gat on of res3ons b l t es, letLs see what ha33ens when some new reDu rements are s3e4 f ed. Su33ose I am now told to - ve s3e4 al nstru4t ons to -raduate students who are ass st nat the 4onferen4e. Perha3s they need to 4olle4t 4ourse evaluat ons and ta6e them to the 4onferen4e off 4e before they 4an -o to the ne2t 4lass. In the f rst 4ase, I would have to mod fy the 4ontrol 3ro-ram to d st n-u sh the -raduate students from the under-raduates, and then - ve s3e4 al nstru4t ons to the -raduate students. ItLs 3oss ble that I would have to mod fy th s 3ro-ram 4ons derably. >owever, n the se4ond 4aseEwhere 3eo3le are res3ons ble for themselvesEI would 5ust have to wr te an add t onal rout ne for -raduate students to follow. The 4ontrol 3ro-ram would st ll 5ust say, 1Jo to your ne2t 4lass.1 Ca4h 3erson would s m3ly follow the nstru4t ons a33ro3r ate for h mself or herself. Th s s a s -n f 4ant d fferen4e for the 4ontrol 3ro-ram. In one 4ase, t would have to be mod f ed every t me there was a new 4ate-ory of students w th s3e4 al nstru4t ons that they m -ht be e23e4ted to follow. In the other one, new 4ate-or es of students have to be res3ons ble for themselves. There are three d fferent th n-s -o n- on that ma6e th s ha33en. They are!

The 3eo3le are res3ons ble for themselves, nstead of the 4ontrol 3ro-ram be nres3ons ble for them. <(ote that to a44om3l sh th s, a 3erson must also be aware of what ty3e of student he or she s.=

The 4ontrol 3ro-ram 4an tal6 to d fferent ty3es of 3eo3le <-raduate students and

re-ular students= as f they were e2a4tly the same.

The 4ontrol 3ro-ram does not need to 6now about any s3e4 al ste3s that students m -ht need to ta6e when mov n- from 4lass to 4lass.

To fully understand the m3l 4at ons of th s, tLs m3ortant to establ sh some term nolo-y. In 6%L Distilled, 8art n Bowler des4r bes three d fferent 3ers3e4t ves n the software develo3ment 3ro4ess. These are des4r bed n Table ')'.
R.S

R.S

Bowler, 8., S4ott, ;., 6%L Distilled: A $rief + ide to t"e &tandard Object %odeling Lang age# .nd Edition# Read n-, 8ass.! Add son)

"esley, '%%%, 33. +'T+&.

Table 1-1' Perspectives in the (o&tware Development Process

Perspective Con4e3tual

Description Th s 3ers3e4t ve 1re3resents the 4on4e3ts n the doma n under studyQ . a 4on4e3tual model should be drawn w th l ttle or no re-ard for the software that m -ht m3lement t Q1 1(ow we are loo6 n- at software, but we are loo6 n- at the nterfa4es of the software, not the m3lementat on.1

S3e4 f 4at on

Im3lementat on At th s 3o nt we are at the 4ode tself. 1Th s s 3robably the most often)used 3ers3e4t ve, but n many ways the s3e4 f 4at on 3ers3e4t ve s often a better one to ta6e.1
9oo6 a-a n at the 3rev ous e2am3le of 1Jo to your ne2t 4lass.1 (ot 4e that youEas the nstru4torEare 4ommun 4at n- w th the 3eo3le at the concept al level( In other words, you are tell n- 3eo3le what you want, not how to do t. >owever, the way they -o to the r ne2t 4lass s very s3e4 f 4. They are follow n- s3e4 f 4 nstru4t ons and n do n- so are wor6 n- at the i'ple'entation level( Commun 4at n- at one level <4on4e3tually= wh le 3erform n- at another level < m3lementat on= results n the reDuestor <the nstru4tor= not 6now n- e2a4tly what s ha33en n-, only 6now n- 4on4e3tually what s ha33en n-. Th s 4an be very 3owerful. 9etLs see how to ta6e these not ons and wr te 3ro-rams that ta6e advanta-e of them.
I l@ve RuBoard

I l@ve RuBoard

"he Object-Oriented Paradig!


The ob5e4t)or ented 3arad -m s 4entered on the 4on4e3t of the ob5e4t. Cveryth n- s fo4used on ob5e4ts. I wr te 4ode or-an Ged around ob5e4ts, not fun4t ons. "hat s an ob5e4tF ?b5e4ts have trad t onally been def ned as data w th 'et"ods <the ob5e4t)or ented term for fun4t ons=. 7nfortunately, th s s a very l m t n- way of loo6 n- at ob5e4ts. I w ll loo6 at a better def n t on of ob5e4ts shortly <and a-a n n Cha3ter 0, 1C23and n- ?ur >or Gons1=. "hen I tal6 about the data of an ob5e4t, these 4an be s m3le th n-s l 6e numbers and 4hara4ter str n-s, or they 4an be other ob5e4ts. The advanta-e of us n- ob5e4ts s that I 4an def ne th n-s that are res3ons ble for themselves. <See Table ')&.= ?b5e4ts nherently 6now what ty3e they are. The data n an ob5e4t allow t to 6now what state t s n and the 4ode n the ob5e4t allows t to fun4t on 3ro3erly <that s, do what t s su33osed to do=.

Table 1-)' Ob*ects an# Their Responsibilities

This Ob*ect + Student

,s Responsible -or +
;now n- wh 4h 4lassroom they are n ;now n- wh 4h 4lassroom they are to -o to ne2t Jo n- from one 4lassroom to the ne2t

Instru4tor Classroom

Tell n- 3eo3le to -o to ne2t 4lassroom >av n- a lo4at on

# re4t on - ver J ven two 4lassrooms, - v n- d re4t ons from one 4lassroom to the other
In th s 4ase, the ob5e4ts were dent f ed by loo6 n- at the ent t es n the 3roblem doma n. I dent f ed the res3ons b l t es <or methods= for ea4h ob5e4t by loo6 n- at what these ent t es need to do. Th s s 4ons stent w th the te4hn Due of f nd n- ob5e4ts by loo6 n- for the nouns n the reDu rements and f nd n- methods by loo6 n- for verbs. I f nd th s te4hn Due to be Du te l m t n- and w ll show a better way throu-hout the boo6. Bor now, t s a way to -et us started.

The best way to th n6 about what an ob5e4t s, s to th n6 of t as someth n- w th res3ons b l t es. A -ood des -n rule s that ob5e4ts should be res3ons ble for themselves and should have those res3ons b l t es 4learly def ned. Th s s why I say one of the res3ons b l t es of a student ob5e4t s 6now n- how to -o from one 4lassroom to the ne2t. I 4an also loo6 at ob5e4ts us n- the framewor6 of BowlerLs 3ers3e4t ves!

At the concept al level# an ob5e4t s a set of res3ons b l t es.

R,S

R,S

I am rou-hly 3ara3hras n- Bertrand 8eyerLs wor6 of #es -n by Contra4t as outl ned n Object-Oriented &oftware

Constr ction, 733er Saddle R ver, (.J.! Prent 4e >all, '%%*, 3. ..'.

At the specification level# an ob5e4t s a set of methods that 4an be nvo6ed by other ob5e4ts or by tself.

At the i'ple'entation level# an ob5e4t s 4ode and data.

7nfortunately, ob5e4t)or ented des -n s often tau-ht and tal6ed about only at the m3lementat on levelE n terms of 4ode and dataErather than at the 4on4e3tual or s3e4 f 4at on level. But there s -reat 3ower n th n6 n- about ob5e4ts n these latter ways as wellO S n4e ob5e4ts have res3ons b l t es and ob5e4ts are res3ons ble for themselves, there has to be a way to tell ob5e4ts what to do. Remember that ob5e4ts have data to tell the ob5e4t about tself and methods to m3lement fun4t onal ty. 8any methods of an ob5e4t w ll be dent f ed as 4allable by other ob5e4ts. The 4olle4t on of these methods s 4alled the ob5e4tLs p blic interface( Bor e2am3le, n the 4lassroom e2am3le, I 4ould wr te the Student ob5e4t w th the method

gotoNextClassroom(). I would not need to 3ass any 3arameters n be4ause ea4h


student would be res3ons ble for tself. That s, t would 6now!

"hat t needs to be able to move >ow to -et any add t onal nformat on t needs to 3erform th s tas6

In t ally, there was only one 6 nd of studentEa re-ular student who -oes from 4lass to 4lass. (ote that there would be many of these 1re-ular students1 n my 4lassroom <my

system=. But what f I want to have more *inds of studentsF It seems neff 4 ent for ea4h student ty3e to have ts own set of methods to tell t what t 4an do, es3e4 ally for tas6s that are 4ommon to all students. A more eff 4 ent a33roa4h would be to have a set of methods asso4 ated w th all students that ea4h one 4ould use or ta lor to the r own needs. I want to def ne a 1-eneral student1 to 4onta n the def n t ons of these 4ommon methods. Then, I 4an have all manner of s3e4 al Ged students, ea4h of whom has to 6ee3 tra46 of h s or her own 3r vate nformat on. In ob5e4t)or ented terms, th s -eneral student s 4alled a class( A 4lass s a def n t on of the behav or of an ob5e4t. It 4onta ns a 4om3lete des4r 3t on of!

The data elements the ob5e4t 4onta ns The methods the ob5e4t 4an do

The way these data elements and methods 4an be a44essed

S n4e the data elements an ob5e4t 4onta ns 4an vary, ea4h ob5e4t of the same ty3e may have d fferent data but w ll have the same fun4t onal ty <as def ned n the methods=. To -et an ob5e4t, I tell the 3ro-ram that I want a new ob5e4t of th s ty3e <that s, the 4lass that the ob5e4t belon-s to=. Th s new ob5e4t s 4alled an instance of the 4lass. Creat nnstan4es of a 4lass s 4alled instantiation( "r t n- the 1Jo to the ne2t 4lassroom1 e2am3le us n- an ob5e4t)or ented a33roa4h s mu4h s m3ler. The 3ro-ram would loo6 l 6e th s! '. &. .. ,. +. Start the 4ontrol 3ro-ram. Instant ate the 4olle4t on of students n the 4lassroom. Tell the 4olle4t on to have the students -o to the r ne2t 4lass. The 4olle4t on tells ea4h student to -o to the r ne2t 4lass. Ca4h student! a. B nds where h s ne2t 4lass s

b. 4. /. #one.

#eterm nes how to -et there Joes there

Th s wor6s f ne unt l I need to add another student ty3e, su4h as the -raduate student. I have a d lemma. It a33ears that I must allow any ty3e of student nto the 4olle4t on <e ther re-ular or -raduate student=. The 3roblem fa4 n- me s how do I want the 4olle4t on to refer to ts 4onst tuentsF S n4e I am tal6 n- about m3lement n- th s n 4ode, the 4olle4t on w ll a4tually be an array or someth n- of some ty3e of ob5e4t. If the 4olle4t on were named someth n- l 6e, RegularStudents, then I would not be able to 3ut

GraduateStudents nto the 4olle4t on. If I say that the 4olle4t on s 5ust a -rou3 of
ob5e4ts, how 4an I be sure that I do not n4lude the wron- ty3e of ob5e4t <that s, someth n- that doesnLt do 1Jo to your ne2t 4lass1=F The solut on s stra -htforward. I need a -eneral ty3e that en4om3asses more than one s3e4 f 4 ty3e. In th s 4ase, I want a Student ty3e that n4ludes both RegularStudents and GraduateStudents. In ob5e4t)or ented terms, we 4all Student an abstract class( Abstra4t 4lasses def ne what other, related, 4lasses 4an do. These 1other1 4lasses are 4lasses that re3resent a 3art 4ular ty3e of related behav or. Su4h a 4lass s often 4alled a concrete class be4ause t re3resents a s3e4 f 4, or non4han- n-, m3lementat on of a 4on4e3t. In the e2am3le, the abstra4t 4lass s Student. There are two ty3es of Students re3resented by the 4on4rete 4lasses, RegularStudents and GraduateStudents.

RegularStudent s one 6 nd of Student and GraduateStudent s also a 6 nd of Student.


Th s ty3e of relat onsh 3 s 4alled an is-a relat onsh 3, wh 4h s formally 4alled in"eritance( Thus, the RegularStudent 4lass in"erits fro' Student. ?ther ways to say th s would be, the GraduateStudent derives fro'# speciali7es# or is a s bclass of Student. Jo n- the other way, 1the Student 4lass s the base class# generali7es# or s the

s perclass of GraduateStudent and of RegularStudent. Abstra4t 4lasses a4t as 3la4eholders for other 4lasses. I use them to def ne the methods the r der ved 4lasses must m3lement. Abstra4t 4lasses 4an also 4onta n 4ommon methods that 4an be used by all der vat ons. "hether a der ved 4lass uses the default behav or or re3la4es t w th ts own var at on s u3 to the der vat on <th s s 4ons stent w th the mandate that ob5e4ts be res3ons ble for themselves=. Th s means that I 4an have the 4ontroller 4onta n Students. The referen4e ty3e used w ll be Student. The 4om3 ler 4an 4he46 that anyth n- referred to by th s Student referen4e s, n fa4t, a 6 nd of Student. Th s - ves the best of both worlds!

The 4olle4t on only needs to deal w th Students <thereby allow n- the nstru4tor ob5e4t 5ust to deal w th students=.

Net, I st ll -et ty3e 4he46 n- <only Students that 4an 1Jo to the r ne2t 4lassroom1 are n4luded=.

And, ea4h 6 nd of Student s left to m3lement ts fun4t onal ty n ts own way.

Abstract classes are !ore than classes that do not get instantiated$
Abstra4t 4lasses are often des4r bed as 4lasses that do not -et nstant ated. Th s def n t on s a44urateEat the m3lementat on level. But that s too l m ted. It s more hel3ful to def ne abstra4t 4lasses at the 4on4e3tual level. Thus, at the 4on4e3tual level, abstra4t 4lasses are s m3ly 3la4eholders for other 4lasses. !"at is# t"ey give s a way to assign a na'e to a set of related classes( !"is lets s treat t"is set as one concept( In the ob5e4t)or ented 3arad -m, you must 4onstantly th n6 about your 3roblem from all three levels of 3ers3e4t ve.

S n4e the ob5e4ts are res3ons ble for themselves, there are many th n-s they do not need to e23ose to other ob5e4ts. Carl er, I ment oned the 4on4e3t of the p blic interfaceEthose methods that are a44ess ble by other ob5e4ts. In ob5e4t)or ented systems, the ma n ty3es of a44ess b l ty are!

P blicE Anyth n- 4an see t. ProtectedE ?nly ob5e4ts of th s 4lass and der ved 4lasses 4an see t.

PrivateE ?nly ob5e4ts from th s 4lass 4an see t.

Th s leads to the 4on4e3t of encaps lation( Cn4a3sulat on has often been des4r bed s m3ly as h d n- data. ?b5e4ts -enerally do not e23ose the r nternal data members to the outs de world <that s, the r v s b l ty s 3rote4ted or 3r vate=. But en4a3sulat on refers to more than h d n- data. In -eneral, en4a3sulat on means any *ind of "iding( In the e2am3le, the nstru4tor d d not 6now wh 4h were the re-ular students and wh 4h were the -raduate students. The ty3e of student s h dden from the nstru4tor <I am en4a3sulat n- the ty3e of student=. As you w ll see later n the boo6, th s s a very m3ortant 4on4e3t. Another term to learn s poly'orp"is'( In ob5e4t)or ented lan-ua-es, we often refer to ob5e4ts w th one ty3e of referen4e that s an abstra4t 4lass ty3e. >owever, what we are a4tually referr n- to are s3e4 f 4 nstan4es of 4lasses der ved from the r abstra4t 4lasses. Thus, when I tell the ob5e4ts to do someth n- 4on4e3tually throu-h the abstra4t referen4e, I -et d fferent behav or, de3end n- u3on the s3e4 f 4 ty3e of der ved ob5e4t I have. Polymor3h sm der ves from poly <mean n- many= and 'orp" <mean n- form=. Thus, t means 'any for's( Th s s an a33ro3r ate name be4ause I have many d fferent forms of behav or for the same 4all. In the e2am3le, the nstru4tor tells the students to 1Jo to your ne2t 4lassroom.1 >owever, de3end n- u3on the ty3e of student, they w ll e2h b t d fferent behav or <hen4e 3olymor3h sm=.

+eview of Object-Oriented "er!inology Term Description

?b5e4t

An ent ty that has res3ons b l t es. I m3lement these by wr t n- a 4lass < n 4ode= that def nes data members <the var ables asso4 ated w th the ob5e4ts= and methods <the fun4t ons asso4 ated w th the ob5e4ts=. The re3os tory of methods. #ef nes the data members of ob5e4ts. Code s or-an Ged around the 4lass.

Class

Cn4a3sulat on Ty3 4ally def ned as data)h d n-, but better thou-ht of as any 6 nd of h d n-. Inher tan4e >av n- one 4lass be a s3e4 al 6 nd of another 4lass. These s3e4 al Ged 4lasses are 4alled der vat ons of the base 4lass <the n t al 4lass=. The base 4lass s somet mes 4alled the su3er4lass wh le the der ved 4lasses are somet mes 4alled the sub4lasses. A 3art 4ular e2am3le of a 4lass < t s always an ob5e4t=.

Instan4e

Instant at on The 3ro4ess of 4reat n- an nstan4e of a 4lass. Polymor3h sm Be n- able to refer to d fferent der vat ons of a 4lass n the same way, but -ett n- the behav or a33ro3r ate to the der ved 4lass be n- referred to. Pers3e4t ves There are three d fferent 3ers3e4t ves for loo6 n- at ob5e4ts! concept al# specification# and i'ple'entation( These d st n4t ons are hel3ful n understand n- the relat onsh 3 between abstra4t 4lasses and the r der vat ons. The abstra4t 4lass def nes how to solve th n-s 4on4e3tually. It also - ves the s3e4 f 4at on for 4ommun 4at n- w th any ob5e4t der ved from t. Ca4h der vat on 3rov des the s3e4 f 4 m3lementat on needed.

I l@ve RuBoard

I l@ve RuBoard

Object-Oriented Progra!!ing in Action


9etLs re)e2am ne the sha3es e2am3le d s4ussed at the be- nn n- of the 4ha3ter. >ow would I m3lement t n an ob5e4t)or ented mannerF Remember that t has to do the follow n-! '. &. .. ,. 9o4ate the l st of sha3es n the database. ?3en u3 the l st of sha3es. Sort the l st a44ord n- to some rules. # s3lay the nd v dual sha3es on the mon tor.

To solve th s n an ob5e4t)or ented manner, I need to def ne the ob5e4ts and the res3ons b l t es they would have. The ob5e4ts I would need are!

Class

Responsibilities ."etho#s/
getCollection E-et a s3e4 f ed 4olle4t on of sha3es

ShapeDataBase Shape <an abstra4t 4lass=


display Edef nes nterfa4e for Sha3es getX Ereturn I lo4at on of Sha3e <used for sort n-= getY Ereturn N lo4at on of Sha3e <used for sort n-=

Square
Sha3e=

<der ved

from
display Ed s3lay a sDuare <re3resented by th s
ob5e4t=

Circle
Sha3e=

<der ved

from
display Ed s3lay a 4 r4le <re3resented by th s
ob5e4t=

Collection

display Etell all 4onta ned sha3es to d s3lay sort Esort the 4olle4t on of sha3es

Display

drawLine Edraw a l ne on the s4reen drawCircle Edraw a 4 r4le on the s4reen

The ma n 3ro-ram would now loo6 l 6e th s! '. &. 8a n 3ro-ram 4reates an nstan4e of the database ob5e4t. 8a n 3ro-ram as6s the database ob5e4t to f nd the set of sha3es I am nterested n and to nstant ate a 4olle4t on ob5e4t 4onta n n- all of the sha3es <a4tually, t w ll nstant ate 4 r4les and sDuares that the 4olle4t on w ll hold=. .. ,. +. 8a n 3ro-ram as6s the 4olle4t on to sort the sha3es. 8a n 3ro-ram as6s the 4olle4t on to d s3lay the sha3es. The 4olle4t on as6s ea4h sha3e t 4onta ns to d s3lay tself. Ca4h sha3e d s3lays tself <us n- the Display ob5e4t= a44ord n- to the ty3e of sha3e I have. 9etLs see how th s hel3s to handle new reDu rements <remember, reDu rements always 4han-e=. Bor e2am3le, 4ons der the follow n- new reDu rements!

/.

A## new 0in#s o& shapes .such as a trian!le/' To ntrodu4e a new 6 nd of sha3e, only two ste3s are reDu red! ) Create a new der vat on of Shape that def nes the sha3e. ) In the new der vat on, m3lement a vers on of the d s3lay method that s a33ro3r ate for that sha3e.

Chan!e the sortin! al!orithm' To 4han-e the method for sort n- the sha3es, only one ste3 s reDu red! ) 8od fy the method n Collection. Cvery sha3e w ll use the new al-or thm.

1ottom line2 The ob5e4t)or ented a33roa4h has l m ted the m3a4t of 4han- nreDu rements. There are several advanta-es to en4a3sulat on. The fa4t that t h des th n-s from the user d re4tly m3l es the follow n-!

7s n- th n-s s eas er be4ause the user does not need to worry about m3lementat on ssues.

Im3lementat ons 4an be 4han-ed w thout worry n- about the 4aller. <S n4e the 4aller d dnLt 6now how t was m3lemented n the f rst 3la4e, there shouldnLt be any de3enden4 es.=

The ns des of an ob5e4t are un6nown to outs de ob5e4tsEthey are used by the ob5e4t to hel3 m3lement the fun4t on s3e4 f ed by the ob5e4tLs nterfa4e.

B nally, 4ons der the 3roblem of unwanted s de effe4ts that ar se when fun4t ons are 4han-ed. Th s 6 nd of bu- s addressed effe4t vely w th en4a3sulat on. The nternals of ob5e4ts are un6nown to other ob5e4ts. If I use en4a3sulat on and follow the strate-y that ob5e4ts are res3ons ble for themselves, then the only way to affe4t an ob5e4t w ll be to 4all a method on that ob5e4t. The ob5e4tLs data and the way t m3lements ts res3ons b l t es are sh elded from 4han-es 4aused by other ob5e4ts.

Encaps#lation saves #s$

The more I ma6e my ob5e4ts res3ons ble for the r own behav ors, the less the 4ontroll n- 3ro-rams have to be res3ons ble for.

Cn4a3sulat on ma6es 4han-es to an ob5e4tLs nternal behav or trans3arent to other ob5e4ts.

Cn4a3sulat on hel3s to 3revent unwanted s de effe4ts.

I l@ve RuBoard

I l@ve RuBoard

)pecial Object .ethods


I have tal6ed about methods that are 4alled by other ob5e4ts or 3oss bly used by an ob5e4t tself. But what ha33ens when ob5e4ts are 4reatedF "hat ha33ens when they -o awayF If ob5e4ts are self)4onta ned un ts, then t would be a -ood dea to have methods to handle these s tuat ons.

These s3e4 al methods do, n fa4t, e2 st and are 4alled constr ctors and destr ctors( A 4onstru4tor s a s3e4 al method that s automat 4ally 4alled when the ob5e4t s 4reated. Its 3ur3ose s to handle start n- u3 the ob5e4t. Th s s 3art of an ob5e4tLs mandate to be res3ons ble for tself. The 4onstru4tor s the natural 3la4e to do n t al Gat ons, set default nformat on, set u3 relat onsh 3s w th other ob5e4ts, or do anyth n- else that s needed to ma6e a well)def ned ob5e4t. All ob5e4t)or ented lan-ua-es loo6 for a 4onstru4tor method and e2e4ute t when the ob5e4t s 4reated. By us n- 4onstru4tors 3ro3erly t s eas er to el m nate <or at least m n m Ge= un n t al Ged var ables. Th s ty3e of error usually o44urs from 4arelessness on the 3art of the develo3er. By hav n- a set, 4ons stent 3la4e for all n t al Gat ons throu-hout your 4ode <that s, the 4onstru4tors of your ob5e4ts= t s eas er to ensure that n t al Gat ons ta6e 3la4e. Crrors 4aused by un n t al Ged var ables are easy to f 2 but hard to f nd, so th s 4onvent on <w th the automat 4 4all n- of the 4onstru4tor= 4an n4rease the eff 4 en4y of 3ro-rammers. A destru4tor s a s3e4 al method that hel3s an ob5e4t 4lean u3 after tself when the ob5e4t -oes out of e2 sten4eP that s, when the ob5e4t s destroyed. All ob5e4t)or ented lan-ua-es loo6 for a destru4tor method and e2e4ute t when the ob5e4t s be n- deleted. As w th the 4onstru4tor, the use of the destru4tor s 3art of the ob5e4tLs mandate to be res3ons ble for tself. #estru4tors are ty3 4ally used for releas n- resour4es when ob5e4ts are no lon-er needed. S n4e Java has -arba-e 4olle4t on <auto)4leanu3 of ob5e4ts no lon-er n use=, destru4tors are not as m3ortant n Java as they are n C::. In C::, t s 4ommon for an ob5e4tLs destru4tor also to destroy other ob5e4ts that are used only by th s ob5e4t.

I l@ve RuBoard

I l@ve RuBoard

)#!!ary
In th s 4ha3ter, I have shown how ob5e4t or entat on hel3s us m n m Ge 4onseDuen4es of sh ft n- reDu rements on a system and how t 4ontrasts w th fun4t onal de4om3os t on. I 4overed a number of the essent al 4on4e3ts n ob5e4t)or ented 3ro-ramm n- and have ntrodu4ed and des4r bed the 3r mary term nolo-y. These are essent al to understand nthe 4on4e3ts n the rest of th s boo6. <See Tables '). and '),.=

Table 1-3' Ob*ect-Oriente# Concepts

Concept Bun4t onal de4om3os t on

Review Stru4tured 3ro-rammers usually a33roa4h 3ro-ram des -n w th f nctional deco'position. Bun4t onal de4om3os t on s the method of brea6 n- down a 3roblem nto smaller and smaller fun4t ons. Ca4h fun4t on s subd v ded unt l t s mana-eable. Chan- n- reDu rements are nherent to the develo3ment 3ro4ess. Rather than blam n- users or ourselves about the seem n-ly m3oss ble tas6 of -ett n- -ood and 4om3lete reDu rements, we should use develo3ment methods that deal w th 4han- n- reDu rements more effe4t vely. ?b5e4ts are def ned by the r res3ons b l t es. ?b5e4ts s m3l fy the tas6s of 3ro-rams that use them by be n- res3ons ble for themselves.

Chan- nreDu rements

?b5e4ts

An ob5e4t has s3e4 al methods that are 4alled when t s 4reated and Constru4tors and destru4tors deleted. These s3e4 al methods are!

Constr ctors# wh 4h n t al Ge or set u3 an ob5e4t. Destr ctors# wh 4h 4lean u3 an ob5e4t when t s deleted.

All ob5e4t)or ented lan-ua-es use 4onstru4tors and destru4tors to hel3 mana-e ob5e4ts.

Table 1-4' Ob*ect-Oriente# Terminolo!$

Term Abstra4t 4lass

De&inition #ef nes the methods and 4ommon attr butes of a set of 4lasses that are 4on4e3tually s m lar. Abstra4t 4lasses are never nstant ated.

Attr bute Class Constru4tor #er ved 4lass

#ata asso4 ated w th an ob5e4t <also 4alled a data member=. Blue3r nt of an ob5e4tEdef nes the methods and data of an ob5e4t of ts ty3e. S3e4 al method that s nvo6ed when an ob5e4t s 4reated. A 4lass that s s3e4 al Ged from a su3er4lass. Conta ns all of the attr butes and methods of the su3er4lass but may also 4onta n other attr butes or d fferent method m3lementat ons. S3e4 al method that s nvo6ed when an ob5e4t s deleted. Any 6 nd of h d n-. ?b5e4ts en4a3sulate the r data. Abstra4t 4lasses en4a3sulate the r der ved 4on4rete 4lasses. A method of analys s n wh 4h a 3roblem s bro6en nto smaller and smaller fun4t ons. The way that a 4lass s s3e4 al Ged, used to relate der ved 4lasses from the r abstra4t ons. A 3art 4ular ob5e4t of a 4lass. The 3ro4ess of 4reat n- an nstan4e of a 4lass. C ther data or method of a 4lass. Bun4t ons that are asso4 ated w th an ob5e4t. An ent ty w th res3ons b l t es. A s3e4 al, self)4onta ned holder of both data and methods that o3erate on that data. An ob5e4tLs data are 3rote4ted from e2ternal ob5e4ts. The ab l ty of related ob5e4ts to m3lement methods that are s3e4 al Ged to the r ty3e. A 4lass from wh 4h other 4lasses are der ved. Conta ns the master def n t ons of attr butes and methods that all der ved 4lasses w ll use <and 3oss bly w ll overr de=.

#estru4tor Cn4a3sulat on Bun4t onal de4om3os t on Inher tan4e Instan4e Instant at on 8ember 8ethod ?b5e4t

Polymor3h sm Su3er4lass

I l@ve RuBoard

I l@ve RuBoard

Chapter /$ "he &.0 he &nified .odeling 0ang#age

?verv ew

"hat Is the 789F "hy 7se the 789F The Class # a-ram Intera4t on # a-rams Summary
I l@ve RuBoard

I l@ve RuBoard

Overview
Th s 4ha3ter - ves a br ef overv ew of the 7n f ed 8odel n- 9an-ua-e <789=, wh 4h s the model n- lan-ua-e of the ob5e4t)or ented 4ommun ty. If you do not already 6now the 789, th s 4ha3ter w ll - ve you the m n mal understand n- you w ll need to be able to read the d a-rams 4onta ned n th s boo6. In th s 4ha3ter,

I des4r be what the 789 s and why to use t. I d s4uss the 789 d a-rams that are essent al to th s boo6!

) The Class # a-ram ) The Intera4t on # a-ram

I l@ve RuBoard

I l@ve RuBoard

1hat %s the &.02


The 789 s a v sual lan-ua-e <mean n- a draw n- notat on w th semant 4s= used to 4reate models of 3ro-rams. By models of 3ro-rams, I mean a d a-rammat 4 re3resentat on of the 3ro-rams n wh 4h one 4an see the relat onsh 3s between the ob5e4ts n the 4ode.

The 789 has several d fferent d a-ramsEsome for analys s, others for des -n, and st ll others for m3lementat on <or more a44urately, for the d ssem nat on, that s, the d str but on of the 4ode= <see Table &)'=. Ca4h d a-ram shows the relat onsh 3s amon- the d fferent sets of ent t es, de3end n- u3on the 3ur3ose of the d a-ram.

Table )-1'

"5 Dia!rams an# Their Purposes

6hen 7ou Are+ In the analys s 3hase

se the

"5 Dia!ram+

se Case Dia!rams8 wh 4h nvolve ent t es ntera4t nw th the system <say, users and other systems= and the fun4t on 3o nts that I need to m3lement.

Activit$ Dia!rams8 wh 4h fo4us on wor6flow of the 3roblem doma n <the a4tual s3a4e where 3eo3le and other a-ents are wor6 n-, the sub5e4t area of the 3ro-ram= rather than the lo- 4 flow of the 3ro-ram. Note:S n4e th s boo6 s 3r n4 3ally fo4used on des -n, I w ll not 4over 7se Case # a-rams or A4t v ty # a-rams here.

9oo6 nat ntera4t ons

object

,nteraction Dia!rams8 wh 4h show how s3e4 f 4 ob5e4ts ntera4t w th ea4h other. S n4e they deal w th specific 4ases rather than -eneral s tuat ons, they are hel3ful both when 4he46 n- reDu rements and when 4he46 n- des -ns. The most 3o3ular 6 nd of Intera4t on # a-ram s the (e9uence Dia!ram'

In the des -n 3hase

Class Dia!rams8 wh 4h deta l the relat onsh 3s between the 4lasses.

9oo6 n- at an ob5e4tLs be"aviors that d ffer based u3on the state that the ob5e4t s n

(tate Dia!rams8 wh 4h deta l the d fferent states an ob5e4t may be n as well as the trans t ons between these states.

In the 3hase

de3loyment

Deplo$ment Dia!rams8 wh 4h show how d fferent modules w ll be de3loyed. I w ll not tal6 about these d a-rams here.

I l@ve RuBoard

I l@ve RuBoard

1hy &se the &.02


The 789 s used 3r mar ly for 4ommun 4at onEw th myself, my team members, and w th my 4ustomers. Poor reDu rements <e ther n4om3lete or na44urate= are ub Du tous n the f eld of software develo3ment. The 789 - ves us tools to -ather better reDu rements. The 789 - ves a way to determ ne f my understand n- of the system s the same as othersL. Be4ause systems are 4om3le2 and have d fferent ty3es of nformat on that must be 4onveyed, t offers d fferent d a-rams s3e4 al G n- n the d fferent ty3es of nformat on. ?ne easy way to see the value of the 789 s to re4all your last several des -n rev ews. If you have ever been n a rev ew where someone starts tal6 n- about the r 4ode and des4r bes t w thout a model n- lan-ua-e l 6e the 789, almost 4erta nly the r tal6 was both 4onfus n- as well as be n- mu4h lon-er than ne4essary. The 789 s not only a better way of des4r b n- ob5e4t)or ented des -ns, t also for4es the des -ner to th n6 throu-h h s or her a33roa4h <s n4e t must be wr tten down=.
I l@ve RuBoard

I l@ve RuBoard

"he Class Diagra!


The most bas 4 of 789 d a-rams s the Class # a-ram. It both des4r bes 4lasses and shows the relat onsh 3s between them. The ty3es of relat onsh 3s that are 3oss ble are

"hen one 4lass s a 16 nd of1 another 4lass! the is-a relat onsh 3 "hen there are asso4 at ons between two 4lasses

) ?ne 4lass 14onta ns1 another 4lass! the "as-a relat onsh 3

) ?ne 4lass 1uses1 another 4lass There are var at ons on these themes. Bor e2am3le, to say someth n- 4onta ns someth nelse 4an mean that

The 4onta ned tem s a 3art of the 4onta n n- tem <l 6e an en- ne n a 4ar=. I have a 4olle4t on of th n-s that 4an e2 st on the r own <l 6e a r3lanes at an a r3ort=.

The f rst e2am3le s 4alled co'position wh le the se4ond s 4alled aggregation(

R'S

R'S

Jamma, >elm, Johnson, and Al ss des <the Jan- of Bour= 4all the f rst 1a--re-at on1 and the se4ond 14om3os t on1Ee2a4tly the

reverse of the 789. >owever, the Jan- of Bour boo6 was wr tten before the 789 was f nal Ged. The 3resented def n t on s, n fa4t, 4ons stent w th the 789Ls. Th s llustrates some of the mot vat on for the 789P before t 4ame out there were several d fferent model nlan-ua-es, ea4h w th ts own notat on and terms.

B -ure &)' llustrates several m3ortant th n-s. B rst, ea4h re4tan-le re3resents a 4lass. In the 789, I 4an re3resent u3 to three th n-s n a 4lass!

The name of the 4lass The data members of the 4lass

The methods <fun4t ons= of the 4lass

-i!ure )-1' The Class Dia!ram:its three variations'

I have three d fferent ways of show n- these.

The left'ost rectangle shows 5ust the 4lassL name. I would use th s ty3e of 4lass re3resentat on when more deta led nformat on s not needed.

The 'iddle rectangle shows both the name and the methods of the 4lass. In th s 4ase, the Square has the method display( The 3lus s -n <:= n front of display
R&S

<the name of the method= means that th s method s 3ubl 4Ethat s, ob5e4ts other than ob5e4ts of th s 4lass 4an 4all t.

R&S

"henever we refer to a 4lass name, we w ll bold t as done here.

The rig"t'ost rectangle shows what I had before <the name and methods of the 4lass= as well as data members of the 4lass. In th s 4ase, the m nus s -n <)= before the data member lengt" <wh 4h s of ty3e double= nd 4ates that th s data memberLs value s 3r vate, that s t s unava lable to anyth n- other than the ob5e4t to wh 4h t belon-s.
R.S

R.S

In some lan-ua-es, ob5e4ts of the same ty3e 4an share ea4h otherLs 3r vate data.

&.0 notation for access$


Nou 4an 4ontrol the a44ess b l ty of a 4lassL data and method members. Nou 4an use the 789 to notate wh 4h a44ess b l ty you want ea4h member to have. The three ty3es of a44ess b l ty ava lable n most ob5e4t)or ented lan-ua-es are as follows!

Public2 notated w th a 3lus s -n <:=.

Th s means all ob5e4ts 4an a44ess th s data or method.

Protecte#2 notated w th a 3ound s -n <U=.

Th s means only th s 4lass and all of ts der vat ons < n4lud n- der vat ons from ts der vat ons= 4an a44ess th s data or method.

Private2 notated w th a m nus s -n <)=.

Th s means that only methods of th s 4lass 4an a44ess th s data or method. <(ote! Some lan-ua-es further restr 4t th s to the 3art 4ular ob5e4t.=

Class # a-rams 4an also show relat onsh 3s between d fferent 4lasses. B -ure &)& shows the relat onsh 3 between the Shape 4lass and several 4lasses that der ve from t.

-i!ure )-)' The Class Dia!ram showin! the is-a relationships'

B -ure &)& re3resents several th n-s. B rst, the arrowhead under the Shape 4lass means that those 4lasses 3o nt n- to Shape der ve from Shape. Burthermore, s n4e Shape s italici7ed that means t s an abstra4t 4lass. An abstra4t 4lass s a 4lass that s used to def ne an nterfa4e for the 4lasses that der ve from t. There are a4tually two d fferent 6 nds of "as-a relat onsh 3s. ?ne ob5e4t 4an have another ob5e4t where the 4onta ned ob5e4t s a 3art of the 4onta n n- ob5e4tEor not. In B -ure &)., I show Airports 1hav n-1 Aircraft. Aircraft are not 3art of Airports, but I 4an st ll say the Airport has them. Th s ty3e of relat onsh 3 s 4alled aggregation( -i!ure )-3' The Class Dia!ram showin! the "as-a relationship'

In th s d a-ram, I also show that an Aircraft s e ther a Jet or a Helicopter. I 4an see that Aircraft s an abstra4t 4lass be4ause ts name s shown n tal 4s. That means that an Airport w ll have e ther Jet or Helicopter but w ll treat them the same <as

Aircraft=. The o3en <unf lled= d amond on the r -ht of the Airport 4lass nd 4ates the
a--re-at on relat onsh 3. The other ty3e of "as-a relat onsh 3 s where the 4onta nment means the 4onta ned ob5e4t s a 3art of the 4onta n n- ob5e4t. Th s ty3e of relat onsh 3 s also 4alled co'position( B -ure &), shows that a Car has Tires as 3arts <that s, the Car s made u3 of Tires and other th n-s=. Th s ty3e of "as-a relat onsh 3, 4alled 4om3os t on, s de3 4ted by the f lled n d amond. Th s d a-ram also shows that a Car uses a GasStation. The ses relat onsh 3 s de3 4ted by a dashed l ne w th an arrow. Th s s also 4alled a de3enden4y relat onsh 3. -i!ure )-4' The Class Dia!ram showin! composition an# the ses relationship'

Both 4om3os t on and a--re-at on nvolve one ob5e4t 4onta n n- one or more ob5e4ts. Com3os t on, however, m3l es the 4onta ned ob5e4t s a 3art of the 4onta n n- ob5e4t, whereas a--re-at on means the 4onta ned ob5e4ts are more l 6e a 4olle4t on of th n-s. "e 4an 4ons der 4om3os t on to be an unshared asso4 at on, w th the 4onta ned ob5e4tLs l fet me be n- 4ontrolled by ts 4onta n n- ob5e4t. The a33ro3r ate use of 4onstru4tor and destru4tor methods s useful here to hel3 fa4 l tate ob5e4t 4reat on and destru4t on.

Notes in the &.0$

In B -ure &)+, there s a new symbol! the (ote. The bo2 4onta n n- the messa-e 1o3en d amonds mean a--re-at on1 s a note. They are meant to loo6 l 6e 3 e4es of 3a3er w th the r -ht 4orner folded ba46. Nou often see them w th a l ne 4onne4t n- them to a 3art 4ular 4lass nd 4at n- they relate 5ust to that 4lass. -i!ure )-;' The Class Dia!ram with a Note'

Class # a-rams show the relat onsh 3s between 4lasses. " th 4om3os t on and a--re-at on, however, the relat onsh 3 s more s3e4 f 4ally about ob5e4ts of that ty3e of 4lass. Bor e2am3le, t s true Airports have Aircraft, but more s3e4 f 4ally, s3e4 f 4 a r3orts have s3e4 f 4 a r4raft. The Duest on may ar seE1how many a r4raft does an a r3ort haveF1 Th s s 4alled the cardinality of the relat onsh 3. I show th s n B -ures &)/ and &)*. -i!ure )-<' The car#inalit$ o& the

Airport-Aircraft relationship'

-i!ure )-=' The car#inalit$ o& the

Car-Tire relationship'

B -ure &)/ tells us that when I have an Airport, t has from $ to any number <re3resented by an aster s6 here, but somet mes by the letter 1n1= of Aircraft. The 1$..'1 on the Airport s de means that when I have an Aircraft, t 4an be 4onta ned by e ther $ or ' Airport < t may be n the a r=. B -ure &)* tells us that when I have a Car, t has e ther , or + t res < t may or may not have a s3are=. T res are on e2a4tly one 4ar. I have heard some 3eo3le assume no s3e4 f 4at on of 4ard nal ty assumes that there s one ob5e4t. That s not 4orre4t. If 4ard nal ty s not s3e4 f ed there s no assum3t on made as to how many ob5e4ts there are. As before, the dashed l ne between Car and GasStation n B -ure &)* shows that there s a de3enden4y between the two. The 789 uses a dashed arrow to nd 4ate semant 4 relat onsh 3s <mean n-s= between two model elements.

I l@ve RuBoard

I l@ve RuBoard

%nteraction Diagra!s

Class # a-rams show stat 4 relat onsh 3s between 4lasses. In other words, they do not show us any a4t v ty. Althou-h very useful, somet mes I need to show how the ob5e4ts nstant ated from these 4lasses a4tually wor6 to-ether. The 789 d a-rams that show how ob5e4ts ntera4t w th ea4h other are 4alled -nteraction Diagra's( The most 4ommon ty3e of Intera4t on # a-ram s the SeDuen4e # a-ram, su4h as shown n B -ure &)0. -i!ure )->' (e9uence Dia!ram &or the shapes pro!ram'

I l@ve RuBoard

I l@ve RuBoard

)#!!ary
The 3ur3ose of the 789 s to both flesh out your des -ns and to 4ommun 4ate them. #o not worry so mu4h about 4reat n- d a-rams the 1r -ht1 way. Th n6 about the best way to 4ommun 4ate the 4on4e3ts n your des -n. In other words,

If you th n6 someth n- needs to be sa d, use a (ote to say t. If you arenLt sure about an 4on or a symbol and you have to loo6 t u3 to f nd out ts mean n-, n4lude a note to e23la n t s n4e others may be un4lear about ts mean n-, too.

Jo for 4lar ty.

?f 4ourse, th s means you should not use the 789 n nonstandard waysEthat does not 4ommun 4ate 3ro3erly e ther. Just 4ons der what you are try n- to 4ommun 4ate as your draw your d a-rams.

I l@ve RuBoard

I l@ve RuBoard

Part %%: "he 0i!itations of "raditional Object-Oriented Design


Part Overview In th s 3art, I solve a real)world 3roblem us n- standard ob5e4t)or ented methods. Th s was a 3roblem I wor6ed on when I was 5ust be- nn n- to learn des -n 3atterns.

Chapter .

Discusses These Topics


A des4r 3t on of the CA#@CA8 3roblem! e2tra4t nformat on from a lar-e 4om3uter)a ded des -n@4om3uter)a ded manufa4tur n- <CA#@CA8= database to feed a 4om3le2 and e23ens ve analys s 3ro-ram.

Be4ause the CA#@CA8 system 4ont nues to evolve, the 3roblem 4r es out for fle2 ble 4ode.

8y f rst solut on to the CA#@CA8 3roblem, us n- standard ob5e4t)or ented methods.

At the t me I a4tually wor6ed on th s 3roblem, I hadnLt yet learned the essen4e of the 3r n4 3les beh nd many des -n 3atterns. Th s resulted n an n t al solut on that over)rel ed on nher tan4e. It was easy to des -n and the n t al solut on wor6ed, but I ended u3 w th too many s3e4 al 4ases.

8y solut on had s -n f 4ant 3roblemsEd ff 4ult ma ntenan4e and nfle2 b l tyE5ust the th n-s I ho3ed to avo d w th ob5e4t) or ented des -n.

9ater, n Part IA, I w ll rev s t the 3roblem n Cha3ter '&, 1Solv n- the CA#@CA8 Problem w th Patterns.1 I w ll solve the 3roblem a-a n us n- des -n 3atterns to or4hestrate the a33l 4at onLs ar4h te4ture and ts m3lementat on deta ls. By do n- th s, I 4reate a solut on that s mu4h eas er to ma nta n and s mu4h more fle2 ble.

Th s 3art s m3ortant to read be4ause t llustrates a ty3 4al 3roblem that results n trad t onal ob5e4t)or ented des -nEtaller)than)ne4essary nher tan4e h erar4h es that have t -ht 4ou3l n- and low 4ohes on.
I l@ve RuBoard

I l@ve RuBoard

Chapter 3$ A Proble! "hat Cries O#t for lexible Code

?verv ew C2tra4t n- Informat on from a CA#@CA8 System

7nderstand the Ao4abulary #es4r be the Problem The Cssent al Challen-es and A33roa4hes Summary
I l@ve RuBoard

I l@ve RuBoard

Overview
Th s 4ha3ter - ves an overv ew of a 3roblem we want to solve! e2tra4t n- nformat on from a lar-e CA#@CA8 database to feed a 4om3le2 and e23ens ve analys s 3ro-ram. Be4ause the CA#@CA8 system 4ont nues to evolve, the 3roblem 4r es out for fle2 ble 4ode. In th s 4ha3ter, I - ve an overv ew of the CA#@CA8 3roblem, the vo4abulary of the doma n, and m3ortant features of the 3roblem.
I l@ve RuBoard

I l@ve RuBoard

Extracting %nfor!ation fro! a CAD4CA. )yste!


I am now -o n- to rev ew a 3ast des -n of m ne that -ot me on the road to the ns -hts 4onta ned n th s boo6. I was su33ort n- a des -n 4enter n wh 4h en- neers used a CA#@CA8 system to ma6e draw n-s of sheet metal 3arts. An e2am3le of one of these 3arts s shown n B -ure .)'. -i!ure 3-1' Example o& a piece o& sheet metal'

8y 3roblem was to wr te a 4om3uter tool to e2tra4t nformat on from the CA#@CA8 system so that an e23ert system 4ould use t n a 3art 4ular way. The e23ert system needed th s nformat on n order to 4onrol the manufa4tur n- of the 3art. S n4e the e23ert system was 4om3le2 to mod fy and would have a lon-er l fe than the 4urrent vers on of the CA#@CA8 system, I wanted to wr te the nformat on)e2tra4t n- tool so that t 4ould eas ly be ada3ted to new rev s ons of the CA#@CA8 system.

1hat are expert syste!s2


An expert syste' s a s3e4 al 4om3uter system that uses the rules of a human e23ert<s= to ma6e automated de4 s ons. Bu ld n- e23ert systems nvolves two ste3s. B rst, a4Du re and model the set of rules that e23erts use to ma6e de4 s ons and a44om3l sh the tas6. Se4ond, m3lement th s set of rules n the 4om3uter systemP th s ste3 usually uses some sort of 4ommer4 ally ava lable e23ert system tool. The f rst ste3 s by far the more d ff 4ult ass -nment for the analyst.

I l@ve RuBoard

I l@ve RuBoard

&nderstand the 5ocab#lary


The f rst tas6 n analys s s to understand the vo4abulary used by the users and the e23erts n the 3roblem doma n. The most m3ortant terms used are those that des4r be the

d mens ons and -eometry n the sheet metal. As shown n B -ure .)', a 3 e4e of sheet metal s 4ut to a 3art 4ular s Ge and has sha3es 4ut out ns de t. C23erts 4all these 4utouts by the -eneral name 1feature.1 A 3 e4e of sheet metal 4an be fully s3e4 f ed by ts e2ternal d mens ons and the features 4onta ned n t. The ty3es of sha3esEfeaturesEthat may be found n a 3 e4e of sheet metal are des4r bed n Table .)'. These are the sha3es the system w ll have to address.

Table 3-1' (hapes -oun# in a Piece o& (heet "etal

(hape Slot

Description Stra -ht 4uts n the metal of 4onstant w dth that term nate w th e ther sDuared or rounded ed-es. Slots may be or ented to any an-le. They are usually 4ut w th a router b t. B -ure .)' has three slots on the left s deP one s or ented vert 4ally wh le the others are or ented hor Gontally. C r4les 4ut nto the sheet metal. Ty3 4ally they are 4ut w th dr ll b ts of vary n- w dth. B -ure .)' has a hole toward the left surrounded by the three slots and has a lar-er hole toward the r -ht of the sheet metal. SDuares w th e ther sDuared or rounded ed-es. These are 4ut by a h -h)3owered 3un4h h tt n- the metal w th -reat m3a4t. B -ure .)' has three 4utoutsP the lower r -ht one s or ented at ,+ de-rees. Preformed sha3es that are not slots, holes, or 4utouts. In these 4ases, a s3e4 al 3un4h has been made to 4reate these Du 46ly. Cle4tr 4al outlets are a 4ommon 1s3e4 al1 4ase. The star sha3e n B -ure .)' s a s3e4 al sha3e.

>ole

Cutout

S3e4 al

Irre-ular Anyth n- else. They are formed by us n- a 4omb nat on of tools. The rre-ularly sha3ed ob5e4t toward the bottom r -ht of B -ure .)' s an rre-ular sha3e.
CA#@CA8 e23erts also use add t onal term nolo-y that s m3ortant to understand, as des4r bed n Table .)&.

Table 3-)' A##itional CAD?CA" Terminolo!$

Term

Description

Jeometry

The des4r 3t on of how a 3 e4e of sheet metal loo6s! the lo4at on of ea4h of the features and the r d mens ons and the e2ternal sha3e of the sheet metal. The 3 e4e of sheet metal tself. I need to be able to store the -eometry of ea4h of the 3arts. or The set of re4ords n the CA#@CA8 database that stores the -eometry of a 3art.

Part #ataset model

(C ma4h ne (umer 4ally 4ontrolled <(C= ma4h ne. A s3e4 al manufa4tur nand (C set tool that 4uts metal us n- a var ety of 4utt n- heads that are 4ontrolled by a 4om3uter 3ro-ram. 7sually, the 4om3uter 3ro-ram s fed the -eometry of the 3art. Th s 4om3uter 3ro-ram s 4om3osed of 4ommands 4alled the (C set.
I l@ve RuBoard

I l@ve RuBoard

Describe the Proble!


I need to des -n a 3ro-ram that w ll allow the e23ert system to o3en and read a model 4onta n n- the -eometry of a 3art that I want to analyGe and then to -enerate the 4ommands for the numer 4ally 4ontrolled <(C= ma4h ne to bu ld the 3 e4e of sheet metal. I am only 4on4erned about sheet metal 3arts n th s e2am3le. >owever, the CA#@CA8 system 4an handle many other 6 nds of 3arts. At a h -h level, I want the system to 3erform the follow n- ste3s!

AnalyGe 3 e4es of sheet metal. See how they should be made, based on the features they 4onta n.

Jenerate a set of nstru4t ons that are readable by manufa4tur n- eDu 3ment. Th s set of nstru4t ons s 4alled an (C set or a numer 4al 4ontrol set.

J ve these nstru4t ons to manufa4tur n- eDu 3ment when I want to ma6e any of these 3arts

The d ff 4ulty w th my 3ro-ramm n- tas6 s that I 4annot s m3ly e2tra4t the features from the dataset and -enerate (C set 4ommands. The ty3e of 4ommands to use and the order

of these 4ommands de3end u3on the features and the r relat on to other features. Bor e2am3le, ta6e a sha3e that s made u3 of several features! a sha3e made u3 of a 4utout w th two slots. ?ne of the slots runs vert 4ally throu-h the 4utout wh le the other runs hor Gontally throu-h t. Th s s shown n B -ure .)&. -i!ure 3-)' A cutout with two slots' 5e&t2 @ow the part loo0s when &inishe#' Ri!ht2 ,t is reall$ compose# o& three &eatures'

It s m3ortant to real Ge that I am a4tually - ven the three features on the r -ht to ma6e u3 the sha3e on the left. That s be4ause the en- neers us n- the CA#@CA8 system ty3 4ally th n6 n terms of the features to ma6e u3 more 4om3le2 sha3es be4ause they 6now that do n- so w ll enable Du 46er manufa4tur n- of the 3arts. The 3roblem s, I 4annot 5ust -enerate the (C set 4ommands for the three features nde3endently of one another and ho3e the 3art 4omes out 3ro3erlyEthere s often a 3art 4ular order that must be used. In the e2am3le, f I do the slots f rst and then the 4utout, as shown n B -ure .)., when the 4utout s made <remember a 4utout s 4reated by us n- a h -h) m3a4t 3un4h=, the sheet metal w ll bend be4ause the slots w ll have wea6ened the metal. -i!ure 3-3' A ba# approach to cuttin! out the openin!s' This se9uence results in wea0ene#8 bent sheet metal'

I must 4reate the sha3e shown n B -ure .)& by 3un4h n- out the 4utout f rst, then do nthe slots. Th s wor6s be4ause the slots are 4reated us n- a router, wh 4h a33l es s deways 3ressure. 8a6 n- the 4utout f rst a4tually ma6es the 5ob eas er, not harder. Th s s shown n B -ure .),. -i!ure 3-4' An expertAs approach to cuttin! out the openin!s' This approach results in correct cutouts'

Bortunately, someone had already wor6ed out the rules for the e23ert system. I d d not have to worry about that. I too6 the t me to e23la n these 4hallen-es so that you 4ould understand the ty3e of nformat on needed by the e23ert system.
I l@ve RuBoard

I l@ve RuBoard

"he Essential Challenges and Approaches

The CA#@CA8 system s 4onstantly evolv n-, 4han- n-. 8y real 3roblem was to ma6e t 3oss ble for the 4om3any to 4ont nue to use ts e23ens ve e23ert system wh le the CA#@CA8 system 4han-ed. In my s tuat on, they were 4urrently us n- one vers on of the CA#@CA8 system, Aers on ' <A'=, and a new vers on, Aers on & <A&=, was 4om n- out shortly. Althou-h one vendor made both vers ons, the two vers ons were not 4om3at ble. Bor a var ety of te4hn 4al and adm n strat ve reasons, t was not 3oss ble to translate the models from one vers on to the ne2t. Thus, the e23ert system needed to be able to su33ort both vers ons of the CA#@CA8 system. In fa4t, the s tuat on was even a l ttle worse than 5ust hav n- to a44omodate two d fferent vers ons of the CA#@CA8 system. I 6new a th rd vers on was 4om n- out before lon-, but d d not 6now when that would ha33en. In order to 3reserve the nvestment n the 4om3anyLs e23ert system, I wanted a system ar4h te4ture a33ro2 mately l 6e the one d a-rammed n B -ure .)+. -i!ure 3-;' @i!h-level view o& m$ solution'

In other words, the a33l 4at on 4an n tal Ge everyth n- so that the e23ert system uses the a33ro3r ate CA#@CA8 system. >owever, the e23ert system has to be able to use e ther vers on. >en4e, I need to ma6e A' and A& loo6 the same to the e23ert system.

Althou-h 3olymor3h sm s def n tely needed at the -eometry e2tra4tor level, t w ll not be needed at the feature level. Th s s be4ause the e23ert system needs to 6now what ty3e of features t s deal n- w th. >owever, we donLt want to ma6e any 4han-es to the e23ert system when Aers on . of the CA#@CA8 system 4omes out. A bas 4 understand n- of ob5e4t)or ented des -n m3l es that I w ll have a h -h)level 4lass d a-ram s m lar to the one shown n B -ure .)/. -i!ure 3-<' Class #ia!ram o& m$ solution'

B1C

R'S

Th s and all other 4lass d a-rams n th s boo6 use the 7n f ed 8odel n- 9an-ua-e <789= notat on. See Cha3ter &, 1The 789EThe

7n f ed 8odel n- 9an-ua-e1 for a des4r 3t on of 789 notat on.

In other words, the e23ert system relates to the CA#@CA8 systems throu-h the Model 4lass. The Main 4lass ta6es 4are of nstant at n- the 4orre4t vers on of the Model <that s,

!Model or "Model=.
(ow, I w ll des4r be the CA#@CA8 systems and how they wor6. 7nfortunately, the two are very d fferent beasts. Aers on ' s essent ally a 4olle4t on of subrout ne l brar es. To -et nformat on from a model, a ser es of 4alls must be made. A ty3 4al set of Duer es would be the follow n-!

(tep '. &.

Do this in CAD?CA" %ersion 1 ?3en model INV and return a handle to t Store th s handle as >

.. ,. ,a. ,b. ,4. Q

Bor model, referred to by >, tell me how many features are 3resent, store as ( Bor ea4h feature n the model referred to by > <from ' to (= for model referred to by >, tell me the I# of the th element and store as I# for model referred to by >, tell me the feature ty3e of I# and store as T for model referred to by >, tell me the I 4oord nate of I# and store as I <use T to determ ne the 3ro3er rout ne to 4all, based on ty3e= Q

Th s system s 3a nful to deal w th and 4learly not ob5e4t)or ented. "hoever s us n- the system must ma nta n the 4onte2t for every Duery manually. Ca4h 4all about a feature must 6now what 6 nd of feature t has. The CA#@CA8 vendor real Ged the nherent l m tat ons of th s ty3e of system. The 3r mary mot vat on for bu ld n- A& was to ma6e t ob5e4t)or ented. The -eometry n A& s therefore stored n ob5e4ts. "hen a system reDuests a model, t -ets ba46 an ob5e4t wh 4h re3resents the model. Th s model ob5e4t 4onta ns a set of ob5e4ts, ea4h re3resent n- a feature. S n4e the 3roblem doma n s based on features, t s not sur3r s n- that the 4lasses A& uses to re3resent these features 4orres3ond e2a4tly to the ones I have ment oned already! slots, holes, 4utouts, s3e4 als, and rre-ulars. Therefore, n A&, I 4an -et a set of ob5e4ts that 4orres3ond to the features that e2 st n the sheet metal. The 789 d a-ram n B -ure .)* shows the 4lasses for the features. -i!ure 3-=' -eature classes o& %)'

The ??J stands for ob5e4t)or ented -eometry, 5ust as a rem nder that A& s an ob5e4t) or ented system.

I l@ve RuBoard

I l@ve RuBoard

)#!!ary
In th s 4ha3ter, I des4r bed the CA#@CA8 3roblem.

I must e2tra4t nformat on from d fferent CA#@CA8 systems n the same way. Th s w ll allow a system n wh 4h the 4om3any has a -reat nvestment <an e23ert system= to 4ont nue wor6 n- w thout e23ens ve mod f 4at ons every t me the CA#@CA8 systems 4han-es.

I have two systems that are m3lemented n 4om3letely d fferent ways, even thou-h they 4onta n essent ally the same nformat on.

Th s tas6 has many s m lar t es to other 3roblems I have run a4ross n 3ro5e4ts. There are d fferent s3e4 f 4 m3lementat ons of systems, but I want to allow other ob5e4ts to 4ommun 4ate w th these d fferent m3lementat ons n the same way.
I l@ve RuBoard

I l@ve RuBoard

Chapter 6$ A )tandard Object-Oriented )ol#tion


?verv ew Solv n- w th S3e4 al Cases Summary Su33lement! C:: Code C2am3les
I l@ve RuBoard

I l@ve RuBoard

Overview
Th s 4ha3ter - ves an n t al solut on to the 3roblem d s4ussed n Cha3ter ., 1A Problem That Cr es ?ut for Ble2 ble Code.1 It s a reasonable f rst attem3t at a solut on and -ets the 5ob done Du 46ly. >owever, t m sses an m3ortant system reDu rement! fle2 b l ty as the CA#@CA8 system 4ont nues to evolve. In th s 4ha3ter, I des4r be a solut on based on ob5e4t or entat on. It s not a -reat solut on, but t s a solut on that would wor6. Note: I w ll only show Java 4ode e2am3les n the ma n body of th s 4ha3ter. The eDu valent C:: 4ode e2am3les 4an be found at the end of th s 4ha3ter.

I l@ve RuBoard

I l@ve RuBoard

)olving with )pecial Cases


J ven the two d fferent CA#@CA8 systems des4r bed n Cha3ter ., 1A Problem That Cr es ?ut for Ble2 ble Code,1 how do I bu ld an nformat on)e2tra4t on system that w ll loo6 the same to a 4l ent ob5e4t re-ardless of wh 4h CA#@CA8 system that I haveF In th n6 n- how to solve th s 3roblem, I reasoned that f I 4an solve t for slots, I 4an use that same solut on for 4utouts, holes, et4. In th n6 n- about slots, I saw that I 4ould eas ly

s3e4 al Ge ea4h 4ase. That s, ILd have a Slot 4lass and ma6e a der vat on for Slots when I had the A' system and another der vat on when I had a A& system. I show th s n B -ure ,)'. -i!ure 4-1' The #esi!n &or slots'

I 4om3lete th s solut on by e2tend n- t for ea4h of the feature ty3es, as shown n B -ure ,) &. -i!ure 4-)' Ori!inal solution to the problem o& extractin! in&ormation'

?f 4ourse, B -ure ,)& s 3retty h -h)level. Ca4h of the w th the 4orres3ond n- A' l brary. Ca4h of the 4orres3ond n- ob5e4t n the A& model.

!### 4lasses would 4ommun 4ate

"### 4lasses would 4ommun 4ate w th the

Th s s eas er to v sual Ge by loo6 n- at ea4h 4lass nd v dually.

!Slot would be m3lemented by remember n- the model t belon-s to and ts I#

n the A' system when t s nstant ated. Then, whenever one of the

!Slot

methods s 4alled to -et nformat on about t, the method would have to 4all a seDuen4e of subrout ne 4alls n A' to -et that nformat on.

"Slot would be m3lemented n a s m lar fash on, e24e3t that, n th s 4ase, ea4h "Slot ob5e4t would 4onta n the slot ob5e4t 4orres3ond n- to t n the A& system.
Then, whenever the ob5e4t was as6ed for nformat on, t would s m3ly 3ass th s reDuest on to the $$GSlot ob5e4t and 3ass the res3onse ba46 to the 4l ent ob5e4t that or - nally reDuested t.

A more deta led d a-ram n4or3orat n- the A' and A& systems s shown n B -ure ,).. -i!ure 4-3' A &irst solution'

I am -o n- to 3rov de 4ode e2am3les for a 4ou3le of the 4lasses n th s des -n. These e2am3les are 5ust to hel3 you understand how th s des -n 4ould be m3lemented. If you feel 4omfortable that you 4ould m3lement th s des -n, feel free to s6 3 the follow n- Java 4ode e2am3les <C:: 4ode e2am3les a33ear at the end of th s 4ha3ter=. Example 4-1 Java Co#e -ra!ments2 ,nstantiatin! the %1 -eatures

// segment of code that instantiates the features // no error checking provided // purposes only // each feature o!"ect needs to know the model num!er // and feature #$ it corresponds to in order to retrieve // information when requested% Note how this information // is passed into each o!"ect&s constructor // open model '()odel modelNum* '(+pen)odel( modelName), n-lements * '(.etNum!erof-lements(modelNum), /eature features01* new /eature0)23/-2456-71, // do for each feature in the model for (i* 8, i 9 n-lements, i::) ; // determine feature present and create // appropriate feature o!"ect switch( '(.et4ype( modelNum, i)) ; case 7<+4: features0i1* new '(7lot( modelNum, '(.et#$( modelNum, i)), !reak, case =+<-: features0i1* new '(=ole( modelNum, '(.et#$( modelNum, i)), !reak, %%% > >
Example 4-) Java Co#e -ra!ments2 ,mplementation o& %1 "etho#s

for illustration

// modelNum and my#$ are private mem!ers containing // information a!out the model and feature (in '() this

// feature corresponds to class '(7lot ; dou!le get3 () ; // call appropriate method for '( to get needed // information% Note: this method may actually // call several methods in '( // to get the information% return '(.et3for7lot( modelNum, my#$), > class '(=ole ; dou!le get3 () ; // call appropriate method for '( to get needed // information% Note: this method may actually // call several methods in '( // to get the information% return '(.et3for=ole( modelNum, my#$), > >
Example 4-3 Java Co#e -ra!ments2 ,nstantiatin! the %) -eatures

// segment of code that instantiates the features // no error checking provided // purposes only // each feature o!"ect needs to know the feature in the // '? system it corresponds to in order to retrieve // information when requested% Note how this information // is passed into each o!"ect&s constructor // open model '?)odel my)odel* '?+pen)odel( modelName), n-lements* my)odel%getNum-lements(), /eature features01* new /eature0)23/-2456-71, ++./eature oog/, // do for each feature in the model for (i* 8, i 9 n-lements, i::) ; // determine feature present and create // appropriate feature o!"ect oog/* my)odel%get-lement(i), for illustration

switch( oog/%my4ype()) ; case 7<+4: features0i1* new '?7lot( oog/), !reak, case =+<-: features0i1* new '?=ole( oog/), !reak, %%% > >
Example 4-4 Java Co#e -ra!ments2 ,mplementation o& %) "etho#s

// oog/ is a reference to the feature o!"ect in '? that // the o!"ect containing it corresponds to class '?7lot ; dou!le get3 () ; // call appropriate method on oog/ to get needed // information% return oog/%get3(), > > class '?=ole ; dou!le get3 () ; // call appropriate method on oog/ to get needed // information% return oog/%get3(), > >
In B -ure ,)., I have added a few of the methods that are needed by the features. (ote how they d ffer de3end n- u3on the ty3e of feature. Th s means I do not have 3olymor3h sm a4ross features. Th s s not a 3roblem, however, s n4e the e23ert system needs to 6now what ty3e of feature t has anyway. Th s s be4ause the e23ert system needs d fferent 6 nds of nformat on from d fferent ty3es of features.

Th s br n-s u3 the 3o nt that I am not so nterested n 3olymor3h sm of the features. Rather, I need the ab l ty to 3lu-)and)3lay d fferent CA#@CA8 systems w thout 4han- nthe e23ert system. "hat I am try n- to doEhandle mult 3le CA#@CA8 vers ons trans3arentlyE- ves me several 4lues that th s solut on s not a -ood one!

0ed ndancy a'ongst 'et"ods8 I 4an eas ly ma- ne that the methods that are ma6 n- 4alls to the A' system w ll have many s m lar t es between them. Bor e2am3le, the '(get3 for Slot and '(get3 for Hole w ll be very s m lar.

%essyE Th s s not always a -ood 3red 4tor, but t s another fa4tor that re nfor4es my d s4omfort w th the solut on.

!ig"t co plingE Th s solut on has t -ht 4ou3l n- be4ause the features are related to ea4h other nd re4tly. These relat onsh 3s man fest themselves as the l 6ely need to mod fy all of the features f the follow n- o44urs! ) A new CA#@CA8 system s reDu red. ) An e2 st n- CA#@CA8 system s mod f ed.

Low co"esionE Cohes on s fa rly low s n4e methods to 3erform 4ore fun4t ons are s4attered amon-st the 4lasses.

>owever, my -reatest 4on4ern 4omes from loo6 n- nto the future. Ima- ne what w ll ha33en when the th rd vers on of the CA#@CA8 system arr ves. The 4omb nator al e23los on w ll 6 ll usO 9oo6 at the th rd row of the 4lass d a-ram n B -ure ,)..

There are f ve ty3es of features. Ca4h ty3e of feature has a 3a r of 4lasses, one for ea4h CA#@CA8 system.

"hen I -et the th rd vers on, I w ll have -rou3s of three, not -rou3s of two.

Instead of ten 4lasses, I w ll have f fteen.

Th s s 4erta nly not a system I w ll have fun ma nta n n-O

A pitfall of analysis: too !#ch attention to details too early$


?ne 4ommon 3roblem that we analysts 4an have s that we d ve nto the deta ls too early n the develo3ment 3ro4ess. It s natural be4ause t s easy to wor6 w th these deta ls. Solut ons for the deta ls are usually a33arent, but are not ne4essar ly the best th n- to start w th. #elay as lon- as you 4an before you 4omm t to the deta ls. In th s 4ase, I a4h eved one ob5e4t ve! a 4ommon API for feature nformat on. Also, I def ned my ob5e4ts from a res3ons b l ty 3o nt of v ew. >owever, I d d th s at the 3r 4e of 4reat n- s3e4 al 4ases for everyth n-. "hen I -et new s3e4 al 4ases, I w ll have to m3lement them as su4h. >en4e, the h -h ma ntenan4e 4osts.

Th s was my f rst)blush solut on and I mmed ately d sl 6ed t. 8y d sl 6e -rew more from my ntu t on than from the more lo- 4al reasons I -ave above. I felt that there were 3roblems. In th s 4ase, I felt stron-ly that a better solut on e2 sted. Net, two hours later, th s was st ll the best I 4ould 4ome u3 w th. The 3roblem, t turned out, was my -eneral a33roa4h, as w ll be seen later n th s boo6.

Pay attention to yo#r instincts$


Jut nst n4t s a sur3r s n-ly 3owerful nd 4ator of the Dual ty of a des -n. I su--est that develo3ers learn to l sten to the r nst n4ts. By -ut nst n4t, I mean the sensat on n your stoma4h when you see someth nyou do not l 6e. I 6now th s sounds uns4 ent f 4 <and t s=, but my e23er en4e has shown me 4ons stently that when I have an nst n4t ve d sl 6e for a des -n, a better one l es around the 4orner. ?f 4ourse, there are somet mes several d fferent 4orners nearby and ILm not always sure where the solut on s.

I l@ve RuBoard

I l@ve RuBoard

)#!!ary
I showed how easy t s to solve th s 3roblem by s3e4 al)4as n- everyth n-. The solut on s stra -htforward. It allows me to add add t onal methods w thout 4han- n- what I already have. >owever, there are several d sadvanta-es to t! h -h redundan4y, low 4ohes on, and 4lass e23los on <from future 4han-es=. The overrel an4e on nher tan4e w ll result n h -her ma ntenan4e 4osts than should o44ur <or at least, than I feel should o44ur=.
I l@ve RuBoard

I l@ve RuBoard

)#pple!ent: C77 Code Exa!ples


Example 4-; CDD Co#e -ra!ments2 ,nstantiatin! the %1 -eatures

// segment of code that instantiates the features // no error checking provided // purposes only // each feature o!"ect needs to know the model num!er // and feature #$ it corresponds to in order to retrieve // information when requested% Note how this information // is passed into each o!"ect&s constructor // open model modelNum* '(+pen)odel( modelName), n-lements* '(.etNum!erof-lements(modelNum), /eature @features0)23/-2456-71, // do for each feature in the model for (i* 8, i 9 n-lements, i::) ; // determine feature present and create // appropriate feature o!"ect switch( '(.et4ype( modelNum, i)) ; case 7<+4: features0i1* new '(7lot( modelNum, '(.et#$( modelNum, i)), for illustration

!reak, case =+<-: features0i1* new '(=ole( modelNum, '(.et#$( modelNum, i)), !reak, %%% > >
Example 4-< CDD Co#e -ra!ments2 ,mplementation o& %1 "etho#s

// modelNum and my#$ are private mem!ers containing // information a!out the model and feature (in '() this // feature corresponds to dou!le '(7lot::get3 () ; // call appropriate method for '( to get needed // information% Note: this method may actually // call several methods in '( // to get the information% return '(.et3for7lot( modelNum, my#$), > dou!le '(=ole::get3 () ; // call appropriate method for '( to get needed // information% Note: this method may actually // call several methods in '( // to get the information% return '(.et3for=ole( modelNum, my#$), >
Example 4-= CDD Co#e -ra!ments2 ,nstantiatin! the %) -eatures

// segment of code that instantiates the features // no error checking provided // purposes only // each feature o!"ect needs to know the feature in the // '? system it corresponds to in order to retrieve for illustration

// information when requested% Note how this information // is passed into each o!"ect&s constructor // open model my)odel* '?+pen)odel( modelName), n-lements* my)odel AgetNum-lements(), /eature @features0)23/-2456-71, ++./eature @oog/, // do for each feature in the model for (i* 8, i 9 n-lements, i::) ; // determine feature present and create // appropriate feature o!"ect oog/* my)odel Aget-lement(i), switch( oog/ Amy4ype()) ; case 7<+4: features0i1* new '?7lot( oog/), !reak, case =+<-: features0i1* new '?=ole( oog/), !reak, %%% > > >
Example 4-> CDD Co#e -ra!ments2 ,mplementation o& %) "etho#s

// oog/ is a reference to the feature o!"ect in '? that // the o!"ect containing it corresponds to dou!le '?7lot::get3 () ; // call appropriate method on oog/ to get needed // information% return oog/ Aget3(), > dou!le '?=ole::get3 () ;

// call appropriate method on oog/ to get needed // information% return oog/ Aget3(), >
I l@ve RuBoard

I l@ve RuBoard

Part %%%: Design Patterns


Part Overview Th s 3art ntrodu4es des -n 3atterns! what they are and how to use them. Bour 3atterns 3ert nent to the CA#@CA8 3roblem <Cha3ter ., 1A Problem That Cr es ?ut for Ble2 ble Code1= are des4r bed. They are 3resented nd v dually and then related to the earl er 3roblem. In learn n- these 3atterns, I em3has Ge the ob5e4t)or ented strate- es es3oused by the Jan- of Bour <as the authors Jamma, >elm, Johnson, and Al ss des are often referred to= n the r sem nal wor6, Design Patterns: Ele'ents of 0e sable Object-Oriented &oftware.

Chapter +

Discusses These Topics


An ntrodu4t on to des -n 3atterns.

The 4on4e3t of des -n 3atterns, the r or - ns n ar4h te4ture, and how they a33ly n the d s4 3l ne of software des -n.

The mot vat ons for study n- des -n 3atterns. The -aca#e pattern2 what t s, where t s used, and how t s m3lemented.

>ow the Ba4ade 3attern relates to the CA#@CA8 3roblem. The A#apter pattern2 what t s, where t s used, and how t s m3lemented.

Com3ar son between the Ada3ter 3attern and the Ba4ade 3attern.

>ow the Ada3ter 3attern relates to the CA#@CA8 3roblem.

Some m3ortant 4on4e3ts n ob5e4t)or ented 3ro-ramm n-! 3olymor3h sm, abstra4t on, 4lasses, and en4a3sulat on. It uses what has been learned n Cha3ters +T*.

The 1ri#!e pattern' Th s 3attern s Du te a b t more 4om3le2 than the 3rev ous 3atterns. It s also mu4h more usefulP therefore, I -o nto -reat deta l w th the Br d-e 3attern.

>ow the Br d-e 3attern relates to the CA#@CA8 3roblem. The Abstract -actor$ pattern8 wh 4h fo4uses on 4reat nfam l es of ob5e4ts. "hat the 3attern s, how t s used and m3lemented.

'$

>ow the Abstra4t Ba4tory 3attern relates to the CA#@CA8 3roblem.

At the end of th s se4t on, the reader w ll understand what des -n 3atterns are, why they are useful, and w ll be fam l ar w th four s3e4 f 4 3atterns. The reader w ll also see how these 3atterns relate to the earl er CA#@CA8 3roblem. Th s nformat on, however, may not be enou-h to 4reate a better des -n than we arr ved at by overrely n- on nher tan4e. >owever, the sta-e s set for us n- 3atterns n a way d fferent from the way most des -n 3attern 3ra4t t oners use them.
I l@ve RuBoard

I l@ve RuBoard

Chapter 8$ An %ntrod#ction to Design Patterns

?verv ew #es -n Patterns Arose from Ar4h te4ture and Anthro3olo-y 8ov n- from Ar4h te4tural to Software #es -n Patterns "hy Study #es -n PatternsF

?ther Advanta-es to Study n- #es -n Patterns Summary


I l@ve RuBoard

I l@ve RuBoard

Overview
Th s 4ha3ter ntrodu4es the 4on4e3t of des -n 3atterns. In th s 4ha3ter,

I d s4uss the or - ns of des -n 3atterns n ar4h te4ture and how they a33ly n the d s4 3l ne of software des -n.

I d s4uss the mot vat ons for study n- des -n 3atterns.

#es -n 3atterns are 3art of the 4utt n- ed-e of ob5e4t)or ented te4hnolo-y. ?b5e4t)or ented analys s tools, boo6s, and sem nars are n4or3orat n- des -n 3atterns. Study -rou3s on des -n 3atterns abound. It s often su--ested that 3eo3le learn des -n 3atterns only after they have mastered bas 4 ob5e4t)or ented s6 lls. I have found that the o33os te s true! learn n- des -n 3atterns early n the learn n- of ob5e4t)or ented s6 lls -reatly hel3s to m3rove understand n- of ob5e4t)or ented analys s and des -n. Throu-hout the rest of the boo6, I w ll d s4uss not only des -n 3atterns, but also how they reveal and re nfor4e -ood ob5e4t)or ented 3r n4 3les. I ho3e to m3rove both your understand n- of these 3r n4 3les and llustrate why the des -n 3atterns be n- d s4ussed here re3resent -ood des -ns. Some of th s mater al may seem abstra4t or 3h loso3h 4al. But - ve t a 4han4eO Th s 4ha3ter lays the foundat on for your understand n- of des -n 3atterns. 7nderstand n- th s mater al w ll enhan4e your ab l ty to understand and wor6 w th new 3atterns. I have ta6en many of my deas from Chr sto3her Ale2anderLs !"e !i'eless ,ay of $ ilding. I w ll d s4uss these deas throu-hout th s boo6.
R'S

R'S

Ale2ander, C., Ish 6awa, S., S lverste n, 8., !"e !i'eless ,ay of $ ilding, (ew Nor6! ?2ford 7n vers ty Press, '%*%.

I l@ve RuBoard

I l@ve RuBoard

Design Patterns Arose fro! Architect#re and Anthropology


Nears a-o, an ar4h te4t named Chr sto3her Ale2ander as6ed h mself, 1Is Dual ty ob5e4t veF1 Is beauty truly n the eye of the beholder or would 3eo3le a-ree that some th n-s are beaut ful and some are notF (ow, the 3art 4ular form of beauty that Ale2ander was nterested n was one of ar4h te4tural Dual ty! what ma6es us 6now when an ar4h te4tural des -n s -oodF Bor e2am3le, f a 3erson were -o n- to des -n an entryway for a house, how would he or she 6now that the des -n was -oodF Can we 6now -ood des -nF Is there an ob5e4t ve bas s for su4h a 5ud-mentF A bas s for des4r b n- our 4ommon 4onsensusF Ale2ander 3ostulates that there s su4h an ob5e4t ve bas s w th n ar4h te4tural systems. The 5ud-ment that a bu ld n- s beaut ful s not s m3ly a matter of taste. "e 4an des4r be beauty throu-h an ob5e4t ve bas s that 4an be measured. The d s4 3l ne of 4ultural anthro3olo-y d s4overed the same th n-. That body of wor6 su--ests that w th n a 4ulture, nd v duals w ll a-ree to a lar-e e2tent on what s 4ons dered to be a -ood des -n, what s beaut ful. Cultures ma6e 5ud-ments on -ood des -n that trans4end nd v dual bel efs. I bel eve that there are trans4end n- 3atterns that serve as ob5e4t ve bases for 5ud- n- des -n. A ma5or bran4h of 4ultural anthro3olo-y loo6s for su4h 3atterns to des4r be the behav ors and values of a 4ulture.
R&S

R&S

The anthro3olo- st Ruth Bened 4t s a 3 oneer n 3attern)based analys s of 4ultures. Bor e2am3les, see Bened 4t, R., !"e

C"rysant"e' ' and t"e &word, Boston! >ou-hton 8 ffl n, '%,/.

The 3ro3os t on beh nd des -n 3atterns s that the Dual ty of software systems 4an also be measured ob5e4t vely. If you a44e3t the dea that t s 3oss ble to re4o-n Ge and des4r be a -ood Dual ty des -n, then how do you -o about 4reat n- oneF I 4an ma- ne Ale2ander as6 n- h mself,

,"at is present in a good 1 ality design t"at is not present in a poor 1 ality design5 and ,"at is present in a poor 1 ality design t"at is not present in a good 1 ality design5 These Duest ons s3r n- from Ale2anderLs bel ef that f Dual ty n des -n s ob5e4t ve, then we should be able to dent fy what ma6es des -ns -ood and what ma6es des -ns bad. Ale2ander stud ed th s 3roblem by ma6 n- many observat ons of bu ld n-s, towns, streets, and v rtually every other as3e4t of l v n- s3a4es that human be n-s have bu lt for themselves. >e d s4overed that, for a 3art 4ular ar4h te4tural 4reat on, -ood 4onstru4ts had th n-s n 4ommon w th ea4h other. Ar4h te4tural stru4tures d ffer from ea4h other, even f they are of the same ty3e. Net even thou-h they are d fferent, they 4an st ll be of h -h Dual ty. Bor e2am3le, two 3or4hes may a33ear stru4turally d fferent and yet both may st ll be 4ons dered h -h Dual ty. They m -ht be solv n- d fferent 3roblems for d fferent houses. ?ne 3or4h may be a trans t on from the wal6way to the front door. Another 3or4h m -ht be a 3la4e for shade on a hot day. ?r two 3or4hes m -ht solve a 4ommon 3roblem <trans t on= n d fferent ways. Ale2ander understood th s. >e 6new that stru4tures 4ouldnLt be se3arated from the 3roblem they are try n- to solve. Therefore, n h s Duest to dent fy and des4r be the 4ons sten4y of Dual ty n des -n, Ale2ander real Ged that he had to loo6 at d fferent stru4tures that were des -ned to solve the same 3roblem. Bor e2am3le, B -ure +)' llustrates two solut ons to the 3roblem of demar6 n- an entryway. -i!ure ;-1' (tructures ma$ loo0 #i&&erent but still solve a common problem'

Ale2ander d s4overed that by narrow n- h s fo4us n th s wayEby loo6 n- at stru4tures that solve s m lar 3roblemsEhe 4ould d s4ern s m lar t es between des -ns that were h -h Dual ty. >e 4alled these s m lar t es, patterns( >e def ned a 3attern as 1a solut on to a 3roblem n a 4onte2t.1 Ca4h 3attern des4r bes a 3roblem wh 4h o44urs over and over a-a n n our env ronment and then des4r bes the 4ore of the solut on to that 3roblem, n su4h a way that you 4an use th s solut on a m ll on t mes over, w thout ever do n- t the same way tw 4e.
R.S

R.S

Ale2ander, C., Ish 6awa, S., S lverste n, 8., A Pattern Lang age, (ew Nor6! ?2ford 7n vers ty Press, '%**, 3. 2.

9etLs rev ew some of Ale2anderLs wor6 to llustrate th s. In Table +)' I w ll 3resent an e24er3t from h s !"e !i'eless ,ay of $ ilding, an e24ellent boo6 that 3resents the
R,S

3h loso3hy of 3atterns su44 n4tly.

R,S

Ale2ander, C., Ish 6awa, S., S lverste n, 8., !"e !i'eless ,ay of $ ilding, (ew Nor6! ?2ford 7n vers ty Press, '%*%.

Table ;-1' Excerpt &rom !"e !i'eless ,ay of $ ilding

Alexan#er (a$s +

"$ Comments +

In the same way, a 4ourtyard, wh 4h s A 3attern always has a name and has 3ro3erly formed, hel3s 3eo3le 4ome to a 3ur3ose. >ere, the 3atternLs name l fe n t. s Courtyard and ts 3ur3ose s to hel3 3eo3le to 4ome to l fe n t. Cons der the for4es at wor6 n a Althou-h t m -ht be obv ous 4ourtyard. 8ost fundamental of all, somet mes, t s m3ortant to state 3eo3le see6 some 6 nd of 3r vate e23l 4 tly the 3roblem be n- solved, outdoor s3a4e, where they 4an s t wh 4h s the reason for hav n- the

under the s6y, see the stars, en5oy the 3attern n the f rst 3la4e. Th s s what sun, 3erha3s 3lant flowers. Th s s Ale2ander does here for Courtyard. obv ous. But there are more subtle for4es too. >e 3o nts out a d ff 4ulty w th the Bor nstan4e, when a 4ourtyard s too s m3l f ed solut on and then - ves us t -htly en4losed, has no v ew out, a way to solve the 3roblem that he 3eo3le feel un4omfortable, and tend to has 5ust 3o nted out. stay away Q they need to see out nto some lar-er and more d stant s3a4e. ?r a-a n, 3eo3le are 4reatures of hab t. Bam l ar ty somet mes 6ee3s us from If they 3ass n and out of the see n- the obv ous. The value of a 4ourtyard, every day, n the 4ourse of 3attern s that those w th less the r normal l ves, the 4ourtyard e23er en4e 4an ta6e advanta-e of be4omes fam l ar, a natural 3la4e to -o what others have learned before Q and t s used. them! both what must be n4luded to have a -ood des -n, and what must be avo ded to 6ee3 from a 3oor des -n. But a 4ourtyard w th only one way n, a 3la4e you only -o when you 1want1 to -o there, s an unfam l ar 3la4e, tends to stay unused Q 3eo3le -o more often to 3la4es wh 4h are fam l ar. ?r a-a n, there s a 4erta n abru3tness about suddenly ste33 n- out, from the ns de, d re4tly to the outs de Q t s subtle, but enou-h to nh b t you. If there s a trans t onal s3a4eEa 3or4h >e 3ro3oses a solut on to a 3oss bly or a veranda, under 4over, but o3en to overloo6ed 4hallen-e to bu ld n- a the a rEth s s 3sy4holo- 4ally half way -reat 4ourtyard. between ndoors and outdoors, and ma6es t mu4h eas er, more s m3le, to ta6e ea4h of the smaller ste3s that br n-s you out nto the 4ourtyard Q "hen a 4ourtyard has a v ew out to a Ale2ander s tell n- us how to bu ld a -reat lar-er s3a4e, has 4ross n- 3aths from 4ourtyard Q d fferent rooms, and has a veranda or a 3or4h, these for4es 4an resolve Q and then tells us why t s -reat. themselves. The v ew out ma6es t 4omfortable, the 4ross n- 3aths hel3 -enerate a sense of hab t there, the

3or4h ma6es t eas er to -o out more often Q and -radually the 4ourtyard be4omes a 3leasant 4ustomary 3la4e to be.
To rev ew, Ale2ander says that a des4r 3t on of a 3attern nvolves four tems!

The name of the 3attern The 3ur3ose of the 3attern, the 3roblem t solves

>ow we 4ould a44om3l sh th s

The 4onstra nts and for4es we have to 4ons der n order to a44om3l sh t

Ale2ander 3ostulated that 3atterns 4an solve v rtually every ar4h te4tural 3roblem that one w ll en4ounter. >e further 3ostulated that 3atterns 4ould be used to-ether to solve 4om3le2 ar4h te4tural 3roblems. >ow 3atterns wor6 to-ether w ll be d s4ussed later n th s boo6. Bor now, I want to fo4us on h s 4la m that 3atterns are useful to solve s3e4 al Ged 3roblems.

I l@ve RuBoard

I l@ve RuBoard

.oving fro! Architect#ral to )oftware Design Patterns


"hat does all of th s ar4h te4tural stuff have to do w th us software develo3ersF "ell, n the early '%%$s some smart develo3ers ha33ened u3on Ale2anderLs wor6 n 3atterns. They wondered f what was true for ar4h te4tural 3atterns would also be true for software des -n.
R+S

R+S

The CSPRIT 4onsort um n Curo3e was do n- s m lar wor6 n the '%0$s. CSPRITLs Pro5e4t '$%0 and Pro5e4t +&,0 develo3ed a 3attern)

based des -n methodolo-y 4alled ;nowled-e Analys s and #es -n Su33ort <;A#S= that was fo4used on 3atterns for 4reat n- e23ert systems. ;aren Jardner e2tended the ;A#S analys s 3atterns to ob5e4t or entat on. See Jardner, ;., Cognitive Patterns: Proble'&olving 9ra'ewor*s for Object !ec"nology, (ew Nor6! Cambr d-e 7n vers ty Press, '%%0.

"ere there 3roblems n software that o44ur over and over a-a n that 4ould be

solved n somewhat the same mannerF

"as t 3oss ble to des -n software n terms of 3atterns, 4reat n- s3e4 f 4 solut ons based on these 3atterns only after the 3atterns had been dent f edF

The -rou3 felt the answer to both of these Duest ons was 1uneDu vo4ally yes.1 The ne2t ste3 was to dent fy several 3atterns and develo3 standards for 4atalo- n- new ones. Althou-h many 3eo3le were wor6 n- on des -n 3atterns n the early '%%$s, the boo6 that had the -reatest nfluen4e on th s fled- n- 4ommun ty was Design Patterns: Ele'ents of 0e sable Object-Oriented &oftware by Jamma, >elm, Johnson, and Al ss des. In
R/S

re4o-n t on of the r m3ortant wor6, these four authors are 4ommonly and affe4t onately 6nown as the Jan- of Bour.

R/S

Jamma, C., >elm, R., Johnson, R., Al ss des, J., Design Patterns: Ele'ents of 0e sable Object-Oriented &oftware , Read n-, 8ass.!

Add son)"esley, '%%+.

Th s boo6 served several 3ur3oses!

It a33l ed the dea of des -n 3atterns to software des -n. It des4r bed a stru4ture w th n wh 4h to 4atalo- and des4r be des -n 3atterns.

It 4atalo-ed &. su4h 3atterns.

It 3ostulated ob5e4t)or ented strate- es and a33roa4hes based on these des -n 3atterns.

It s m3ortant to real Ge that the authors d d not 4reate the 3atterns des4r bed n the boo6. Rather, the authors dent f ed these 3atterns as already e2 st n- w th n the software 4ommun ty, 3atterns that refle4ted what had been learned about h -h)Dual ty des -ns for s3e4 f 4 3roblems <note the s m lar ty to Ale2anderLs wor6=. Today, there are several d fferent forms for des4r b n- des -n 3atterns. S n4e th s s not a boo6 about wr t n- des -n 3atterns, I w ll not offer an o3 n on on the best stru4ture for des4r b n- 3atternsP however, the follow n- tems l sted n Table +)& need to be n4luded n any des4r 3t on.

Bor ea4h 3attern that I 3resent n th s boo6, I 3resent a one)3a-e summary of the 6ey features that des4r bes that 3attern.

Table ;-)' Ee$ -eatures o& Patterns

,tem (ame Intent Problem Solut on Part 4 3ants Collaborators ConseDuen4es Im3lementat on

Description All 3atterns have a un Due name that dent f es them. The 3ur3ose of the 3attern. The 3roblem that the 3attern s try n- to solve. >ow the 3attern 3rov des a solut on to the 3roblem n the 4onte2t n wh 4h t shows u3. and The ent t es nvolved n the 3attern. The 4onseDuen4es of us n- the 3attern. Invest -ates the for4es at 3lay n the 3attern.
>ow the 3attern 4an be m3lemented. Note: Im3lementat ons are 5ust 4on4rete man festat ons of the 3attern and should not be 4onstrued as the 3attern tself.

JoB Referen4e

"here to loo6 n the Jan- of Bour te2t to -et more nformat on.

Conse,#ences4 orces
The term conse1 ences s used n des -n 3atterns and s often m sunderstood. In everyday usa-e, 4onseDuen4es usually 4arr es a ne-at ve 4onnotat on. <Nou never hear someone say, 1I won the lotteryO As a conse1 ence# I now do not have to -o to wor6O1= " th n the des -n 3attern 4ommun ty, on the other hand, 4onseDuen4es s m3ly refers to 4ause and effe4t. That s, f you m3lement th s 3attern n su4h)and)su4h a way, th s s how t w ll affe4t and be affe4ted by the for4es 3resent.

I l@ve RuBoard

I l@ve RuBoard

1hy )t#dy Design Patterns2

(ow that you have an dea about what des -n 3atterns are, you may st ll be wonder n-, 1"hy study themF1 There are several reasons that are obv ous and some that are not so obv ous. The most 4ommonly stated reasons for study n- 3atterns are be4ause 3atterns allow us to!

0e se sol tionsE By reus n- already establ shed des -ns, I -et a head start on my 3roblems and avo d gotc"as( I -et the benef t of learn n- from the e23er en4e of others. I do not have to re nvent solut ons for 4ommonly re4urr n- 3roblems.

Establis" co''on ter'inologyE Commun 4at on and teamwor6 reDu re a 4ommon base of vo4abulary and a 4ommon v ew3o nt of the 3roblem. #es -n 3atterns 3rov de a 4ommon 3o nt of referen4e dur n- the analys s and des -n 3hase of a 3ro5e4t.

>owever, there s a th rd reason to study des -n 3atterns! Patterns - ve you a h -her)level 3ers3e4t ve on the 3roblem and on the 3ro4ess of des -n and ob5e4t or entat on. Th s frees you from the tyranny of deal n- w th the deta ls too early. By the end of th s boo6, I ho3e you w ll a-ree that th s s one of the -reatest reasons to study des -n 3atterns. It w ll sh ft your m ndset and ma6e you a more 3owerful analyst. To llustrate th s advanta-e, I want to relate a 4onversat on between two 4ar3enters about how to bu ld the drawers for some 4ab nets.
R*S

R*S

Th s se4t on s ns3 red by a tal6 - ven by Ral3h Johnson and s ada3ted by the authors.

Cons der two 4ar3enters d s4uss n- how to bu ld the drawers for some 4ab nets. Carpenter 12 >ow do you th n6 we should bu ld these drawersF Carpenter )2 "ell, I th n6 we should ma6e the 5o nt by 4utt n- stra -ht down nto the wood, and then 4ut ba46 u3 ,+ de-rees, and then -o n- stra -ht ba46 down, and then ba46 u3 the other way ,+ de-rees, and then -o n- stra -ht ba46 down, and then Q

(ow, your 5ob s to f -ure out what they are tal6 n- aboutO IsnLt that a 4onfus n- des4r 3t onF "hat s Car3enter & 3res4r b n-F The deta ls 4erta nly -et n the wayO 9etLs try to draw out h s des4r 3t on.

Carpenter ) (a$s + 1"ell, I th n6 we should ma6e the 5o nt by 4utt n- stra -ht down nto the wood, and then 4ut ba46 u3 ,+ de-rees Q1

6hich 5oo0s 5i0e +

1Q then -o n- stra -ht ba46 down, and then ba46 u3 the other way ,+ de-rees, and then -o n- stra -ht ba46 down, and then Q1

1Q unt l you end u3 w th a dovetail joint( That s what I was des4r b n-O1

#oesnLt th s sound l 6e 4ode rev ews you have heardF The one where the 3ro-rammer des4r bes the 4ode n terms su4h as, And then, I use a ">I9C 9??P here to do Q followed by a ser es of IB statements to do Q and here I use a S"ITC> to handle Q Nou -et a des4r 3t on of the deta ls of the 4ode, but you have no dea w"at t"e progra' is doing and w"y it is doing it: ?f 4ourse, no self)res3e4t n- 4ar3enter would tal6 l 6e th s. "hat would really ha33en s someth n- l 6e! Carpenter 12 Should we use a doveta l 5o nt or a m ter 5o ntF Already we see a Dual tat ve d fferen4e. The 4ar3enters are d s4uss n- d fferen4es n the Dual ty of solut ons to a 3roblemP the r d s4uss on s at a h -her level, a more abstra4t level. They avo d -ett n- bo--ed down n the deta ls of a 3art 4ular solut on. "hen the 4ar3enter s3ea6s of a m ter 5o nt, he or she has the follow n- 4hara4ter st 4s of the solut on n m nd!

-t is a si'pler sol tionE A m ter 5o nt s a s m3le 5o nt to ma6e. Nou 4ut the ed-es of the 5o n n- 3 e4es at ,+ de-rees, abut them, and then na l or -lue them to-ether <see B -ure +)&=. -i!ure ;-)' A miter *oint

-t is lig"tweig"tE A m ter 5o nt s wea6er than a doveta l. It 4annot hold to-ether

under -reat stress.

-t is inconspic o s8 The m ter 5o ntLs s n-le 4ut s mu4h less not 4eable than the doveta l 5o ntLs mult 3le 4uts.

"hen the 4ar3enter s3ea6s of a doveta l 5o nt <wh 4h we des4r bed how to ma6e on 3a-e 0'=, he or she has other 4hara4ter st 4s of the solut on n m nd. These 4hara4ter st 4s may not be obv ous to a layman, but would be 4learly understood by any 4ar3enter.

-t is a 'ore co'plex sol tionE It s more nvolved to ma6e a doveta l 5o nt. Thus, t s more e23ens ve.

-t is i'pervio s to te'perat re and " 'idity E As these 4han-e, the wood e23ands or 4ontra4ts. >owever, the doveta l 5o nt w ll rema n sol d.

-t is independent of t"e fastening syste' E In fa4t, doveta l 5o nts do not even de3end u3on -lue to wor6.

-t is a 'ore aest"etically pleasing jointE It s beaut ful to loo6 at when made well.

In other words, the doveta l 5o nt s a stron-, de3endable, beaut ful 5o nt that s 4om3le2 <and therefore e23ens ve= to ma6e. So, when Car3enter ' as6ed, Should we use a doveta l 5o nt or a m ter 5o ntF the real Duest on that was be n- as6ed was, Should we use a 5o nt that s e23ens ve to ma6e but s both beaut ful and durable, or should we 5ust ma6e a Du 46 and d rty 5o nt that w ll last at least as lon- unt l the 4he46 4learsF "e m -ht say the 4ar3entersL d s4uss on really o44urs at two levels! the surfa4e level of the r words, and the real 4onversat on, wh 4h o44urs at a h -her level <a 'eta-level= that s h dden from the layman and wh 4h s mu4h r 4her n mean n-. Th s h -her level s the level of 14ar3enter 3atterns1 and refle4ts the real des -n ssues for the 4ar3enters. In the f rst 4ase, Car3enter & obs4ures the real ssues by d s4uss n- the deta ls of the

m3lementat ons of the 5o nts. In the se4ond 4ase, Car3enter ' wants to de4 de wh 4h 5o nt to use based on 4osts and Dual ty of the 5o nt. "ho s more eff 4 entF "ho would you rather wor6 w thF Th s s one th n- I mean when I say that 3atterns 4an hel3 ra se the level of your th n6 n-. Nou w ll learn later n the boo6 that when you ra se your level of th n6 n- l 6e th s, new des -n methods be4ome ava lable. Th s s where the real 3ower of 3atterns l es.

I l@ve RuBoard

I l@ve RuBoard

Other Advantages to )t#dying Design Patterns


8y e23er en4e w th develo3ment -rou3s wor6 n- w th des -n 3atterns s that des -n 3atterns hel3ed both nd v dual learn n- and team develo3ment. Th s o44urred be4ause the more 5un or team members saw that the sen or develo3ers who 6new des -n 3atterns had someth n- of value and these 5un or members wanted t. Th s 3rov ded mot vat on for them to learn some of these 3owerful 4on4e3ts. 8ost des -n 3atterns also ma6e software more mod f able. The reason for th s s that they are t me)tested solut ons. Therefore, they have evolved nto stru4tures that 4an handle 4han-e more read ly than what often f rst 4omes to m nd as a solut on. #es -n 3atterns, when they are tau-ht 3ro3erly, 4an be used to -reatly n4rease the understand n- of bas 4 ob5e4t)or ented des -n 3r n4 3les. I have seen th s 4ountless t mes n the ntrodu4tory ob5e4t)or ented 4ourses I tea4h. In those 4lasses, I start w th a br ef ntrodu4t on to the ob5e4t)or ented 3arad -m. I then 3ro4eed to tea4h des -n 3atterns, us n- them to llustrate the bas 4 ob5e4t)or ented 4on4e3ts <en4a3sulat on, nher tan4e, and 3olymor3h sm=. By the end of the three)day 4ourse, althou-h weLve been tal6 n- mostly about 3atterns, these 4on4e3tsEwh 4h were 5ust ntrodu4ed to many of the 3art 4 3antsE feel l 6e they are old fr ends. The Jan- of Bour su--ests a few strate- es for 4reat n- -ood ob5e4t)or ented des -ns. In 3art 4ular, they su--est the follow n-!

#es -n to nterfa4es. Bavor 4om3os t on over nher tan4e.

B nd what var es and en4a3sulate t.

These strate- es were em3loyed n most of the des -n 3atterns d s4ussed n th s boo6. Cven f you do not learn a lot of des -n 3atterns, study n- a few should enable you to learn why these strate- es are useful. " th that understand n- 4omes the ab l ty to a33ly them to your own des -n 3roblems even f you do not use des -n 3atterns d re4tly. Another advanta-e s that des -n 3atterns allow you or your team to 4reate des -ns for 4om3le2 3roblems that do not reDu re lar-e nher tan4e h erar4h es. A-a n, even f des -n 3atterns are not used d re4tly, avo d n- lar-e nher tan4e h erar4h es w ll result n m3roved des -ns.

I l@ve RuBoard

I l@ve RuBoard

)#!!ary
In th s 4ha3ter, I des4r bed what des -n 3atterns are. Chr sto3her Ale2ander says 13atterns are solut ons to a 3roblem n a 4onte2t.1 They are more than a 6 nd of tem3late to solve oneLs 3roblems. They are a way of des4r b n- the mot vat ons by n4lud n- both what we want to have ha33en alon- w th the 3roblems that are 3la-u n- us. I loo6ed at reasons for study n- des -n 3atterns. Su4h study hel3s to

Reuse e2 st n-, h -h)Dual ty solut ons to 4ommonly re4urr n- 3roblems. Cstabl sh 4ommon term nolo-y to m3rove 4ommun 4at ons w th n teams.

Sh ft the level of th n6 n- to a h -her 3ers3e4t ve.

#e4 de whether I have the r -ht des -n, not 5ust one that wor6s.

Im3rove nd v dual learn n- and team learn n-.

Im3rove the mod f ab l ty of 4ode.

Ba4 l tate ado3t on of m3roved des -n alternat ves, even when 3atterns are not used e23l 4 tly.

# s4over alternat ves to lar-e nher tan4e h erar4h es.

I l@ve RuBoard

I l@ve RuBoard

Chapter 9$ "he acade Pattern

?verv ew Introdu4 n- the Ba4ade Pattern 9earn n- the Ba4ade Pattern B eld (otes! The Ba4ade Pattern Relat n- the Ba4ade Pattern to the CA#@CA8 Problem Summary
I l@ve RuBoard

I l@ve RuBoard

Overview
I w ll start the study of des -n 3atterns w th a 3attern that you have 3robably m3lemented n the 3ast but may not have had a name for! the Ba4ade 3attern. In th s 4ha3ter,

I e23la n what the Ba4ade 3attern s and where t s used. I 3resent the 6ey features of the 3attern.

I 3resent some var at ons on the Ba4ade 3attern.

I relate the Ba4ade 3attern to the CA#@CA8 3roblem.

I l@ve RuBoard

I l@ve RuBoard

%ntrod#cing the acade Pattern


A44ord n- to Jan- of Bour, the ntent of the Ba4ade 3attern s to! 1Prov de a un f ed nterfa4e to a set of nterfa4es n a subsystem. Ba4ade def nes a h -her) level nterfa4e that ma6es the subsystem eas er to use.1
R'S

R'S

Jamma, C., >elm, R., Johnson, R., Al ss des, J., Design Patterns: Ele'ents of 0e sable Object-Oriented &oftware , Read n-, 8ass.!

Add son)"esley, '%%+, 3. '0+.

Bas 4ally, th s s say n- that we need a new way to ntera4t w th a system that s eas er than the 4urrent way, or we need to use the system n a 3art 4ular way <su4h as us n- a .) # draw n- 3ro-ram n a &)# way=. "e 4an bu ld su4h a method of ntera4t on be4ause we only need to use a subset of the system n Duest on.
I l@ve RuBoard

I l@ve RuBoard

0earning the acade Pattern


?n4e, I wor6ed as a 4ontra4tor for a lar-e en- neer n- and manufa4tur n- 4om3any. 8y f rst day on the 5ob, the te4hn 4al lead of the 3ro5e4t was not n. (ow, th s 4l ent d d not want to 3ay me by the hour and not have anyth n- for me to do. They wanted me to be do n- someth n-, even f t was not usefulO >avenLt you had days l 6e th sF So, one of the 3ro5e4t members found someth n- for me to do. She sa d, 1Nou are -o n- to have to learn the CA#@CA8 system we use some t me, so you m -ht as well start now. Start w th these manuals over here.1 Then she too6 me to the set of do4umentat on. I am not ma6 n- th s u3! there were ; feet of manuals for me to read Q ea4h 3a-e 0W 2 '' n4hes and n small 3r ntO Th s was one 4om3le2 systemO -i!ure <-1' Ei!ht &eet o& manuals F one complex s$stemG

(ow, f you and I and say another four or f ve 3eo3le were on a 3ro5e4t that needed to use th s system, not all of us would have to learn the ent re th n-. Rather than waste everyoneLs t me, we would 3robably draw straws, and the loser would have to wr te rout nes that the rest of us would use to nterfa4e w th the system. Th s 3erson would determ ne how I and others on our team were -o n- to use the system and what API would be best for our 3art 4ular needs. She would then 4reate a new 4lass or 4lasses that had the nterfa4e we reDu red. Then, I and the rest of the 3ro-ramm n4ommun ty 4ould use th s new nterfa4e w thout hav n- to learn the ent re 4om3l 4ated system <see B -ure /)&=. -i!ure <-)' ,nsulatin! clients &rom the subs$stem'

(ow, th s a33roa4h only wor6s when us n- a subset of the systemLs 4a3ab l t es or when ntera4t n- w th t n a 3art 4ular way. If everyth n- n the system needs to be used, t s unl 6ely that I 4an 4ome u3 w th a s m3ler nterfa4e <unless the or - nal des -ners d d a 3oor 5ob=. Th s s the Ba4ade 3attern. It enables us to use a 4om3le2 system more eas ly, e ther to use 5ust a subset of the system or use the system n a 3art 4ular way. "e have a 4om3l 4ated system of wh 4h we need to use only a 3art. "e end u3 w th a s m3ler, eas er) to)use system or one that s 4ustom Ged to our needs. 8ost of the wor6 st ll needs to be done by the underly n- system. The Ba4ade 3rov des a 4olle4t on of eas er)to)understand methods. These methods use the underly n- system to m3lement the newly def ned fun4t ons.

"he acade Pattern: :ey eat#res Intent Nou want to s m3l fy how to use an e2 st nsystem. Nou need to def ne your own nterfa4e. Nou need to use only a subset of a 4om3le2 system. ?r you need to ntera4t w th the system n a 3art 4ular way. The Ba4ade 3resents a new nterfa4e for the

Problem

Solut on

also to redu4e the number of ob5e4ts that a 4l ent ob5e4t must deal w th. Bor e2am3le, su33ose I have a Client ob5e4t that must deal w th Data%ases, Models, and &le'ents. The Client must f rst o3en the Data%ase and -et a Model. Then t Duer es the Model to -et an &le'ent. B nally, t as6s the &le'ent for nformat on. It m -ht be a lot eas er to 4reate a Data%ase(acade that 4ould be Duer ed by the Client <see B -ure /),=. -i!ure <-4' -aca#e re#uces the number o& ob*ects &or the client'

Su33ose that n add t on to us n- fun4t ons that are n the system, I also need to 3rov de some new fun4t onal ty. In th s 4ase, I am -o n- beyond a s m3le subset of the system. In th s 4ase, the methods I wr te for the Ba4ade 4lass may be su33lemented by new rout nes for the new fun4t onal ty. Th s s st ll the Ba4ade 3attern, but e23anded w th new fun4t onal ty. The Ba4ade 3attern sets the -eneral a33roa4hP t -ot me started. The Ba4ade 3art of the 3attern s the fa4t that I am 4reat n- a new nterfa4e for the 4l ent to use nstead of the e2 st n- systemLs nterfa4e. I 4an do th s be4ause the Client ob5e4t does not need to use all of the fun4t ons n my or - nal system.

Patterns set a general approach$


A 3attern 5ust sets the -eneral a33roa4h. "hether or not to add new fun4t ons de3ends u3on the s tuat on at hand. Patterns are blue3r nts to -et you startedP they are not 4arved n stone.

The Ba4ade 4an also be used to h de, or en4a3sulate, the system. The Ba4ade 4ould 4onta n the system as 3r vate members of the Ba4ade 4lass. In th s 4ase, the or - nal system would be l n6ed n w th the Ba4ade 4lass, but not 3resented to users of the Ba4ade 4lass. There are a number of reasons to en4a3sulate the system!

!rac* syste' sageE By for4 n- all a44esses to the system to -o throu-h the Ba4ade, I 4an eas ly mon tor system usa-e.

&wap o t syste'sE I may need to 4han-e out systems n the future. By ma6 nthe or - nal system a 3r vate member of the Ba4ade 4lass, I 4an sw t4h out the system for a new one w th m n mal effort. There may st ll be a s -n f 4ant amount of effort reDu red, but at least I w ll only have to 4han-e the 4ode n one 3la4e <the Ba4ade 4lass=.

I l@ve RuBoard

I l@ve RuBoard

+elating the acade Pattern to the CAD4CA. Proble!


Th n6 of the e2am3le above. The Ba4ade 3attern 4ould be useful to hel3

!Slots,

!Holes, et4., use the !Syste'. I w ll do 5ust that n the solut on n Cha3ter '&, 1Solv nthe CA#@CA8 Problem w th Patterns.1

I l@ve RuBoard

I l@ve RuBoard

)#!!ary
The Ba4ade 3attern s so named be4ause t 3uts u3 a new front <a fa4ade= n front of the or - nal system. The Ba4ade 3attern a33l es when

Nou do not need to use all of the fun4t onal ty of a 4om3le2 system and 4an 4reate a new 4lass that 4onta ns all of the rules for a44ess n- that system. If th s s a subset of the or - nal system, as t usually s, the API that you 4reate n new 4lass

should be mu4h s m3ler than the or - nal systemLs API.

Nou want to en4a3sulate or h de the or - nal system.

Nou want to use the fun4t onal ty of the or - nal system and want to add some new fun4t onal ty as well.

The 4ost of wr t n- th s new 4lass s less than the 4ost of everybody learn n- how to use the or - nal system or s less than you would s3end on ma ntenan4e n the future.

I l@ve RuBoard

I l@ve RuBoard

Chapter ;$ "he Adapter Pattern


?verv ew Introdu4 n- the Ada3ter Pattern 9earn n- the Ada3ter Pattern B eld (otes! The Ada3ter Pattern Relat n- the Ada3ter Pattern to the CA#@CA8 Problem Summary Su33lement! C:: Code C2am3le
I l@ve RuBoard

I l@ve RuBoard

Overview
I w ll 4ont nue our study of des -n 3atterns w th the Ada3ter 3attern. The Ada3ter 3attern s a very 4ommon 3attern, and, as you w ll see, t s used w th many other 3atterns.

I e23la n what the Ada3ter 3attern s, where t s used, and how t s m3lemented.

I 3resent the 6ey features of the 3attern.

I use the 3attern to llustrate 3olymor3h sm.

I llustrate how the 789 4an be used at d fferent levels of deta l.

I 3resent some observat ons on the Ada3ter 3attern from my own 3ra4t 4e, n4lud n- a 4om3ar son of the Ada3ter 3attern and the Ba4ade 3attern.

I relate the Ada3ter 3attern to the CA#@CA8 3roblem.

Note! I w ll only show Java 4ode e2am3les n the ma n body of th s 4ha3ter. The eDu valent C:: 4ode e2am3les 4an be found at the end of th s 4ha3ter.

I l@ve RuBoard

I l@ve RuBoard

%ntrod#cing the Adapter Pattern


A44ord n- to the Jan- of Bour, the ntent of the Ada3ter 3attern s to Convert the nterfa4e of a 4lass nto another nterfa4e that the 4l ents e23e4t. Ada3ter lets 4lasses wor6 to-ether that 4ould not otherw se be4ause of n4om3at ble nterfa4es.
R'S

R'S

Jamma, C., >elm, R., Johnson, R., Al ss des, J., Design Patterns: Ele'ents of 0e sable Object-Oriented &oftware , Read n-, 8ass.!

Add son)"esley, '%%+, 3. '0+.

Bas 4ally, th s s say n- that we need a way to 4reate a new nterfa4e for an ob5e4t that does the r -ht stuff but has the wron- nterfa4e.

I l@ve RuBoard

I l@ve RuBoard

0earning the Adapter Pattern


The eas est way to understand the ntent of the Ada3ter 3attern s to loo6 at an e2am3le of

where t s useful. 9etLs say I have been - ven the follow n- reDu rements!

Create 4lasses for 3o nts, l nes, and sDuares that have the behav or 1d s3lay.1

The 4l ent ob5e4ts should not have to 6now whether they a4tually have a 3o nt, a l ne, or a sDuare. They 5ust want to 6now that they have one of these sha3es.

In other words, I want to en4om3ass these s3e4 f 4 sha3es n a h -her)level 4on4e3t that I w ll 4all a 1d s3layable sha3e.1 (ow, as I wor6 throu-h th s s m3le e2am3le, try to ma- ne other s tuat ons that you have run nto that are s m lar, su4h as

Nou have wanted to use a subrout ne or a method that someone else has wr tten be4ause t 3erforms some fun4t on that you need.

Nou 4annot n4or3orate the rout ne d re4tly nto your 3ro-ram.

The nterfa4e or the way of 4all n- the 4ode s not e2a4tly eDu valent to the way that ts related ob5e4ts need to use t.

In other words, althou-h the system w ll "ave 3o nts, l nes, and sDuares, I want the 4l ent ob5e4ts to t"in* I have only s"apes(

Th s allows the 4l ent ob5e4ts to deal w th all these ob5e4ts n the same wayEfreed from hav n- to 3ay attent on to the r d fferen4es.

It also enables me to add d fferent 6 nds of sha3es n the future w thout hav n- to 4han-e the 4l ents <see B -ure *)'=. -i!ure =-1' The ob*ects we have + shoul# all loo0 *ust li0e Hshapes'H

I w ll ma6e use of 3olymor3h smP that s, I w ll have d fferent ob5e4ts n my system, but I

want the 4l ents of these ob5e4ts to ntera4t w th them n a 4ommon way. In th s 4ase, the 4l ent ob5e4t w ll s m3ly tell a 3o nt, l ne, or sDuare to do someth n- su4h as d s3lay tself or und s3lay tself. Ca4h 3o nt, l ne, and sDuare s then res3ons ble for 6now n- the way to 4arry out the behav or that s a33ro3r ate to ts ty3e. To a44om3l sh th s, I w ll 4reate a Shape 4lass and then der ve from t the 4lasses that re3resent 3o nts, l nes, and sDuares <see B -ure *)&=. -i!ure =-)'

)oints8 *ines8 an# Squares are t$pes o& Shape'

B)C

R&S

Th s and all other 4lass d a-rams n th s boo6 use the 7n f ed 8odel n- 9an-ua-e <789= notat on. See Cha3ter &, 1The 789EThe

7n f ed 8odel n- 9an-ua-e,1 for a des4r 3t on of 789 notat on.

B rst, I must s3e4 fy the 3art 4ular behav or that Shapes are -o n- to 3rov de. To a44om3l sh th s, I def ne an nterfa4e n the Shape 4lass for the behav or and then m3lement the behav or a33ro3r ately n ea4h of the der ved 4lasses. The behav ors that a Shape needs to have are!

Set a ShapeLs lo4at on. Jet a ShapeLs lo4at on. # s3lay a Shape. B ll a Shape. Set the 4olor of a Shape.

7nd s3lay a Shape.

I show these n B -ure *).. -i!ure =-3'

)oints8 *ines8 an# Squares showin! metho#s'

Su33ose I am now as6ed to m3lement a 4 r4le, a new 6 nd of Shape <remember, reDu rements always 4han-eO=. To do th s, I w ll want to 4reate a new 4lassE CircleEthat m3lements the sha3e 14 r4le1 and der ve t from the Shape 4lass so that I 4an st ll -et 3olymor3h 4 behav or. (ow, I am fa4ed w th the tas6 of hav n- to wr te the display, fill and undisplay methods for Circle. That 4ould be a 3a n. Bortunately, as I s4out around for an alternat ve <as a -ood 4oder always should=, I d s4over that J ll down the hall has already wr tten a 4lass she 4alled ++Circle that deals w th 4 r4les already <see B -ure *),=. 7nfortunately, she d dnLt as6 me what she should name the methods <and I d d not as6 herO=. She named the methods -i!ure =-4' JillAs

++Circle class'

displayIt fillIt undisplayIt

I 4annot use ++Circle d re4tly be4ause I want to 3reserve 3olymor3h 4 behav or w th

Shape. There are two reasons for th s!


- "ave different na'es and para'eter listsE The method names and 3arameter l sts are d fferent from ShapeLs method names and 3arameter l sts.

- cannot derive itE (ot only must the names be the same, but the 4lass must be der ved from Shape as well.

It s unl 6ely that J ll w ll be w ll n- to let me 4han-e the names of her methods or der ve

++Circle from Shape. To do so would reDu re her to mod fy all of the other ob5e4ts that
are 4urrently us n- ++Circle. Plus, I would st ll be 4on4erned about 4reat nunant 4 3ated s de effe4ts when I mod fy someone elseLs 4ode. I have what I want almost w th n rea4h, but I 4annot use t and I do not want to rewr te t. "hat 4an I doF I 4an ma6e a new 4lass that does der ve from Shape and therefore m3lements ShapeLs nterfa4e but avo ds rewr t n- the 4 r4le m3lementat on n ++Circle <see B -ure *)+=.

-i!ure =-;' The A#apter pattern2

Circle HwrapsH ++Circle.

Class Circle der ves from Shape.

Circle 4onta ns ++Circle. Circle 3asses reDuests made to the Circle ob5e4t on throu-h to the ++Circle
ob5e4t.

The d amond at the end of the l ne between Circle and ++Circle n B -ure *)+ nd 4ates that Circle 4onta ns an ++Circle. "hen a Circle ob5e4t s nstant ated, t must nstant ate a 4orres3ond n- ++Circle ob5e4t. Anyth n- the Circle ob5e4t s told to do w ll -et 3assed on to the ++Circle ob5e4t. If th s s done 4ons stently, and f the ++Circle ob5e4t has the 4om3lete fun4t onal ty the Circle ob5e4t needs <I w ll d s4uss shortly what ha33ens f th s s not the 4ase=, the Circle ob5e4t w ll be able to man fest ts behav or by lett n- the ++Circle ob5e4t do the 5ob. An e2am3le of wra33 n- s shown n C2am3le *)'. Example =-1 Java Co#e -ra!ments2 ,mplementin! the A#apter Pattern

class Circle extends 7hape ; %%% private 33Circle pxc, %%% pu!lic Circle () ; pxc* new 33Circle(), >

void pu!lic display() ; pxc%display#t(), > >


7s n- the Ada3ter 3attern allowed me to 4ont nue us n- 3olymor3h sm w th Shape. In other words, the 4l ent ob5e4ts of Shape do not 6now what ty3es of sha3es are a4tually 3resent. Th s s also an e2am3le of our new th n6 n- of en4a3sulat on as wellEthe 4lass

Shape en4a3sulates the s3e4 f 4 sha3es 3resent. The Ada3ter 3attern s most 4ommonly
used to allow for 3olymor3h sm. As we shall see n later 4ha3ters, t s often used to allow for 3olymor3h sm reDu red by other des -n 3atterns.

"he Adapter Pattern: :ey eat#res Intent Problem 8at4h an e2 st n- ob5e4t beyond 4ontrol to a 3art 4ular nterfa4e. your

A system has the r -ht data and behav or but the wron- nterfa4e. Ty3 4ally used when you have to ma6e someth n- a der vat ve of an abstra4t 4lass we are def n n- or already have. The Ada3ter 3rov des a wra33er w th the des red nterfa4e.

Solut on

Part 4 3ants and The Adapter ada3ts the nterfa4e of an Adaptee to mat4h that of the AdapterLs Collaborators Target <the 4lass t der ves from=. Th s allows the Client to use the Adaptee as f t were a ty3e of Target. ConseDuen4es The Ada3ter 3attern allows for 3ree2 st nob5e4ts to f t nto new 4lass stru4tures w thout be n- l m ted by the r nterfa4es. Conta n the e2 st n- 4lass n another 4lass. >ave the 4onta n n- 4lass mat4h the reDu red nterfa4e and 4all the methods of the 4onta ned 4lass. Pa-es '.%T'+$.

Im3lementat on

JoB Referen4e

-i!ure =-<' (tan#ar#8 simpli&ie# view o& the A#apter pattern'

I l@ve RuBoard

I l@ve RuBoard

ield Notes: "he Adapter Pattern


?ften, I w ll be n a s tuat on s m lar to the one above, but the ob5e4t be n- ada3ted does not do all the th n-s I need. In th s 4ase, I 4an st ll use the Ada3ter 3attern, but t s not su4h a 3erfe4t f t. In th s 4ase,

Those fun4t ons that are m3lemented n the e2 st n- 4lass 4an be ada3ted.

Those fun4t ons that are not 3resent 4an be m3lemented n the wra33 n- ob5e4t.

Th s does not - ve me Du te the same benef t, but at least I do not have to m3lement all of the reDu red fun4t onal ty. The Ada3ter 3attern frees me from worry n- about the nterfa4es of e2 st n- 4lasses when I am do n- a des -n. If I have a 4lass that does what I need, at least 4on4e3tually, then I 6now that I 4an always use the Ada3ter 3attern to - ve t the 4orre4t nterfa4e. Th s w ll be4ome more m3ortant as you learn a few more 3atterns. 8any 3atterns reDu re 4erta n 4lasses to der ve from the same 4lass. If there are 3ree2 st n- 4lasses, the Ada3ter 3attern 4an be used to ada3t t to the a33ro3r ate abstra4t 4lass <as Circle ada3ted

++Circle to Shape=.

There are a4tually two ty3es of Ada3ter 3atterns!

Object Adapter patternE The Ada3ter 3attern I have been us n- s 4alled an ?b5e4t Ada3ter be4ause t rel es on one ob5e4t <the ada3t n- ob5e4t= 4onta n n- another <the ada3ted ob5e4t=.

Class Adapter patternE Another way to m3lement the Ada3ter 3attern s w th mult 3le nher tan4e. In th s 4ase, t s 4alled a Class Ada3ter 3attern.

The de4 s on of wh 4h Ada3ter 3attern to use s based on the d fferent for4es at wor6 n the 3roblem doma n. At a 4on4e3tual level, I may -nore the d st n4t onP however, when t 4omes t me to m3lement t, I need to 4ons der more of the for4es nvolved.
R.S

R.S

Bor hel3 n de4 d n- between ?b5e4t Ada3ter and Class Ada3ter, see 3a-es ',&T',, n the Jan- of Bour boo6.

In my 4lasses on des -n 3atterns, someone almost always states that t sounds as f both the Ada3ter 3attern and the Ba4ade 3attern are the same. In both 4ases there s a 3ree2 st n- 4lass <or 4lasses= that does have the nterfa4e that s needed. In both 4ases, I 4reate a new ob5e4t that has the des red nterfa4e <see B -ure *)*=. -i!ure =-=' A Client ob*ect usin! another8 preexistin! ob*ect with the wron! inter&ace'

"ra33ers and ob5e4t wra33ers are terms that you hear a lot about. It s 3o3ular to th n6 about wra33 n- le-a4y systems w th ob5e4ts to ma6e them eas er to use. At th s h -h v ew, the Ba4ade and the Ada3ter 3atterns do seem s m lar. They are both wra33ers. But they are d fferent 6 nds of wra33ers. Nou need to understand the d fferen4es, wh 4h 4an be subtle. B nd n- and understand n- these more subtle d fferen4es - ves ns -ht nto a 3atternLs 3ro3ert es. 9etLs loo6 at some d fferent for4es nvolved w th these 3atterns <see Table *)'=.

Table =-1' Comparin! the -aca#e Pattern with the A#apter Pattern

-aca#e Are there 3ree2 st n- 4lassesF Is there an nterfa4e we must des -n toF #oes an ob5e4t need to behave 3olymor3h 4allyF Is a s m3ler nterfa4e neededF
Table *)' tells us the follow n-!

A#apter Nes Nes Probably (o

Nes (o (o Nes

In both the Ba4ade and Ada3ter 3attern I have 3ree2 st n- 4lasses. In the Ba4ade, however, I do not have an nterfa4e I must des -n to, as I do n the Ada3ter 3attern.

I am not nterested n 3olymor3h 4 behav or n the Ba4ade, wh le n the Ada3ter, I 3robably am. <There are t mes when we 5ust need to des -n to a 3art 4ular API and therefore must use an Ada3ter. In th s 4ase, 3olymor3h sm may not be an ssueE thatLs why I say 13robably1=.

In the 4ase of the Ba4ade 3attern, the mot vat on s to s m3l fy the nterfa4e. " th the Ada3ter, wh le s m3ler s better, I am try n- to des -n to an e2 st n- nterfa4e and 4annot s m3l fy th n-s even f a s m3ler nterfa4e were otherw se 3oss ble.

Somet mes 3eo3le draw the 4on4lus on that another d fferen4e between the Ba4ade and the Ada3ter 3attern s that the Ba4ade h des mult 3le 4lasses beh nd t wh le the Ada3ter only h des one. "h le th s s often true, t s not 3art of the 3attern. It s 3oss ble that a Ba4ade 4ould be used n front of a very 4om3le2 ob5e4t wh le an Ada3ter wra33ed several small ob5e4ts that between them m3lemented the des red fun4t on. 1ottom line2 A Ba4ade si'plifies an nterfa4e wh le an Ada3ter converts the nterfa4e nto a 3ree2 st n- nterfa4e.
I l@ve RuBoard

I l@ve RuBoard

+elating the Adapter Pattern to the CAD4CA. Proble!


In the CA#@CA8 3roblem <Cha3ter ., 1A Problem That Cr es ?ut for Ble2 ble Code1=, the

features n the A& model w ll be re3resented by $$G(eature ob5e4ts. 7nfortunately, these ob5e4ts do not have the 4orre4t nterfa4e <from my 3ers3e4t ve= be4ause I d d not des -n them. I 4annot ma6e them der ve from the (eature 4lasses, yet, when I use the A& system, they would do our 5ob 3erfe4tly. In th s 4ase, the o3t on of wr t n- new 4lasses to m3lement th s fun4t on s not even 3resentEI must 4ommun 4ate w th the $$G(eature ob5e4ts. The eas est way to do th s s w th the Ada3ter 3attern.

I l@ve RuBoard

I l@ve RuBoard

)#!!ary
The Ada3ter 3attern s a very useful 3attern that 4onverts the nterfa4e of a 4lass <or 4lasses= nto another nterfa4e, wh 4h we need the 4lass to have. It s m3lemented by 4reat n- a new 4lass w th the des red nterfa4e and then wra33 n- the or - nal 4lass methods to effe4t vely 4onta n the ada3ted ob5e4t.
I l@ve RuBoard

I l@ve RuBoard

)#pple!ent: C77 Code Exa!ple


Example =-) CDD Co#e -ra!ments2 ,mplementin! the A#apter Pattern

class Circle : pu!lic 7hape ; % % % private: 33Circle @pxc, % > Circle::Circle () ; % > void Circle::display () ; % % pxc* new 33Circle, % %

pxc Adisplay#t(), >


I l@ve RuBoard

I l@ve RuBoard

Chapter <$ Expanding O#r =ori>ons


?verv ew ?b5e4ts! the Trad t onal A ew and the (ew A ew Cn4a3sulat on! the Trad t onal A ew and the (ew A ew B nd "hat Is Aary n- and Cn4a3sulate It Commonal ty@Aar ab l ty and Abstra4t Classes Summary
I l@ve RuBoard

I l@ve RuBoard

Overview
In 3rev ous 4ha3ters, I d s4ussed three fundamental 4on4e3ts of ob5e4t)or ented des -n! ob5e4ts, en4a3sulat on, and abstra4t 4lasses. >ow a des -ner v ews these 4on4e3ts s m3ortant. The trad t onal ways are s m3ly too l m t n-. In th s 4ha3ter I ste3 ba46 and refle4t on to3 4s d s4ussed earl er n the boo6. 8y ntent s to des4r be a new way of see nob5e4t)or ented des -n, wh 4h 4omes from the 3ers3e4t ve that des -n 3atterns 4reate. In th s 4ha3ter,

I 4om3are and 4ontrast the trad t onal way of loo6 n- at ob5e4tsEas a bundle of data and methodsEw th the new wayEas th n-s w th res3ons b l t es.

I 4om3are and 4ontrast the trad t onal way of loo6 n- at en4a3sulat onEas h d ndataEw th the new wayEas the ab l ty to h de anyth n-. Cs3e4 ally m3ortant s to see that en4a3sulat on 4an be used to 4onta n var at on n behav or.

I 4om3are and 4ontrast the trad t onal way of us n- nher tan4eEfor s3e4 al Gat on

and reuseEw th the new wayEas a method of 4lass fy n- ob5e4ts.

The new v ew3o nts allow for 4onta n n- var at on of behav ors n ob5e4ts.

I show how the 4on4e3tual, s3e4 f 4at on, and m3lementat on 3ers3e4t ves relate to an abstra4t 4lass and ts der ved 4lasses.

Perha3s th s new 3ers3e4t ve s not all that or - nal. I bel eve that th s 3ers3e4t ve s one that many develo3ers of the des -n 3atterns held when they develo3ed what ended u3 be n- 4alled a 3attern. Certa nly, t s a 3ers3e4t ve that s 4ons stent w th the wr t n-s of Chr sto3her Ale2ander, J m Co3l en, and the Jan- of Bour. "h le t may not be or - nal, t has also not been e23ressed n Du te the way I do n th s 4ha3ter and n th s boo6. I have had to d st ll th s way of loo6 n- at 3atterns from the way des -n 3atterns behave and how they have been des4r bed by others. "hen I 4all t a new 3ers3e4t ve, what I mean s that t s most l 6ely a new way for most develo3ers to v ew ob5e4t or entat on. It was 4erta nly new to me when I was learn ndes -n 3atterns for the f rst t me.

I l@ve RuBoard

I l@ve RuBoard

Objects: the "raditional 5iew and the New 5iew


The trad t onal v ew of ob5e4ts s that they are data w th methods. ?ne of my tea4hers 4alled them 1smart data.1 It s 5ust a ste3 u3 from a database. Th s v ew 4omes from loo6 n- at ob5e4ts from an m3lementat on 3ers3e4t ve. "h le th s def n t on s a44urate, as e23la ned n Cha3ter ', 1The ?b5e4t)?r ented Parad -m,1 t s based on the m3lementat on 3ers3e4t ve. A more useful def n t on s one based on the 4on4e3tual 3ers3e4t veEan ob5e4t s an ent ty that has res3ons b l t es. These res3ons b l t es - ve the ob5e4t ts behav or. Somet mes, I also th n6 of an ob5e4t as an ent ty that has s3e4 f 4 behav or. Th s s a better def n t on be4ause t hel3s to fo4us on what the ob5e4ts are su33osed to do#

not s m3ly on how to m3lement them. Th s enables me to bu ld the software n two ste3s! '. &. 8a6e a 3rel m nary des -n w thout worry n- about all of the deta ls nvolved. Im3lement the des -n a4h eved.

7lt mately, th s 3ers3e4t ve allows for better ob5e4t sele4t on and def n t on < n a sense, the ma n 3o nt of des -n anyway=. ?b5e4t def n t on s more fle2 bleP by fo4us n- on what an ob5e4t does, nher tan4e allows us to use d fferent, s3e4 f 4 behav ors when needed. A fo4us on m3lementat on may a4h eve th s, but fle2 b l ty ty3 4ally 4omes at a h -her 3r 4e. It s eas er to th n6 n terms of res3ons b l t es be4ause that hel3s to def ne the ob5e4tLs 3ubl 4 nterfa4e. If an ob5e4t has a res3ons b l ty, there must be some way to as6 t to 3erform ts res3ons b l ty. >owever, t does not m3ly anyth n- about what s inside the ob5e4t. The nformat on for wh 4h the ob5e4t s res3ons ble may not even be ns de the ob5e4t tself. Bor e2am3le, su33ose I have a Shape ob5e4t and ts res3ons b l t es are

To 6now where t s lo4ated To be able to draw tself on a d s3lay

To be able to remove tself from a d s3lay

These res3ons b l t es m3ly that a 3art 4ular set of method 4alls must e2 st!

get<ocation( %%% ) draw7hape( %%% ) un$raw7hape( %%% )

There s no m3l 4at on about what s ns de of Shape. I only 4are that Shape s res3ons ble for ts own behav ors. It may have attr butes ns de t or t may have methods that 4al4ulate or even refer to other ob5e4ts. Thus, Shape m -ht 4onta n attr butes about ts lo4at on or t m -ht refer to another database ob5e4t to -et ts lo4at on. Th s - ves you the fle2 b l ty you need to meet your model n- ob5e4t ves.

Interest n-ly, you w ll f nd that fo4us n- on mot vat on rather than on m3lementat on s a re4urr n- theme n des -n 3atterns. 9oo6 at ob5e4ts th s way. 8a6e t your bas 4 v ew3o nt for ob5e4ts. If you do, you w ll have su3er or des -ns.
I l@ve RuBoard

I l@ve RuBoard

Encaps#lation: the "raditional 5iew and the New 5iew


In my 4lasses on 3attern)or ented des -n, I often as6 my students, 1"ho has heard en4a3sulat on def ned as Ldata h d n-LF1 Almost everyone ra ses h s or her hand. Then I 3ro4eed to tell a story about my umbrella. ;ee3 n m nd that I l ve n Seattle, wh 4h has a re3utat on for be n- wetter than t s, but s st ll a 3retty wet 3la4e n the fall, w nter, and s3r n-. >ere, umbrellas and hooded 4oats are 3ersonal fr endsO 9et me tell you about my -reat umbrella. It s lar-e enou-h to -et ntoO In fa4t, three or four other 3eo3le 4an -et n t w th me. "h le we are n t, stay n- out of the ra n, I 4an move t from one 3la4e to another. It has a stereo system to 6ee3 me enterta ned wh le I stay dry. AmaG n-ly enou-h, t 4an also 4ond t on the a r to ma6e t warmer or 4older. It s one 4ool umbrella. 8y umbrella s 4onven ent. It s ts there wa t n- for me. It has wheels on t so that I do not have to 4arry t around. I donLt even have to 3ush t be4ause t 4an 3ro3el tself. Somet mes, I w ll o3en the to3 of my umbrella to let n the sun. <"hy I am us n- my umbrella when t s sunny outs de s beyond meO= In Seattle, there are hundreds of thousands of these umbrellas n all 6 nds of 4olors. 8ost 3eo3le 4all them cars( But I th n6 of m ne as an umbrella be4ause an umbrella s someth n- you use to 6ee3 out of the ra n. 8any t mes, wh le I am wa t n- outs de for someone to meet me, I s t n my 1umbrella1 to stay dryO ?f 4ourse, a 4ar snLt really an umbrella. Nes, you 4an use t to say out of the ra n, but that

s too l m ted a v ew of a 4ar. In the same way, en4a3sulat on snLt 5ust for h d n- data. That s too l m ted a v ew of en4a3sulat on. To th n6 of t that way 4onstra ns my m nd when I des -n. Cn4a3sulat on should be thou-ht of as 1any 6 nd of h d n-.1 In other words, t can h de data. But t 4an also h de m3lementat ons, der ved 4lasses, or any number of th n-s. Cons der the d a-ram shown n B -ure 0)'. Nou m -ht re4olle4t th s d a-ram from Cha3ter *, 1The Ada3ter Pattern.1 -i!ure >-1' A#aptin!

++Circle with Circle'

B -ure 0)' shows many 6 nds of en4a3sulat on!

Encaps lation of dataE The data n )oint, *ine, Square, and Circle are h dden from everyth n- else.

Encaps lation of 'et"odsE Bor e2am3le, CircleLs set<ocation. Encaps lation of s bclassesE Cl ents of Shape do not see )oints, *ines,

Squares, or Circles.
Encaps lation of ot"er objectsE (oth n- but Circle s aware of ##Circle.

?ne ty3e of en4a3sulat on s thus a4h eved when there s an abstra4t 4lass that behaves 3olymor3h 4ally w thout the 4l ent of the abstra4t 4lass 6now n- what 6 nd of der ved 4lass a4tually s 3resent. Burthermore, ada3t n- nterfa4es h des what s beh nd the ada3t nob5e4t.

The advanta-e of loo6 n- at en4a3sulat on th s way s that t - ves me a better way to s3l t u3 <de4om3ose= my 3ro-rams. The en4a3sulat n- layers be4ome the nterfa4es I des -n to. By en4a3sulat n- d fferent 6 nds of Shapes, I 4an add new ones w thout 4han- n- any of the 4l ent 3ro-rams us n- them. By en4a3sulat n- ++Circle beh nd Circle, I 4an 4han-e th s m3lementat on n the future f I 4hoose to or need to. "hen the ob5e4t)or ented 3arad -m was f rst 3resented, reuse of 4lasses was touted as be n- one of ts b - benef ts. Th s reuse was usually a4h eved by 4reat n- 4lasses and then der v n- new 4lasses based on these base 4lasses. >en4e the term speciali7ed 4lasses for those sub4lasses that were der ved from other 4lasses <wh 4h were 4alled generali7ed 4lasses=. I am not ar-u n- w th the a44ura4y of th s, rather I am 3ro3os n- what I bel eve to be a more 3owerful way of us n- nher tan4e. In the e2am3le above, I 4an do my des -n based on d fferent s3e4 al ty3es of Shapes <that s, )oints, *ines, Squares and Circles=. >owever, th s w ll 3robably not have me h de these s3e4 al 4ases when I th n6 about us n-

ShapesEI w ll 3robably ta6e advanta-e of the 6nowled-e of these 4on4rete 4lasses.


If, however, I th n6 about Shapes as a way of 4lass fy n- )oints, *ines, Squares and

Circles, I 4an more eas ly th n6 about them as a whole. Th s w ll ma6e t more l 6ely I
w ll des -n to an nterfa4e <Shapes=. It also means f I -et a new Shape, I w ll be less l 6ely to have des -ned myself nto a d ff 4ult ma ntenan4e 3os t on <be4ause no 4l ent ob5e4t 6nows what 6 nd of Shape t s deal n- w th anyway=.

I l@ve RuBoard

I l@ve RuBoard

ind 1hat %s 5arying and Encaps#late %t


In Design Patterns: Ele'ents of 0e sable Object-Oriented &oftware , the Jan- of Bour su--ests the follow n-! Consider w"at s"o ld be variable in yo r design( Th s a33roa4h s the o33os te of fo4us non the 4ause of redes -n. Instead of 4ons der n- what m -ht force a 4han-e to a des -n,

4ons der what you want to be able to 4han-e w thout redes -n. The fo4us here s on encaps lating t"e concept t"at varies# a theme of many des -n 3atterns.
R'S

R'S

Jamma,C.,>elm,R.,Johnson,R.,Al ss des,J., Design Patterns:Ele'ents of 0e sable Object-Oriented &oftware , Read n-,8ass.!Add son)

"esley,'%%+,3.&%.

R'S

Jamma,C.,>elm,R.,Johnson,R.,Al ss des,J., Design Patterns:Ele'ents of 0e sable Object-Oriented &oftware , Read n-,8ass.!Add son)

"esley,'%%+,3.&%.

?r, as I l 6e to re3hrase t, 1B nd what var es and en4a3sulate t.1 These statements seem odd f you only th n6 about en4a3sulat on as data)h d n-. They are mu4h more sens ble when you th n6 of en4a3sulat on as h d n- 4lasses us n- abstra4t 4lasses. 7s n- 4om3os t on of a referen4e to an abstra4t 4lass h des the var at ons. In effe4t, many des -n 3atterns use en4a3sulat on to 4reate layers between ob5e4tsE enabl n- the des -ner to 4han-e th n-s on d fferent s des of the layers w thout adversely affe4t n- the other s de. Th s 3romotes loose)4ou3l n- between the s des. Th s way of th n6 n- s very m3ortant n the Br d-e 3attern, wh 4h w ll be d s4ussed n Cha3ter %, 1The Br d-e Pattern.1 >owever, before 3ro4eed n-, I want to show a b as n des -n that many develo3ers have. Su33ose I am wor6 n- on a 3ro5e4t that models d fferent 4hara4ter st 4s of an mals. 8y reDu rements are the follow n-!

Ca4h ty3e of an mal 4an have a d fferent number of le-s.

) An mal ob5e4ts must be able to remember and retr eve th s nformat on.

Ca4h ty3e of an mal 4an have a d fferent ty3e of movement.

) An mal ob5e4ts must be able to return how lon- t w ll ta6e to move from one 3la4e to another - ven a s3e4 f ed ty3e of terra n. A ty3 4al a33roa4h of handl n- the var at on n the number of le-s would be to have a data member 4onta n n- th s value and hav n- methods to set and -et t. >owever, one ty3 4ally

ta6es a d fferent a33roa4h to handl n- var at on n behav or. Su33ose there are two d fferent methods for mov n-! wal6 n- and fly n-. These reDu rements need two d fferent 3 e4es of 4ode! one to handle wal6 n- and one to handle fly n-P a s m3le var able wonLt wor6. J ven that I have two d fferent methods, I seem to be fa4ed w th a 4ho 4e of a33roa4h!

>av n- a data member that tells me what ty3e of movement my ob5e4t has. >av n- two d fferent ty3es of Ani'als <both der ved from the base Ani'al 4lass= Eone for wal6 n- and one for fly n-.

7nfortunately, both of these a33roa4hes have 3roblems!

!ig"t co plingE The f rst a33roa4h <us n- a fla- w th 3resumably a sw t4h based on t= may lead to t -ht 4ou3l n- f the fla- starts m3ly n- other d fferen4es. In any event, the 4ode w ll l 6ely be rather messy.

!oo 'any detailsE The se4ond a33roa4h reDu res that I also mana-e the subty3e of Ani'al. And I 4annot handle Ani'als that 4an both wal6 and fly.

A th rd 3oss b l ty e2 sts! have the Ani'al 4lass 4onta n an ob5e4t that has the a33ro3r ate movement behav or. I show th s n B -ure 0)&. -i!ure >-)'

Ani'al containin! Ani'alMo,e'ent ob*ect'

Th s may seem l 6e over6 ll at f rst. >owever, tLs noth n- more than an Ani'al 4onta n nan ob5e4t that 4onta ns the movement behav or of the Ani'al. Th s s very analo-ous to hav n- a member 4onta n n- the number of le-sE n wh 4h 4ase an ntr ns 4 ty3e ob5e4t s

4onta n n- the number of le-s. I sus3e4t these a33ear more d fferent n 4on4e3t than they really are, be4ause B -ures 0)& and 0). a33ear to be d fferent. -i!ure >-3' (howin! containment as a member'

8any develo3ers tend to th n6 that one ob5e4t 4onta n n- another ob5e4t s nherently d fferent from an ob5e4t hav n- a mere data member. But data members that a33ear not to be ob5e4ts < nte-ers and doubles, for e2am3le= really are. In ob5e4t)or ented 3ro-ramm n-, everyt"ing s an ob5e4t, even these ntr ns 4 data ty3es, whose behav or s ar thmet 4. 7s n- ob5e4ts to 4onta n var at on n attr butes and us n- ob5e4ts to 4onta n var at on n behav or are very s m larP th s 4an be most eas ly shown throu-h an e2am3le. 9etLs say I am wr t n- a 3o nt)of)sale system. In th s system, there s a sales re4e 3t. ?n th s sales re4e 3t there s a total. I 4ould start out by ma6 n- th s total be a ty3e dou%le. >owever, f I am deal n- w th an nternat onal a33l 4at on, I Du 46ly real Ge I need to handle monetary 4onvers ons, and so forth. I m -ht therefore ma6e a Money 4lass that 4onta ns an amount and a 4urren4y. Total 4an now be of ty3e Money. 7s n- the Money 4lass th s way a33ears to be us n- an ob5e4t 5ust to 4onta n more data. >owever, when I need to 4onvert Money from one 4urren4y to the ne2t, t s the Money ob5e4t tself that should do the 4onvers on, be4ause ob5e4ts should be res3ons ble for themselves. At f rst t may a33ear that th s 4onvers on 4an be done by s m3ly hav nanother data member that s3e4 f es what the 4onvers on fa4tor s. >owever, t may be more 4om3l 4ated than th s. Bor e2am3le, 3erha3s I need to be able to 4onvert 4urren4y based on 3ast dates. In that 4ase, f I add behav ors to the Money or

Currency 4lasses I am essent ally add n- d fferent behav ors to the SalesReceipt as
well, based u3on wh 4h Money ob5e4ts <and therefore wh 4h Currency ob5e4ts= t 4onta ns. I w ll demonstrate th s strate-y of us n- 4onta ned ob5e4ts to 3erform reDu red behav or n

the ne2t few des -n 3atterns.

I l@ve RuBoard

I l@ve RuBoard

Co!!onality45ariability and Abstract Classes


Cons der B -ure 0),. It shows the relat onsh 3 between -i!ure >-4' The relationship between commonalit$?variabilit$ anal$sis8 perspectives8 an# abstract classes'

Commonal ty@var ab l ty analys s The 4on4e3tual, s3e4 f 4at on, and m3lementat on 3ers3e4t ves

An abstra4t 4lass, ts nterfa4e, and ts der ved 4lasses

As you 4an see n B -ure 0),, 4ommonal ty analys s relates to the 4on4e3tual v ew of the 3roblem doma n and var ab l ty analys s relates to the m3lementat on, that s, to s3e4 f 4 4ases. The s3e4 f 4at on 3ers3e4t ve l es n the m ddle. Both 4ommonal ty and var ab l ty are nvolved n th s 3ers3e4t ve. The s3e4 f 4at on des4r bes how to 4ommun 4ate w th a set of ob5e4ts that are 4on4e3tually s m lar. Ca4h of these ob5e4ts re3resents a var at on of the 4ommon 4on4e3t. Th s s3e4 f 4at on be4omes an abstra4t 4lass or an nterfa4e at the m3lementat on level.

In the new 3ers3e4t ve of ob5e4t)or ented des -n, I 4an now say the follow n-!

"appin! with Abstract Classes

Discussion

Abstra4t 4lass the An abstra4t 4lass re3resents the 4ore 4on4e3t that 4entral b nd n- 4on4e3t b nds to-ether all of the der vat ves of the 4lass. Th s 4ore 4on4e3t s what def nes the 4ommonal ty. Commonal ty wh 4h The 4ommonal t es def ne the abstra4t 4lasses I need abstra4t 4lasses to use to use. Aar at ons der vat on of abstra4t 4lass The var at ons dent f ed wit"in that 4ommonal ty an be4ome der vat ons of the abstra4t 4lasses.

S3e4 f 4at on The nterfa4e for these 4lasses 4orres3onds to the nterfa4e for abstra4t s3e4 f 4at on level. 4lass
Th s s m3l f es the des -n 3ro4ess of the 4lasses nto a two)ste3 3ro4edure!

6hen De&inin! + An abstra4t <4ommonal ty= #er ved 4lasses

7ou "ust As0 7oursel&+ to handle all of the

4lass "hat interface s needed responsibilities of th s 4lassF

J ven th s 3art 4ular m3lementat on <th s var at on=, how 4an I m3lement t w th the - ven s3e4 f 4at onF

The relat onsh 3 between the s3e4 f 4at on 3ers3e4t ve and the 4on4e3tual 3ers3e4t ve s th s! -t identifies t"e interface - need to se to "andle all of t"e cases of t"is concept <t"at is# t"e co''onality=( The relat onsh 3 between the s3e4 f 4at on 3ers3e4t ve and the m3lementat on 3ers3e4t ve s th s! +iven t"is specification# "ow can - i'ple'ent t"is partic lar case <t"is variation=5

I l@ve RuBoard

I l@ve RuBoard

)#!!ary
The trad t onal way of th n6 n- about ob5e4ts, en4a3sulat on, and nher tan4e s very

l m t n-. Cn4a3sulat on e2 sts for so mu4h more than s m3ly h d n- data. By e23and n- the def n t on to n4lude any 6 nd of h d n-, I 4an use en4a3sulat on to 4reate layers between ob5e4tsEenabl n- me to 4han-e th n-s on one s de of a layer w thout adversely affe4t nthe other s de. Inher tan4e s better used as a method of 4ons stently deal n- w th d fferent 4on4rete 4lasses that are 4on4e3tually the same rather than as a means of s3e4 al Gat on. The 4on4e3t of us n- ob5e4ts to hold var at ons n behav or s not unl 6e the 3ra4t 4e of us n- data members to hold var at ons n data. Both allow for the en4a3sulat on <and therefore e2tens on= of the data@behav or be n- 4onta ned.
I l@ve RuBoard

I l@ve RuBoard

Chapter ?$ "he 'ridge Pattern

?verv ew Introdu4 n- the Br d-e Pattern 9earn n- the Br d-e Pattern! An C2am3le An ?bservat on About 7s n- #es -n Patterns 9earn n- the Br d-e Pattern! #er v n- It The Br d-e Pattern n Retros3e4t B eld (otes! 7s n- the Br d-e Pattern Summary Su33lement! C:: Code C2am3les
I l@ve RuBoard

I l@ve RuBoard

Overview
I w ll 4ont nue our study of des -n 3atterns w th the Br d-e 3attern. The Br d-e 3attern s Du te a b t more 4om3le2 than the other 3atterns you 5ust learnedP t s also mu4h more useful. In th s 4ha3ter,

I der ve the Br d-e 3attern by wor6 n- throu-h an e2am3le. I w ll -o nto -reat deta l to hel3 you learn th s 3attern.

I 3resent the 6ey features of the 3attern.

I 3resent some observat ons on the Br d-e 3attern from my own 3ra4t 4e.

I l@ve RuBoard

I l@ve RuBoard

%ntrod#cing the 'ridge Pattern


A44ord n- to the Jan- of Bour, the ntent of the Br d-e 3attern s to 1#e)4ou3le an abstra4t on from ts m3lementat on so that the two 4an vary nde3endently.1
R'S

R'S

Jamma, C., >elm, R., Johnson, R., Al ss des, J., Design Patterns: Ele'ents of 0e sable Object-Oriented &oftware , Read n-, 8ass.!

Add son)"esley, '%%+, 3. '+'.

I remember e2a4tly what my f rst thou-hts were when I read th s! 4 "5 And then, 4ow co'e - nderstand every word in t"is sentence b t - "ave no idea w"at it 'eans5: I 6new that

De-co ple means to have th n-s behave nde3endently from ea4h other or at least e23l 4 tly state what the relat onsh 3 s, and

Abstraction s how d fferent th n-s are related to ea4h other 4on4e3tually.

And I thou-ht that i'ple'entations were the way to bu ld the abstra4t onsP but I was 4onfused about how I was su33osed to se3arate abstra4t ons from the s3e4 f 4 ways that m3lemented them. It turns out that mu4h of my 4onfus on was due to m sunderstand n- what m3lementat ons meant. -'ple'entations here means the ob5e4ts that the abstra4t 4lass and ts der vat ons use to m3lement themselves w th <not the der vat ons of the abstra4t 4lass, wh 4h are 4alled 4on4rete 4lasses=. But to be honest, even f I had understood t 3ro3erly, I am not sure how mu4h t would have hel3ed. The 4on4e3t e23ressed n th s senten4e s 5ust hard to understand at f rst. If you are also 4onfused about the Br d-e 3attern at th s 3o nt, that s o6ay. If you understand the stated ntent, then you are that mu4h ahead. The Br d-e 3attern s one of the tou-hest 3atterns to understand n 3art be4ause t s so 3owerful and a33l es to so many s tuat ons. Also, t -oes a-a nst a 4ommon tenden4y to handle s3e4 al 4ases w th nher tan4e. >owever, t s also an e24ellent e2am3le of follow ntwo of the mandates of the des -n 3attern 4ommun ty! 1f nd what var es and en4a3sulate t1 and 1favor ob5e4t 4om3os t on over 4lass nher tan4e1 <as you w ll see=.
I l@ve RuBoard

I l@ve RuBoard

0earning the 'ridge Pattern: An Exa!ple


To learn the th n6 n- beh nd the Br d-e 3attern and what t s try n- to do, I w ll wor6 throu-h an e2am3le from s4rat4h. Start n- w th reDu rements, I w ll der ve the 3attern and then see how to a33ly t. Perha3s th s e2am3le w ll seem bas 4. But loo6 at the 4on4e3ts d s4ussed n th s e2am3le and then try to th n6 of s tuat ons that you have en4ountered that are s m lar, hav n-

Aar at ons n abstra4t ons of a 4on4e3t, and

Aar at ons n how these 4on4e3ts are m3lemented.

Nou w ll see that th s e2am3le has many s m lar t es to the CA#@CA8 3roblem d s4ussed

earl er. But rather than - ve you all the reDu rements u3 front, I am -o n- to - ve them a l ttle at a t me, 5ust as they were - ven to me. Nou 4anLt always see the var at ons at the be- nn n- of the 3roblem. 1ottom line2 #ur n- reDu rements def n t on, e23lore for var at ons early and oftenO Su33ose I have been - ven the tas6 of wr t n- a 3ro-ram that w ll draw re4tan-les w th e ther of two draw n- 3ro-rams. I have been told that when I nstant ate a re4tan-le, I w ll 6now whether I should use draw n- 3ro-ram ' <D)!= or draw n- 3ro-ram & <D)"=. The re4tan-les are def ned as two 3a rs of 3o nts, as re3resented n B -ure %)'. The d fferen4es between the draw n- 3ro-rams are summar Ged n Table %)'. -i!ure I-1' Positionin! the rectan!le'

Table I-1' Di&&erent Drawin! Pro!rams

DP1 7sed to draw drawBaBline( x?, y?) a l ne 7sed to draw drawBaBcircle( a 4 r4le

DP)

x(,

y(, drawline( y(, y?)

x(,

x?,

x, y, r) drawcircle( x, y, r)

8y 4ustomer told me that the 4olle4t on <the 4l ent of the re4tan-les= does not want to worry about what ty3e of draw n- 3ro-ram t should use. It o44urs to me that s n4e the re4tan-les are told what draw n- 3ro-ram to use when nstant ated, I 4an have two d fferent 6 nds of re4tan-le ob5e4ts! one that uses D)! and one that uses D)". Ca4h would have a draw method but would m3lement t d fferently. I show th s n B -ure %)&.

-i!ure I-)' Desi!n &or rectan!les an# #rawin! pro!rams . D)! an#

D)"/'

By hav n- an abstra4t 4lass Rectangle, I ta6e advanta-e of the fa4t that the only d fferen4e between the d fferent ty3es of Rectangles are how they m3lement the

drawLine method. The !Rectangle s m3lemented by hav n- a referen4e to a D)!


ob5e4t and us n- that ob5e4tLs draw_a_line method. The

"Rectangle s m3lemented

by hav n- a referen4e to a D)" ob5e4t and us n- that ob5e4tLs drawline method. >owever, by nstant at n- the r -ht ty3e of Rectangle, I no lon-er have to worry about th s d fferen4e. Example I-1 Java Co#e -ra!ments

class 6ectangle ; pu!lic void draw () ; draw<ine(Bx(,By(,Bx?,By(), draw<ine(Bx?,By(,Bx?,By?), draw<ine(Bx?,By?,Bx(,By?), draw<ine(Bx(,By?,Bx(,By(), > a!stract protected void draw<ine ( dou!le x(, dou!le y(, dou!le x?, dou!le y?),

> class '(6ectangle extends 6ectangle ; draw<ine( dou!le x(, dou!le y(, dou!le x?, dou!le y?) ; $C(%drawBaBline( x(,y(,x?,y?), > > class '?6ectangle extends 6ectangle ; draw<ine( dou!le x(, dou!le y(, dou!le x?, dou!le y?) ; // arguments are different in $C? // and must !e rearranged $C?%drawline( x(,x?,y(,y?), > >
(ow, su33ose that after 4om3let n- th s 4ode, one of the inevitable t"ree <death, ta2es, and 4han- n- reDu rements= 4omes my way. I am as6ed to su33ort another 6 nd of sha3e Eth s t me, a 4 r4le. >owever, I am also - ven the mandate that the 4olle4t on ob5e4t does not want to 6now the d fferen4e between Rectangles and Circles. It o44urs to me that I 4an s m3ly e2tend the a33roa4h ILve already started by add nanother level to my 4lass h erar4hy. I only need to add a new 4lass, 4alled Shape, from wh 4h I w ll der ve the Rectangle and Circle 4lasses. Th s way, the Client ob5e4t 4an 5ust refer to Shape ob5e4ts w thout worry n- about what 6 nd of Shape t has been - ven. As a be- nn n- ob5e4t)or ented analyst, t m -ht seem natural to m3lement these reDu rements us n- only nher tan4e. Bor e2am3le, I 4ould start out w th someth n- l 6e B -ure %)&, and then, for ea4h 6 nd of Shape, m3lement the sha3e w th ea4h draw n3ro-ram, der v n- a vers on of D)! and a vers on of D)" for Rectangle and der v n- a vers on of D)! and a vers on of D)" one for Circle. I would end u3 w th B -ure %).. -i!ure I-3' A strai!ht&orwar# approach2 implementin! two shapes an# two #rawin! pro!rams'

I m3lement the Circle 4lass the same way that I m3lemented the Rectangle 4lass. >owever, th s t me, I m3lement draw by us n- drawCircle nstead of drawLine. Example I-) Java Co#e -ra!ments

a!stract class 7hape ; a!stract pu!lic void draw (), > a!stract class 6ectangle extends 7hape ; pu!lic void draw () ; draw<ine(Bx(,By(,Bx?,By(), draw<ine(Bx?,By(,Bx?,By?), draw<ine(Bx?,By?,Bx(,By?), draw<ine(Bx(,By?,Bx(,By(), > a!stract protected void draw<ine( dou!le x(, dou!le y(, dou!le x?, dou!le y?), > class '(6ectangle extends 6ectangle ; protected void draw<ine (

dou!le x(, dou!le y(, dou!le x?, dou!le y?) ; $C(%drawBaBline( x(,y(,x?,y?), > > class '?6ectangle extends 6ectangle ; protected void draw<ine ( dou!le x(, dou!le x?, dou!le y(, dou!le y?) ; $C?%drawline( x(,x?,y(,y?), > > a!stract class Circle extends 7hape ; pu!lic void draw () ; drawCircle( x,y,r), > a!stract protected void drawCircle ( dou!le x, dou!le y, dou!le r), > class '(Circle extends Circle ; protected void drawCircle() ; $C(%drawBaBcircle( x,y,r), > > class '?Circle extends Circle ; protected void drawCircle() ; $C?%drawcircle( x,y,r), > >
To understand th s des -n, letLs wal6 throu-h an e2am3le. Cons der what the draw method of a

!Rectangle does. RectangleLs draw method s the same as before <4all n- drawLine four t mes as
needed=.

drawLine s m3lemented by 4all n- D)!Ls draw_a_line.

In a4t on, th s loo6s l 6e B -ure %),. -i!ure I-4' (e9uence Dia!ram when have a

!Rectangle'

+eading a )e,#ence Diagra!$


As I d s4ussed n Cha3ter &, 1The 789EThe 7n f ed 8odel n- 9an-ua-e,1 the d a-ram n B -ure %), s a s3e4 al 6 nd of ntera4t on d a-ram 4alled a &e1 ence Diagra'( It s a 4ommon d a-ram n the 789. Its 3ur3ose s to show the ntera4t on of ob5e4ts n the system.

Ca4h bo2 at the to3 re3resents an ob5e4t. It may be named or not. If an ob5e4t has a name, t s - ven to the left of the 4olon.

The 4lass to wh 4h the ob5e4t belon-s s shown to the r -ht of the 4olon. Thus, the m ddle ob5e4t s named 'yRectangle and s an nstan4e of

!Rectangle.
Nou read the d a-ram from the to3 down. Ca4h numbered statement s a messa-e sent from one ob5e4t to e ther tself or to another ob5e4t.

The seDuen4e starts out w th the unnamed Client ob5e4t 4all n- the

draw method of 'yRectangle.


Th s method 4alls ts own drawLine method four t mes <shown n ste3s &, ,, /, and 0=. (ote the arrow 3o nt n- ba46 to the 'yRectangle n the t mel ne.

drawLine 4alls D)!Ls draw_a_line. Th s s shown n ste3s ., +, * and %.

Cven thou-h the Class # a-ram ma6es t loo6 l 6e there are many ob5e4ts, n real ty, I am only deal n- w th three ob5e4ts <see B -ure %)+=! -i!ure I-;' The ob*ects present'

The 4l ent us n- the re4tan-le The

!Rectangle ob5e4t

The D)! draw n- 3ro-ram

"hen the 4l ent ob5e4t sends a messa-e to the

!Rectangle ob5e4t <4alled

'yRectangle= to 3erform draw, t 4alls RectangleLs draw method result n- n ste3s &
throu-h %. 7nfortunately, th s a33roa4h ntrodu4es new 3roblems. 9oo6 at B -ure %). and 3ay attent on to the th rd row of 4lasses. Cons der the follow n-!

The 4lasses n th s row re3resent the four s3e4 f 4 ty3es of Shapes that I have. "hat ha33ens f I -et another draw n- 3ro-ram, that s, another var at on n m3lementat onF I w ll have six d fferent 6 nds of Shapes <two Shape 4on4e3ts t mes three draw n- 3ro-rams=.

Ima- ne what ha33ens f I then -et another ty3e of Shape, another var at on n 4on4e3t. I w ll have nine d fferent ty3es of Shapes <three Shape 4on4e3ts t mes three draw n- 3ro-rams=.

The 4lass e23los on 3roblem ar ses be4ause n th s solut on, the abstra4t on <the 6 nds of

Shapes= and the m3lementat on <the draw n- 3ro-rams= are t -htly 4ou3led. Ca4h ty3e of
sha3e must 6now what ty3e of draw n- 3ro-ram t s us n-. I need a way to se3arate the var at ons n abstra4t on from the var at ons n m3lementat on so that the number of 4lasses only -rows l nearly <see B -ure %)/=. -i!ure I-<' The 1ri#!e pattern separates variations in abstraction an# implementation'

Th s s e2a4tly the ntent of the Br d-e 3attern! RtoS de)4ou3le an abstra4t on from ts m3lementat on so that the two 4an vary nde3endently.
R&S

R&S

Jamma, C., >elm, R., Johnson, R., Al ss des, J., Design Patterns: Ele'ents of 0e sable Object-Oriented &oftware , Read n-, 8ass.!

Add son)"esley, '%%+, 3. '+'.

Before show n- a solut on and der v n- the Br d-e 3attern, I want to ment on a few other 3roblems <beyond the 4omb nator al e23los on=. 9oo6 n- at B -ure %)., as6 yourself what else s 3oor about th s des -n.

#oes there a33ear to be redundan4yF "ould you say th n-s have h -h 4ohes on or low 4ohes onF

Are th n-s t -htly or loosely 4ou3ledF

"ould you want to have to ma nta n th s 4odeF

"he over#se of inheritance$


As a be- nn n- ob5e4t)or ented analyst, I had a tenden4y to solve the 6 nd of 3roblem I have seen here by us n- s3e4 al 4ases, ta6 n- advanta-e of nher tan4e. I loved the dea of nher tan4e be4ause t seemed new and 3owerful. I used t whenever I 4ould. Th s seems to be normal for many be- nn n- analysts, but t s na ve! - ven th s new 1hammer,1 everyth n- seems l 6e a na l. 7nfortunately, many a33roa4hes to tea4h n- ob5e4t)or ented des -n fo4us on data abstra4t onEma6 n- des -ns overly based on the 1 is)ness1 of the ob5e4ts. As I be4ame an e23er en4ed ob5e4t)or ented des -ner, I was st ll stu46 n the 3arad -m of des -n n- based on nher tan4eEthat s, loo6 n- at the 4hara4ter st 4s of my 4lasses based on the r 1is)ness.1 Chara4ter st 4s of ob5e4ts should be based on the r res3ons b l t es, not on what they m -ht 4onta n or be. ?b5e4ts, of 4ourse, may be res3ons ble for - v n- nformat on about themselvesP for e2am3le, a 4ustomer ob5e4t may need to be able to tell you ts name. Th n6 about ob5e4ts n terms of the r res3ons b l t es, not n terms of the r stru4ture. C23er en4ed ob5e4t)or ented analysts have learned to use nher tan4e sele4t vely to real Ge ts 3ower. 7s n- des -n 3atterns w ll hel3 you move alon- th s learn n4urve more Du 46ly. It nvolves a trans t on from us n- a d fferent s3e4 al Gat on for ea4h var at on < nher tan4e= to mov n- these var at ons nto used or owned ob5e4ts <4om3os t on=.

"hen I f rst loo6ed at these 3roblems, I thou-ht that 3art of the d ff 4ulty m -ht have been that I s m3ly was us n- the wron- 6 nd of nher tan4e h erar4hy. Therefore, I tr ed the alternate h erar4hy shown n B -ure %)*.

-i!ure I-=' An alternative implementation'

I st ll have the same four 4lasses re3resent n- all of my 3oss ble 4omb nat ons. >owever, by f rst der v n- vers ons for the d fferent draw n- 3ro-rams, I el m nated the redundan4y between the D)! and D)" 3a46a-es. 7nfortunately, I am unable to el m nate the redundan4y between the two ty3es of

Rectangles and the two ty3es of Circles, ea4h 3a r of wh 4h has the same draw
method. In any event, the 4lass e23los on that was 3resent before s st ll 3resent here. The seDuen4e d a-ram for th s solut on s shown n B -ure %)0. -i!ure I->' (e9uence Dia!ram &or new approach'

"h le th s may be an m3rovement over the or - nal solut on, t st ll has a 3roblem w th s4al n-. It also st ll has some of the or - nal 4ohes on and 4ou3l n- 3roblems. 1ottom line2 I do not want to have to ma nta n th s vers on e therO There must be a better way.

0oo( for alternatives in initial design$


Althou-h my alternat ve des -n here was not s -n f 4antly better than my or - nal

des -n, t s worth 3o nt n- out that f nd n- alternat ves to an or - nal des -n s a -ood 3ra4t 4e. Too many develo3ers ta6e what they f rst 4ome u3 w th and -o w th that. I am not endors n- an n)de3th study of all 3oss ble alternat ves <another way of -ett n- 13aralys s by analys s1=. >owever, ste33 n- ba46 and loo6 n- at how we 4an over4ome the des -n def 4 en4 es n our or - nal des -n s a -reat 3ra4t 4e. In fa4t, t was 5ust th s ste33 n- ba46, a refusal to move forward w th a 6nown, 3oor des -n, that led me to understand n- the 3owerful methods of us n- des -n 3atterns that th s ent re boo6 s about.

I l@ve RuBoard

I l@ve RuBoard

An Observation Abo#t &sing Design Patterns


"hen 3eo3le be- n to loo6 at des -n 3atterns, they often fo4us on the solut ons the 3atterns offer. Th s seems reasonable be4ause they are advert sed as 3rov d n- -ood solut ons to the 3roblems at hand. >owever, th s s start n- at the wron- end. "hen you learn 3atterns by fo4us n- on the solut ons they 3resent, t ma6es t hard to determ ne the s tuat ons n wh 4h a 3attern a33l es. Th s only tells us w"at to do but not w"en to se it or w"y to do it( I f nd t mu4h more useful to fo4us on the 4onte2t of the 3atternEthe 3roblem t s try nto solve. Th s lets me 6now the w"en and the w"y( It s more 4ons stent w th the 3h loso3hy of Ale2anderLs 3atterns! 1Ca4h 3attern des4r bes a 3roblem wh 4h o44urs over and over a-a n n the env ronment, and then des4r bes the 4ore of the solut on to that 3roblem Q1
R.S

R.S

Ale2ander, C., Ish 6awa, S., S lverste n, 8., A Pattern Lang age: !owns>$ ildings>Constr ction, (ew Nor6! ?2ford 7n vers ty Press,

'%**, 3. 2.

"hat I have done here s a 4ase n 3o nt. "hat s the 3roblem be n- solved by the Br d-e 3atternF !"e $ridge pattern is sef l w"en yo "ave an abstraction t"at "as different i'ple'entations( -t allows t"e abstraction and t"e i'ple'entation to vary independently

of eac" ot"er( The 4hara4ter st 4s of the 3roblem f t th s n 4ely. I 4an 6now that I ou-ht to be us n- the Br d-e 3attern even thou-h I do not 6now yet how to m3lement t. Allow n- for the abstra4t on to vary nde3endently from the m3lementat on would mean I 4ould add new abstra4t ons w thout 4han- n- my m3lementat ons and v 4e versa. The 4urrent solut on does not allow for th s nde3endent var at on. I 4an see that t would be better f I 4ould 4reate an m3lementat on that would allow for th s. It s very m3ortant to real Ge that, w thout even 6now n- how to m3lement the Br d-e 3attern, you 4an determ ne that t would be useful n th s s tuat on. Nou w ll f nd that th s s -enerally true of des -n 3atterns. That s, you 4an dent fy when to a33ly them to your 3roblem doma n before 6now n- e2a4tly how to m3lement them.

I l@ve RuBoard

I l@ve RuBoard

0earning the 'ridge Pattern: Deriving %t


(ow that you have been throu-h the 3roblem, we are n a 3os t on to der ve the Br d-e 3attern to-ether. #o n- the wor6 to der ve the 3attern w ll hel3 you to understand more dee3ly what th s 4om3le2 and 3owerful 3attern does. 9etLs a33ly some of the bas 4 strate- es for -ood ob5e4t)or ented des -n and see how they hel3 to develo3 a solut on that s very mu4h l 6e the Br d-e 3attern. To do th s, I w ll be us n- the wor6 of J m Co3l en on 4ommonal ty and var ab l ty analys s.
R,S

R,S

Co3le n, J., % lti-Paradig' Design for C??( Read n-, 8ass.! Add son)"esley, '%%0.

Design patterns are sol#tions that occ#r again and again$


#es -n 3atterns are solut ons that have re4urred n several 3roblems and have therefore 3roven themselves over t me to be -ood solut ons. The a33roa4h I am ta6 n- n th s boo6 s to derive the 3attern n order to tea4h t so that you 4an understand ts 4hara4ter st 4s.

In th s 4ase, I 6now the 3attern I want to der veEthe Br d-e 3atternEbe4ause I was shown t by the Jan- of Bour and have seen how t wor6s n my own 3roblem doma ns. It s m3ortant to note that 3atterns are not really der ved. By def n t on, they must be re4urr n-Ehav n- been demonstrated n at least three nde3endent 4asesEto be 4ons dered 3atterns. "hat I mean by 1der ve1 s that we w ll -o throu-h a des -n 3ro4ess where you 4reate the 3attern as f you d d not 6now t. Th s s to llustrate some 6ey 3r n4 3les and useful strate- es.

Co3l enLs wor6 on 4ommonal ty@var ab l ty analys s tells us how to f nd var at ons n the 3roblem doma n and dent fy what s 4ommon a4ross the doma n. Ident fy where th n-s vary <4ommonal ty analys s= and then dent fy how they vary <var ab l ty analys s=. A44ord n- to Co3l en, 1Commonal ty analys s s the sear4h for 4ommon elements that hel3s us understand how fam ly members are the same.1 Thus, the 3ro4ess of f nd n- out how
R+S

th n-s are 4ommon def nes the fam ly n wh 4h these elements belon- <and hen4e, where th n-s vary=.

R+S

b d, 3. /..

Aar ab l ty analys s reveals how fam ly members vary. Aar ab l ty only ma6es sense w th n a - ven 4ommonal ty. Commonal ty analys s see6s stru4ture that s unl 6ely to 4han-e over t me, wh le var ab l ty analys s 4a3tures stru4ture that s l 6ely to 4han-e. Aar ab l ty analys s ma6es sense only n terms of the 4onte2t def ned by the asso4 ated 4ommonal ty analys s Q Brom an ar4h te4tural 3ers3e4t ve, 4ommonal ty analys s - ves the ar4h te4ture ts lon-ev tyP var ab l ty analys s dr ves ts f tness for use.
R/S

R/S

b d, 33. /$, /,.

In other words, f var at ons are the s3e4 f 4 4on4rete 4ases n the doma n, 4ommonal ty def nes the 4on4e3ts n the doma n that t e them to-ether. The 4ommon 4on4e3ts w ll be re3resented by abstra4t 4lasses. The var at ons found by var ab l ty analys s w ll be m3lemented by the 4on4rete 4lasses <that s, 4lasses der ved from the abstra4t 4lass w th s3e4 f 4 m3lementat ons=.

It s almost a2 omat 4 w th ob5e4t)or ented des -n methods that the des -ner s su33osed to loo6 n the 3roblem doma n, dent fy the nouns 3resent, and 4reate ob5e4ts re3resent nthem. Then, the des -ner f nds the verbs relat n- to those nouns <that s, the r a4t ons= and m3lement them by add n- methods to the ob5e4ts. Th s 3ro4ess of fo4us n- on nouns and verbs ty3 4ally leads to lar-er 4lass h erar4h es than we m -ht want. I su--est that us n- 4ommonal ty@var ab l ty analys s as a 3r mary tool n 4reat n- ob5e4ts s a better a33roa4h than loo6 n- at 5ust nouns and verbs <a4tually, I bel eve th s s a restatement of J m Co3l enLs wor6=. There are two bas 4 strate- es to follow n 4reat n- des -ns to deal w th the var at ons!

B nd what var es and en4a3sulate t.

Bavor 4om3os t on over nher tan4e.

In the 3ast, develo3ers often rel ed on e2tens ve nher tan4e trees to 4oord nate these var at ons. >owever, the se4ond strate-y says to try 4om3os t on when 3oss ble. The ntent of th s s to be able to 4onta n the var at ons n nde3endent 4lasses, thereby allow n- for future var at ons w thout affe4t n- the 4ode. ?ne way to do th s s to have ea4h var at on 4onta ned n ts own abstra4t 4lass and then see how the abstra4t 4lasses relate to ea4h other.

+eviewing encaps#lation$
8ost ob5e4t)or ented develo3ers learned that 1en4a3sulat on1 s data)h d n-. 7nfortunately, th s s a very l m t n- def n t on. True, en4a3sulat on does h de data, but t 4an be used n many other ways. If you loo6 ba46 at B -ure *)&, you w ll see en4a3sulat on o3erates at many levels. ?f 4ourse, t wor6s at h d n- data for ea4h of the 3art 4ular Shapes. >owever, not 4e that the Client ob5e4t s not aware of the 3art 4ular 6 nds of sha3es. That s, the Client ob5e4t has no dea that the Shapes t s deal n- w th are Rectangles and Circles. Thus, the 4on4rete 4lasses that Client deals w th are h dden <or en4a3sulated= from

Client. Th s s the 6 nd of en4a3sulat on that the Jan- of Bour s tal6 n- about


when they say, 1f nd what var es and en4a3sulate t1. They are f nd n- what var es, and en4a3sulat n- t 1beh nd1 an abstra4t 4lass <see Cha3ter 0,

1C23and n- ?ur >or Gons1=.

Bollow th s 3ro4ess for the re4tan-le draw n- 3roblem. B rst, dent fy what t s that s vary n-. In th s 4ase, t s d fferent ty3es of Shapes and d fferent ty3es of draw n- 3ro-rams. The 4ommon 4on4e3ts are therefore sha3es and draw n- 3ro-rams. I re3resent th s n B -ure %)% <note that the 4lass names are shown n tal 4s be4ause the 4lasses are abstra4t=. -i!ure I-I' 6hat is var$in!'

At th s 3o nt, I mean for Shape to en4a3sulate the 4on4e3t of the ty3es of sha3es that I have. Sha3es are res3ons ble for 6now n- how to draw themselves. Dra-ing ob5e4ts, on the other hand, are res3ons ble for draw n- l nes and 4 r4les. I re3resent these res3ons b l t es by def n n- methods n the 4lasses. The ne2t ste3 s to re3resent the s3e4 f 4 var at ons that are 3resent. Bor Shape, I have re4tan-les and 4 r4les. Bor draw n- 3ro-rams, I w ll have a 3ro-ram that s based on D)! < !Dra-ing= and one based on D)" < "Dra-ing=, res3e4t vely. I show th s n B -ure %) '$. -i!ure I-1J' Represent the variations'

At th s 3o nt, the d a-ram s s m3ly not onal. I 6now that

!Dra-ing w ll use D)! and

"Dra-ing w ll use D)" but I have not sa d "ow( I have s m3ly 4a3tured the 4on4e3ts of
the 3roblem doma n <sha3es and draw n- 3ro-rams= and have shown the var at ons that are 3resent. J ven these two sets of 4lasses, I need to as6 how they w ll relate to one another. I do not want to 4ome u3 w th a new set of 4lasses based on an nher tan4e tree be4ause I 6now what ha33ens f I do that <loo6 at B -ures %). and %)* to refresh your memory=. Instead, I want to see f I 4an relate these 4lasses by hav n- one use the other <that s, follow the mandate to favor 4om3os t on over nher tan4e=. The Duest on s, wh 4h 4lass uses the otherF Cons der these two 3oss b l t es! e ther Shape uses the Dra-ing 3ro-rams or the

Dra-ing 3ro-rams use Shape.


Cons der the latter 4ase f rst. If draw n- 3ro-rams 4ould draw sha3es d re4tly, then they would have to 6now some th n-s about sha3es n -eneral! what they are, what they loo6 l 6e. But th s v olates a fundamental 3r n4 3le of ob5e4ts! an ob5e4t should only be res3ons ble for tself. It also v olates en4a3sulat on. Dra-ing ob5e4ts would have to 6now s3e4 f 4 nformat on about the Shapes <that s, the 6 nd of Shape= n order to draw them. The ob5e4ts are not really res3ons ble for the r own behav ors. (ow, 4ons der the f rst 4ase. "hat f I have Shapes use Dra-ing ob5e4ts to draw themselvesF Shapes wouldnLt need to 6now what ty3e of Dra-ing ob5e4t t used s n4e I 4ould have Shapes refer to the Dra-ing 4lass. Shapes also would be res3ons ble for 4ontroll n- the draw n-. Th s loo6s better to me. B -ure %)'' shows th s solut on. -i!ure I-11' Tie the classes to!ether'

In th s des -n, Shape uses Dra-ing to man fest ts behav or. I left out the deta ls of

!Dra-ing us n- the D)! 3ro-ram and "Dra-ing us n- the D)" 3ro-ram. In B -ure %)
'&, I add th s as well as the 3rote4ted methods drawLine and drawCircle < n Shape=, wh 4h 4alls Dra-ingLs drawLine, and drawCircle, res3e4t vely. -i!ure I-1)' Expan#in! the #esi!n'

One r#le@ one place$


A very m3ortant m3lementat on strate-y to follow s to have only one 3la4e where you m3lement a rule. In other words, f you have a rule how to do th n-s, only m3lement that on4e. Th s ty3 4ally results n 4ode w th a -reater number of smaller methods. The e2tra 4ost s m n mal, but t el m nates du3l 4at on and often 3revents many future 3roblems. #u3l 4at on s bad not only be4ause of the e2tra wor6 n ty3 n- th n-s mult 3le t mes, but be4ause of the l 6el hood of

someth n- 4han- n- n the future and then for-ett n- to 4han-e t n all of the reDu red 3la4es. "h le the draw method or Rectangle 4ould d re4tly 4all the drawLine method of whatever Dra-ing ob5e4t the Shape has, I 4an m3rove the 4ode by 4ont nu n- to follow the one rule, one 3la4e strate-y and have a drawLine method n Shape that 4alls the drawLine method of ts Dra-ing ob5e4t. I am not a 3ur st <at least not n most th n-s=, but f there s one 3la4e where I th n6 t s m3ortant to always follow a rule, t s here. In the e2am3le below, I have a drawLine method n Shape be4ause that des4r bes my rule of draw n- a l ne w th Dra-ing. I do the same w th drawCircle for 4 r4les. By follow n- th s strate-y, I 3re3are myself for other der ved ob5e4ts that m -ht need to draw l nes and 4 r4les. "here d d the one rule, one 3la4e strate-y 4ome fromF "h le many have do4umented t, t has been n the fol6lore of ob5e4t)or ented des -ners for a lont me. It re3resents a best 3ra4t 4e of des -ners. 8ost re4ently, ;ent Be46 4alled th s the 1on4e and only on4e rule.1
R,S

>e def nes t as 3art of h s 4onstra nts!

The system <4ode and tests to-ether= must 4ommun 4ate everyth n- you want to 4ommun 4ate.

The system must 4onta n no du3l 4ate 4ode. <' and & to-ether 4onst tute the ?n4e and ?nly ?n4e rule=.

R,S

Be46, ;., Extre'e Progra''ing Explained: E'brace C"ange, Read n-, 8ass.! Add son "esley, &$$$, 33. '$0T'$%.

B -ure %)'. llustrates the se3arat on of the Shape abstra4t on from the Dra-ing m3lementat on. -i!ure I-13' Class #ia!ram illustratin! separation o& abstraction an# implementation'

Brom a method 3o nt of v ew, th s loo6s fa rly s m lar to the nher tan4e)based m3lementat on <su4h as shown n B -ure %).=. The b --est d fferen4e s that the methods are now lo4ated n d fferent ob5e4ts. I sa d at the be- nn n- of th s 4ha3ter that my 4onfus on over the Br d-e 3attern was due to my m sunderstand n- of the term 1 m3lementat on.1 I thou-ht that m3lementat on referred to how I m3lemented a 3art 4ular abstra4t on. The Br d-e 3attern let me see that v ew n- the m3lementat on as someth n- outs de of my ob5e4ts, someth n- that s sed by the ob5e4ts, - ves me mu4h -reater freedom by h d n-

the var at ons n m3lementat on from my 4all n- 3ro-ram. By des -n n- my ob5e4ts th s way, I also not 4ed how I was 4onta n n- var at ons n se3arate 4lass h erar4h es. The h erar4hy on the left s de of B -ure %)'. 4onta ns the var at ons n my abstra4t ons. The h erar4hy on the r -ht s de of B -ure %)'. 4onta ns the var at ons n how I w ll m3lement those abstra4t ons. Th s s 4ons stent w th the new 3arad -m for 4reat n- ob5e4ts <us n4ommonal ty@var ab l ty analys s= that I ment oned earl er. It s eas est to v sual Ge th s when you remember that there are only three ob5e4ts to deal w th at any one t me, even thou-h there are several 4lasses <see B -ure %)',=. -i!ure I-14' There are onl$ three ob*ects at a time'

A reasonably 4om3lete 4ode e2am3le s shown n C2am3le %). for Java and n the C2am3les be- nn n- on 3a-e '+* for C::. Example I-3 Java Co#e -ra!ments

class Client ; pu!lic static void main (7tring argv01) ; 7hape r(, r?, $rawing dp, dp* new '($rawing(), r(* new 6ectangle(dp,(,(,?,?), dp* new '?$rawing (), r?* new Circle(dp,?,?,D), r(%draw(), r?%draw(), > > a!stract class 7hape ; a!stract pu!lic draw() , private $rawing Bdp, 7hape ($rawing dp) ;

Bdp* dp, > protected void draw<ine ( dou!le x(,dou!le y(, dou!le x?,dou!le y?) ; Bdp%draw<ine(x(,y(,x?,y?), > protected void drawCircle ( dou!le x,dou!le y,dou!le r) ; Bdp%drawCircle(x,y,r), > > a!stract class $rawing ; a!stract pu!lic void draw<ine ( dou!le x(, dou!le y(, dou!le x?, dou!le y?), a!stract pu!lic void drawCircle ( dou!le x,dou!le y,dou!le r), > class '($rawing extends $rawing ; pu!lic void draw<ine ( dou!le x(,dou!le y(, dou!le x?,dou!le y?) ; $C(%drawBaBline(x(,y(,x?,y?), > pu!lic void drawCircle ( dou!le x,dou!le y,dou!le r) ; $C(%drawBaBcircle(x,y,r), > > class '?$rawing extends $rawing ; pu!lic void draw<ine ( dou!le x(,dou!le y(, dou!le x?,dou!le y?) ; // arguments are different in $C? // and must !e rearranged $C?%drawline(x(,x?,y(,y?), > pu!lic void drawCircle ( dou!le x, dou!le y,dou!le r) ;

$C?%drawcircle(x,y,r), > > class 6ectangle extends 7hape ; private dou!le Bx(, Bx?, By(, By?, pu!lic 6ectangle ( $rawing dp, dou!le x(,dou!le y(, dou!le x?,dou!le y?) ; super( dp) , Bx(* x(, Bx?* x? , By(* y(, By?* y?, > pu!lic void draw () ; draw<ine(Bx(,By(,Bx?,By(), draw<ine(Bx?,By(,Bx?,By?), draw<ine(Bx?,By?,Bx(,By?), draw<ine(Bx(,By?,Bx(,By(), > > class Circle extends 7hape ; private dou!le Bx, By, Br, pu!lic Circle ( $rawing dp, dou!le x,dou!le y,dou!le r) ; super( dp) , Bx* x, By* y, Br* r , > pu!lic void draw () ; drawCircle(Bx,By,Br), > > // Ee&ve !een given the implementations for $C( and $C? class $C( ; static pu!lic void drawBaBline ( dou!le x(,dou!le y(, dou!le x?,dou!le y?) ; // implementation

> static pu!lic void drawBaBcircle( dou!le x,dou!le y,dou!le r) ; // implementation > > class $C? ; static pu!lic void drawline ( dou!le x(,dou!le x?, dou!le y(,dou!le y?) ; // implementation > static pu!lic void drawcircle ( dou!le x,dou!le y,dou!le r) ; // implementation > >
I l@ve RuBoard

I l@ve RuBoard

"he 'ridge Pattern in +etrospect


(ow that youLve seen how the Br d-e 3attern wor6s, t s worth loo6 n- at t from a more 4on4e3tual 3o nt of v ew. As shown n B -ure %)'., the 3attern has an abstra4t on 3art <w th ts der vat ons= and an m3lementat on 3art. "hen des -n n- w th the Br d-e 3attern, t s useful to 6ee3 these two 3arts n m nd. The m3lementat onLs nterfa4e should be des -ned 4ons der n- the d fferent der vat ons of the abstra4t 4lass that t w ll have to su33ort. (ote that a des -ner shouldnLt ne4essar ly 3ut n an nterfa4e that w ll m3lement all 3oss ble der vat ons of the abstra4t 4lass <yet another 3oss ble route to 3aralys s by analys s=. ?nly those der vat ons that a4tually are be n- bu lt need be su33orted. T me and t me a-a n, the authors have seen that the mere 4ons derat on of fle2 b l ty at th s 3o nt often -reatly m3roves a des -n. Note: In C::, the Br d-e 3atternLs m3lementat on must be m3lemented w th an abstra4t 4lass def n n- the 3ubl 4 nterfa4e. In Java, e ther an abstra4t 4lass or an nterfa4e 4an be used. The 4ho 4e de3ends u3on whether m3lementat ons share 4ommon tra ts that abstra4t 4lasses 4an ta6e advanta-e of. See Peter CoadLs )ava Design, d s4ussed on 3a-e

.'/ of the B bl o-ra3hy, for more on th s.


I l@ve RuBoard

I l@ve RuBoard

ield Notes: &sing the 'ridge Pattern


(ote that the solut on 3resented n B -ures %)'& and %)'. nte-rates the Ada3ter 3attern w th the Br d-e 3attern. I do th s be4ause I was - ven the draw n- 3ro-rams that I must use. These draw n- 3ro-rams have 3ree2 st n- nterfa4es w th wh 4h I must wor6. I must use the Ada3ter to ada3t them so that they 4an be handled n the same way. "h le t s very 4ommon to see the Ada3ter 3attern n4or3orated nto the Br d-e 3attern, the Ada3ter 3attern s not 3art of the Br d-e 3attern.

"he 'ridge Pattern: :ey eat#res Intent Problem #e4ou3le a set of m3lementat ons from the set of ob5e4ts us n- them. The der vat ons of an abstra4t 4lass must use mult 3le m3lementat ons w thout 4aus n- an e23los on n the number of 4lasses. #ef ne an nterfa4e for all m3lementat ons to use and have the der vat ons of the abstra4t 4lass use that.

Solut on

Part 4 3ants and The Collaborators the

A%straction def nes the nterfa4e for


ob5e4ts

be nm3lemented. The .'ple'entor def nes the nterfa4e for the s3e4 f 4 m3lementat on 4lasses. Classes der ved from the A%straction use 4lasses der ved from the .'ple'entor w thout 6now nwh 4h 3art 4ular Concrete.'ple'entor s n use. The de4ou3l n- of the m3lementat ons from the ob5e4ts that use them n4reases e2tens b l ty. Cl ent ob5e4ts are not aware of m3lementat on ssues.

Cn4a3sulate the m3lementat ons n an

ConseDuen4es

Im3lementat on

abstra4t 4lass.

Conta n a handle to t n the base 4lass of the abstra4t on be n- m3lemented. Note: In Java, you 4an use nterfa4es nstead of an abstra4t 4lass for the m3lementat on.

JoB Referen4e

Pa-es '+'T'/&.

-i!ure I-1;' (tan#ar#8 simpli&ie# view o& the 1ri#!e pattern'

"hen two or more 3atterns are t -htly nte-rated <l 6e my Br d-e and Ada3ter=, the result s 4alled a 4om3os te des -n 3attern. 3atternsO
R*S,R0S

It s now 3oss ble to tal6 about 3atterns of

R*S

Com3ound des -n 3atterns used to be 4alled 4om3os te des -n 3atterns, but are now 4alled 4om3ound des -n 3atterns to avo d

4onfus on w th the 4om3os te 3attern.

R0S

Bor more nformat on, refer to R ehle, #., 1Com3os te #es -n Patterns,1 In, Proceedings of t"e @AAB Conference on Object-Oriented

Progra''ing &yste's# Lang ages and Applications <??PS9A L%*=, (ew Nor6! AC8 Press, '%%*, 33. &'0T&&0. Also refer to 1Com3os te #es -n Patterns <They ArenLt "hat Nou Th n6=,1 C?? 0eport, June '%%0.

Another th n- to not 4e s that the ob5e4ts re3resent n- the abstra4t on <the Shapes= were - ven the r m3lementat on wh le be n- nstant ated. Th s s not an nherent 3art of the 3attern, but t s very 4ommon. (ow that you understand the Br d-e 3attern, t s worth rev ew n- the Jan- of BourLs Im3lementat on se4t on n the r des4r 3t on of the 3attern. They d s4uss d fferent ssues relat n- to how the abstra4t on 4reates and@or uses the m3lementat on. Somet mes when us n- the Br d-e 3attern, I w ll share the m3lementat on ob5e4ts a4ross

several abstra4t on ob5e4ts.

In Java, th s s no 3roblemP when all the abstra4t on ob5e4ts -o away, the -arba-e 4olle4tor w ll real Ge that the m3lementat on ob5e4ts are no lon-er needed and w ll 4lean them u3.

In C::, I must somehow mana-e the m3lementat on ob5e4ts. There are many ways to do th sP 6ee3 n- a referen4e 4ounter or even us n- the S n-leton 3attern are 3oss b l t es. It s n 4e, however, not to have to 4ons der th s effort. Th s llustrates another advanta-e of automat 4 -arba-e 4olle4t on.

"h le the solut on I develo3ed w th the Br d-e 3attern s far su3er or to the or - nal solut on, t s not 3erfe4t. ?ne way of measur n- the Dual ty of a des -n s to see how well t handles var at on. >andl n- a new m3lementat on s very easy w th a Br d-e 3attern n 3la4e. The 3ro-rammer s m3ly needs to def ne a new 4on4rete m3lementat on 4lass and m3lement t. (oth n- else 4han-es. >owever, th n-s may not -o so smoothly f I -et a new 4on4rete e2am3le of the abstra4t on. I may -et a new 6 nd of Shape that 4an be m3lemented w th the m3lementat ons already n the des -n. >owever, I may also -et a new 6 nd of Shape that reDu res a new draw n- fun4t on. Bor e2am3le, I may have to m3lement an ell 3se. The 4urrent Dra-ing 4lass does not have the 3ro3er method to do ell 3ses. In th s 4ase, I have to mod fy the m3lementat ons. >owever, even f th s o44urs, I at least have a well)def ned 3ro4ess for ma6 n- these 4han-es <that s, mod fy the nterfa4e of the Dra-ing 4lass or nterfa4e, and mod fy ea4h Dra-ing der vat ve a44ord n-ly=Eth s lo4al Ges the m3a4t of the 4han-e and lowers the r s6 of an unwanted s de effe4t. 1ottom line2 Patterns do not always - ve 3erfe4t solut ons. >owever, be4ause 3atterns re3resent the 4olle4t ve e23er en4e of many des -ners over the years, they are often better than the solut ons you or I m -ht 4ome u3 w th on our own. In the real world, I do not always start out w th mult 3le m3lementat ons. Somet mes, I 6now that new ones are possible# but they show u3 une23e4tedly. ?ne a33roa4h s to 3re3are for mult 3le m3lementat ons by always us n- abstra4t ons. Nou -et a very -ener 4 a33l 4at on.

But I do not re4ommend th s a33roa4h. It leads to an unne4essary n4rease n the number of 4lasses you have. It s m3ortant to wr te 4ode n su4h a way that when mult 3le m3lementat ons do o44ur <wh 4h they often w ll=, t s not d ff 4ult to mod fy the 4ode to n4or3orate the Br d-e 3attern. 8od fy n- 4ode to m3rove ts stru4ture w thout add nfun4t on s 4alled refactoring( As def ned by 8art n Bowler, 1Refa4tor n- s the 3ro4ess of 4han- n- a software system n su4h a way that t does not alter the e2ternal behav or of the 4ode yet m3roves ts nternal stru4ture.1
R%S

R%S

Bowler, 8., 0efactoring: -'proving t"e Design of Existing Code, Read n-, 8ass.! Add son)"esley, &$$$, 3. 2v .

In des -n n- 4ode, I was always attend n- to the 3oss b l ty of refa4tor n- by follow n- the one rule, one 3la4e mandate. The drawLine method was a -ood e2am3le of th s. Althou-h the 3la4e the 4ode was a4tually m3lemented var ed, mov n- t around was fa rly easy.

+efactoring$
Refa4tor n- s 4ommonly used n ob5e4t)or ented des -n. >owever, t s not str 4tly an ?? th n- Q It s mod fy n- 4ode to m3rove ts stru4ture w thout add nfun4t on.

"h le der v n- the 3attern, I too6 the two var at ons 3resent <sha3es and draw n3ro-rams= and en4a3sulated ea4h n the r own abstra4t 4lass. That s, the var at ons of sha3es are en4a3sulated n the Shape 4lass, the var at ons of draw n- 3ro-rams are en4a3sulated n the Dra-ing 4lass. Ste33 n- ba46 and loo6 n- at these two 3olymor3h 4 stru4tures, I should as6 myself, 1"hat do these abstra4t 4lasses re3resentF1 Bor the sha3es, t s 3retty ev dent that the 4lass re3resents d fferent 6 nds of sha3es. The Dra-ing abstra4t 4lass re3resents how I w ll m3lement the Shapes. Thus, even n the 4ase where I des4r bed how new reDu rements for the Dra-ing 4lass may ar se <say, f I need to m3lement ell 3ses= there s a 4lear relat onsh 3 between the 4lasses.
I l@ve RuBoard

I l@ve RuBoard

)#!!ary
In learn n- the Br d-e 3attern, I loo6ed at a 3roblem where there were two var at ons n the 3roblem doma nEsha3es and draw n- 3ro-rams. In the 3roblem doma n, ea4h of these var ed. The 4hallen-e 4ame n try n- to m3lement a solut on based on all of the s3e4 al 4ases that e2 sted. The n t al solut on, wh 4h na vely used nher tan4e too mu4h, resulted n a redundant des -n that had t -ht 4ou3l n- and low 4ohes on, and was thus d ff 4ult to ma nta n. Nou learned the Br d-e 3attern by follow n- the bas 4 strate- es for deal n- w th var at on!

B nd what var es and en4a3sulate t.

Bavor 4om3os t on over nher tan4e.

B nd n- what var es s always a -ood ste3 n learn n- about the 3roblem doma n. In the draw n- 3ro-ram e2am3le, I had one set of var at ons us n- another set of var at ons. Th s nd 4ates that the Br d-e 3attern w ll 3robably be useful. In -eneral, you should dent fy wh 4h 3atterns to use by mat4h n- them w th the 4hara4ter st 4s and behav ors n the 3roblem doma n. By understand n- the w"ys and w"ats of the 3atterns n your re3erto re, you 4an be more effe4t ve n 3 46 n- the ones that w ll hel3 you. Nou 4an sele4t 3atterns to use before de4 d n- how the 3atternLs m3lementat on w ll be done. By us n- the Br d-e 3attern, the des -n and m3lementat on are more robust and better able to handle 4han-es n the future. "h le I fo4used on the 3attern dur n- the 4ha3ter, t s worth 3o nt n- out several ob5e4t) or ented 3r n4 3les that are used n the Br d-e 3attern.

Concept ?b5e4ts res3ons ble themselves Abstra4t 4lass

Discussion are I had d fferent 6 nds of Shapes, but all drew themselves for <v a the draw method=. The Dra-ing 4lasses were res3ons ble for draw n- elements of ob5e4ts. I used abstra4t 4lasses to re3resent the 4on4e3ts. I a4tually had re4tan-les and 4 r4les n the 3roblem doma n. The 4on4e3t 1Shape1 s someth n- that l ves str 4tly n our

head, a dev 4e to b nd the two 4on4e3ts to-etherP therefore, I re3resent t n the Shape 4lass as an abstract class( Shape w ll never -et nstant ated be4ause t never e2 sts n the 3roblem doma n <only Rectangles and Circles do=. The same th n- s true w th draw n3ro-rams. Cn4a3sulat on v a I have two e2am3les of en4a3sulat on throu-h the use of an abstra4t 4lass an abstra4t 4lass n th s 3roblem.

A 4l ent deal n- w th the Br d-e 3attern w ll have only a der vat on of Shape v s ble to t. >owever, the 4l ent w ll not 6now what ty3e of Shape t has < t w ll be 5ust a Shape to the 4l ent=. Thus, I have en4a3sulated th s nformat on. The advanta-e of th s s f a new ty3e of Shape s needed n the future, t does not affe4t the 4l ent ob5e4t.

The Dra-ing 4lass h des the d fferent draw n- der vat ons from the Shapes. In 3ra4t 4e, the abstra4t on may 6now wh 4h m3lementat on t uses be4ause t m -ht nstant ate t. See 3a-e '++ of the Jan- of Bour boo6 for an e23lanat on as to why th s m -ht be a -ood th n- to do. >owever, even when that o44urs, th s 6nowled-e of m3lementat ons s l m ted to the abstra4t onLs 4onstru4tor and s eas ly 4han-ed.

?ne rule, 3la4e

one The abstra4t 4lass often has the methods that a4tually use the m3lementat on ob5e4ts. The der vat ons of the abstra4t 4lass 4all these methods. Th s allows for eas er mod f 4at on f needed, and allows for a -ood start n- 3o nt even before m3lement n- the ent re 3attern.

I l@ve RuBoard

I l@ve RuBoard

)#pple!ent: C77 Code Exa!ples


Example I-4 CDD Co#e -ra!ments2 Rectan!les Onl$

void 6ectangle::draw () ;

draw<ine(Bx(,By(,Bx?,By(), draw<ine(Bx?,By(,Bx?,By?), draw<ine(Bx?,By?,Bx(,By?), draw<ine(Bx(,By?,Bx(,By(), > void '(6ectangle::draw<ine (dou!le x(, dou!le y(, dou!le x?, dou!le y?) ; $C(%drawBaBline(x(,y(,x?,y?), > void '?6ectangle::draw<ine (dou!le x(, dou!le y(, dou!le x?, dou!le y?) ; $C?%drawline(x(,x?,y(,y?), >
Example I-; CDD Co#e -ra!ments2 Rectan!les an# Circles without 1ri#!e

class 7hape ; pu!lic: void draw ()*8, > class 6ectangle : 7hape ; pu!lic: void draw(), protected: void draw<ine( dou!le x(,y(, x?,y?)*8, > void 6ectangle::draw () ; draw<ine(Bx(,By(,Bx?,By(), draw<ine(Bx?,By(,Bx?,By?), draw<ine(Bx?,By?,Bx(,By?), draw<ine(Bx(,By?,Bx(,By(), > // '(6ectangle and '?6ectangle !oth derive from // 6ectangle header files not shown void '(6ectangle::draw<ine ( dou!le x(,y(, x?,y?) ; $C(%drawBaBline(x(,y(,x?,y?), > void '?6ectangle::draw<ine (

dou!le x(,y(, x?,y?) ; $C?%drawline(x(,x?,y(,y?), > > class Circle : 7hape ; pu!lic: void draw() , protected: void drawCircle( dou!le x, y, F) , > void Circle::draw () ; drawCircle(), > // '(Circle and '?Circle !oth derive from Circle // header files not shown void '(Circle::drawCircle ( $C(%drawBaBcircle(x, y, r), > void '?Circle::drawCircle ( $C?%drawcircle(x, y, r), >
Example I-< CDD Co#e -ra!ments2 The 1ri#!e ,mplemente#

void main (7tring argv01) ; 7hape @s(, 7hape @s?, $rawing @dp(, @dp?, dp(* new '($rawing, s(*new 6ectangle(dp,(,(,?,?), dp?* new '?$rawing, s?* new Circle(dp,?,?,G), s( Adraw(), s? Adraw(),

delete s(, delete s?, delete dp(, delete dp?, > // N+4-: )emory management not tested% // #ncludes not shown% class 7hape ; pu!lic: draw()*8, private: $rawing @Bdp, > 7hape::7hape ($rawing @dp) ; Bdp* dp, > void 7hape::draw<ine( dou!le x(, dou!le y(, dou!le x?, dou!le y?) Bdp Adraw<ine(x(,y(,x?,y?), > 6ectangle::6ectangle ($rawing @dp, dou!le x(, y(, x?, y?) : 7hape( dp) ; Bx(* x(, Bx?* x?, By(* y(, By?* y?, > void 6ectangle::draw () ; draw<ine(Bx(,By(,Bx?,By(), draw<ine(Bx?,By(,Bx?,By?), draw<ine(Bx?,By?,Bx(,By?), draw<ine(Bx(,By?,Bx(,By(), > class Circle ; pu!lic: Circle ( $rawing @dp, dou!le x, dou!le y, dou!le r), >, Circle::Circle ( $rawing @dp, dou!le x, dou!le y, dou!le r) : 7hape(dp) ; Bx* x, By* y,

Br* r, > Circle::draw () ; drawCircle( Bx, By, Br), > class $rawing ; pu!lic: virtual void draw<ine ( dou!le x(, dou!le y(, dou!le x?, dou!le y?)*8, >, class '($rawing : pu!lic $rawing ; pu!lic: void draw<ine ( dou!le x(, dou!le y(, dou!le x?, dou!le y?), void drawCircle( dou!le x, dou!le y, dou!le r), >, void '($rawing::draw<ine ( dou!le x(, dou!le y(, dou!le x?, dou!le y?) ; $C(%drawBaBline(x(,y(,x?,y?), > void '($rawing::drawCircle ( dou!le x(, dou!le y, dou!le r) ; $C(%drawBaBcircle (x,y,r), > class '?$rawing : pu!lic $rawing ; pu!lic: void draw<ine ( dou!le x(, dou!le y(, dou!le x?, dou!le y?), void drawCircle( dou!le x, dou!le y, dou!le r), >, void '?$rawing::draw<ine ( dou!le x(, dou!le y(, dou!le x?, dou!le y?) ; $C?%drawline(x(,x?,y(,y?),

> void '?$rawing::drawCircle ( dou!le x, dou!le y, dou!le r) ; $C?%drawcircle(x, y, r), > // Ee have !een given the implementations for // $C( and $C? class $C( ; pu!lic: static void drawBaBline ( dou!le x(, dou!le y(, dou!le x?, dou!le y?), static void drawBaBcircle ( dou!le x, dou!le y, dou!le r), >, class $C? ; pu!lic: static void drawline ( dou!le x(, dou!le x?, dou!le y(, dou!le y?), static void drawcircle ( dou!le x, dou!le y, dou!le r), >,
I l@ve RuBoard

I l@ve RuBoard

Chapter *A$ "he Abstract actory Pattern

?verv ew Introdu4 n- the Abstra4t Ba4tory Pattern 9earn n- the Abstra4t Ba4tory Pattern! An C2am3le 9earn n- the Abstra4t Ba4tory Pattern! Im3lement n- It

B eld (otes! The Abstra4t Ba4tory Pattern Relat n- the Abstra4t Ba4tory Pattern to the CA#@CA8 Problem Summary Su33lement! C:: Code C2am3les
I l@ve RuBoard

I l@ve RuBoard

Overview
I w ll 4ont nue our study of 3atterns w th the Abstra4t Ba4tory 3attern, wh 4h s used to 4reate fam l es of ob5e4ts. In th s 4ha3ter,

I der ve the 3attern by wor6 n- throu-h an e2am3le. I 3resent the 6ey features of the Abstra4t Ba4tory 3attern.

I relate the Abstra4t Ba4tory 3attern to the CA#@CA8 3roblem.

I l@ve RuBoard

I l@ve RuBoard

%ntrod#cing the Abstract actory Pattern


A44ord n- to the Jan- of Bour, the ntent of the Abstra4t Ba4tory 3attern s to 13rov de an nterfa4e for 4reat n- fam l es of related or de3endent ob5e4ts w thout s3e4 fy n- the r 4on4rete 4lasses.1
R'S

R'S

Jamma, C., >elm, R., Johnson, R., Al ss des, J., Design Patterns: Ele'ents of 0e sable Object-Oriented &oftware , Read n-, 8ass.!

Add son)"esley, '%%+, 3. 0*.

Somet mes, several ob5e4ts need to be nstant ated n a 4oord nated fash on. Bor e2am3le, when deal n- w th user nterfa4es, the system m -ht need to use one set of ob5e4ts to wor6 on one o3erat n- system and another set of ob5e4ts to wor6 on a d fferent o3erat n-

system. The Abstra4t Ba4tory 3attern ensures that the system always -ets the 4orre4t ob5e4ts for the s tuat on.

I l@ve RuBoard

I l@ve RuBoard

0earning the Abstract actory Pattern: An Exa!ple


Su33ose I have been - ven the tas6 of des -n n- a 4om3uter system to d s3lay and 3r nt sha3es from a database. The ty3e of resolut on to use to d s3lay and 3r nt the sha3es de3ends on the 4om3uter that the system s 4urrently runn n- on! the s3eed of ts CP7 and the amount of memory that t has ava lable. 8y system must be 4areful about how mu4h demand t s 3la4 n- on the 4om3uter. The 4hallen-e s that my system must 4ontrol the dr vers that t s us n-! low)resolut on dr vers n a less)4a3able ma4h ne and h -h)resolut on dr vers n a h -h)4a3a4 ty ma4h ne, as shown n Table '$)'.

Table 1J-1' Di&&erent Drivers &or Di&&erent "achines

-or #river+ # s3lay

,n a low-capacit$ machine8 use+ 9R## 9ow)resolut on d s3lay dr ver

,n a hi!h-capacit$ machine8 use+ >R## > -h)resolut on d s3lay dr ver >RP# > -h)resolut on 3r nt dr ver

Pr nt

9RP# 9ow)resolut on 3r nt dr ver

In th s e2am3le, the fam l es of dr vers are mutually e24lus ve, but th s s not usually the 4ase. Somet mes, d fferent fam l es w ll 4onta n ob5e4ts from the same 4lasses. Bor e2am3le, a m d)ran-e ma4h ne m -ht use a low)resolut on d s3lay dr ver <9R##= and a h -h)resolut on 3r nt dr ver <>RP#=. The fam l es to use are based on the 3roblem doma n! wh 4h sets of ob5e4ts are reDu red for a - ven 4aseF In th s 4ase, the un fy n- 4on4e3t fo4uses on the demands that the

ob5e4ts 3ut on the system!

A low-resol tion fa'ily8 9R## and 9RP#, those dr vers that 3ut low demands on the system

A "ig"-resol tion fa'ily8 >R## and >RP#, those dr vers that 3ut h -h demands on the system

8y f rst attem3t m -ht be to use a sw t4h to 4ontrol the sele4t on of dr ver, as shown n C2am3le '$)'. Example 1J-1 Java Co#e -ra!ments2 A (witch to Control 6hich Driver to se

// H2'2 C+$- /62.)-N4 class 2pControl ; % % % % % % void do$raw () ; switch (6-7+<54#+N) ; case <+E: // use lrdd case =#.=: // use hrdd > > void doCrint () ; % % % switch (6-7+<54#+N) ; case <+E: // use lrpd case =#.=: // use hrpd > > >
"h le th s does wor6, t 3resents 3roblems. The rules for determ n n- wh 4h dr ver to use are nterm 2ed w th the a4tual use of the dr ver. There are 3roblems both w th 4ou3l n- and w th 4ohes on!

!ig"t co pling8 If I 4han-e the rule on the resolut on <say, I need to add a 8I##9C value=, I must 4han-e the 4ode n two 3la4es that are otherw se not related.

Low co"esion8 I am - v n- doDraw and doPrint two unrelated ass -nments! they must both 4reate a sha3e and must also worry about wh 4h dr ver to use.

T -ht 4ou3l n- and low 4ohes on may not be a 3roblem r -ht now. >owever, they usually n4rease ma ntenan4e 4osts. Also, n the real world, I would l 6ely have many more 3la4es affe4ted than 5ust the two shown here.

)witches !ay indicate a need for abstraction$


?ften, a sw t4h nd 4ates <'= the need for 3olymor3h 4 behav or, or <&= the 3resen4e of m s3la4ed res3ons b l t es. Cons der nstead a more -eneral solut on su4h as abstra4t on or - v n- the res3ons b l ty to other ob5e4ts. Another alternat ve would be to use nher tan4e. I 4ould have two d fferent ApControls! one that uses low)resolut on dr vers and one that uses h -h)resolut on dr vers. Both would be der ved from the same abstra4t 4lass, so 4ommon 4ode 4ould be ma nta ned. I show th s n B -ure '$)'. -i!ure 1J-1' Alternative ):han#lin! variation with inheritance'

"h le nher tan4e 4ould wor6 n th s s m3le 4ase, t has so many d sadvanta-es that I would rather stay w th the sw t4hes. Bor e2am3le!

Co'binatorial explosion8 Bor ea4h d fferent fam ly and ea4h new fam ly I -et n the future, I must 4reate a new 4on4rete 4lass <that s, a new vers on of

ApControl=.
6nclear 'eaningE The resultant 4lasses do not hel3 4lar fy what s -o n- on. I have s3e4 al Ged ea4h 4lass to a 3art 4ular s3e4 al 4ase. If I want my 4ode to be easy to ma nta n n the future, I need to str ve to ma6e t as 4lear as 3oss ble what s -o n- on. Then, I do not have to s3end a lot of t me try n- to relearn what that se4t on of 4ode s try n- to do.

Need to favor co'position8 B nally, t v olates the bas 4 rule to 1favor 4om3os t on over nher tan4e.1

In my e23er en4e, I have found that sw t4hes often nd 4ate an o33ortun ty for abstra4t on. In th s e2am3le, 9R## and >R## are both d s3lay dr vers and 9RP# and >RP# are both 3r nt dr vers. The abstra4t ons would therefore be display drivers and print drivers( B -ure '$)& shows th s 4on4e3tually. I say 14on4e3tually1 be4ause *RDD and HRDD do not really

der ve from the same abstra4t 4lass. -i!ure 1J-)' Drivers an# their abstractions'

Note: At th s 3o nt, I do not have to be 4on4erned that they der ve from d fferent 4lasses be4ause I 6now I 4an use the Ada3ter 3attern to ada3t the dr vers, ma6 n- t a33ear they belon- to the a33ro3r ate abstra4t 4lass.

#ef n n- the ob5e4ts th s way would allow for ApControl to use a DisplayDri,er and a

)rintDri,er w thout us n- sw t4hes. ApControl s mu4h s m3ler to understand be4ause


t does not have to worry about the ty3e of dr vers t has. In other words, ApControl would use a DisplayDri,er ob5e4t or a )rintDri,er ob5e4t w thout hav n- to worry about the dr verLs resolut on. See B -ure '$). and the 4ode n C2am3le '$)&. -i!ure 1J-3'

ApControl usin! #rivers in the i#eal situation'

Example 1J-) Java Co#e -ra!ments2

sin! Pol$morphism to (olve the Problem

// H2'2 C+$- /62.)-N4 class 2pControl ; % % > void doCrint () ; % > >
?ne Duest on rema ns! >ow do I 4reate the a33ro3r ate ob5e4tsF I 4ould have ApControl do t, but th s 4an 4ause ma ntenan4e 3roblems n the future. If I have to wor6 w th a new set of ob5e4ts, I w ll have to 4han-e ApControl. Instead, f I use a 1fa4tory1 ob5e4t to nstant ate the ob5e4ts I need, I w ll have 3re3ared myself for new fam l es of ob5e4ts. In th s e2am3le, I w ll use a fa4tory ob5e4t to 4ontrol the 4reat on of the a33ro3r ate fam ly of dr vers. The ApControl ob5e4t w ll use another ob5e4tEthe fa4tory ob5e4tEto -et the a33ro3r ate ty3e of d s3lay dr ver and the a33ro3r ate ty3e of 3r nt dr ver for the 4urrent 4om3uter be n- used. The ntera4t on would loo6 someth n- l 6e the one shown n B -ure '$),. -i!ure 1J-4'

% %

% %

void do$raw () ; my$isplay$river%draw(),

myCrint$river%print(),

ApControl !ets its #rivers &rom a &actor$ ob*ect'

Brom ApControlLs 3o nt of v ew, th n-s are now 3retty s m3le. It lets Res(actory worry about 6ee3 n- tra46 of wh 4h dr vers to use. Althou-h I am st ll fa4ed w th wr t n- 4ode to do th s tra46 n-, I have de4om3osed the 3roblem a44ord n- to res3ons b l ty. ApControl has the res3ons b l ty for 6now n- how to wor6 w th the a33ro3r ate ob5e4ts. Res(actory has the res3ons b l ty for de4 d n- wh 4h ob5e4ts are a33ro3r ate. I 4an use d fferent fa4tory ob5e4ts or even 5ust one ob5e4t <that m -ht use sw t4hes=. In any 4ase, t s better than what I had before. Th s 4reates 4ohes on! all that Res(actory does s 4reate the a33ro3r ate dr versP all

ApControl does s use them.


There are ways to avo d the use of sw t4hes n Res(actory tself. Th s would allow me to ma6e future 4han-es w thout affe4t n- any e2 st n- fa4tory ob5e4ts. I 4an en4a3sulate a var at on n a 4lass by def n n- an abstra4t 4lass that re3resents the fa4tory 4on4e3t. In the

4ase of Res(actory, I have two d fferent behav ors <methods=!

J ve me the d s3lay dr ver I should use. J ve me the 3r nt dr ver I should use.

Res(actory 4an be nstant ated from one of two 4on4rete 4lasses and der ved from an
abstra4t 4lass that has these 3ubl 4 methods, as shown n B -ure '$)+. -i!ure 1J-;' The ResBa4tory en4a3sulates the var at ons.

)trategies for bridging analysis and design$


Below are three 6ey strate- es nvolved n the Abstra4t Ba4tory.

(trate!$

(hown in the Desi!n

B nd what var es The 4ho 4e of wh 4h dr ver ob5e4t to use and en4a3sulate t. was vary n-. So, I en4a3sulated t n Res(actory. Bavor 4om3os t on Put th s var at on n a se3arate ob5e4tE Res(actoryEand have ApControl/ over nher tan4e. use t as o33osed to hav n- two d fferent ApControl ob5e4ts. #es -n to nterfa4es, ApControl 6nows how to as6 not to Res(actory to nstant ate dr versE t m3lementat ons. does not 6now <or 4are= how Res(actory s a4tually do n- t.

I l@ve RuBoard

I l@ve RuBoard

0earning the Abstract actory Pattern: %!ple!enting %t


C2am3le '$). shows how to m3lement the Abstra4t Ba4tory ob5e4ts for th s des -n. Example 1J-3 Java Co#e -ra!ments2 ,mplementation o& Res-actor$

a!stract class 6es/actory ; a!stract pu!lic $isplay$river get$isp$rvr(), a!stract pu!lic Crint$river getCrt$rvr(), > class <ow6es/act extends 6es/actory ; pu!lic $isplay$river get$isp$rvr() ; return new <6$$(), > pu!lic Crint$river getCrt$rvr() ; return new <6C$(), > > class =igh6es/act extends 6es/actory ; pu!lic $isplay$river get$isp$rvr() ; return new =6$$(), > pu!lic Crint$river getCrt$rvr() ; return new =6C$(), > >
To f n sh the solut on, I have the ApControl tal6 w th the a33ro3r ate fa4tory ob5e4t <e ther *o-Res(act or HighRes(act=P th s s shown n B -ure '$)/. (ote that

Res(actory s abstra4t, and that th s h d n- of Res(actoryLs m3lementat on s what


ma6es the 3attern wor6. >en4e, the name Abstra4t Ba4tory for the 3attern.

-i!ure 1J-<' ,nterme#iate solution usin! the Abstract -actor$'

ApControl s - ven e ther a *o-Res(act ob5e4t or a HighRes(act ob5e4t. It as6s th s


ob5e4t for the a33ro3r ate dr vers when t needs them. The fa4tory ob5e4t nstant ates the 3art 4ular dr ver <low or h -h resolut on= that t 6nows about. ApControl does not need to worry about whether a low)resolut on or a h -h)resolut on dr ver s returned s n4e t uses both n the same manner. I have -nored one ssue! 9R## and >R## may not have been der ved from the same abstra4t 4lass <as may be true of 9RP# and >RP#=. ;now n- the Ada3ter 3attern, th s does not 3resent mu4h of a 3roblem. I 4an s m3ly use the stru4ture I have n B -ure '$)/, but ada3t the dr vers as shown n B -ure '$)*. -i!ure 1J-=' (olvin! the problem with the Abstract -actor$ an# A#apter'

The m3lementat on of th s des -n s essent ally the same as the one before t. The only d fferen4e s that now the fa4tory ob5e4ts nstant ate ob5e4ts from 4lasses I have 4reated that ada3t the ob5e4ts I started w th. Th s s an m3ortant model n- method. By 4omb n nthe Ada3ter 3attern w th the Abstra4t Ba4tory 3attern n th s way, I 4an treat these 4on4e3tually s m lar ob5e4ts as f they were s bl n-s even f they are not. Th s enables the Abstra4t Ba4tory to be used n more s tuat ons. In th s 3attern,

The 4l ent ob5e4t 5ust 6nows who to as6 for the ob5e4ts t needs and how to use them.

The Abstra4t Ba4tory 4lass s3e4 f es wh 4h ob5e4ts 4an be nstant ated by def n n- a method for ea4h of these d fferent ty3es of ob5e4ts. Ty3 4ally, an Abstra4t Ba4tory ob5e4t w ll have a method for ea4h ty3e of ob5e4t that must be nstant ated.

The 4on4rete fa4tor es s3e4 fy wh 4h ob5e4ts are to be nstant ated.

I l@ve RuBoard

I l@ve RuBoard

ield Notes: "he Abstract actory Pattern


#e4 d n- wh 4h fa4tory ob5e4t s needed s really the same as determ n n- wh 4h fam ly of ob5e4ts to use. Bor e2am3le, n the 3re4ed n- dr ver 3roblem, I had one fam ly for low) resolut on dr vers and another fam ly for h -h)resolut on dr vers. >ow do I 6now wh 4h set I wantF In a 4ase l 6e th s, t s most l 6ely that a 4onf -urat on f le w ll tell me. I 4an then wr te a few l nes of 4ode that nstant ate the 3ro3er fa4tory ob5e4t based on th s 4onf -urat on nformat on. I 4an also use an Abstra4t Ba4tory so I 4an use a subsystem for d fferent a33l 4at ons. In th s 4ase, the fa4tory ob5e4t w ll be 3assed to the subsystem, tell n- the subsystem wh 4h ob5e4ts t s to use. In th s 4ase, t s usually 6nown by the ma n system wh 4h fam ly of ob5e4ts the subsystem w ll need. Before the subsystem s 4alled, the 4orre4t fa4tory ob5e4t would be nstant ated.

"he Abstract actory Pattern: :ey eat#res Intent Problem Solut on Nou want to have fam l es or sets of ob5e4ts for 3art 4ular 4l ents <or 4ases=. Bam l es of related ob5e4ts need to be nstant ated. Coord nates the 4reat on of fam l es of ob5e4ts. J ves a way to ta6e the rules of how to 3erform the nstant at on out of the 4l ent ob5e4t that s us n- these 4reated ob5e4ts.

Part 4 3ants and The A%stract(actory def nes the Collaborators nterfa4e for how to 4reate ea4h member of the fam ly of ob5e4ts reDu red. Ty3 4ally, ea4h fam ly s 4reated by hav n- ts own un Due Concrete(actory. ConseDuen4es The 3attern solates the rules of wh 4h ob5e4ts to use from the lo- 4 of how to use these ob5e4ts. #ef ne an abstra4t 4lass that s3e4 f es wh 4h

Im3lementat on

ob5e4ts are to be made. Then m3lement one 4on4rete 4lass for ea4h fam ly. Tables or f les 4an also be used to a44om3l sh the same th n-. JoB Referen4e Pa-es 0*T%/.

B -ure '$)0 shows a Client us n- ob5e4ts der ved from two d fferent server 4lasses <A%stract)roductA and A%stract)roductB=. It s a des -n that s m3l f es, h des m3lementat ons, and ma6es a system more ma nta nable. -i!ure 1J->' (tan#ar#8 simpli&ie# view o& the Abstract -actor$ pattern'

The 4l ent ob5e4t does not 6now wh 4h 3art 4ular 4on4rete m3lementat ons of the server ob5e4ts t has be4ause the fa4tory ob5e4t has the res3ons b l ty to 4reate them.

The 4l ent ob5e4t does not even 6now wh 4h 3art 4ular fa4tory t uses s n4e t only 6nows that t has an Abstra4t Ba4tory ob5e4t. It has a Concrete(actory! or a

Concrete(actory" ob5e4t, but t doesnLt 6now wh 4h one.

I have h dden <en4a3sulated= from the Client the 4ho 4e about wh 4h server ob5e4ts are be n- used. Th s w ll ma6e t eas er n the future to ma6e 4han-es n the al-or thm for ma6 n- th s 4ho 4e be4ause the Client s unaffe4ted. The Abstra4t Ba4tory 3attern affords us a new 6 nd of de4om3os t onEde4om3os t on by res3ons b l ty. 7s n- t de4om3oses our 3roblem nto

"ho s us n- our 3art 4ular ob5e4ts <ApControl= "ho s de4 d n- u3on wh 4h 3art 4ular ob5e4ts to use < A%stract(actory=

7s n- the Abstra4t Ba4tory s nd 4ated when the 3roblem doma n has d fferent fam l es of ob5e4ts 3resent and ea4h fam ly s used under d fferent 4 r4umstan4es. Nou may def ne fam l es a44ord n- to any number of reasons. C2am3les n4lude!

# fferent o3erat n- systems <when wr t n- 4ross)3latform a33l 4at ons= # fferent 3erforman4e -u del nes

# fferent vers ons of a33l 4at ons

# fferent tra ts for users of the a33l 4at on

?n4e you have dent f ed the fam l es and the members for ea4h fam ly, you must de4 de how you are -o n- to m3lement ea4h 4ase <that s, ea4h fam ly=. In my e2am3le, I d d th s by def n n- an abstra4t 4lass that s3e4 f ed wh 4h fam ly member ty3es 4ould be nstant ated. Bor ea4h fam ly, I then der ved a 4lass from th s abstra4t 4lass that would nstant ate these fam ly members. Somet mes you w ll have fam l es of ob5e4ts but do not want to 4ontrol the r nstant at on w th a d fferent der ved 4lass for ea4h fam ly. Perha3s you want someth n- more dynam 4. C2am3les m -ht be

Nou want to have a 4onf -urat on f le that s3e4 f es wh 4h ob5e4ts to use. Nou 4an use a sw t4h based on the nformat on n the 4onf -urat on f le that nstant ates the 4orre4t ob5e4t.

Ca4h fam ly 4an have a re4ord n a database that 4onta ns nformat on about wh 4h ob5e4ts t s to use. Ca4h 4olumn <f eld= n the database nd 4ates wh 4h s3e4 f 4 4lass ty3e to use for ea4h ma6e method n the Abstra4t Ba4tory.

If you are wor6 n- n Java, you 4an ta6e the 4onf -urat on f le 4on4e3t one ste3 further. >ave the nformat on n the f eld names re3resent the 4lass name to use. It does not need to be the full 4lass name as lon- as you have a set 4onvent on. Bor e2am3le, you 4ould have a set 3ref 2 or suff 2 to add to the name n the f le. 7s n- JavaLs Class 4lass you 4an nstant ate the 4orre4t ob5e4t based on these names.
R&S

R&S

Bor a -ood des4r 3t on of JavaLs Class 4lass see C46el, B., !"in*ing in )ava, 733er Saddle R ver, (.J.! Prent 4e >all, &$$$.

In real)world 3ro5e4ts, members n d fferent fam l es do not always have a 4ommon 3arent. Bor e2am3le, n the earl er dr ver e2am3le, t s l 6ely that the 9R## and >R## dr ver 4lasses are not der ved from the same 4lass. In 4ases l 6e th s, t s ne4essary to ada3t them so an Abstra4t Ba4tory 3attern 4an wor6.
I l@ve RuBoard

I l@ve RuBoard

+elating the Abstract actory Pattern to the CAD4CA. Proble!


In the CA#@CA8 3roblem, the system w ll have to deal w th many sets of features, de3end n- u3on wh 4h CA#@CA8 vers on t s wor6 n- w th. In the A' system, all of the features w ll be m3lemented for A'. S m larly, n the A& system, all of the features w ll be m3lemented for A&. The fam l es that I w ll use for the Abstra4t Ba4tory 3attern w ll be A' Beatures and A& Beatures.
I l@ve RuBoard

I l@ve RuBoard

)#!!ary
The Abstra4t Ba4tory s used when you must 4oord nate the 4reat on of fam l es of ob5e4ts. It - ves a way to ta6e the rules re-ard n- how to 3erform the nstant at on out of the 4l ent ob5e4t that s us n- these 4reated ob5e4ts.

B rst, dent fy the rules for nstant at on and def ne an abstra4t 4lass w th an nterfa4e that has a method for ea4h ob5e4t that needs to be nstant ated.

Then, m3lement 4on4rete 4lasses from th s 4lass for ea4h fam ly.

The 4l ent ob5e4t uses th s fa4tory ob5e4t to 4reate the server ob5e4ts that t needs.

I l@ve RuBoard

I l@ve RuBoard

)#pple!ent: C77 Code Exa!ples


Example 1J-4 CDD Co#e -ra!ments2 A (witch to Control 6hich Driver to se

// C:: C+$- /62.)-N4 // class 2pControl % % % % % % void 2pControl::do$raw () ; switch (6-7+<54#+N) ; case <+E: // use lrdd case =#.=: // use hrdd > > void 2pControl::doCrint () ; % % % switch (6-7+<54#+N) ; case <+E: // use lrpd case =#.=: // use hrpd > >
Example 1J-; CDD Co#e -ra!ments2 sin! Pol$morphism to (olve the Problem

// C:: C+$- /62.)-N4 // class 2pControl % % > void 2pControl::doCrint () ; % >


Example 1J-< CDD Co#e -ra!ments2 ,mplementation o& Res-actor$

% %

% %

void 2pControl::do$raw () ; my$isplay$river Adraw(),

myCrint$river Aprint(),

class 6es/actory ; pu!lic: virtual $isplay$river @get$isp$rvr()*8, virtual Crint$river @getCrt$rvr()*8, > class <ow6es/act : pu!lic 6es/actory ; pu!lic: $isplay$river @get$isp$rvr(), Crint$river @getCrt$rvr(), > $isplay$river @<ow6es/act::get$isp$rvr() ; return new <6$$, > Crint$river @<ow6es/act::getCrt$rvr() ; return new <6C$, > class =igh6es/act : pu!lic 6es/actory ; pu!lic: $isplay$river @get$isp$rvr(), Crint$river @getCrt$rvr(), > $isplay$river @=igh6es/act::get$isp$rvr() ; return new =6$$,

> Crint$river @=igh6es/act::getCrt$rvr() ; return new =6C$, >


I l@ve RuBoard

I l@ve RuBoard

Part %5: P#tting %t All "ogether: "hin(ing in Patterns


Part Overview In th s 3art, I 3ro3ose an a33roa4h to des -n n- ob5e4t)or ented systems based on 3atterns. I have 3roven th s a33roa4h n my own des -n 3ra4t 4e. I a33ly th s a33roa4h to the CA#@CA8 3roblem that we have been e2am n n- s n4e Cha3ter ., 1A Problem That Cr es ?ut for Ble2 ble Code.1 Th s a33roa4h f rst tr es to understand the 4onte2t n wh 4h ob5e4ts show u3.

Chapter ''

Discusses These Topics


A d s4uss on of Chr sto3her Ale2anderLs deas and how e23erts use these deas to des -n.

'&

A33l 4at on of th s a33roa4h to solve the CA#@CA8 3roblem f rst 3resented n Cha3ter ..

A 4om3ar son of th s solut on w th the solut on I develo3ed n Cha3ter ,.

'.

A summary of what I have d s4ussed about ob5e4t)or entat on and des -n 3atterns.

I l@ve RuBoard

The 4on4e3ts here are what I 4all pattern-oriented design(

I l@ve RuBoard

Chapter **$ B=ow Do Experts Design2

?verv ew Bu ld n- by Add n- # st n4t ons Summary


I l@ve RuBoard

I l@ve RuBoard

Overview
"hen try n- to des -n, how do you startF #o you f rst -et the deta ls and see how they are 3ut to-etherF ?r do you loo6 from the b - 3 4ture and brea6 t down. ?r s there another wayF Chr sto3her Ale2anderLs a33roa4h s to fo4us on the h -h)level relat onsh 3sE n a sense, wor6 n- from the to3 down. Before ma6 n- any des -n de4 s on, he feels t s essent al to understand the 4onte2t of the 3roblem we are solv n-. >e uses 3atterns to def ne these relat onsh 3s. >owever, more than 5ust 3resent n- a 4olle4t on of 3atterns, he offers us an ent re a33roa4h to des -n. The area about wh 4h he s wr t n- s ar4h te4ture, des -n n3la4es where 3eo3le l ve and wor6, but h s 3r n4 3les a33ly to software des -n as well. In th s 4ha3ter,

I d s4uss Ale2anderLs a33roa4h to des -n. I des4r be how to a33ly th s n the software arena.

I l@ve RuBoard

I l@ve RuBoard

'#ilding by Adding Distinctions


(ow that you have a handle on some of the des -n 3atterns, t s t me to see how they 4an wor6 to-ether. Bor Ale2ander, t s not enou-h to s m3ly des4r be nd v dual 3atterns. >e uses them to develo3 a new 3arad -m for des -n.

> s boo6, !"e !i'eless ,ay of $ ilding, s both about 3atterns and how they wor6 to-ether. Th s s a beaut ful boo6. It s one of my favor te boo6s both on a 3ersonal level and on a 3rofess onal level. It has hel3ed me a33re4 ate th n-s n my l fe, to understand the env ronment n wh 4h I l ve, and also to a4h eve better software des -n. >ow 4an th s beF >ow 4an a boo6 about des -n n- bu ld n-s and towns have su4h a 3rofound nfluen4e on des -n n- softwareF I bel eve t s be4ause t des4r bes a 3arad -m that Ale2ander says a des -ner should wor6 from. Any des -ner. It s th s 3arad -m of des -n that I f nd most nterest n-. I w sh that I 4ould say I had mmed ately ado3ted Ale2anderLs ns -hts the f rst t me I read h s boo6P however, that was not the 4ase. 8y n t al rea4t on to th s boo6 was, 1Th s s very nterest n-. It ma6es sense.1 And then I went ba46 to the trad t onal des -n methods that I had been us n- for so lon-. But somet mes the old say n-s turn out to be true. As n, 19u46 s when o33ortun ty meets w th 3re3aredness.1 ?r, 1Chan4e favors the 3re3ared m nd.1 I -ot 1lu46y1 and that has made all the d fferen4e. " th n a few wee6s of read n- !"e !i'eless ,ay of $ ilding, I was fa4ed w th an o33ortun ty. I was on a des -n 3ro5e4t and my standard a33roa4hes werenLt wor6 n-. I had des -ns, but they werenLt -ood enou-h. All of my tr ed and true des -n methods were fa l n- me. I was very frustrated. Bortunately, I was w se enou-h to try a new wayE Ale2anderLs wayEand was del -hted w th the results. In the ne2t 4ha3ter, I w ll des4r be what I d d. But f rst, letLs loo6 at what Ale2ander offers us. Design is often t"o g"t of as a process of synt"esis# a process of p tting toget"er t"ings# a process of co'bination. A44ord n- to th s v ew, a whole s 4reated by 3utt n- to-ether 3arts. The 3arts 4ome f rst! and the form of the whole 4omes se4ond.
R'S

R'S

Ale2ander, C., Ish 6awa, S., S lverste n, 8., !"e !i'eless ,ay of $ ilding, (ew Nor6! ?2ford 7n vers ty Press, '%*%, 3. ./0.

It s natural to des -n from 3arts to the whole, start n- w th the 4on4rete th n-s that I

6now. "hen I f rst read th s, I thou-ht, 1Nes. That s 3retty mu4h how I loo6 at th n-s. I f -ure out what I need and then 3ut t to-ether.1 That s, I dent fy my 4lasses and then see how they wor6 to-ether. After assembl n- the 3 e4es, I may ste3 ba46 to see that they f t n the b - 3 4ture. But even when I sw t4h my fo4us from lo4al to -lobal, I am st ll th n6 n- about the 3 e4es throu-hout the 3ro4ess. As an ob5e4t)or ented develo3er, these pieces are ob5e4ts and 4lasses. I dent f ed them. I def ned behav or and nterfa4es. But I started w th 3 e4es and ty3 4ally stayed fo4used on them. Th n6 about the or - nal CA#@CA8 solut on n Cha3ter ,, 1A Standard ?b5e4t)?r ented Solut on.1 I started out th n6 n- about the d fferent 4lasses I needed! slots, holes, 4utouts, and so on. ;now n- that I needed to relate these to a A' system and a A& system, I thou-ht I needed a set of these 4lasses that wor6ed w th A' and another set of these 4lasses that wor6ed w th A&. B nally, after 4om n- u3 w th these 4lasses, I saw how they t ed to-ether. $ t it is i'possible to for' anyt"ing w"ic" "as t"e c"aracter of nat re by adding prefor'ed parts(
R&S

R&S

b d, 3. ./0.

Ale2anderLs thes s s that bu ld n- from the 3 e4es s not a -ood way to des -n. Cven thou-h Ale2ander s tal6 n- about ar4h te4ture, many software des -n 3ra4t t oners whom I res3e4t sa d that h s ns -hts were val d for us as well. I had to o3en my m nd to th s new way of th n6 n-. And when I d d so, I heard Ale2ander say that 1-ood software des -n 4annot be a4h eved s m3ly by add n- to-ether 3reformed 3arts1 < .e., 3arts def ned before see n- how they would f t to-ether=. ,"en parts are 'od lar and 'ade before t"e w"ole# by definition t"en# t"ey are identical# and it is i'possible for every part to be ni1 e# according to its position in t"e w"ole . Cven more m3ortant, t s m3ly s not 3oss ble for any 4omb nat on of modular 3arts to 4onta n

the number of 3atterns wh 4h must be 3resent s multaneously n a 3la4e wh 4h s al ve.

R.S

R.S

b d, 33. ./0T./%.

Ale2anderLs tal6 about modular ty was 4onfus n- to me at f rst. Then I real Ged that f we start out w th modules before we have the b - 3 4ture, the modules would be the same, s n4e there would be no reason to for them to be d fferent. Th s seems to be the -oal of reuse. #onLt we want to use e2a4tly the same modules a-a n and a-a nF Nes. But we also want ma2 mum fle2 b l ty and robustness. S m3ly 4reat nmodules does not -uarantee th s. ?n4e I started to learn how to use des -n 3atternsEas Ale2ander tea4hesEI learned how to 4reate reusableEand fle2 bleE4lasses to a -reater e2tent than I had been able to do before. I be4ame a better des -ner. -t is only possible to 'a*e a place w"ic" is alive by a process in w"ic" eac" part is 'odified by its position in t"e w"ole(
R,S

R,S

b d, 3. ./%.

"hen you read alive# th n6 rob st and flexible syste's( Carl er, Ale2ander sa d that 3arts need to be un Due so that they 4an ta6e advanta-e of the r 3art 4ular s tuat on. (ow, he ta6es th s dee3er. It s n 4o3 n- w th and f tt n- nto the surround n-s that - ves a 3la4e ts 4hara4ter. Th n6 of e2am3les n ar4h te4ture!

A &wiss villageE Nour m ndLs eye br n-s u3 a v lla-e of 4losely nestled 4otta-es, ea4h loo6 n- Du te s m lar to the one ne2t to t, but ea4h one d fferent n ts own way. The d fferen4es are not arb trary, but refle4t the f nan4 al means of the bu lder and owner as well as the need of the bu ld n- to blend n w th ts mmed ate surround n-s. The effe4t s a very n 4e, 4omfortable ma-e.

An A'erican s b rbE All of the houses are 3retty mu4h 4oo6 e)4utter des -ns. Attent on s rarely 3a d to the natural surround n-s of the house. Covenants and standards attem3t to enfor4e th s homo-ene ty. The effe4t s a de3ersonal Gat on of

the houses and s not at all 3leas n-. A33ly n- th s to software des -n m -ht seem a b t too 14on4e3tual1 at th s 3o nt. Bor now, t s enou-h to understand that the -oal s to des -n 3 e4esE4lasses, ob5e4tsEw th n the 4onte2t n wh 4h they must l ve n order to 4reate robust and fle2 ble systems. -n s"ort# eac" part is given its specific for' by its existence in t"e context of t"e larger w"ole( Th s s a d fferent at n- 3ro4ess. It v ews des -n as a seDuen4e of a4ts of co'plexificationC stru4ture s n5e4ted nto the whole by o3erat n- on the whole and 4r n6l n- t, not by add n- l ttle 3arts to one another. In the 3ro4ess of d fferent at on, the whole - ves b rth to ts 3arts! The form of the whole, and ts 3arts, 4ome nto be n- s multaneously. The ma-e of the d fferent at n- 3ro4ess s the -rowth of an embryo.
R+S

R+S

b d, 3. .*$.

1Com3le2 f 4at on.1 "hat n the world does that meanF IsnLt the -oal to ma6e th n-s s m3ler, not more 4om3le2F "hat Ale2ander s des4r b n- s a way to th n6 about des -n that starts by loo6 n- at the 3roblem n ts s m3lest terms and then adds add t onal features <d st n4t ons=, ma6 n- the des -n more 4om3le2 as we -o be4ause we are add n- more nformat on. Th s s a very natural 3ro4ess. "e do t all the t me. Bor e2am3le, su33ose you need to arran-e a room for a le4ture w th an aud en4e of ,$ 3eo3le. As you des4r be your reDu rements to someone, you m -ht say someth n- l 6e, 1ILll need a room .$ feet by .$ feet1 <start n- s m3le=. Then, 1ILd l 6e the 4ha rs arran-ed theater style! , rows of 01 <add n- nformat on, you have made the des4r 3t on of the room more 4om3le2=. And then, 1I need a le4tern at the front of the room1 <even more 4om3le2=. !"e nfolding of a design in t"e 'ind of its creator# nder t"e infl ence of lang age# is j st t"e sa'e( Ca4h 3attern s an o3erator that d fferent ates s3a4e! that s, t 4reates d st n4t ons where no d st n4t on was before. And n the lan-ua-e the o3erat ons are arran-ed n seDuen4e! so

that, as they are done, one after another, -radually a 4om3lete th n- s born, -eneral n the sense that t shared ts 3atterns w th other 4om3arable th n-sP s3e4 f 4 n the sense that t s un Due, a44ord n- to ts 4 r4umstan4es. The lan-ua-e s a seDuen4e of these o3erators, n wh 4h ea4h one further d fferent ates the ma-e, wh 4h s the 3rodu4t of the 3rev ous d fferent at ons.
R/S

R/S

b d, 33. .*&T.*..

Ale2ander asserts that des -n should start w th a s m3le statement of the 3roblem, then ma6e t more deta led <4om3le2= by n5e4t n- nformat on nto the statement. Th s nformat on ta6es the form of a 3attern. To Ale2ander, a 3attern def nes relat onsh 3s between the ent t es n h s 3roblem doma n. Bor e2am3le, 4ons der the Courtyard 3attern d s4ussed n Cha3ter +, 1An Introdu4t on to #es -n Patterns.1 The 3attern must des4r be the ent t es that are nvolved n a 4ourtyard and how they relate. Cnt t es su4h as

The o3en s3a4es of the 4ourtyard The 4ross n- 3aths

The v ews outward

And even the 3eo3le who are -o n- to use the 4ourtyard

Th n6 n- n terms of how these ent t es need to relate to ea4h other - ves us a 4ons derable amount of nformat on w th wh 4h to des -n the 4ourtyard. "e ref ne the des -n of the 4ourtyard by th n6 n- about the other 3atterns that would e2 st n the 4onte2t of the 4ourtyard 3attern, su4h as 3or4hes or verandas fa4 n- the 4ourtyard. "hat ma6es th s analyt 4al method so 3owerful s that t does not have to rely on my e23er en4e or my ntu t on or my 4reat v ty. Ale2anderLs thes s s that these 3atterns e2 st nde3endent of any 3erson. A s3a4e s al ve be4ause t follows a natural 3ro4ess, not s m3ly be4ause the des -ner was a -en us. S n4e the Dual ty of a des -n s de3endent u3on follow n- th s natural 3ro4ess, t should not be sur3r s n- that Dual ty solut ons for s m lar 3roblems a33ear very mu4h al 6e.

Based on th s, he - ves us the rules a -ood des -ner would follow.

One at a ti'eE Patterns should be a33l ed one at a t me n seDuen4e. Context firstE A33ly those 3atterns f rst that 4reate the 4onte2t for the other 3atterns.

Patterns define relationships$


The 3atterns that Ale2ander des4r bes def ne relat onsh 3s between the ent t es n the 3roblem doma n. These 3atterns are not as m3ortant as the relat onsh 3s but - ve us a way to tal6 about them.

Ale2anderLs a33roa4h also a33l es to software des -n. Perha3s not l terally but 4erta nly 3h loso3h 4ally. "hat would Ale2ander say to software des -nersF

Alexan#erAs (teps Ident fy 3atterns

Discussion Ident fy the 3atterns that are 3resent n your 3roblem. Th n6 about your 3roblem n terms of the 3atterns that are 3resent. Remember, the 3ur3ose of the 3attern s to def ne relat onsh 3s amon- ent t es.

Start w th 4onte2t Ident fy the 3atterns that 4reate the 4onte2t for the other 3atterns 3atterns. These should be your start n- 3o nt. Then, wor6 nward 9oo6 at the rema n n- 3atterns and at any other 3atterns from the 4onte2t that you m -ht have un4overed. Brom th s set, 3 46 the 3atterns that def ne the 4onte2t for the 3atterns that would rema n. Re3eat. Ref ne the des -n As you ref ne, always 4ons der the 4onte2t m3l ed by the 3atterns. Im3lement The m3lementat on n4or3orates the deta ls d 4tated by the 3atterns.

&sing Alexander in software design: a personal observation$


The f rst t me I used Ale2anderLs a33roa4h, I too6 h s words too l terally. > s 4on4e3tsErooted n ar4h te4tureEdo not usually translate d re4tly to software des -n <or other 6 nds of des -n=. In some ways, I was lu46y n my early e23er en4es n us n- des -n 3atterns n that the 3roblems I solved had the

3atterns follow 3retty well)def ned orders of 4onte2t. >owever, th s also wor6ed a-a nst me n that I na vely assumed that th s method would wor6 n -eneral < t does not=. Th s was 4om3ounded by the fa4t that many 6ey des -ners n the software 4ommun ty were es3ous n- the develo3ment of 13attern lan-ua-es1Eloo6 n- for formal ways to a33ly Ale2ander to software. I nter3reted th s to mean that we were 4lose to be n- able to a33ly Ale2anderLs a33roa4h d re4tly n software des -n <I no lon-er bel eve th s to be true=. S n4e Ale2ander sa d 3atterns n ar4h te4ture had 3redeterm ned orders of 4onte2t, I assumed 3atterns n software also had th s 3redeterm ned order. That s, one ty3e of 3attern would always 4reate the 4onte2t for another ty3e. I be-an to evan-el Ge about Ale2anderLs a33roa4hEas I understood tEwh le tea4h n- others. A few months and a few 3ro5e4ts later, I be-an to see the 3roblems. There were 4ases where a 3reset order of 4onte2ts d d not wor6. >av n- been tra ned as a mathemat 4 an, I only needed one 4ountere2am3le to d s3rove my theory. Th s started me Duest on n- everyth n- about my a33roa4hE someth n- I usually d d, but had for-otten n my e24 tement. S n4e that early sta-e, I now loo6 at the principles u3on wh 4h Ale2anderLs wor6 s based. "h le they man fest themselves d fferently n ar4h te4ture and n software develo3ment, these 3r n4 3les do a33ly to software des -n. I see t n m3roved des -ns. I see t n more ra3 d and robust analys s. I e23er en4e t every t me I have to ma nta n my software.

I l@ve RuBoard

I l@ve RuBoard

)#!!ary
#es -n s normally thou-ht of as a 3ro4ess of synthes s, a 3ro4ess of 3utt n- th n-s to-ether. In software, a 4ommon a33roa4h s to loo6 mmed ately for ob5e4ts and 4lasses and 4om3onents and then th n6 about how they should f t to-ether.

In !"e !i'eless ,ay of $ ilding, Chr sto3her Ale2ander des4r bed a better a33roa4h, one that s based on 3atterns! '. Start out w th a 4on4e3tual understand n- of the whole n order to understand what needs to be a44om3l shed. &. .. ,. +. Ident fy the 3atterns that are 3resent n the whole. Start w th those 3atterns that 4reate the 4onte2t for the others. A33ly these 3atterns. Re3eat w th the rema n n- 3atterns, as well as w th any new 3atterns that were d s4overed alon- the way. /. B nally, ref ne the des -n and m3lement w th n the 4onte2t 4reated by a33ly nthese 3atterns one at a t me. As a software develo3er, you may not be able to a33ly Ale2anderLs 3attern lan-ua-e a33roa4h d re4tly. >owever, des -n n- by add n- 4on4e3ts w th n the 4onte2t of 3rev ously 3resented 4on4e3ts s surely someth n- that all of us 4an do. ;ee3 th s n m nd as you learn new 3atterns later n th s boo6. 8any 3atterns 4reate robust software be4ause they def ne 4onte2ts w th n wh 4h the 4lasses that m3lement them 4an wor6.

I l@ve RuBoard

I l@ve RuBoard

Chapter */$ )olving the CAD4CA. Proble! with Patterns


?verv ew Rev ew of the CA#@CA8 Problem Th n6 n- n Patterns Th n6 n- n Patterns! Ste3 '

Th n6 n- n Patterns! Ste3 &a Th n6 n- n Patterns! Ste3 &b Th n6 n- n Patterns! Ste3 &4 Th n6 n- n Patterns! Ste3 &d <Ba4ade= Th n6 n- n Patterns! Ste3 &d <Ada3ter= Th n6 n- n Patterns! Ste3 &d <Abstra4t Ba4tory= Th n6 n- n Patterns! Ste3 . Com3ar son w th the Prev ous Solut on Summary
I l@ve RuBoard

I l@ve RuBoard

Overview
In th s 4ha3ter, I a33ly des -n 3atterns to solve the CA#@CA8 3roblem 3resented n Cha3ter ., 1A Problem That Cr es ?ut for Ble2 ble Code.1 In th s 4ha3ter,

I wal6 throu-h the methods needed to solve the earl er CA#@CA8 3roblem. I ta6e you throu-h the n t al des -n 3hase. The deta ls of m3lementat on are left to you.

I 4om3are the new solut on w th the 3rev ous solut on.

I l@ve RuBoard

I l@ve RuBoard

+eview of the CAD4CA. Proble!

In Cha3ter ., I des4r bed the reDu rements for the CA#@CA8 3roblem, a real)world 3roblem that f rst -ot me on the road to us n- des -n 3atterns. The 3roblem doma n s n 4om3uter systems to su33ort a lar-e en- neer n- or-an Gat on, s3e4 f 4ally, to su33ort the r CA#@CA8 system. The bas 4 reDu rement s to 4reate a 4om3uter 3ro-ram that 4an read a CA#@CA8 dataset and e2tra4t the features that an e2 st n- e23ert system needs to be able to do ntell -ent des -n. Th s system s su33osed to sh eld the e23ert system from the CA#@CA8 system. The 4om3l 4at on s that the CA#@CA8 system was n the m dst of 4han-es. Potent ally, there 4ould be mult 3le vers ons of the CA#@CA8 system that the e23ert system would have to nterfa4e w th. After n t al nterv ews, I develo3ed the h -h)level system ar4h te4ture shown n B -ure '&) ' and the follow n- set of reDu rements for the system! -i!ure 1)-1' @i!h-level view o& the solution'

Re9uirement Read a CA#@CA8 model and e2tra4t features

Description
8y system must be able to analyGe and e2tra4t CA#@CA8 des4r 3t ons of 3 e4es of sheet metal.

The e23ert system then determ nes how the sheet

metal should be made and -enerates the reDu red nstru4t ons so that a robot 4an ma6e t.

Be able to deal w th many 6 nds of 3arts

In t ally, I am 4on4erned w th sheet metal 3arts.

Ca4h sheet metal 3art 4an have mult 3le 6 nds of features, n4lud n- slots, holes, 4utouts, s3e4 als, and rre-ulars. It s unl 6ely that there w ll be other features n the future.

>andle mult 3le vers ons of the CA#@CA8 system

Brom B -ure '&)', you 4an nfer that I need the ab l ty to 3lu-)and)3lay d fferent CA#@CA8 systems w thout hav n- to 4han-e the e23ert system.

I l@ve RuBoard

I l@ve RuBoard

"hin(ing in Patterns
Nou have learned several 3atterns and have seen Ale2anderLs 3h loso3hy of des -n! start w th the b - 3 4ture and add deta ls. To a44om3l sh th s on a software 3ro5e4t, I use the follow n- ste3s! '. B nd the 3atterns I have n my 3roblem doma n. Th s s the set of 3atterns to be analyGed. &. Bor the set of 3atterns to be analyGed, do the follow n-! a. b. 4. P 46 the 3attern that 3rov des the most 4onte2t for the other 3atterns. A33ly th s 3attern to my h -hest 4on4e3tual des -n. Ident fy any add t onal 3atterns that m -ht have 4ome u3. Add them to the set of 3atterns to be analyGed. d. .. Re3eat for the sets of 3atterns that have not yet been analyGed.

Add deta l as needed to the des -n. C23and the method and 4lass def n t ons.

Adm ttedly th s wor6s only when you 4an understand the ent re 3roblem doma n n terms of 3atterns. 7nfortunately, th s does not ha33en all the t me. #es -n 3atterns - ve you the way to -et started and then you have to f ll n the rest by dent fy n- relat onsh 3s amon-st the 4on4e3ts n the 3roblem doma n. The method for do n- th s uses 4ommonal ty@var ab l ty analys s and s outs de the s4o3e of th s boo6. >owever, you 4an -et more nformat on about CAA on th s boo6Ls "eb s te at htt3!@@www.netob5e4t ves.4om@d3e23la ned.
I l@ve RuBoard

I l@ve RuBoard

"hin(ing in Patterns: )tep *


In the 3rev ous 4ha3ters, I dent f ed four 3atterns n the CA#@CA8 3roblem. They are!

Abstra4t Ba4tory Ada3ter

Br d-e

Ba4ade

(o other 3atterns stand out at th s 3o nt, but I am o3en to some add t onal ones show nu3.
I l@ve RuBoard

I l@ve RuBoard

"hin(ing in Patterns: )tep /a


I w ll wor6 throu-h the 3atterns, sele4t n- them based on how ea4h 3attern 4reates the 4onte2t for the other 3atterns. "hen determ n n- wh 4h 3atterns 4reate the 4onte2t for others n my 3roblem doma n, I a33ly an easy te4hn Due! I loo6 throu-h all 3oss ble 3a r n-s of the 3atterns, ta6en two at a t me. In th s 4ase, there are s 2 3oss ble 3a r n-s, as shown n B -ure '&)&. -i!ure 1)-)' Di&&erent possible relationships between the patterns'

If you have several other 3atterns, t may loo6 l 6e th s 3ro4ess 4ould -et very nvolved. That turns out not to be the 4ase. " th a l ttle e23er en4e, many of the 3atterns 4an eas ly be el m nated u3 front from 4ontent on for the 3r mary 3attern. 7sually, you have to deal w th only a handful or so. In th s 4ase, there are few enou-h 4omb nat ons that we 4an loo6 at all of the 3oss b l t es. "hat e2a4tly do we mean when we say one 3attern 4reates the 4onte2t for anotherF ?ne def n t on of context s the nterrelated 4ond t ons n wh 4h someth n- e2 sts or o44ursEan env ronment, a sett n-. In the 4ourtyard e2am3le n Cha3ter '', 1>ow #o C23erts #es -nF1 Ale2ander sa d that a 3or4h e2 sts n the 4onte2t of the 4ourtyard. The 4ourtyard def nes the env ronment or the sett n-s n wh 4h the 3or4h e2 sts. A 3attern n a system often relates to other 3atterns n the system by 3rov d n- a 4onte2t for these other 3atterns. In your analys s, t s always valuable to loo6 for whether and how a 3attern relates to the other 3atterns, to loo6 for the 4onte2ts that the 3attern 4reates or 3rov des for the other 3atterns as well as those 4onte2ts n wh 4h the 3attern tself e2 sts. Nou may not be able to f nd these every t me. But, by loo6 n-, you w ll 4reate h -her)

Dual ty solut ons. 9oo6 n- for 4onte2t s an essent al tool to add to your ba- of analys s and des -n tools.

A r#le to #se when considering context$


#ur n- one of my 3ro5e4ts, I was refle4t n- on my des -n a33roa4hes. I not 4ed someth n- that I d d 4ons stently, almost un4ons4 ously! I never worr ed about how I was -o n- to nstant ate my ob5e4ts unt l I 6new what I wanted my ob5e4ts to be. 8y 4h ef 4on4ern was w th relat onsh 3s between ob5e4ts as f they already e2 sted. I assumed that I would be able to 4onstru4t the ob5e4ts that f t n these relat onsh 3s when the t me 4omes to do so. The reason I do th s s that I need to m n m Ge the number of th n-s that I have to 6ee3 n my head dur n- a des -n. 7sually I 4an do so w th a m n mal amount of r s6 when I delay th n6 n- about how to nstant ate ob5e4ts that meet my reDu rements. "orry n- too early s 4ounter3rodu4t veP t s better not to worry about nstant at n- ob5e4ts unt l I 6now what t s that I need to nstant ate. I w ll let tomorrow ta6e 4are of tselfEat least when t 4omes to nstant at onO Perha3s th s seems sens ble to you. I had never heard t stated as a rule and I wanted to 4he46 t out before ado3t n- t as un versal. I trust my ntu t on as a des -ner, but I am 4erta nly not fool3roof. So, I have 4onferred w th several other e23er en4ed develo3ers on th s sub5e4tP w thout e24e3t on, they also follow th s rule. That - ves me 4onf den4e to offer t to you! 0 le: Consider w"at yo need to "ave in yo r syste' before yo concern yo rself wit" "ow to create it( Th s f ts Ale2anderLs 4onte2t rule! "hen you have a des -n 3attern that nvolves 4reat n- ob5e4ts, the ob5e4ts set the 4onte2t for the 3attern.

"hen I am 4ons der n- wh 4h 3attern 4reates 4onte2t for the others, I be- n w th Abstra4t Ba4tory. The Abstra4t Ba4toryLs 4onte2t s determ ned by the ob5e4ts t needs to nstant ate, as shown by the follow n-!

There w ll be a set of 'a*e methods, the m3lementat on of ea4h hav n- a ret rn new xxx n t.

At th s t me, I do not 6now what xxx w ll be.

xxx w ll be determ ned by the ob5e4ts I am us n-.

The ob5e4ts that I w ll need to use are def ned by other 3atterns.

S n4e I 4annot even def ne the Abstra4t Ba4tory unt l I 6now the 4lasses the other 3atterns w ll def ne, t s not the sen ormost 3attern <the 3attern that 4reates the 4onte2t for the other 3atterns=. Therefore, I re5e4t t for now as the 3attern to start wor6 n- on. In fa4t, the Abstra4t Ba4tory w ll be the last 3attern I do <unless another 4reat onal 3attern shows u3 dur n- my n t al des -n, n wh 4h 4ase, both 4reat onal 3atterns w ll v e for be nlast=.

)enior!ost patterns constrain the other patterns$


&enior'ost s my term for the one or two 3atterns that establ sh a 4onte2t for the other 3atterns n my system. Th s s the 3attern that 4onstra ns what the other 3atterns 4an do. ?ther terms you 4ould use are o ter'ost patterns or context-setting patterns(

There are three 3a rs of 3atterns left to 4ons der!

Ada3terTBr d-e Br d-eTBa4ade

Ba4adeTAda3ter

As someone new to 3atterns, I may not see any 3attern that s obv ously de3endent on another 3attern, nor any 3attern that sets the 4onte2t for all others. "hen there s not an obv ous 4ho 4e, I have to wor6 throu-h the 4omb nat on of 3atterns systemat 4ally loo6 n- for the follow n-!

#oes one 3attern def ne how the other 3attern behavesF

#o two 3atterns mutually nfluen4e ea4h otherF

The Ada3ter 3attern s about mod fy n- the nterfa4e of a 4lass nto another nterfa4e that the 4l ent s e23e4t n-. In th s 4ase, the nterfa4e that needs ada3t n- s the $$G(eature. The Br d-e 3attern s about se3arat n- mult 3le 4on4rete e2am3les of an abstra4t on from the r m3lementat on. In th s 4ase, the abstra4t on s (eature and the m3lementat ons are the A' and A& systems. It sounds l 6e the Br d-e w ll need the Ada3ter to mod fy

$$G(eatureLs nterfa4e, that s, the Br d-e w ll use the Ada3ter.


Clearly there s some relat onsh 3 between Br d-e and Ada3ter. Can I def ne one of the 3atterns w thout another, or s one of the 3atterns needed by anotherF 9oo6 n- at the 3atterns tells us what to do.

I 4an tal6 about the Br d-e 3attern as se3arat n- the (eatures from the A' and A& systems w thout a4tually 6now n- how I w ll use the A' and A& systems.

>owever, I 4annot tal6 about us n- an Ada3ter 3attern to mod fy the A& systemLs nterfa4e w thout 6now n- what t w ll be mod f ed nto. " thout the Br d-e 3attern, th s nterfa4e doesnLt e2 st. The Ada3ter 3attern e2 sts to mod fy the A& systemLs nterfa4e to the m3lementat on nterfa4e the Br d-e 3attern def nes.

Thus, the Br d-e 3attern 4reates the 4onte2t for the Ada3ter 3attern. I 4an el m nate the Ada3ter 3attern as a 4and date for sen ormost 3attern.

"he relationship between context and used by.


?ften, t seems that when one 3attern uses another 3attern, the 3attern that s used s w th n the 4onte2t of the 3attern do n- the us n-. There are l 6ely e24e3t ons to th s rule, but t seems to hold most of the t me.

(ow I only have to 4om3are Br d-eTBa4ade and Ba4adeTAda3ter. I w ll loo6 at the Br d-e and Ba4ade relat onsh 3 f rst be4ause f the Br d-e turns out to be the 3r mary 3attern there as well, I do not need to 4ons der the Ada3terTBa4ade

relat onsh 3 <remember, I am only try n- to dent fy the sen ormost 3attern at th s 3o nt=. It should be read ly a33arent that the same lo- 4 that a33l ed to Br d-e and Ada3ter also a33l es to Br d-e and Ba4ade!

I w ll be us n- the Ba4ade 3attern to s m3l fy the A' systemLs nterfa4e.

But what w ll be us n- the new nterfa4e I 4reateF ?ne of the m3lementat ons of the Br d-e 3attern.

Therefore, the Br d-e 3attern 4reates the 4onte2t for the Ba4ade. The Br d-e s the sen ormost 3attern. A44ord n- to Ale2ander, I am su33osed to start w th the whole. Jo n- ba46 to the be- nn n-, I f nd that I do not yet have the 4onte2t for the Br d-e.
I l@ve RuBoard

I l@ve RuBo ard

"hin

So, I retra 4e the des n ste3s unt l I 4om e to the

4ont e2t n wh 4 h the Br de 3atte rn show s u3. I want to bu ld a syste m that trans lates CA#@ CA8 mod els nto an (C set to - ve to a ma4

h ne so that the 3art des4 r bed by the mod el 4an be bu lt <see B -ur e '&) .=. -i!u

?f 4our se, I had

e23a nded th s des n by not n that I 4ould use ob5e 4t) or en ted des n te4h n Du es to have the e23e rt syste m use a

Mod el
4lass to -et

ts nfor mat on.

Mod el
woul d have two vers ons, one for ea4h of the CA#@ CA8 syste ms. Th s s show n n B -ur e '&) ,. -i!u

Rem emb er, I am not 4on4 erne d abou t the des n of the e23e rt syste m.

"h l e nter est n <and , n man y ways more 4hall enn-=, that des n had alrea dy been wor6 ed out. 8y fo4us s on the des n of the

Mod el. I

6now that the

Mod el
4ons sts of

(ea ture
s, as show n n B -ur e '&) +.R'S

R'S

The

d fferen 4es betwee n

!M odel/
and

"M odel/
3resent l ttle d ff 4ult y. Therefo re, I w ll only d s4uss

Mod

el
.

-eneral

-i!u

(ow, I am read y for the Br de 3atte rn. It s a33a rent that I have mult 3le

(ea

ture
s <the abstr a4t o n= w th mult 3le CA#@ CA8 syste ms <the m3l eme ntat ons=. Thes e are the ob5e 4ts that set the 4ont e2t for the Br de 3atte

rn. The Br de 3atte rn relat es the

(ea ture
s to the d ffer ent CA#@ CA8 syste m m3l eme ntat ons. The

(ea ture/
4lass s the

A%s tra cti

on n
the Br de 3atte rn wh le the A' and A& syste ms are the

.'p le' ent ati ons.


But what abou t the

Mod elF
Is there a Br de 3atte rn

3res ent here as wellF (ot reall y. I 4an bu ld the

Mod el
us n nher tan4 e be4a use the only th nabou t the

Mod el
that var e s s the m3l eme

ntat on that s be n used . In th s 4ase, I 4ould ma6 e der v at on s of the

Mod el
for ea4h CA#@ CA8 syste m as n B -ur e '&) /. If I tr ed

us n -a Br de 3atte rn for the

Mod el,
ILd -et the des n show n n B -ur e '&) *. -i!u

-i!u

(ote that I do not reall y have a

Br de 3atte rn n B -ur e '&)* be4a use

Mod el s
not vary ne24e 3t for the m3l eme ntat on. In the

(ea ture
,I have d ffer ent ty3e s of

(ea

ture
s that have d ffer ent ty3e s of m3l eme ntat ons Ea Br de 3atte rn does e2 st here. I start m3l eme nt nthe Br de 3atte rn by us n -

(ea ture/
as the abstr a4t o n and us n - A' and A& as the bas s for the m3l eme ntat ons. To trans late the 3robl em nto the Br de 3atte rn, I

start w th the stan dard e2a m3le of the Br de 3atte rn and then subs t tut e 4lass es nto t. B -ur e '&)0 show s the stan dard Ged, s m3l f ed form

<som et m es 4alle d the cano nical form =. -i!u

In the 3robl em,

(ea ture/
ma3 s to

A%s tra cti on.


Ther

e are f ve d ffer ent 6 nds of featu res! slot, hole, 4uto ut, rreular, and s3e4 al. The m3l eme ntat ons are the A' and A& syste msP I 4hoo se to nam

e the 4lass es res3 ons b le for thes e m3l eme ntat ons

!. 'p
and

". 'p,
res3 e4t v ely. Subs t tut nthe 4lass es nto the 4ano n 4al Br de

3atte rn - ves B -ur e '&) %. -i!u

In B -ur e '&) %, the

(ea ture
s are be n m3l eme nted by an

.'p (ea ture


, wh 4 h s e the ra

!. 'p
or a

". 'p.
In th s des n,

.'p (ea ture/


woul d have to have an nter fa4e that allow ed for

(ea

ture/
to -et what ever nfor mat on t need ed to - ve

Mod el
the nfor mat on t reDu este d. Thus ,

.'p (ea ture/


woul d have an nter fa4e n4lu d n-

meth ods su4h as

It woul d also have meth ods used by only som e

(ea ture
s!

Note : ?nly featu res that need th s nfor mat on shoul d 4all th s meth od. 9ater ,I w ll tal6 abou t how to use th s 4ont e2tu al nfor mat

on to hel3 debu - the 4ode .

I l@ve RuBo ard

I l@ve RuBo ard

"hin

8ayb eI 4ann ot see how to fns h the m3l eme ntat on yet, and that

s o6ay. I st ll have other 3atte rns to a33ly . 9oo6 n- at B -ur e '&) %, I shoul d as6 mys elf f any other 3atte rns show u3 that I had not 3rev ously

dent f ed. I do not see any add t onal 3atte rns. Ther e s only the 4hall en-e of hoo6 nthe A' and A& CA#@ CA8 syste ms nto the des n. That s

what the Ba4a de and Ada3 ter 3atte rns w ll do for me.

I l@ve RuBo ard

I l@ve RuBo ard

"hin

(e2t ,I need to ver f y f any of the

rema n n3atte rns 4reat ea 4ont e2t for ea4h other . In th s 4ase, Ba4a de and Ada3 ter now 4lear ly relat e to d ffer ent 3 e4e s of the des n and are

nde 3end ent of ea4h other . Ther efore ,I 4an a33l y them n what ever orde rI 4hoo se. I w ll arb t rar ly 3 46 the Ba4a de to a33l y ne2t, wh 4 h

resul ts n B -ur e '&) '$. -i!u

A33l y nthe Ba4a de 3atte rn mea ns that I nser ta fa4a

de betw een the A' mod ules and the

!. 'p
ob5e 4t that s -o n - to use them .

!( aca de
has s m3l f ed meth ods that relat e to what

!.

'p
need s to do. Ca4h meth od n

!( aca de
w ll loo6 l 6e a ser e s of fun4t on 4alls on the A' syste m. The 6 nd of nfor mat on that I

need n orde r to 4all thes e fun4t ons w ll deter m ne how

!. 'p s
m3l eme nted. Bor e2a m3le , whe n us n - A', I need to tell t wh 4 h mod

el to use and what the

(ea ture
Ls I# s. All

!. 'p
ob5e 4ts that use the

!( aca de
w ll there fore need to 6now th s nfor mat on. S n4 e th s

s m3l eme ntat on) s3e4 f4 nfor mat on, t w ll need to 6now t tself , rathe r than -ett n- t from the 4all n -

(ea ture
. Thus , na A' syste

m, ea4h

(ea ture/
w ll need ts own

!. 'p
ob5e 4t <to reme mber syste m) s3e4 f4 nfor mat on abou t the featu re=. I w ll -o over th s n more deta

l on4e the -ene ral ar4h te4tu re s 4om 3lete d.

"a(i

Carl er n th s 4ha3 ter, I men t one d that som e of the met hods of the m3l eme ntat

by us n - an asse rt n the

get Edg eTy pe


met hod that ver f es that the 4all n -

(ea tur e s
of the a33r o3r a te ty3e.

I l@ve RuBo ard

I l@ve RuBo ard

"hin

>av na33l ed Ba4a de, I 4an now a33l y Ada3 ter. Th s resul ts n B -ur e '&) ''. -i!u

I l@ve RuBo ard

I l@ve RuBo ard

"hin

All that s left s the Abstr a4t Ba4to ry. As t turns out, th s 3atte

rn s not need ed. The rat o nale for us n - an Abstr a4t Ba4to ry was to ensu re all of the m3l eme ntat on ob5e 4ts were of ty3e

! f
I had a A' syste

m or of ty3e

" f
I had a A& syste m. >ow ever, the

Mod el
ob5e 4t tself w ll 6now th s. Ther e s no 3o nt m3l eme nt na 3atte rn f som e other ob5e

4t 4an eas l y en4a 3sula te the rules of 4reat on. I left the Abstr a4t Ba4to ry n the set of 3atte rns be4a use wh le I was f rst solv nth s 3robl

em I dd th n6 the Abstr a4t Ba4to ry was 3res ent. It also llust rates how th n6 nthat a 3atte rn s 3res ent whe n t s not s not ne4e ssar l y

4oun ter3r odu4 t ve.


I l@ve RuBo ard

I l@ve RuBo ard

"hin

The deta ls of the des n may st ll ta6e som e wor6 . >ow ever, I woul d 4ont

nue w th the des n by follo w nAle2 ande rLs man date of des n nby 4ont e2t. Bor e2a m3le , whe nI see how I need to m3l eme nt a

Slo

t(e atu re
4lass or the

!. 'p
4lass ,I shoul d reme mber how the 3atte rns nvol ved are used . In th s 4ase, I note that n the Br de 3atte

rn, the meth ods nvol v nthe abstr a4t o ns are nde 3end ent of m3l eme ntat on. Th s mea ns that the

A%s tra cti on


4lass <(ea

ture
= and all of

ts der v at on s <Slo

t(e atu re, Hol e(e atu re,


and so forth = 4ont an no m3l eme ntat on nfor mat on. Im3l eme ntat on nfor mat on s

left to the

.'p le' ent ati on


4lass es. Th s mea ns the

(ea ture/
der v at on s w ll have meth ods su4h as

get Loc ati on


and

get Len gth,

wh le the

.'p le' ent ati ons


w ll 4ont an a way to a44e ss th s reDu red nfor mat on. A

!. 'p
ob5e 4t, for e2a m3le , woul d need to 6now

the I# of the

(ea ture/
n the A' syste m. S n4 e ea4h

(ea ture/
has a un D ue I#, th s mea ns there w ll be one

.'p le' ent ati on


ob5e

4t for ea4h

(ea ture/
ob5e 4t. The meth ods n the

!. 'p
ob5e 4t w ll use th s I# to as6 the

!( aca de
for nfor mat on abou t the ob5e 4t.

A 4om 3ara ble solut on w ll e2 st for the A& m3l eme ntat ons. In th s 4ase, the

". 'p
ob5e 4ts w ll 4ont an a refer en4e to the

$$G (ea ture/

n Dues t on.
I l@ve RuBo ard

I l@ve RuBo ard

Co!

Com 3are th s new solut on, show n n B -ur e '&) '', w th the earl er solut on, wh 4 h s

show n a-a n n B -ur e '&) '&. -i!u

Anot her way to 4om 3are two solut

ons s to read them . In other word s, the d a-r ams v sua lly show nher tan4 e <the is-a relat onsh 3= and 4om 3os t on <the "asa relat onsh 3=. Read

thes e d a-r ams us n thos e word s wher e the relat onsh 3s are 3res ent. In the or nal solut on, I had a mod el that 4ont a ns

(ea

ture
s.

(ea ture
s are e the r slot featu res, hole featu res, 4uto ut featu res, rreular featu res, or s3e4 al featu res. Slot featu res are e the r A' slots or

A& slots . A' slots use the A' syste m wh le the A& slots use the

$$G Slot
. >ole featu res are e the r A' hole featu res or A& hole featu res. A'

hole featu res use the A' syste m wh le the A& hole featu res use the

$$G Hole
. Jett nt red of th s alrea dy, arenL t youF (ow read the

lates t solut on. I have a mod el that 4ont a ns

(ea ture
s.

(ea ture
s are e the r slot featu res, hole featu res, 4uto ut featu res, rreular featu res, or

s3e4 al featu res. All featu res 4ont an an m3l eme ntat on wh 4 h s e the ra A' m3l eme ntat on or a A& m3l eme ntat on. A' m3l eme ntat ons

use a A' Ba4a de to a44e ss the A' syste m wh le A& m3l eme ntat ons ada3 t an

$$G (ea ture


. ThatL s t. It soun ds mu4 h bette r than 5ust

a 3ort on of the other solut on.

I l@ve RuBo ard

I l@ve RuBo ard

)#!

In th s 4ha3 ter, I show ed how the stan dard way of do n des -

ns 4an often lo46 us nto syste ms that are hard to ma n ta n. ?fte n, t 4an be d ff 4 ult to see the fores t for the trees be4a use I be4o me overl y fo4us

ed on the deta ls of the syste mE the 4lass es. Chr s to3h er Ale2 ande r - ves us a bette r way. By us n 3atte rns n the 3robl em dom

a n, I 4an loo6 at the 3robl em na d ffer ent way. I start w th the b3 4tu re and add d st n4t o ns as I -o. Ca4h 3atte rn - ves me more nfor mat on

than what I had befor eI used t. By sele4 t nthe 3atte rn that 4reat es the b -est 3 4tu reE the 4ont e2t for the syste mE and then nser t n-

the ne2t s -n f 4an t 3atte rn, I deve lo3e d an a33l 4at o n ar4h te4tu re that I 4ould not have seen by loo6 n- at the 4lass es alon e. Thus ,I be-

n to learn to des n by 4ont e2t nste ad of by 3utt nto-et her 3 e4e s that were dent f ed lo4all y. 9 6e the two 4ar3 enter s n Cha3 ter +, 1An

Intro du4t on to #es -n Patte rns,1 who were try n - to de4 d e betw een a dove ta l 5o nt and a m ter 5o nt, t s the 4ont e2t that shoul d sha3 e the des -

n. In des n de4 s ons, we often -et bo-ed dow n by the deta ls and for-e t abou t the lar-e r 4ont e2t of the syste m. The deta ls 4ast a

4loud arou nd the b -er 3 4tu re by fo4us nus on small , lo4al de4 s ons. Patte rns - ve you the lanua-e to r se abov e the deta ls and br nthe

4ont e2t nto the d s4u ss on n 3ra4t 4al ways . Th s ma6 es t more l 6ely that you w ll see the for4e s 3res ent n the 3robl em dom a n. Patte rns

hel3 us a33l y what other des ners befor e us have learn ed abou t what does and does not wor6 . In so do n -, they hel3 to 4reat e syste ms that

are robu st, ma n ta na ble, and al ve .

I l@ve RuBo ard

I l@ve RuBo ard

Cha

?v er ve w Th e ?3 en ) Clo se

d Pr n4 3le Th e Pr n4 3le of #e snn fro m Co nt e2t Th e Pr n4 3le of Cn 4a 3s ula tn Aa

ra to n Su m ma ry
I l@ve RuBo ard

I l@ve RuBo ard

Over

Prev ously ,I des4 r bed how des n 3atte rns 4an be used at both the

lo4al and -lob al level s. At lo4al level s, 3atte rns tell us how to solve 3art 4ular 3robl ems w th n the 4ont e2t of the 3atte rns. At -lob al level s,

3atte rns 4reat ea ma3 of how the 4om 3one nts of the a33l 4at o n nter relat e w th one anot her. ?ne way to stud y des n 3atte rns s to

learn how to use them more effe4 t vel y at both the lo4al and -lob al level s. They w ll - ve you tools to -et a bette r hand le on your 3robl em. Anot

her way to stud y des n 3atte rns s learn the r me4 han s ms and the 3r n4 3les and strat e- es that unde rl e them . 9ear n nthes e w ll m3r ove

your ab l t es as an anal yst and des ner. Nou w ll 6now what to do even n s tua t ons whe na des n 3atte rn has not yet been deve lo3e d, be4a

use you w ll alrea dy have the bu ld nblo46 s need ed to solve the 3robl em. In th s 4ha3 ter,

I l@ve RuBo ard

I l@ve RuBo ard

"he

Soft ware 4lear ly need s to be e2te ns bl e. >ow ever, ma6 n4han -es to soft ware runs the r s6 of ntro

du4 n3robl ems. Th s d le mma led Bertr and 8eye r to 3ro3 ose the open close d princ iple.
R'S

To

3ara 3hra se th s 3r n4 3le, the mod ules, meth ods,

and 4lass es shoul d be o3en for e2te ns on , wh le 4lose d for mod f 4at on.R&S In other word s, we must des n our soft ware so that we 4an e2te nd the 4a3a

blt es of our soft ware w th out 4han - nt.

R'S

8eyer, B., ObjectOriente d &oftwar e Constr ction, 733er Saddle R ver, (.J.! Prent 4 e >all, '%%*, 3. +*.

R&S

See

th s boo6Ls "eb s te for a l n6 to 1The ?3en) Closed Pr n4 3l

e,1 an e24elle nt art 4le by Robert C. 8art n. Jo to htt3!@@ www.n etob5e4 t ves.4o m@d3e2 3la ned.

As 4ontr ad 4t ory as th s may soun d at f rst, you have alrea dy seen e2a m3le s of t. In the Br d-

e 3atte rn, for nsta n4e, t s Du te 3oss ble to add new m3l eme ntat ons <that s, to e2te nd the soft ware = w th out 4han - nany of the e2 st

n4lass es.
I l@ve RuBo ard

I l@ve RuBo ard

"he

Ale2 ande r tells us to des n from 4ont e2t, to 4reat e the b3 4tu re befor e des n n-

the deta ls n wh 4 h our 3 e4e s a33e ar. 8ost des n 3atte rns follo w th s a33r oa4h , som e to a -reat er e2te nt than other s. ?f the four 3atte

rns I have des4 r bed so far, the Br de 3atte rn s the best e2a m3le of th s. Refer to the Br de 3atte rn d a-r am n Cha3 ter %, 1The Br d-

e Patte rn,1 <see B -ur e %) '.=. "he n de4 d nhow to des n the

.'p le' ent ati on


4lass es, th n6 abou t the r 4ont e2t! the way that the 4lass

es der v ed from the

A%s tra cti on


4lass w ll use them . Bor e2a m3le , fI were wr t n- a syste m that need ed to draw sha3 es on d ffer ent

ty3e s of hard ware and that there fore reDu red d ffer ent m3l eme ntat ons, I woul d use a Br de 3atte rn. The Br de tells me that the sha3

es w ll use my m3l eme ntat ons <that s, the draw n3rorams I w ll wr te = thro u-h a 4om mon nter fa4e. #es -n n from 4ont e2t, as Ale2

ande r woul d have , mea ns that I shoul d f rst loo6 at the reDu reme nts of my sha3 esE that s, what am I -o n - to have to draw F

Thes e sha3 es w ll deter m ne the reDu red beha v ors for my m3l eme ntat ons. Bor e2a m3le , the m3l eme ntat ons <the draw n3rorams = may

have to draw l nes, 4 r4le s, and so forth . By us n 4om mon al ty@ var a b l ty anal ys s n 4on5 un4t on w th the 4ont e2t w th n wh 4 h my

4lass es o44ur ,I 4an s mu ltane ously see both the 4ase sI must hand le now and 3oss ble futur e 4ase s. I 4an then de4 d e how -ene ral G ed I want

to ma6 e the m3l eme ntat ons base d on the 4ost of e2tra -ene ral G at on . Th s often leads to a more -ene ral m3l eme ntat on than I woul d have

thou -ht of other w se, but w th only marnally h -h er 4ost. Bor e2a m3le , whe n loo6 n- at my need s to draw sha3 es, I m -h t read ly dent

fy l nes and 4 r4le s as reDu reme nts. If I as6 mys elf, 1"h at sha3 es do I not su33 ort w th l nes and 4 r4le s,1 I m -h t not 4 e that I woul d not

be able to m3l eme nt ell 3s es. (ow I have a 4ho 4 e!

The 3r or e2a m3le llust rates anot her

m3o rtant 4on4 e3t n des n! 5ust be4a use an o33o rtun ty e2 st s does nLt mea n t has to be 3urs ued. 8y e23e r en4 e w th des n 3atte rns

s that they - ve me ns hts nto my 3robl em dom a ns. >ow ever, I donLt alwa ys <nor even usual ly= a4t on thes e ns hts by wr t nfor

s tua t ons that have not yet ar se n. >ow ever, by hel3 nme des n from 4ont e2t, the 3atte rns them selve s allow me to ant 4 3ate 3oss ble var a

t on be4a use I have dvd ed my syste m nto well) beha ved 4lass es, there by ma6 n4han -es eas e r to a44o mmo date. #es -n 3atte rns hel3 me see

wher e var a t ons may o44ur , not so mu4 h wh 4 h 3art 4ular var a t ons w ll o44ur . The well) def n ed nter fa4e I use to 4ont an my 4urre nt var a t ons

often 4ont a ns the m3a 4ts of new reDu reme nts 5ust as well. The Abstr a4t Ba4to ry s anot her -ood e2a m3le of des n nby 4ont e2t. I may

unde rstan d early on that a fa4to ry ob5e 4t of som e sort w ll be used to 4oor d nat e the nsta nt at on of fam l es <or sets= of ob5e 4ts. >ow ever,

there are man y d ffer ent ways to m3l eme nt t as follo ws.

,m ple me nta tio n+ 7s nder ved 4las ses

7s na s nle ob5 e4t w th sw t 4he s

7s na 4onf -ur at o n f le w th sw t 4he s

7s na 4onf -ur at o n f le w th RTT I <ru nt me) ty3 e) den tf4 at o n=

" th all of thes e

4ho 4 es, how do you de4 d e wh 4 h one to use to m3l eme nt the Abstr a4t Ba4to ryF #e4 de from the 4ont e2t n wh 4 h t a33e ars. Ca4h

of the four 4ase s 5ust show n has adva ntaes over the other s, de3e nd n u3on fa4to rs su4h as

"

Th s l st s not 4om 3lete , nor even s the l st of m3l eme ntat on 3oss blt es. "hat shoul d be ev d ent to you,

howe ver, s that try n - to de4 d e how to m3l eme nt an Abstr a4t Ba4to ry w th out unde rstan d nhow t w ll be used <that s, w th out unde rstan d n-

ts 4ont e2t= sa foolLs erran d.

=ow

"he n try n - to de4 de betw een alter nat v e m3l eme ntat ons, man y deve lo3er s as6 the Dues

t on, 1"h 4h of thes e m3l eme ntat ons s bett erF1 Th s s not the best Dues t on to as6. The 3robl em s that often one m3l eme ntat on s not

nher ently bett er than anot her. A bett er set of Dues t ons to as6 s, for ea4h alter nat v e, 17nd er what 4 r4u msta n4es woul d th s alter nat v

e be bett er than the othe r alter nat v eF1 Then as6, 1"h 4h of thes e 4 r4u msta n4e s most l 6e my 3robl em dom a nF1 It s a smal l matt er of

sto3 3 nand ste3 3 nba46 . 7s n th s a33r oa4h tend s to 6ee3 me mor e awar e of the var a t on and s4ala b l ty ssue s n my 3robl em dom a n.

The Ada3 ter 3atte rn llust rates des n from 4ont e2t be4a use t almo st alwa ys show s u3 w th na 4ont e2t. By def n t on, an Ada3 ter s used to

4onv ert an e2 st nnter fa4e nto anot her nter fa4e. The obv ous Dues t on s, 1>ow do I 6now what to 4onv ert the e2 st nnter fa4e toF1 Nou ty3 4

ally donLt unt l the 4ont e2t s 3res ente d <that s, the 4lass to wh 4 h you are ada3 t n-= . I have alrea dy show n that Ada3 ters 4an

be used to ada3 ta 4lass to f t the role of a 3atte rn that s 3res ent. Th s was the 4ase n my CA#@ CA8 3robl em, wher eI had an e2 st nm3l

eme ntat on that need ed to be ada3 ted nto my Br de) dr ve n m3l eme ntat on. The Ba4a de 3atte rn s very sml ar to the Ada3 ter 3atte rn n

term s of 4ont e2t. Ty3 4 ally, t s def n ed n the 4ont e2t of other 3atte rns or 4lass es. That s, I must wa t unt l I 4an see who want s to use the Ba4a de n

orde r to des n ts nter fa4e. Carly on n my use of 3atte rns I tend ed to th n6 I 4ould alwa ys f nd wh 4 h 3atte rns 4reat ed 4ont e2t for other s. In

Ale2 ande rLs A Patt ern Lang age , he s able to do 5ust that w th 3atte rns n ar4h te4tu re. S n4 e man y 3eo3 le are tal6 nabou t 3atte rn

lanua-e s for soft ware ,I won dere d, 1"hy 4anLt IF1 It see ms 3rett y 4lear that Ada3 ters and Ba4a des woul d alwa ys be def n ed n the 4ont e2t

of som eth n else. R -ht F "ron -. ?ne -reat adva ntae of be n -a soft ware deve lo3er who also tea4 hes s that I have the o33o rtun ty to

-et nvol ved n man y more 3ro5e 4ts than I 4ould 3oss bly be nvol ved n as a deve lo3er only. Carly n my tea4 h ns of des n 3atte rns I thou

-ht Ada3 ters and Ba4a des woul d alwa ys 4om e after other non4 reat onal 3atte rns n the orde r of def n n4ont e2t. In fa4t, they usual ly do.

>ow ever, som e syste ms have the reDu reme nt of bu ld nto a 3art 4ular nter fa4e. In th s 4ase, t s a Ba4a de or an Ada3 ter <5ust one of man y n

the syste m, of 4our se= that may be the sen o rmos t 3atte rn.

I l@ve RuBo ard

I l@ve RuBo ard

"he

Seve ral 3eo3 le have rema r6ed abou

ta 4erta n sml ar ty n all of the des ns they have seen me 4reat e! my nher tan4 e h era r4h e s rarel y -o more than two level s of 4lass es dee3

. Thos e that do ty3 4 ally ft nto a des n 3atte rn stru4 ture that reDu res two level s as a base for the der v ed 4lass es <the #e4o rator

3atte rn, d s4u ssed n Cha3 ter '+, s an e2a m3le that uses three level s=. The reas on for th s s that one of my des n -oals s neve r to

have a 4lass 4ont an two th ns that are vary nthat are som ehow 4ou3l ed to ea4h other . The 3atte rns I have des4 r bed so far do llust rate d ffer ent

ways of en4a 3sula t nvar a t on effe4 t vely . The Br de 3atte rn s an e24el lent e2a m3le of en4a 3sula ted var a t on. The m3l eme ntat ons 3res

ent n the Br de 3atte rn are all d ffer ent but are a44e ssed thro u-h a 4om mon nter fa4e. (ew m3l eme ntat ons 4an be a44o mmo date d by

m3l eme nt nthem w th n th s nter fa4e. The Abstr a4t Ba4to ry en4a 3sula tes the var a t on of wh 4 h sets or fam l es of ob5e 4ts 4an be

nsta nt at ed. Ther e are man y d ffer ent ways of m3l eme nt nth s 3atte rn. It s usef ul to note that even f one m3l eme ntat on s nta lly 4hos en

and then t s deter m ne d anot her way woul d have been bette r, the m3l eme ntat on 4an be 4han -ed w th out affe4 t nany other 3art of the syste

m <s n4 e the nter fa4e for the fa4to ry does not 4han -e, only the way t s m3l eme nted =. Thus , the not o n of the Abstr a4t Ba4to ry tself < m3l eme

nt nto an nter fa4e= h des all of the var a t ons of how to 4reat e the ob5e 4ts. The Ada3 ter 3atte rn s a tool to be used to ta6e d s3a rate ob5e 4ts and

- ve them a 4om mon nter fa4e. Th s s often need ed now that I am des n nto nter fa4es as 4alle d for n man y 3atte rns. The Ba4a de ty3 4

ally does not en4a 3sula te var a t on. >ow ever, I have seen man y 4ase s wher ea Ba4a de was used to wor6 w th a 3art 4ular subs yste m. Then

, whe n anot her subs yste m 4am e alon -, a Ba4a de for the new subs yste m was bu lt w th the sam e nter fa4e. Th s new 4lass was a

4om b nat on Ba4a de and Ada3 ter n that the 3r m ary mot vat o n was s m3l f 4at on, but now had the adde d 4ons tra n t of be n - the sam e as

the one used befor e so none of the 4l en t ob5e 4ts woul d need to 4han -e. 7s n -a Ba4a de th s way h des var a t ons n the subs yste ms be n

used . Patte rns are not 5ust abou t en4a 3sula t nvar a t on, howe ver. They also dent fy relat onsh 3s betw een var a t ons .I w ll show more

abou t th s n the ne2t se4t on of th s boo6 . Refer r nto the Br de 3atte rn a-a n, note that the 3atte rn not only def n es and en4a 3sula tes

the var a t ons n the abstr a4t o n and m3l eme ntat on, but also def n es the relat onsh 3 betw een the two sets of var a t ons .
I l@ve RuBo ard

I l@ve RuBo ard

)#!

In th s 4ha3 ter, I have show n how 3atte rns llust rate two 3owe rful des n strat e- es !

Thes e strat e- es allow us to defer de4 s ons unt l we 4an see the ram f 4at ons of thes e de4 s ons. 9oo6 n- at the 4ont e2t from wh 4 h we are des n n-

- ves us bette r des ns. By en4a 3sula t nvar a t on, I 4an a44o mmo date man y futur e var a t ons that may ar se but woul d not be a44o mmo date

d whe nI do not try to ma6 e my des ns more -ene ral) 3ur3 ose. Th s s 4r t 4 al for thos e 3ro5e 4ts that do not have all of the reso ur4e s

you woul d l 6e to have <n other word s, all 3ro5e 4ts=. By en4a 3sula t nvar a t on a33r o3r a tely, I 4an m3l eme nt only thos e featu res I need w th out

sa4r f4n futur e Dual ty. Try n - to f -ur e out and a44o mmo date all 3oss ble var a t ons ty3 4 ally does not lead to bette r syste ms but often leads

to no syste m at all. Th s s 4alle d 3aral ys s by anal ys s.

I l@ve RuBo ard

I l@ve RuBo ard

Par t 5 : = a n d l i

n g 5 a r i a t i o n s w i t h D e s i g n P a t t e r n s
Part Over

view In th s 3art, I wor6 thro u-h anot her 4ase stud y. In th s 4ase stud y, I w ll 4ons der the reDu reme nts for the 3robl em one at a t me, rathe

r than s3e4 fy never y reDu reme nt u3 front .I w ll des4 r be a syste m that s 4urre ntly wor6 nwhe na new reDu reme nt 4om es n that for4e

s me to f nd the best way to mod fy the 4ode .I w ll use th s 3ro4 ess to 3res ent a few new des n 3atte rnsE one for ea4h new reDu reme nt.

Cha pte r

',

'+

'/

'*

'0

'%

Re Du re me nts Th e Str at ey Pat ter n Be ld (o tes ! 7s nth e Str at ey Pat ter n Su

m ma ry
I l@ve RuBo ard

I l@ve RuBo ard

Over

Th s 4ha3 ter ntro du4e sa new 4ase stud y, wh 4 h 4om es from the area of etailin g

<ele4 tron 4 reta l nover the Inter net=. It also bens a solut on us n - the Strat e-y 3atte rn. The solut on to th s 4ase stud y w ll 4ont nue to evolv

e thro u-h Cha3 ter &$, 1The Anal ys s 8atr 2.1 In th s 4ha3 ter,

I l@ve RuBo

ard

I l@ve RuBo ard

An

8any t me s n l fe and man y t me s n soft ware a33l 4at o ns, you have to ma6 e 4ho 4 es abou t the -ene ral a33r

oa4h to 3erfo rm n -a tas6 or solv n- a 3robl em. 8ost of us have learn ed that ta6 n - the eas e st route n the short run 4an lead to ser o us 4om 3l 4a

t ons n the lonrun. Bor e2a m3le , none of us woul d -nor e ol 4han -es for our 4ar beyo nd a 4erta n 3o nt . True, I may not 4han -e the

ol ever y .,$$ $ m les , but I also do not wa t unt l .$,$ $$ m les befor e 4han - nthe ol<f I dd so, there woul d be no need to 4han -e the

ol any more ! the 4ar woul d not wor6 O=. ?r 4ons der des6 to3 f l nEthe te4h n Du e man y of us have of us n - the to3s of our des6 s as a f l n-

4ab n et. It wor6 s well n the short run, but n the lonrun, t be4o mes touh to f nd th ns as the 3 les -row . # sa ster often 4om es n the lon-

run from subo 3t m al de4 s ons mad e n the short run. 7nfo rtun ately , whe n t 4om es to soft ware deve lo3m ent, man y 3eo3 le have not learn

ed thes e lesso ns yet. 8any 3ro5e 4ts are only 4on4 erne d w th hand l nmm ed at e, 3res s nneed s, w th out 4on4 ern for futur e ma n tena

n4e. Ther e are seve ral reas ons 3ro5e 4ts tend to -nor e lon-) term ssue s l 6e ease of ma n tena n4e or ab l t y to 4han -e. Com mon e24u ses n4lu

de

The 4ho 4 es see m to be

S n4 e man a-e

ment s unde r 3res sure to del v er and not to ma n ta n, may be thes e resul ts are not sur3r s n. >ow ever, w th a mom entLs refle 4t on

, t be4o mes a33a rent that there s an unde rly n bel e f syste m that 3rev ents man y soft ware deve lo3er s from see n other alter nat v esE the

bel e f that des n nfor 4han -e s more 4ostl y than des n nw th out 4ons der n 4han -e. But th s s not ne4e ssar l y the 4ase. Inde ed, the

o33o s te s often true! "he n you ste3 ba46 to 4ons der how your syste m may 4han -e over t me, a bette r des n usual ly be4o mes a33a rent

to you En v rtu ally the sam e amo unt of t me that woul d be reDu red to do a 1stan dard 1 -et) t) done )now des n. The a33r oa4h I use

n the follo w n4ase stud y 4ons ders how syste ms may 4han -e. >ow ever, t s m3o rtant to note that I w ll be ant 4 3at nthat 4han -es w ll o44u

r and loo6 to see w"er e they w ll o44ur .I w ll not be try n - to ant 4 3ate the e2a4 t natu re of the 4han -e. Th s a33r oa4h s base d on the 3r n4

3les des4 r bed n the Janof Bour boo6 !

R'S

Jam ma, C., >el m, R., John son, R., Al ss des, J., Desi gn Patt erns : Ele 'en ts of 0e sabl e

Obj ectOrie nted &oft war e Rea d n, 8as s.! Add son) "esl ey, '%% +, 3. '0.

R&S

b d, 3. &$.

R.S

b d, 3. &%.

"hat I su--

est s that whe n fa4e d w th mod fy n4ode to hand le a new reDu reme nt, you shoul d at least 4ons der follo w nthes e strat e- es . If follo w nthes

e strat e- es w ll not 4ost s -n f 4an tly more to des n and m3l eme nt, then use them . Nou 4an e23e 4t a lon-) term bene ft from do n - so, w th only

a mod est short ) term 4ost <f any=. I am not 3ro3 os nto follo w thes e strat e- es bl nd ly, howe ver. I 4an test the valu e of an alter nat v

e des n by e2a mn nhow well t 4onf orms to the -ood 3r n4 3les of ob5e 4t) or en ted des n. Th s s esse nt all y the sam e a33r oa4h I

used n der v nthe Br de 3atte rn n Cha3 ter %, 1The Br de Patte rn.1 In that 4ha3 ter, I mea sure d the Dual ty of alter nat v e des ns by see n

wh 4 h one follo wed ob5e 4t) or en ted 3r n4 3les the best.

I l@ve RuBo ard

I l@ve RuBo ard

%niti

Su33 ose I am wr t nan e) ta l syste

m that su33 orts sales n the 7n te d Stat es. The -ene ral ar4h te4tu re has a 4ontr oller ob5e 4t that hand les sales reDu ests. It dent f es whe

na sales orde r s be n reDu este d and hand s the reDu est off to a

Sal es$ rder/


ob5e 4t to 3ro4 ess the order . The syste m loo6s som eth n

l 6e B -ur e ',) '. -i!u

The fun4t ons of

Sal es$ rder/


n4lu de

>

Som e of thes e fun4t ons are l 6ely to be m3l eme nted w th the hel3 of other ob5e 4ts. Bor e2a m3le ,

Sal es$ rder/


woul d not ne4e ssar l

y 3r nt tself P rathe r, t serv es as a hold er for nfor mat on abou t sales orde rs. A 3art 4ular

Sal es$ rder/


ob5e 4t 4ould 4all a

Sal esT ic0 et

ob5e 4t that 3r nt s the

Sal es$ rder


.
I l@ve RuBo ard

I l@ve RuBo ard

=an

After wr t nth s a33l 4at o n, su33 ose I re4e ve a new reDu reme

nt to 4han -e the way I have to hand le ta2e s. (ow, I have to be able to hand le ta2e s on orde rs from 4ust omer s outs de the 7n te d

Stat es. At a mn mum ,I w ll need to add new rules for 4om 3ut n thes e ta2e s. >ow 4an I hand le thes e new rules FI 4ould atte m3t

to reus e the e2 st n-

Sal es$ rder/


ob5e 4t, 3ro4 ess n th s new s tua t on l 6e a new 6 nd of sales order , only w th a d ffer ent set of ta2at

on rules . Bor e2a m3le , for Cana d an sales ,I 4ould der v ea new 4lass 4alle d

Can adi anS ale s$r der


from

Sal es$ rder/


that woul d overr de the

ta2 rules .I show th s solut on n B -ur e ',) &. -i!u

(ow, des n 3atte rns re3e atedl y dem

onstr ate a fund ame ntal rule of des n 3atte rns! 1Bav or ob5e 4t 4om 3os t on over 4lass nher tan4 e.1R,S The solut on n B -ur e ',)& does 5ust the

o33o s teO In other word s, I have hand led the var a t on n ta2 rules by us n nher tan4 e to der v ea new 4lass w th the new rule.

R,S

b d,

3. &$.

>ow

4ould I a33r oa4h th s d ffer ently F Bollo w nthe rules I state d earl er! atte m3t to 14on s der what shoul d be var a ble n your des n1 Q and 1en4

a3sul ate the 4on4 e3t that var e s.1R+S

R+S

b d,

3. &%.

Bollo w nth s two) ste3 a33r oa4h ,I shoul d do the follo w n! '. B

&. C

In

th s e2a m3le ,I have alrea dy dent f ed that the ta2 rules are vary n-. To en4a 3sula te them woul d mea n 4reat nan abstr a4t 4lass that def n

es how to a44o m3l s h ta2at on 4on4 e3tu ally, and then der v e 4on4 rete 4lass es for ea4h of the var a t ons . In other word s, I 4ould 4reat ea

Cal

cTa#/
ob5e 4t that def n es the nter fa4e to a44o m3l s h th s tas6. I 4ould then der v e the s3e4 f4 vers ons need ed. I show th s n B -ur e ',)

.. -i!u

Cont nu n - on, I now use 4om 3os t on nste ad of nher tan4 e. Th s mea ns, nste ad of ma6 nd ffer

ent vers ons of sales orde rs <us n nher tan4 e=, I w ll 4ont an the var a t on w th 4om 3os t on. That s, I w ll have one

Sal es$ rder/


4lass and have

t 4ont an the

Cal cTa#/
4lass to hand le the var a t ons .I show th s n B -ur e ',) ,. -i!u

&.0

In the 789, t s 3oss ble to def n e 3ara mete rs n the met hods . Th s

s on has m3r oved . Sale s ta2 s hand led n ts own 4lass . Anot her adva ntae s that as I -et new ta2 reDu reme nts, I s m3l y need to der v

ea new 4lass from

Cal cTa#/
that m3l eme nts them . B nall y, t be4o mes eas e r to sh ft res3 ons b l t es . Bor e2a m3le , n the nher tan4 e) base d

a33r oa4h ,I "ad to have the

Tas 0Co ntr oll er


de4 d e wh 4 h ty3e of

Sal es$ rder/


to use. " th the new stru4 ture, I 4an have e the r the

Tas

0Co ntr oll er


do t or the

Sal es$ rder/


do t. To have the

Sal es$ rder/


do t, I woul d have som e 4onf -urat on ob5e 4t that woul d let t

6now wh 4 h ta2 ob5e 4t to use <3ro babl y the sam e one the

Tas 0Co ntr oll er


was us n -=. I show th s n B -ur e ',) +. -i!u

Th s a33r oa4h allow s the bus ness rule to vary nde 3end ently from the

Sal es$ rder/


ob5e 4t that

uses t. (ote how th s wor6 s well for 4urre nt var a t ons I have as well as any futur e ones that m -h t 4om e alon -. Csse nt all y, th s

use of en4a 3sula t nan al-or thm n an abstr a4t 4lass <Cal

cTa#
= and us n one of them at a t me nter 4han -eab ly s the Strat e-y 3atte rn.
I l@ve RuBo

ard

I l@ve RuBo ard

"he

A44o rd nto the Janof Bour, the Strat e-y 3atte rnLs nten t s to #ef n ea fam l y of al-or thm s, en4a 3sula te

ea4h one, and ma6 e them nter 4han -eab le. Strat e-y lets the al-or thm vary nde 3end ently from the 4l en ts that use t.R/S

R/S

b d,

3. .'+.

The Strat e-y

3atte rn s base d on a few 3r n4 3les !

"he

Inte nt

&.
B

..
B

"h l e t s true I have to mod fy the

Sal es$ rder/


and

Cal cTa#/
4lass es to hand le th s 4ase, t s 4lear

how to do th s. I am not l 6ely to ntro du4e a 3robl em be4a use of th s. Te4h n 4all y, the Strat e-y 3atte rn s abou t en4a 3sula t nal-or thm s.

>ow ever, n 3ra4t 4e, I have foun d that t 4an be used for en4a 3sula t nv rtu ally any 6 nd of rule. In -ene ral, whe nI am do n anal ys s

and I hear abou t a33l y nd ffer ent bus ness rules at d ffer ent t me s, I 4ons der the 3oss b l ty of a Strat e-y 3atte rn hand l nth s var a t on for

me. The Strat e-y 3atte rn reDu res that the al-or thm s <bus ness rules = be n en4a 3sula ted now le outs de of the 4lass that s us n -

them <the

Con te#t
=. Th s mea ns that the nfor mat on need ed by the strat e- es must e the r be 3ass ed n or obta ned n som e other man

ner. The only ser o us draw ba46 I have foun d w th the Strat e-y 3atte rn s the num ber of add t onal 4lass es I have to 4reat e. "h l e well

wort h the 4ost, there are a few th nsI have done to mn m Ge th s whe nI have 4ontr ol of all of the strat e- es . In th s s tua t on, fI am us n - C: :, I m -h

t have the abstr a4t strat e-y head er f le 4ont an all of the head er f les for the 4on4 rete strat e- es .I also have the abstr a4t strat e-y 433 f le

4ont an the 4ode for the 4on4 rete strat e- es . If I am us n Java, I use nner 4lass es n the abstr a4t strat e-y 4lass to 4ont an all of the 4on4 rete strat

e- es .I do not do th s fI do not have 4ontr ol over all of the strat e- es P that s, f other 3roram mers need to m3l eme nt the r own al-or thm

s.

I l@ve RuBo ard

I l@ve RuBo ard

)#!

The Strat e-y 3atte rn s a way to def n ea fam l y of al-or thm s. Con4 e3tu ally, all of thes e

al-or thm s do the sam e th ns. They 5ust have d ffer ent m3l eme ntat ons. I show ed an e2a m3le that used a fam l y of ta2 4al4u lat o n

al-or thm s. In an nter nat o nal e)ta l syste m, there m -h t be d ffer ent ta2 al-or thm s to use for d ffer ent 4oun tr es. Strat e-y woul d allow me to en4a

3sula te thes e rules n one abstr a4t 4lass and have a fam l y of 4on4 rete der v at on s. By der v nall the d ffer ent ways of 3erfo rm n - the

al-or thm from an abstr a4t 4lass , the ma n mod ule <Sal

es$ rder/
n the e2a m3le abov e= does not need to worr y abou t wh 4 h of man y 3oss

blt es s a4tu ally n use. Th s allow s for new var a t ons but also 4reat es the need to man a-e thes e var a t ons Ea 4hall en-e I w ll d s4u ss n Cha3 ter

&$, 1The Anal ys s 8atr 2.1

I l@ve RuBo ard

I l@ve RuBo ard

Cha

?v er ve w A 9t tle 8o re #e ta l Th e

#e 4or at or Pat ter n A3 3ly nth e #e 4or at or Pat ter n to th e Ca se St ud y An ot he r C2

am 3le ! In 3u t@ ?u t3 ut Be ld (o tes ! 7s nth e #e 4or at or Pat ter n Su m ma ry Su 33l

em en t! C: : Co de C2 am 3le s
I l@ve RuBo ard

I l@ve RuBo ard

Over

Th s 4ha3 ter 4ont nues the e) ta l n 4ase stud y

ntro du4e d n Cha3 ter ',, 1The Strat e-y Patte rn.1 In th s 4ha3 ter,

I l@ve RuBo ard

I l@ve RuBo ard

B -ur e ',)& show ed the bas 4 stru4 ture of the 4ase stud y. B -ur e '+)' show s th s stru4

ture n more deta l. >ere ,I show that the

Sal es$ rder/


ob5e 4t uses a

Sal esT ic0 et


ob5e 4t to 3r nt a sales t 46e t. -i!u

As you saw n Cha3 ter ',,

Sal es$ rder/


uses a

Cal cTa#/
ob5e 4t to 4al4u late the

ta2 on the order . To m3l eme nt the 3r nt nfun4t on,

Sal es$ rder/


4alls the

Sal esT ic0 et


ob5e 4t, reDu est n that t 3r nt the t 46e t.

Th s sa f ne, reas onab ly mod ular des n. In the 3ro4 ess of wr t nthe a33l 4at o n, su33 ose I -et a new reDu reme nt to add head er nfor

mat on to the

Sal esT ic0 et.


>ow 4an I hand le th s new reDu reme ntF If I am wr t nthe syste m to be used by 5ust one 4om 3any , t may

be eas e st s m3l y to add the 4ontr ol of head ers and foote rs n the

Sal esT ic0 et


4lass . Th s s show n n B -ur e '+) &. -i!u

In th s solut on, I have 3ut the 4ontr ol n

Sal esT ic0 et,


w th fla-s say n whet her

t s to 3r nt the head ers or the foote rs. Th s wor6 s Du te well fI do not have to deal w th a lot of o3t o ns or f the sales orde rs us n -

thes e head ers do not 4han -e. If I have to deal w th man y d ffer ent ty3e s of head ers and foote rs, 3r nt nonly one ea4h t me, then I

m -h t 4ons der us n one Strat e-y 3atte rn for the head er and anot her Strat e-y 3atte rn for the foote r. "hat ha33 ens fI have to

3r nt more than one head er and@ or foote r at a t me F ?r what f the orde r of the head ers and@ or foote rs need s to 4han -eF The num ber of 4om

b nat ons 4an Du 46 ly over whel m. In s tua t ons l 6e th s, the #e4o rator 3atte rn 4an be very usef ul. Inste ad of 4ontr oll n adde d fun4t onal

ty by hav n- a 4ontr ol meth od, the #e4o rator 3atte rn says to 4ontr ol t by 4ha n nto-et her the fun4t ons des r ed n the 4orre 4t orde r need ed.

The #e4o rator 3atte rn se3a rates the dyna m4 bu ld nof th s 4ha n of fun4t onal ty from the 4l en t that uses t, n th s 4ase, the

Sal es$ rder

I l@ve RuBo ard

I l@ve RuBo ard

"he

A44o rd nto Janof Bour, the #e4o rator 3atte rnLs nten t s to Atta4 h add t onal res3 ons b

l t es to an ob5e 4t dyna m 4al ly. #e4o rator s 3rov de a fle2 ble alter nat v e to sub4l ass n - for e2te nd n fun4t onal ty.R'S

R'S

Jamma , C., >elm, R., Johnso n, R., Al ss de

s, J., Design Pattern s: Ele'en ts of 0e sab le ObjectOriente d &oftwar e, Read n -, 8ass.! Add son ) "esley, '%%+, 3. .'+.

The #e4o rator 3atte rn wor6 s by allow nme to 4reat ea 4ha n of ob5e

4ts that start s w th the deco rator ob5e 4tsE the ob5e 4ts res3 ons b le for the new fun4t onE and ends w th the or nal ob5e 4t. B -ur e '+). llust rates

th s. -i!u

The 4lass d a-r am of the #e4o rator 3atte rn n B -ur e '+), m3l es the 4ha n of ob5e 4ts show n n B -ur e

'+) .. Ca4h 4ha n start s w th a

Co' pon ent


<a

Con cre teC o'p one nt


or a

Dec ora tor=


. Ca4h

Dec ora tor


s follo wed e the r by anot

her

Dec ora tor


or by the or nal

Con cre teC o'p one nt.


A

Con cre teC o'p one nt


alwa ys ends the 4ha n . -i!u

Bor e2a m3le , n B -ur e '+) ,,

Con cre teD eco rat orB


3erfo rms ts ?3er at on and then 4alls the

?3er at on meth od n

Dec ora tor.


Th s 4alls

Con cre teD eco rat orBL


s tra l n-

Co' pon entL


s ?3er at on .
I l@ve RuBo ard

I l@ve RuBo ard

Appl

In the 4ase stud y, the

Sal esT ic0 et s


the

Con cre teC o'p one nt.


The 4on4 rete de4o rator s are the head ers and foote rs. B -ur e

'+)+ show s the a33l 4at o n of the #e4o rator 3atte rn to the 4ase stud y. -i!u

B -ur e '+)/ show

s the a33l 4at o n of #e4o rator to one head er and one foote r. -i!u

Ca4h #e4o rator ob5e 4t wra3 s ts new fun4t

on arou nd ts tra l nob5e 4t. Ca4h #e4o rator 3erfo rms ts adde d fun4t on e the r befor e ts de4o rated fun4t on <for head ers= or after t <for

foote rs=. The eas e st way to see how t wor6 s s to loo6 at 4ode for a s3e4 f4 e2a m3le and wal6 thro u-h t. See C2a m3le '+) '. Exa

clas s 7ale s4ic ket exte nds Comp onen t ; pu!l ic void prt4 icke t () ; // sale s tick et prin ting code here > > a!st ract clas s $eco rato r exte nds Comp

onen t ; priv ate Comp onen t myCo mp, pu!l ic $eco rato r (Com pone nt myC) ; myCo mp* myC, > pu!l ic void prt4 icke t () ; if (myC omp I* null ) myCo mp%p rt4i

cket (), > > clas s =ead er( exte nds $eco rato r ; pu!l ic =ead er( (Com pone nt myC) ; su per( myC) , > pu!l ic void prt4 icke t () ; // plac e prin ting head er ( code here

supe r%pr t4ic ket( ), > > clas s =ead er? exte nds $eco rato r ; pu!l ic =ead er? (Com pone nt myC) ; su per( myC) , > pu!l ic void prt4 icke t () ; // plac e prin ting head er ?

code here supe r%pr t4ic ket( ), > > clas s /oot er( exte nds $eco rato r ; pu!l ic /oot er( (Com pone nt myC) ; su per( myC) , > pu!l ic void prt4 icke t () ; supe r%pr t4ic ket(

), // plac e prin ting foot er ( code here > > clas s /oot er? exte nds $eco rato r ; pu!l ic /oot er? (Com pone nt myC) ; su per( myC) , > pu!l ic void prt4 icke t () ; supe

r%pr t4ic ket( ), // plac e prin ting foot er ? code here > > clas s 7ale s+rd er ; void prt4 icke t () ; Comp onen t my74 , // .et chai n of $eco rato rs and 7ale s4ic

ket !uil t !y // anot her o!"e ct that know s the rule s to use% // 4his may !e done in cons truc tor inst ead of // each time this is call ed% my74 * Conf igur atio n%ge

t7al es4i cket ()

// Crin t 4ick et with head ers and foot ers as need ed my74 %prt 4ick et() , > >
If I want the sales t 46e t to loo6 l 6e! >CA #CR

' SA9C S TIC; CT B?? TCR ' Then

Con fig ura tio n.g etS ale sTi cke t


retur ns

retu rn( new =ead er(( new /oot er(( new 7ale s4ic

ket( ))),
Th s 4reat es a

Hea der!/
ob5e 4t tra le d by a

(oo ter!/
ob5e 4t tra le d by a

Sal esT ic0 et


ob5e 4t. If I want the sales t 46e t to loo6

l 6e! >CA #CR ' >CA #CR & SA9C S TIC; CT B?? TCR ' Then

Con fig ura tio n.g etS ale sTi cke t


retur ns

retu rn(

new =ead er(( new =ead er? (new /oot er(( new 7ale s4ic ket( )))) ,
Th s 4reat es a

Hea der!/
ob5e 4t tra le d by a

Hea der"/
ob5e 4t tra le d by a

(oo ter!/
ob5e 4t

tra le d by a

Sal esT ic0 et


ob5e 4t. The #e4o rator 3atte rn hel3s to de4o m3o se the 3robl em nto two 3arts !

>

>

Th s allow s me to se3a rate m3l eme nt nthe

Dec ora tors


from the ob5e 4t that deter m ne s how they are used . Th s n4re ases 4ohe s on be4a

use ea4h of the

Dec ora tors


s only 4on4 erne d w th the fun4t on t adds Enot n how t s adde d to the 4ha n .

I l@ve RuBo ard

I l@ve

RuBo ard

Anot

A 4om mon use for the #e4o rator 3atte rn s n strea m I@?. 9etLs loo6 at strea m I@? a l ttle befor e see n how the 3atte rn

4an be used here. I w ll lmt the d s4u ss on to n3ut s n4e out3 ut wor6 s n an anal o-ou s way <f you see how t wor6 s n one d re4 t on, t shoul

d be 4lear how t wor6 s n the other d re4 t on= . Bor any 3art 4ular strea m n3ut , there s e2a4 tly one sour 4e, but there 4an be any num ber < n4l

ud n Gero= of a4t o ns to 3erfo rm on the n3ut strea m. Bor e2a m3le ,I 4an read from

#e3e nd n u3on how the data were sent <or store d= any 4om b nat on of beha v ors s 3oss ble. Th n 6 of t th s way! Any

sour 4e 4an be de4o rated w th any 4om b nat on of beha v ors . Som e of the 3oss blt es ava l able for strea m n3ut are show n n Table '+)

'. #eve lo3er s n ob5e 4t) or en ted lanua-e s 4an ta6e adva ntae of th s by hav nsour 4e and beha v or ob5e 4ts der v e from a 4om mon

abstr a4t 4lass . Ca4h beha v or ob5e 4t 4an be - ve n ts sour 4e or 3r or beha v or n ts 4ons tru4t or. A 4ha n of a4t o ns s then bu lt as the ob5e 4ts them

selve s are nsta nt at ed <ea4 h s - ve na refer en4e to ts tra l nob5e 4t=. The sour 4es der v e from

Con cre teC o'p one nt


<see B -ur e '+) ,=,

wh le the beha v ors are de4o rator s. (ote that

Con cre teC o'p one nt s


now a m sn omer be4a use t s now abstr a4t.

Table

(ou rce s Str

n-

B le

So4 6et <TC P@IP = Ser al 3ort

Par allel 3ort

;ey boa rd
Bor e2a m3le

, to -et the beha v or 1rea d from a f le, de4o m3re ss the n3ut , and then de4r y3t t,1 do the follo w n! '. B

a. Instan

b. Pass a

4. Pass a

&. R

If th s 4l en t

need s to -et ts n3ut from a d ffer ent sour 4e, the 4ha n s 4reat ed by nsta nt at n- a d ffer ent sour 4e ob5e 4t us n - the sam e beha v or ob5e

4ts.

&nd

Java s noto r ous for a 4onf us n arra y of strea m n3ut s and asso 4 ate d 4lass es. It s mu4 h eas er to unde rstan d thes

e 4lass es n the 4ont e2t of the #e4o rator 3att ern. The 4lass es d re4 tly der v ed from

1a, a.i o.. npu tSt rea '


<Byt

eAr ray .np utS tre

that the nsta nt at on of the 4ha n s of ob5e 4ts be 4om 3lete ly de4o u3le d from the

Cli ent
ob5e 4ts that use t. Th s s most ty3 4 ally a44o m3l s

hed thro u-h the use of fa4to ry ob5e 4ts that nsta nt at e the 4ha n s base d u3on som e 4onf -urat on nfor mat on.

"he

Inte nt

nfun4t on dyna m 4al ly. In 3ra4t 4e, t reDu res bu ld n- a 4ha n of ob5e 4ts that - ve the des r ed beha v ors . The f rst ob5e 4t n th s 4ha n s 4alle d by

Cli ent
that had noth n- to do w th the bu ld nof t. By 6ee3 nthe 4reat on of the 4ha n nde 3end ent from ts use, the

Cli ent
ob5e 4t s

not affe4 ted by new reDu reme nts to add fun4t onal ty.
I l@ve RuBo ard

I l@ve RuBo ard

)#p

Exa

clas s 7ale s4ic ket : pu!l ic Comp

onen t ; pu!l ic: void prt4 icke t(), > 7ale s4ic ket: :prt 4ick et() ; // sale s tick et prin ting code here > clas s $eco rato r : pu!l ic Comp onen t ; pu!l ic: virt ual

void prt4 icke t(), $eco rato r( C ompo nent @myC ), priv ate: Comp onen t @myC omp, > $eco rato r::$ ecor ator ( Co mpon ent @myC ) ; myCo mp* myC, > void $eco rato r::p rt4i cket () ;

myCo mp Aprt 4ick et() , > clas s =ead er( : pu!l ic $eco rato r ; pu!l ic: =ead er(( Comp onen t @myC ), void prt4 icke t(), > =ead er(: :=ea der( (Com pone nt @myC ) :

$eco rato r(my C) ; > void =ead er(: :prt 4ick et () ; // plac e prin ting head er ( code here $eco rato r::p rt4i cket (), > clas s =ead er? : pu!l ic $eco rato r ; pu!l ic: =ead

er?( Comp onen t @myC ), void prt4 icke t(), > =ead er?: :=ea der? (Com pone nt @myC ) : $eco rato r(my C) ; > void =ead er?: :prt 4ick et () ; // plac e prin ting head er ? code here $eco

rato r::p rt4i cket (), > clas s /oot er( : pu!l ic $eco rato r ; pu!l ic: /oot er(( Comp onen t @myC ), void prt4 icke t(), > /oot er(: :/oo ter( (Com pone nt @myC ) : $eco rato

r(my C) ; > void /oot er(: :prt 4ick et () ; $eco rato r::p rt4i cket (), // plac e prin ting foot er ( code here > clas s /oot er? : pu!l ic $eco rato r ; pu!l ic: /oot er?( Comp

onen t @myC ), void prt4 icke t(), > /oot er?: :/oo ter? (Com pone nt @myC ) : $eco rato r(my C) ; > void /oot er?: :prt 4ick et () ; $eco rato r::p rt4i cket (), // plac e prin ting foot

er ? code here > void 7ale s+rd er:: prt4 icke t () ;

Comp onen t @my7 4, // .et chai n of $eco rato rs and 7ale s4ic ket !uil t !y // anot her o!"e ct that know s the rule s to

use% // 4his may !e done in cons truc tor inst ead of // each time this is call ed% my74 * Conf igur atio n%ge t7al es4i cket () // Crin t 4ick et with head ers and foot ers as

need ed my74 Aprt 4ick et() , >


I l@ve RuBo ard

I l@ve RuBo ard

Cha

?v er ve w Int ro du 4n th e S n-l

et on Pat ter n A3 3ly nth e S n-l et on Pat ter n to th e Ca se St ud y A Aa ra nt! Th e #o

ubl e) Ch e4 6e d 9o 46 nPat ter n Be ld (o tes ! 7s nth e S n-l et on an d #o ubl e) Ch e4

6e d 9o 46 nPat ter ns Su m ma ry Su 33l em en t! C: : Co de C2 am 3le s


I l@ve RuBo ard

I l@ve RuBo ard

Over

Th s 4ha3 ter 4ont nues the e) ta l n 4ase stud y d s4u ssed n Cha3 ter ',, 1The Strat e-y Patte rn1 and Cha3 ter '+, 1The #e4o rator Patte

rn.1 In th s 4ha3 ter,

The S n-l eton 3atte rn and the #oub le) Che4 6ed 9o46 n-

3atte rn are very s m3l e and very 4om mon. Both are used to ensu re that only one ob5e 4t of a 3art 4ular 4lass s nsta nt at ed. The d st n4t o n

betw een the 3atte rns s that the S n-l eton 3atte rn s used n s n-l e) threa ded a33l 4at o ns wh le the #oub le) Che4 6ed 9o46 n3atte rn s used n

mult threa ded a33l 4at o ns.R'S

R'S

If

you do not 6now what a mult thr eaded a33l 4at on s, donLt worryP you need only 4on4ern yourself w th the S n-let on 3attern at th s t me.

I l@ve RuBo ard

I l@ve RuBo ard

%ntro

A44o rd nto the Janof Bour, the S n-l etonL s nten t s to Cnsu re a 4lass only has one nsta n4e, and 3rov de a -lob al 3o nt of a44e ss to

t.R&S

R&S

Jamma , C., >elm, R., Johnso n, R., Al ss de s, J., Design Pattern s: Ele'en ts of 0e sab le ObjectOriente d &oftwar e, Read n -, 8ass.! Add son ) "esley, '%%+, 3. '&*.

The S n-l eton 3atte rn wor6 s by hav

n- a s3e4 al meth od that s used to nsta nt at e the des r ed ob5e 4t.

"

I l@ve RuBo ard

I l@ve RuBo ard

Appl

In Cha3 ter ',, I en4a 3sula ted the rules abou t ta2e s w th n strat e-y ob5e 4ts. I have to der v ea

Cal cTa#/
4lass for ea4h 3oss ble ta2 4al4u

lat o n rule. Th s mea ns that I need to use the sam e ob5e 4ts over and over a-a n, 5ust alter nat n betw een the r uses. Bor 3erfo rman

4e reas ons, I m -h t not want to 6ee3 nsta nt at nthem and thro w nthem away a-a n and a-a n. And, wh le I 4ould nsta nt at e all of the 3oss

ble strat e- es at the start , th s 4ould be4o me neff 4 ent f the num ber of strat e- es -rew lar-e . <Re mem ber, I may have man y other strat e- es thro u-ho

ut my a33l 4at o n.= Inste ad, t woul d be best to nsta nt at e them as need ed, but only do the nsta nt at on on4e . The 3robl em s that

I do not want to 4reat ea se3a rate ob5e 4t to 6ee3 tra46 of what I have alrea dy nsta nt at ed. Rath er, I woul d l 6e the ob5e 4ts them selve s <that

s, the strat e- es = be res3 ons b le for hand l nthe r own s n-l e nsta nt at on. Th s s the 3ur3 ose of the S n-l eton 3atte rn. It allow s me to nsta

nt at e an ob5e 4t only on4e , w th out reDu r nthe 4l en t ob5e 4ts to be 4on4 erne d w th whet her t alrea dy e2 st s or not. The S n-l eton

4ould be m3l eme nted n 4ode as show n n C2a m3le '/) '. In th s e2a m3le ,I 4reat ea meth od <get

Ins tan ce=


that w ll nsta nt at e at most one

2ST a#
ob5e 4t. The S n-l eton 3rote 4ts a-a nst som eone else nsta nt at nthe

2ST a#
ob5e 4t d re4 tly by ma6 nthe 4ons tru4t or 3r va te,

wh 4 h mea ns that no other ob5e 4t 4an a44e ss t. Exa

clas s 574a x ; priv ate stat ic 574a x inst ance , priv ate 574a x(): pu!l ic

stat ic 574a x get# nsta nce( ) ; if (ins tanc e** null ) inst ance * new 574a x(), retu rn inst ance , > >

"he

Inte nt

Pro ble m

Sol ut o n

Part 43 ant s and Coll abo rato rs

Con seD uen 4es

Im3 lem ent at o n

JoB Ref ere n4e

-i!u

l@ve RuBo ard

I l@ve RuBo ard

Th s 3atte rn only a33l es to mult threa ded a33l 4at o ns. If you are not nvol ved w th mult threa ded a33l 4at o ns

you m -h t want to s6 3 th s se4t on. Th s se4t on assu mes that you have a bas 4 unde rstan d nof mult threa ded ssue s, n4lu d nsyn4 hron Gat o

n. A 3robl em w th the S n-l eton 3atte rn may ar se n mult threa ded a33l 4at o ns. Su33 ose two 4alls to

get Ins tan ce ! are


mad e at e2a4

tly the sam e t me. Th s 4an be very bad. Cons der what 4an ha33 en n th s 4ase ! '. T

&. >

..
B

Is th s a

3robl emF It may or may not be.

) If th e ob5 e4t 4re at es a 4o nn

e4t on , th er e wl l a4t ual ly be tw o 4o nn e4t on s <o ne for ea 4h ob5 e4t =. ) If a 4o un ter

s us ed, th er e wl l be tw o 4o un ter s. It may be very d ff 4 ult to f nd thes e 3robl ems. B rst of all, the dual 4reat

on s very nter m tte ntE t usual ly wonL t ha33 en. Se4o nd, t may not be obv ous why the 4oun ts are off, as only one 4l en t ob5e 4t w ll

4ont an one of the S n-l eton ob5e 4ts wh le all of the other 4l en t ob5e 4ts w ll refer to the other S n-l eton. At f rst, t a33e ars that all I need

to do s syn4 hron Ge the test for whet her the S n-l eton ob5e 4t has been 4reat ed. The only 3robl em s that th s syn4 hron Gat o n may end u3

be n -a seve re bottl ene4 6, be4a use all of the threa ds w ll have to wa t for the 4he4 6 on whet her the ob5e 4t alrea dy e2 st s. Perh a3s

nste ad, I 4ould 3ut som e syn4 hron Gat o n 4ode n after the

if (in sta nce ** nul l)


test. Th s w ll not wor6 e the r. S n4 e t woul d be 3oss

ble that both 4alls 4ould meet the (799 test and then atte m3t to syn4 hron Ge, I 4ould st ll end u3 ma6 ntwo S n-l eton ob5e 4ts, ma6 nthem one at a

t me. The solut on s to do a 1syn 4h1 after the test for (799 and then 4he4 6 a-a n to ma6 e sure the nsta n4e mem ber has not yet been 4reat

ed. I show th s n C2a m3le '/) &. Th s s 4alle d do b lec"ec *ed loc*i ng(R.S The nten t s to o3t m Ge away unne 4ess ary lo46 n-. Th s syn4 hron

Gat o n 4he4 6 ha33 ens at most one t me, so t w ll not be a bottl ene4 6.

R.S

8art n, R., R ehle, #., Bus4hm ann, B., Pattern Lang a ge of Progra ' Design, Read n -, 8ass.! Add son ) "esley, '%%0,

3. ./..

The featu res of doub le) 4he4 6ed lo46 nare as follo ws!

Exa

clas s 574a x exte nds Calc 4ax ;

priv ate stat ic 574a x inst ance , priv ate 574a x() ; >

priv ate sync hron iFed stat ic void do7y nc() ; if (ins tanc e ** null ); inst ance * new 574a x(),

> >

pu!l ic stat ic 574a x get# nsta nce( ) ; if (ins tanc e ** null ); do7y nc() , > retu rn inst ance , > >
I l@ve RuBo ard

I l@ve

RuBo ard

iel

If you 6now you are -o n - to need an ob5e 4t and no 3erfo rman 4e ssue reDu res you to defer nsta nt at on of the ob5e 4t unt l

tLs need ed, t s usual ly s m3l er to have a stat 4 mem ber 4ont an a refer en4e to the ob5e 4t. In mult threa ded a33l 4at o ns, S n-l eton s

ty3 4 ally have to be threa d safe <be4 ause the s n-l e ob5e 4t may be shar ed by mult 3le ob5e 4ts=. Th s mea ns hav nno data mem bers but

us n only var a bles whos e s4o3 e s no lar-e r than a meth od.

I l@ve RuBo ard

I l@ve RuBo ard

)#!

The S n-l eton and #oub le)

Che4 6ed 9o46 n3atte rns are 4om mon 3atte rns to use whe n you want to ensu re that there s only one nsta n4e of an ob5e 4t. The S n-l eton

s used n s n-l e) threa ded a33l 4at o ns wh le the #oub le) Che4 6ed 9o46 n3atte rn s used n mult threa ded a33l 4at o ns.

I l@ve RuBo ard

I l@ve RuBo ard

)#p

Exa

Clas s 574a x ; pu!l ic: stat ic 574a x@ get# nsta nce( ), priv ate: 574a x(), stat ic 574a x@ inst ance , >

574a x@ 574a x::i nsta nce* 8, 574a x@ 574a x::g et#n stan ce () ; if (ins tanc e** 8) ; inst ance * new 574a x, > retu rn inst ance , >
Exa

clas s 574a x :

pu!l ic Calc 4ax ; pu!l ic: stat ic 574a x@ get# nsta nce( ), priv ate: 574a x(), stat ic 574a x@ inst ance , >, 574a x@ 574a x::i nsta nce* 8, 574a x@ 574a x::g

et#n stan ce () ; if (ins tanc e** 8) ; // do sync here if (ins tanc e** 8) ; inst ance * new 574a x, > > retu rn inst ance , >
I l@ve RuBo ard

I l@ve

RuBo ard

Cha

?v er ve w Ca te -o re s of Pat ter ns 8o re Re Du re me nts for th e Ca se St ud

y Th e ?b ser ve r Pat ter n A3 3ly nth e ?b ser ve r to th e Ca se St ud y Be ld (o tes

! 7s nth e ?b ser ve r Pat ter n Su m ma ry Su 33l em en t! C: : Co de C2 am 3le


I l@ve RuBo ard

I l@ve RuBo ard

Over

Th s 4ha3 ter 4ont nues the e) ta l n 4ase stud y d s4u ssed n Cha3 ters ',T '/. In th s 4ha3 ter,

I l@ve RuBo ard

I l@ve RuBo ard

Cate

Ther e are man y 3atte rns

to 6ee3 tra46 of. To hel3 sort th s out, the Janof Bour has -rou 3ed 3atte rns nto three -ene ral 4ate -or e s, as show n n Table '*) '.R'S

R'S

Jamma , C.,

>elm, R., Johnso n, R., Al ss de s, J., Design Pattern s: Ele'en ts of 0e sab le ObjectOriente d &oftwar e, Read n -, 8ass.! Add son ) "esley, '%%+, 3. '$.

Table 1=-1'

Cat e!o r$

Stru 4tur

want to de4o rate t w th add t onal fun4t ons. I have foun d t valu able to th n6 of a fourt h 4ate -ory of 3atte rns, one whos e 3r m ary 3ur3

ose s to de4o u3le ob5e 4ts from ea4h other . ?ne mot vat o n for thes e s to allow for s4ala b l ty or n4re ased fle2 b l ty .I 4all th s 4ate -ory of 3atte

rns deco plin g patt erns( S n4 e most of the 3atte rns n the de4o u3l n 4ate -ory belo n- to the Janof BourL s beha v ora l 4ate -ory, I

4ould almo st 4all them a subs et of the beha v ora l 4ate -ory. I 4hos e to ma6 ea fourt h 4ate -ory s m3l y be4a use my nten t n th s boo6 s to

refle 4t how I loo6 at 3atte rns, fo4us non the r mot vat o nsE n th s 4ase, de4o u3l n -. I woul d not -et too hunu3 on the whys and

wher efore s of the 4lass f 4at ons. They are mea nt to - ve ns hts nto what the 3atte rns are do n -. Th s 4ha3 ter d s4u sses the ?bse rver 3atte rn,

wh 4 h s the best e2a m3le of a de4o u3l n 3atte rn 3atte rn there s. The Janof Bour 4lass f es ?bse rver as a Beha v ora l 3atte rn.
I l@ve RuBo ard

I l@ve RuBo ard

.or

In the 3ro4 ess of wr t nthe a33l 4at o n, su33 ose I -et a new reDu reme nt to ta6e the follo w na4t o ns whe neve ra

new 4ust omer s enter ed nto the syste m!

Are thes e all of the reDu reme ntsF " ll th ns 4han -e n the futur

eF If I am reas onab ly 4erta n that I 6now ever y reDu reme nt, then I 4ould solve the 3robl em by hard ) 4od n - the not f 4at o n beha

v or nto the

Cus to' er
4lass , su4h as show n n B -ur e '*) '. -i!u

Bor e2a m3le , us n - the sam e meth

od that adds a new 4ust omer nto the data base ,I w ll also ma6 e 4alls to the ob5e 4ts that -ene rate wel4 ome lette rs and ver f y 3ost off 4

e addr esse s. Thes e 4lass es have the follo w nres3 ons b l t es !

Cla ss

Cus tom er

to su33 ort d ffer ent 4om 3an esL wel4 ome lette rs, wh 4 h woul d reDu re a d ffer ent

Cus to' er
ob5e 4t for ea4h 4om 3any . Surel y, I 4an do

bette r.
I l@ve RuBo ard

I l@ve RuBo ard

"he

A44o rd nto the Janof Bour, the nten t of the ?bse rver 3atte rn s to 1#ef ne a one) to) man

y de3e nden 4y betw een ob5e 4ts so that whe n one ob5e 4t 4han -es state , all ts de3e nden ts are not f ed and u3da ted auto mat 4ally.

1R&S

R&S

Jamma , C., >elm, R., Johnso n, R., Al ss de s, J., Design Pattern s: Ele'en ts of 0e sab le ObjectOriente d &oftwar e, Read n -, 8ass.! Add son ) "esley, '%%+, 3. &%..

?fte n, I have a set of ob5e 4ts that

need to be not f ed whe neve r an even t o44u rs. I want th s not f 4at o n to o44u r auto mat 4ally. >ow ever, I do not want to 4han -e the broa d4ast n-

ob5e 4t ever yt m e there sa 4han -e to the set of ob5e 4ts l ste n nto the broa d4ast . <Tha t woul d be l 6e hav n- to 4han -e a rad o trans m tte

r ever y t me a new 4ar rad o 4om es to town .= I want to de4o u3le the not f y)ors and the not f y) ees. Th s 3atte rn s a very 4om mon one.

It also -oes by the nam es Dep ende nts and P bli s"& bs cribe #R.S and s anal o-ou s to the not f y 3ro4 ess n C?8 . It s m3l eme nted

n Java w th the

$%s er, er
nter fa4e and the

$%s er, a%le/


4lass <mor e on thes e later =. In rule) base d, e23e rt syste ms, they are often m3l eme

nted w th dae mon rules .

R.S

b d,

3. &%..

I l@ve RuBo ard

I l@ve RuBo ard

Appl

8y a33r oa4h s to loo6 n the 3robl em for 4lues as to what s vary

n-. Then ,I atte m3t to en4a 3sula te the var a t on. In the 4urre nt 4ase, I f nd!

B rst, I must dent fy all of the

ob5e 4ts that want to be not f ed. I w ll 4all thes e the obse rvers s n4e they are wa t nfor an even t to o44ur . I want all of the obse rvers to have

the sam e nter fa4e. If they do not have the sam e nter fa4e, then I woul d have to mod fy the s bj ectE that s, the ob5e 4t that s

tr -er nthe even t <for e2a m3le ,

Cus to' er=,


to hand le ea4h ty3e of obse rver. By hav nall of the obse rvers be of the sam e ty3e, the

sub5 e4t 4an eas l y not f y all of them . To -et all of the obse rvers to be of the sam e ty3e,

In most s tua t ons

,I want the obse rvers to be res3 ons b le for 6now nwhat they are to wat4 h for and I want the sub5 e4t to be free from 6now nwh 4 h obse rvers de3e nd

on t. To do th s, I need to have a way for the obse rvers to re- s ter them selve s w th the sub5 e4t. S n4 e all of the obse rvers are of the

sam e ty3e, I must add two meth ods to the sub5 e4t!

(ow that the

Su% 1ect/
has ts

$%s er, ers


re- s tered , t s

a s m3l e matt er for the

Su% 1ect/
to not f y the

$%s er, ers


whe n the even t o44u rs. To do th s, ea4h

$%s er, er
m3l eme nts a meth od

4alle d

upd ate.
The

Su% 1ect/
m3l eme nts a

not ify
meth od that -oes thro u-h ts l st of

$%s er, ers


and 4alls th s

upd ate
meth od for ea4h

of them . The

upd ate
meth od shoul d 4ont a ns the 4ode to hand le the even t. But not f y nea4h obse rver s not enou -h. An obse rver

may need more nfor mat on abou t the even t beyo nd the s m3l e fa4t that t has o44u rred. Ther efore ,I must also add meth od<s= to the sub5 e4t

that allow the obse rvers to -et what ever nfor mat on they need . B -ur e '*)& show s th s solut on. -i!u

In B -ur e '*) &, the 4lass es relat e to ea4h other as follo ws!

'.
T

&.
"

Ca4h

$%s

er, er
4alls

get Sta te
for nfor mat on on the newl y adde d

Cus to' er
to see what t need s to do. Note : Ty3 4 ally, there woul d be

seve ral meth ods to -et the need ed nfor mat on. (ote n th s 4ase, we use stat 4 meth ods for atta4 h and deta 4h be4a use obse rvers

want to be not f ed for all new

Cus to' ers.


"he n not f ed, they are 3ass ed the refer en4e to the

Cus to' er
4reat ed. C2a m3le '*)' show

s som e of the 4ode reDu red to m3l eme nt th s. Th s a33r oa4h allow s me to add new

$%s er, ers


w th out affe4 t nany e2 st n4lass es. It

also 6ee3 s ever yth n loose ly 4ou3l ed. Th s or-a n Gat on wor6 s fI have 6e3t all of the ob5e 4ts res3 ons b le for them selve s. >ow well does th s

wor6 fI -et a new reDu reme ntF Bor e2a m3le , what fI need to send a lette r w th 4ou3 ons to 4ust omer s lo4at ed w th n &$ m les of one

of the 4om 3any Ls 1br 4 6 and mort ar1 store s. Exa

// Note : $o not actu ally use the name +!se rver // as that is a Hava clas s in "ava %uti l

clas s Cust omer ; stat ic priv ate 'ect or my+! s, stat ic ; my+! s* new 'ect or() , > pu!l ic stat ic void atta ch(+ !ser ver o); my+! s%ad d-le ment (o), >

pu!l ic stat ic void deta ch(+ !ser ver o); my+! s%re move (o), > pu!l ic 7tri ng get7 tate () ; // have othe r meth ods that will give the // requ ired info rmat ion >

pu!l ic void noti fy+! s () ; for (-nu mera tion e * my+! s%el emen ts() , e%ha s)or e-le ment s() ,) ; ((+! serv er) e)%u pdat e(th is), > > >

a!st ract clas

s +!se rver ; pu!l ic +!se rver () ; Cust omer %att ach( this ), > a!st ract pu!l ic void upda te(C usto mer myCu st), > clas s 2ddr 'eri fica tion exte nds +!se rver ;

pu!l ic 2ddr 'eri fica tion () ; supe r(), > pu!l ic void upda te ( Cust omer myCu st) ; // do 2ddr ess veri fica tion stuf f here // can get more info rmat ion a!ou

t cust omer // in ques tion !y usin g myCu st > > clas s Eelc ome< ette r exte nds +!se rver ; pu!l ic Eelc ome< ette r () ; supe r(), > pu!l ic void upda

te (Cus tome r myCu st) ; // do Eelc ome <ett er stuf f // here can get more // info rmat ion a!ou t cust omer // in ques tion !y usin g myCu st > >

To a44o m3l s h th s, I woul d s m3l y add a new obse rver that send s the 4ou3 on. It only does th s for new 4ust omer s lvn w th n the

s3e4 f ed d sta n4e. I 4ould nam e th s obse rver

Bri c0A ndM ort ar


and ma6 e t an obse rver to the

Cus to' er
4lass . B -ur e '*). show

s th s solut on. -i!u

Som et m es, a 4lass that w ll be4o me an

$%s er, er
may alrea dy e2 st

. In th s 4ase, I may not want to mod fy t. If so, I 4an eas l y ada3 t t w th the Ada3 ter 3atte rn. B -ur e '*), show s an e2a m3le of th s. -i!u

"he

The ?bse rver 3att ern s so usef ul that Java 4ont a ns an m3l eme ntat on of t n ts 3a46

a-es . The

$%s er, a%l e


4lass and the

$%s er, er
nter fa4e ma6 e u3 the 3att ern. The

$%s er, a%l e


4lass 3lay s the role of the

Su% 1ec t n

the Jan - of BourL s des4 r 3t on of the 3att ern. Inste ad of the met hods atta4 h, deta 4h, and not f y, Java uses add ?bse rver, delet e?bs erver , and not f

y?bs erve rs, res3 e4t v ely <Jav a also uses u3da te=. Java also - ve s you a few mor e met hods to ma6 e l fe eas er.R'S

R'S

See

htt3!@@5 ava.sun .4om@5& se@'..@

do4s@a3 @ nde2. html for nforma t on on the Java API for

$%s er, er
and

$%s er, a%le


.

"he

Inte nt

Pro ble m

Sol

Im3 lem ent at o n

JoB Ref ere n4e

-i!u

I l@ve RuBo ard

l@ve RuBo ard

iel

The ?bse rver 3atte rn s not mea nt to be used ever y t me there sa de3e nden 4y betw een ob5e 4ts. Bor e2a m3le , na t 46e t

3ro4 ess n syste ma ta2 ob5e 4t hand les ta2e s, t s 4lear that whe n tem s are adde d to the t 46e t the ta2 ob5e 4t must be not f ed so the

ta2 4an be re4al 4ulat ed. Th s s not a -ood 3la4e for an ?bse rver 3atte rn s n4e th s not f 4at o n s 6now n u3 front and other s are not l 6ely to be adde d.

"he n the de3e nden 4 es are f 2ed <or v rtu ally so=, add nan ?bse rver 3atte rn 3rob ably 5ust adds 4om 3le2 ty. If the l st of ob5e 4ts that

need to be not f ed of an even t 4han -es, or s som ehow 4ond t ona l, then the ?bse rver 3atte rn has -reat er valu e. Thes e 4han -es 4an o44u r

e the r be4a use the reDu reme nts are 4han - nor be4a use the l st of ob5e 4ts that need to be not f ed are 4han - n-. The ?bse rver 3atte rn 4an

also be usef ul f the syste m s run unde r d ffer ent 4ond t ons or by d ffer ent 4ust omer s, ea4h hav n- a d ffer ent l st of reDu red obse rvers .

An obse rver may only need to hand le 4erta n 4ase s of an even t. The Br 46 and 8ort ar 4ase was an e2a m3le . In su4h s tua t ons , the obse rver

must f lter out e2tra not f 4at o ns. C2tra neou s not f 4at o ns 4an be el m nate d by sh ft nthe res3 ons b l ty for f lter nout thes e not f 4at o

ns to the

Su% 1ect
. The best way to do th s s for the

Su% 1ect/
to use a Strat e-y 3atte rn to test f not f 4at o n shoul d o44ur . Ca4h obse rver - ves

the

Su% 1ect/
the 4orre 4t strat e-y to use whe n t re- s ters. Som et m es,

Su% 1ect
s w ll 4all the obse rvers L

upd ate
meth od, 3ass nalon

nfor mat on. Th s 4an save the need for 4allb a46s from the obse rvers to the

Su% 1ect
. >ow ever, t s often the 4ase that d ffer ent obse rvers have

d ffer ent nfor mat on reDu reme nts. In th s 4ase, a Strat e-y 3atte rn 4an a-a n be used . Th s t me, the

Str ate gy
ob5e 4t s used for 4all n - the

obse rvers L

upd ate
3ro4 edur e. A-a n, the obse rvers must su33 ly the

Su% 1ect/
w th the a33r o3r a te

Str ate gy
ob5e 4t to use.

I l@ve

RuBo ard

I l@ve RuBo ard

)#!

In learn nthe ?bse rver 3atte rn, I loo6e d at wh 4 h ob5e 4t s best able to hand le futur e var a t on. In the

4ase of the ?bse rver 3atte rn, the ob5e 4t that s tr -er nthe even tE the

Su% 1ect
E 4ann ot ant 4 3ate ever y ob5e 4t that m -h t need

to 6now abou t the even t. To solve th s, I 4reat e an

$%s er, er
nter fa4e and reDu re that all ?bse rvers be res3 ons b le for re- s ter n them selve s

w th th s

Su% 1ect
. "h l eI fo4us ed on the ?bse rver 3atte rn dur n - the 4ha3 ter, t s wort h 3o nt nout seve ral ob5e 4t) or en ted 3r n4

3les that are used n the ?bse rver 3atte rn.

Con cep t ?b5 e4ts are res 3on s bl e for the mse lves

atta ch(+ !ser ver @o), stat ic void deta ch(+ !ser ver @o), 7tri ng get7 tate (), priv ate: 'ect or my+! s, void noti fy+! s(), > Cust omer ::at tach (+!s erve r @o);

my+! s%ad d-le ment (o), > Cust omer ::de tach (+!s erve r @o); my+! s%re move (o), > Cust omer ::ge t7ta te () ; // have othe r meth ods that will // give the requ ired info rmat ion

> Cust omer ::no tify +!s () ; for (-nu mera tion e * my+! s%el emen ts() , e%ha s)or e-le ment s() ,) ; ((+! serv er @) e) A upda te(t his) , > > > clas

s +!se rver ; pu!l ic: +!se rver (), void upda te(C usto mer @myc ust) *8, // make s this a!st ract > +!se rver ::+! serv er () ; Cust omer %att ach( this ), > clas s

2ddr 'eri fica tion : pu!l ic +!se rver ; pu!l ic: 2ddr 'eri fica tion (), void upda te( Cust omer @myC ust) , > 2ddr 'eri fica tion ::2d dr'e rifi cati on () ; > 2ddr 'eri fica

tion ::up date (Cus tome r @myC ust) ; // do 2ddr ess veri fica tion stuf f here // can get more info rmat ion a!ou t // cust omer in ques tion !y usin g myCu st >

clas s Eelc ome< ette r : pu!l ic +!se rver ; pu!l ic: Eelc ome< ette r(), void upda te( Cust omer @myC ust) , > Eelc ome< ette r::u pdat e( C usto mer @myC ust) ; //

do Eelc ome <ett er stuf f here can get more // info rmat ion a!ou t cust omer in ques tion !y // usin g myCu st >
I l@ve RuBo ard

I l@ve RuBo ard

Cha

?v er ve w 8o re Re Du re me nts for th e Ca se St ud y Th e Te m 3la te 8e th od Pat ter

n A3 3ly nth e Te m 3la te 8e th od to th e Ca se St ud y Be ld (o tes ! 7s nth e Te m

3la te 8e th od Pat ter n Su m ma ry


I l@ve RuBo ard

I l@ve RuBo ard

Over

Th s 4ha3 ter 4ont nues the e) ta l n 4ase stud

y d s4u ssed thus far n Cha3 ters ',T '*. In th s 4ha3 ter

I l@ve RuBo ard

I l@ve RuBo ard

.or

In the 3ro4 ess of wr t nthe a33l 4at o n, su33 ose I -et a new reDu reme nt to su33 ort both ?ra4l e and SK9 Serv er data

base s. Both of thes e syste ms are base d on SK9 <Stru 4ture d Kuer y 9anua-e =, the 4om mon stan dard that ma6 es t eas e r to use data base

s. Net, even thou -h th s sa 4om mon stan dard at the -ene ral level , there are st ll d ffer en4e s n the deta ls. I 6now that n -ene ral, whe n

e2e4 ut nDuer es on thes e data base s, I w ll use the follo w nste3s ! '. B

&. S

.. B

,. S

+. R

The s3e4 f4 m3l eme ntat ons of the data base s d ffer , howe ver, reDu r nsl -h tly d ffer ent form att n 3ro4 edur es.

I l@ve

RuBo ard

I l@ve RuBo ard

"he

The Tem 3late 8eth od s a 3atte rn nten ded to hel3 one abstr a4t out a 4om mon 3ro4 ess from d ffer ent 3ro4 edur

es. A44o rd nto the Janof Bour, the nten t of the Tem 3late meth od s to #ef n e the s6ele ton of an al-or thm n an o3er at on , defer r nsom e

ste3s to sub4l asse s. Rede f ne the ste3s n an al-or thm w th out 4han - nthe al-or thmL s stru4 ture.
R'S

R'S

Jamma , C., >elm, R., Johnso n, R., Al ss de s, J., Design Pattern s:

Ele'en ts of 0e sab le ObjectOriente d &oftwar e, Read n -, 8ass.! Add son ) "esley, '%%+, 3. .&+.

In other word s, altho u-h there are d ffer ent meth ods for 4onn e4t n and Duer y n-

?ra4l e data base s and SK9 Serv er data base s, they shar e the sam e 4on4 e3tu al 3ro4 ess. The Tem 3late 8eth od - ves us a way to 4a3t ure

th s 4om mon -rou nd n an abstr a4t 4lass wh le en4a 3sula t nthe d ffer en4e s n der v ed 4lass es. The Tem 3late 8eth od 3atte rn s abou t 4ontr oll n -a

seDu en4e 4om mon to d ffer ent 3ro4 esse s.

I l@ve RuBo ard

I l@ve RuBo ard

Appl

In th s 4ase stud y, the var a t ons n data base a44e

ss o44u r n the 3art 4ular m3l eme ntat ons of the ste3s nvol ved. B -ur e '0)' llust rates th s. -i!u

I have 4reat ed a meth od 4alle d

do" uer y
that hand les the Duer yI need to 3erfo rm. I 3ass n the nam e of the data base and the Duer y

s3e4 f 4at on. The

do" uer y
meth od follo ws the f ve -ene ral ste3s abov e, 3rov d nv rtu al meth ods for the ste3s <su4 h as

for #at Con nec

t
and

for #at Sel ect=


that must be m3l eme nted d ffer ently . The

do" uer y
meth od s m3l eme nted as follo ws. As show n n B -ur e

'0) ', t f rst need s to form at the C?( (CC T 4om man d reDu red to 4onn e4t to the data base . Altho u-h the abstr a4t 4lass <3ue

ryT e'p

late
= 6now s th s form at need s to ta6e 3la4e , t does nLt 6now how to do th s. The e2a4 t form att n 4ode s su33 l ed by the der v ed 4lass

es. Th s s true for form att n - the SC9C CT 4om man d as well. The Tem 3late 8eth od man a-es to do th s be4a use the meth od 4all s mad e va

a refer en4e 3o nt nto one of the der v ed 4lass es. That s, altho u-h

3ue ryC ont rol


has a refer en4e of ty3e

3ue ryT e'p late


, t s a4tu

ally refer r nto an

$ra cle 3T
or an

S3* S,r 3T
ob5e 4t. Thus , whe n the

do" uer y
meth od s 4alle d on e the r of thes e ob5e 4ts, the meth ods

resol ved w ll f rst loo6 for meth ods of the a33r o3r a te der v ed 4lass . 9etLs say our

3ue ryC ont rol


s refer r nto an

$ra cle 3T
ob5e 4t.

S n4 e

$ra cle 3T
does not overr de

3ue ryT e'p late


, the

3ue ryT e'p late


Ls

do" uer y
meth od s nvo 6ed. Th s start s e2e4 ut nunt l t

4alls the

for #at Con nec t


meth od. S n4 e the

$ra cle 3T
ob5e 4t was reDu este d to 3erfo rm

do" uer y,
the

$ra cle $TLs for #at Con nec

t
meth od s 4alle d. After th s, 4ontr ol s retur ned to the

3ue ryT e'p late


Ls

do" uer y
meth od. The 4ode 4om mon to all Duer es s now e2e4 uted

unt l the ne2t var a t on s need edE the

for #at Sel ect


meth od. A-a n, th s meth od s lo4at ed n the ob5e 4t that

3ue ryC ont rol


s refer r n-

to <$ra

cle 3T n
th s e2a m3le =. "he na new data base s en4o unte red, the Tem 3late 8eth od 3rov des us w th a bo le r3lat e <or tem3 late=

to f ll out. "e 4reat ea new der v ed 4lass and m3l eme nt the s3e4 f4 ste3s reDu red for the new data base n t.

I l@ve RuBo ard

I l@ve RuBo

ard

iel

Som et m es a 4lass w ll use seve ral d ffer ent Strat e-y 3atte rns. "he nI f rst loo6e d at the 4lass d a-r am for the Tem 3late 8eth od, I thou

-ht, 1?h, the Tem 3late 8eth od s s m3l ya 4olle 4t on of Strat e- es that wor6 to-et her.1 Th s s danerou s <and usual ly n4or re4t= th n6 n-. "h l e t s

not un4o mmo n for seve ral Strat e- es to a33e ar to be 4onn e4te d to ea4h other , des n nfor th s 4an lead to nfle 2bl ty. The Tem 3late 8eth

od s a33l 4able whe n there are d ffer ent, but 4on4 e3tu ally sml ar 3ro4 esse s. The var a t ons for ea4h 3ro4 ess are 4ou3l ed to-et her be4a use they

are asso 4 ate d w th a 3art 4ular 3ro4 ess. In the e2a m3le I 3res ente d, whe nI need a form at a C?( (CC T 4om man d for an ?ra4l e

data base , fI need a form at a K7C RN 4om man d, tLll be for an ?ra4l e data base as well.

"he

Inte nt

Pro ble m

JoB Ref ere n4e

-i!u

I l@ve RuBo ard

I l@ve RuBo ard

)#!

Som et m es, I have a set of 3ro4 edur

es that I must follo w. The 3ro4 edur es are 4om mon at a h -h level , but m3l eme nt nsom e of the ste3s 4an vary. Bor e2a m3le , Duer y na

SK9 data base s fa rly rout ne at a h -h level , but som e of the deta lsE say, how to 4onn e4t to the data base E 4an vary base d on deta ls su4h

as the 3latf orm. The Tem 3late 8eth od allow s me to def n e the seDu en4e of ste3s and then overr de thos e ste3s that need to 4han -e.

l@ve RuBo ard

I l@ve RuBo ard

Cha

?v er ve w 8o re Re Du re me nts for th e Ca se St ud y Th e Ba 4to

ry 8e th od Pat ter n Be ld (o tes ! 7s nth e Ba 4to ry 8e th od Pat ter n Su m ma ry


I l@ve RuBo ard

I l@ve RuBo ard

Over

Th s 4ha3 ter 4ont nues the e) ta l n 4ase stud y d s4u ssed thus far n Cha3 ters ',T '0. In th s 4ha3 ter,

I l@ve RuBo ard

I l@ve RuBo ard

.or

In Cha3 ter '0, 1The Tem 3late 8eth od Patte

rn,1 I -nor ed the ssue of how to nsta nt at e the data base ob5e 4t reDu red by my 4urre nt 4ont e2t. I may not want to ma6 e the

Cli ent
res3

ons b le for nsta nt at nthe data base ob5e 4t. Inste ad, I m -h t want to - ve that res3 ons b l ty to the

3ue ryT e'p late/


4lass tself . In Cha3

ter '0, ea4h der v at on of the

3ue ryT e'p late/


was s3e4 al Ge d for a 3art 4ular data base . Thus ,I m -h t want to ma6 e ea4h der v at on res3

ons b le for nsta nt at nthe data base to wh 4 h t 4orre s3on ds. Th s woul d be true whet her the

3ue ryT e'p late/


<and ts der v at on s= was the only

4lass us n - the data base or not. B -ur e '%)' show s th s solut on. -i!u

In B -ur e '%) ',

the

do" uer y
meth od n the Tem 3late 8eth od s us n -

#ak eD$
to nsta nt at e the a33r o3r a te data base ob5e 4t.

3ue ryT e'p late/


does not 6now

wh 4 h data base ob5e 4t to nsta nt at eP t only 6now s that one ' st be nsta nt at ed and 3rov des an nter fa4e for ts nsta nt at on. The der v ed

4lass es from

3ue ryT e'p late/


w ll be res3 ons b le for 6now nwh 4 h ones to nsta nt at e. Ther efore , at th s level ,I 4an defer the de4 s ons on

how to nsta nt at e the data base to a meth od n the der v ed 4lass . S n4 e there sa meth od nvol ved n ma6 nan ob5e 4t, th s a33r oa4h

s 4alle da 9act ory %et" od(

P#bl

(ote that the

#ak eD$
met hods are 3rot e4te d <as nd 4 ated by the U s -ns =. In th s 4ase , only the

3ue ryT e'p lat e


4lass and ts der v at on s 4an a44e ss thes e met hods . If I want ob5e 4ts othe r than

3ue ryT e'p lat e to


be able to

a44e ss thes e met hods , then they shou ld be 3ubl 4. Th s s anot her, Du te 4om mon, way to use the Ba4t ory 8eth od. In th s 4ase I st ll have

a der v ed 4lass ma6 nthe de4 s on as to wh 4 h ob5e 4t to nsta nt at e.

I l@ve RuBo ard

I l@ve RuBo ard

"he

The Ba4to ry 8eth od s

a 3atte rn nten ded to hel3 ass n res3 ons b l ty for 4reat on. A44o rd nto the Janof Bour, the nten t of the Ba4to ry 8eth od s to #ef n

e an nter fa4e for 4reat nan ob5e 4t, but let sub4l asse s de4 d e wh 4 h 4lass to nsta nt at e. Ba4to ry 8eth od lets a 4lass defer nsta nt at

on to sub4l asse s.R'S

R'S

Jamma , C., >elm, R., Johnso n, R., Al ss de s, J., Design Pattern s: Ele'en ts of 0e sab le ObjectOriente d &oftwar e, Read n -, 8ass.! Add son ) "esley, '%%+, 3. .&+.

I l@ve RuBo ard

I l@ve RuBo

ard

iel

In the 4lass 4 m3l eme ntat on of the Abstr a4t Ba4to ry, I had an abstr a4t 4lass def n e the meth ods to 4reat ea fam l y of ob5e 4ts. I der v

ed a 4lass for ea4h d ffer ent fam l yI 4ould have . Ca4h of the meth ods def n ed n the abstr a4t 4lass and then overr dde n n the der v ed 4lass es were

follo w nthe Ba4to ry 8eth od 3atte rn. Som et m es t s usef ul to 4reat ea h era r4h 4 al 4lass stru4 ture that s 3aral lel to an e2 st n4lass stru4

ture, w th the new h era r4hy 4ont ann som e dele -ate d res3 ons b l t es . In th s 4ase, t s m3o rtant for ea4h ob5e 4t n the or nal h era r4hy to be

able to nsta nt at e the 3ro3 er ob5e 4t n the 3aral lel h era r4hy. A Ba4to ry 8eth od 4an be used for th s 3ur3 ose. The Ba4to ry 8eth od 3atte

rn s 4om monl y used whe n def n nfram ewor 6s. Th s s be4a use fram ewor 6s e2 st at an abstr a4t level . 7sua lly, they do not 6now and shoul

d not be 4on4 erne d abou t nsta nt at ns3e4 f4 ob5e 4ts. They need to defer the de4 s ons abou t s3e4 f4 ob5e 4ts to the user s of the fram

ewor 6.

"he

Inte nt

Pro ble m

Sol ut o n

Part 43 ant s and Coll abo rato rs

Con seD uen 4es

Im3 lem ent at o n

JoB Ref ere n4e

-i!u

I l@ve RuBo ard

I l@ve RuBo ard

)#!

The Ba4to ry 8eth od 3atte rn s one of the stra -htfo rwar d 3atte

rns that you w ll use a-a n and a-a n. It s used n thos e 4ase s wher e you want to defer the rules abou t nsta nt at nan ob5e 4t to

som e der v ed 4lass . In su4h 4ase s, t s most natu ral to 3ut the m3l eme ntat on of the meth od n the ob5e 4t that s res3 ons b le for that beha v or.

I l@ve RuBo ard

I l@ve RuBo ard

Cha

?v er ve w In th e Re al "o rld ! Aa ra to ns Ca se St ud y n

Aa ra to n! An Int er na to nal C) Ta l Sy ste m Be ld (o tes Su m ma ry


I l@ve RuBo ard

I l@ve RuBo ard

Over

Th s 4ha3 ter 4on4l udes the e) ta l n 4ase stud y d s4u ssed thus far n Cha3 ters ',T '%. (ow that I have d s4u ssed an ent r e set of nd v

dual 3atte rns, t s t me to ste3 ba46 to loo6 at one of the b -est 3robl ems n soft ware deve lo3m ent! hand l nvar a t on w th n the 3robl em dom

a n. #es -n 3atte rns 4an hel3 anal ysts dent fy and or-a n Ge var a t ons su44 essfu lly. In th s 4ha3 ter,

I l@ve RuBo ard

I l@ve RuBo ard

%n

In the real worl d, 3robl ems are not t dy or well beha ved. C24e 3t n the most tr v a l 3robl

ems, there alwa ys see m to be e24e 3t on s and var a t ons that are not well or-a n Ged . They are the 1-ot4 has1 that r se u3 to wre4 6 our f nel y 4raft

ed mod els. Bor e2a m3le , 3at e nts 4om n- to a hos3 tal ty3 4 ally -o to the adm tt noff 4 e f rst. But whe n there sa l fe) threa ten n -

s tua t on, the 3at e nt -oes d re4 tly to the emer -en4 y room befor e hav n- to -o to adm tt n. Thes e are the var a t ons n the real worl d, the d ffer

ent s3e4 al 4ase s that our syste m has to deal w th. And th s s what 4reat es head a4he s for us anal ysts. Can 3atte rns hel3 us deal w th

var a t on more eff 4 ently F I have used an a33r oa4h to ma6 e e23l 4t the var a t ons n the syste m and then use the anal ys s to dent

fy the 3atte rns I ou-h t to use n my des n. The ste3s n my a33r oa4h are as follo ws! '. I

&. P

.. C

,.

+. 7

/. I

*. #

I l@ve RuBo ard

I l@ve RuBo ard

Cas

Su33 ose an e) ta l syste m must 3ro4 ess sales orde

rs n seve ral d ffer ent nat o ns. In t a lly I have to hand le 5ust the 7n te d Stat es and Cana da. I loo6 at my reDu reme nts and see seve ral th n-

s that vary. I note thes e n Table &$) '.

Table

Cas e 7.S. A

Can ada

The var a t ons 3res ente d n th s 3robl em are not too 4om 3l 4a ted. Just by loo6 n-, t see ms obv ous

how to deal w th them .A s m3l e 3robl em, yes. But t llust rates a te4h n Du e for deal nw th var a t on that I have used man y t me s. It sa

s m3l e te4h n Du e but t see ms to s4ale well for man y real) worl d 3robl ems. I 4all th s the Anal ysis %atri x( At th s stae, the ob5e

4t ve s to f nd the conc epts that are vary n-, to f nd 3o nt s of 4om mon al ty, and to un4o ver m ss nreDu reme nts. The 4on4 e3ts 4om e from the

s3e4 f4 reDu reme nts of ea4h 4ase. #es -n and m3l eme ntat on ssue s are hand led n later staes. 9etLs ben by loo6 n- at one 4ase. I loo6

at ea4h fun4t on that I must m3l eme nt and label the 4on4 e3t t re3r esen ts. Ca4h fun4t on 3o nt w ll be 3ut on ts own l ne. I w ll 3ut the 4on4

e3t t re3r esen ts at the far left. I w ll show th s 3ro4 ess ste3 by ste3, start nw th Table &$) &.

Table

Cal4 ulat e fre -ht

(ow, I 4ont nue w th the ne2t 3 e4e of nfor mat on, 1use 7.S. 3ost al rules for ver f y naddr esse s,1 by add nanot her row to hold that 3 e4e

of nfor mat on, as show n n Table &$) ..

Table

Cal4 ulat e fre -ht Aer fy add ress

I 4ont nue thro u-h all of the

4on4 e3ts n the f rst 4ase, as show n n Table &$) ,.

Table

Cal4 ulat e fre -ht Aer fy add ress

Cal4 ulat e ta2

8on ey
(ow, I mov e to the ne2t 4ase and the other 4ase s, one 4olu mn 3er 4ase, 4om 3let nea4h 4ell w th as mu4 h nfor mat on as I

have . The 4om 3lete d matr 2 for the ne2t 4ase s n Table &$) +.

Table )J-;'

Cal4 ulat e fre -ht Aer fy add ress

Cal4 ulat e ta2

8on ey

As I bu ld the matr 2, I d s4o ver -a3s n the reDu reme

nts. I w ll use th s nfor mat on to e23a nd my anal ys s. Thes e n4on s ste n4 es - ve 4lues abou t n4o m3le te nfor mat on from the 4ust omer . That

s, n one 4ase a 4ust omer m -h t ment on som e s3e4 f4 reDu reme nt wh le anot her 4ust omer dd not. Bor e2a m3le , n -ett nreDu reme nts

for the 7n te d Stat es, no ma2 mum we ht may have been ment one d, wh le .'.+ 6 lorams m -h t have been state d for Cana da. By 4om 3ar n - the

reDu reme nts I 4an f ll n the holes by -o n ba46 to my Ame r 4an 4ont a4t and as6 n - her s3e4 f 4all y abou t we ht lmt s <wh 4h, n fa4t,

may not e2 st =. As t me -oes on we -et new 4ase s to hand le <for e2a m3le , we may e23a nd nto Jer man y=. "he n you d s4o ver a new

4on4 e3t for one of the 4ase s, add a new row even f t does not a33l y to any of the other 4ase s. I llust rate th s n Table &$) /.

Table )J-<'

Cal4 ulat e fre -ht Aer fy add ress

Cal4 ulat e ta2

8on ey #at es

8y e23e r en4 e w th 4ust ome rs has tauht me seve ral th ns!

do not, but are try n - to 1hel 3 me out.1

(ow that the 4on4 e3ts are reve aled, what shoul dI do w th what I 6now F >ow do I ben to mov e

towa rd m3l eme ntat onF 9oo6 at the matr 2 n Table &$) /. The f rst row s label ed 1Cal4 ulate fre ht,1 and n4lu des 17se 7PS rates ,1 17se

Cana d an sh 3 3er,1 and 17se Jer man sh 3 3er.1 Th s row re3r esen ts both

In fa4t, ea4h row re3r esen ts s3e4 f4 ways

of m3l eme nt na -ene ral G ed 4on4 e3t. Two of the rows <mon ey and date s= may be hand led at the ob5e 4t level . Bor e2a m3le , mon

ey 4an be hand led w th ob5e 4ts 4ont ann -a 4urre n4y ob5e 4t. 8any 4om 3ute r lanua-e s su33 ort d ffer ent date form ats for d ffer ent nat o

nal t es n the r l brar es. Table &$)* show s the 4on4 e3tu al way of hand l nea4h row.

Table

Cal4

fy add ress Cal4 ulat e ta2 8on ey #at es


Arm ed w th the nfor mat on that som e of the rows re3r esen ta Strat e-y 3atte rn and ea4h 4olu mn

re3r esen ts a fam l y n an Abstr a4t Ba4to ry 3atte rn, I 4an deve lo3 a h -h) level a33l 4at o n des n as show n n B -ur e &$) '. -i!u

I l@ve RuBo ard

I l@ve RuBo ard

iel

In 3ra4t 4e, almo st any 6 nd of 3atte rn that nvol

ves 3oly mor3 h sm 4ould be 3res ent n the Anal ys s 8atr 2. ?f the 3atte rns ILve 3res ente d, th s woul d n4lu de Br de, #e4o rator, Tem 3late , and

?bse rver. ?the r 3atte rns I have used n an Anal ys s 8atr 2 are Com 3os t e, Pro2 y, Cha n of Res3 ons b l ty, Com man d, Itera tor, 8ed ator, and Ast

or. Bor e2a m3le , f n our e)ta l syste mI n4lu ded reDu reme nts on 3r nt nsales t 46e ts and foun d the follo w nvar a t ons !

I woul d n4lu de th s nfor mat on n ts own row, ea4h entry relat n- to the form at of sales t 46e ts. I woul d m3l eme nt

th s rowLs reDu reme nts w th a #e4o rator 3atte rn. Altho u-h the Anal ys s 8atr 2 rarel y 4a3t ures all as3e 4ts of a 3art 4ular 3robl em dom a n, I

have foun d t usef ul for at least 3art of most 3robl em dom a ns. I f nd t most usef ul whe nI am - ve n so man y s3e4 al 4ase s that I 4anLt

-et my head arou nd the b3 4tu re. It s usual ly wors e than th s. Rarel y are d ffer ent 4ase s of reDu reme nts state d to anal ysts or deve lo3er

s n any 4oor d nat ed fash on. Th s does not s -n f 4an tly 4om 3l 4a te the Anal ys s 8atr 2 3ro4 ess, howe ver. In thes e s tua t ons ,I ta6e a

featu re and loo6 n the leftm ost 4olu mn and see what 4on4 e3t t sa var a nt of. If I f nd the 4on4 e3t, I 3ut the featu re n that row. (ot be n able

to f nd su4h a 4on4 e3t nd 4 ates I must 4reat ea new row. In e2tre me s tua t ons , the Anal ys s 8atr 2 may be the only way to -et a hand

le on th ns. I on4e had a 4l en t that l tera lly had doGe ns of s3e4 al 4ase s. Ca4h 4ase was a se3a ratel y deve lo3e d do4u ment 4ontr ol syste

m. The 3robl em was to nterate all of thes e do4u ment 4ontr ol syste ms to-et her. So man y s3e4 al 4ase s were 3res ent <ther e were also

doGe ns of rows = that t was m3o ss bl e to th n6 abou t the ent r e 3robl em all at on4e . The anal ysts dd not have a -ood 4on4 e3tu al -ras 3 of what

was nvol ved. They 5ust tal6e d abou t -ene ral rules and e24e 3t on 4ase s. By 4ons der n ea4h 4ase nd v dual ly, I was able to abstr a4t out the 4om

mon data and beha v or <wh 4h show ed u3 n the leftm ost 4olu mns= and then m3l eme nt them w th des n 3atte rns.
I l@ve RuBo ard

I l@ve RuBo ard

)#!

Aar a t on n 4on4 e3ts 4an be one of the -reat est 4hall en-e s that an anal yst 4an fa4e. In th s 4ha3 ter, I 3res ente da s m3l e anal

ys s tool that I have foun d hel3f ul n ma6 nsens e of su4h var a t on. I 4all th s tool the Anal ys s 8atr 2, and t s base d on the 4on4 e3ts of Chr s

to3h er Ale2 ande r and Jm Co3l en. I a33l ed th s tool to a sam 3le 3robl em to show how t m -h t reve al the ty3e s of 3atte rns that are nher

ent n the 3robl em. "h l e th s tool s very usef ul n 4ont ann var a t on and hel3 nme th n6 abou t my 3robl em dom a n, I do not 3rete nd t

4a3t ures all as3e 4ts of a des n.

I l@ve RuBo ard

I l@ve RuBo ard

Par t 5 % : E n d i n g s a n d

' e g i n n i n g s
Part Over view In th s 3art I 4ont nue w th our new 3ers 3e4t ve on ob5e 4t) or en ted des n. In

3art 4ular ,I des4 r be how des n 3atte rns use th s 3ers 3e4t ve n the r des n and m3l eme ntat on. I 4lose th s se4t on w th re4o mme ndat ons for

furth er read n-.

Cha pte r

&'

&&

I l@ve RuBo ard

I l@ve RuBo ard

Cha

?v er ve w A

Su m ma ry of ?b 5e4 t) ?r en te d Pr n4 3le s >o w #e sn Pat ter ns Cn 4a 3s ula te Im 3le me

nt at on s Co m mo nal ty @A ar ab l ty An aly ss an d #e sn Pat ter ns #e 4o m 3o sn -a Pr obl

em #o ma n nt o Re s3 on sb lt es Rel at on sh 3s " th na Pat ter n Pat ter ns an d Co nt e2t ual

#e sn Be ld (o tes Su m ma ry
I l@ve RuBo ard

I l@ve RuBo ard

Over

At the end of any boo6 , t s alwa ys n 4e to

ste3 ba46 and see what we have -a n ed. In th s boo6 ,I have tr ed to - ve you a bette r and 3erh a3s new unde rstan d nof ob5e 4t) or en ted 3r n4

3les by tea4 h nyou des n 3atte rns and unde rstan d nhow des n 3atte rns e23l an the ob5e 4t) or en ted 3ara d -m . In th s 4ha3 ter, I

rev e w the follo w n!

>

B nall y, I offer som e

f eld note s from my own 3ra4t 4e.


I l@ve RuBo ard

I l@ve RuBo ard

In the 4our se of the d s4u ss on on des n 3atte rns, I have state

da num ber of the 3r n4 3les of the ob5e 4t) or en ted 3ara d -m . Thes e 3r n4 3les 4an be sum mar Ged as follo ws!

) #a ta) hd n) Cla ss hd n<b eh nd an ab str a4t 4la ss or nt erf a4 e= ) Im

3le me nt at on hd n-

I l@ve

RuBo ard

I l@ve RuBo ard

=ow

Seve ral of the des n 3atte rns I have 3res ente d have the 4har a4ter st 4 that they sh el d m3l eme ntat on deta

ls from a

Cli ent
ob5e 4t. Bor e2a m3le , the Br de 3atte rn h des from the

Cli ent
how the 4lass es der v ed from the

A%s tra cti on


are

m3l eme nted. Add t onall y, the

.'p le' ent ati on


nter fa4e h des the fam l y of m3l eme ntat ons from the

A%s tra cti on


and ts der v at on s as

well. In the Strat e-y 3atte rn, the m3l eme ntat ons of ea4h

Con cre teS tra tegy/


are h dd en. Th s s true of most of the 3atte rns des4 r bed

by the Janof Bour ! they - ve ways to h de s3e4 f4 m3l eme ntat ons. The valu e of hdn - the m3l eme ntat ons s that the 3atte rns allow

for eas l y add nnew m3l eme ntat ons, s n4e the 4l en t ob5e 4ts do not 6now how the 4urre nt m3l eme ntat on wor6 s.
I l@ve RuBo ard

I l@ve RuBo ard

Co!

In Cha3 ter %, 1The Br de Patte rn,1 I show ed how the Br de 3atte rn 4an be der v ed us n 4om mon al ty@ var a b l ty

anal ys s. 8any other 3atte rns 4an be der v ed as well, n4lu d nthe Strat e-y, Itera tor, Pro2 y, Stat e, Ast or, Tem 3late 8eth od, and Abstr a4t Ba4to

ry. "hat s more m3o rtant , howe ver, s how man y 3atte rns are m3l eme nted by us n 4om mon al ty@ var a b l ty anal ys s. 9oo6 nfor 4om

mon al t e s 4an hel3 us d s4o ver that a 3atte rn s 3res ent n our 3robl em dom a n. Bor e2a m3le , n the Br de 3atte rn, I may start w th seve

ral s3e4 al 4ase s!

;no w nthe Br de hel3s me see thes e as s3e4 al 4ase s of two 4om mon

al t e s!

The Strat e-y 3atte rn s sml ar n that whe nI see seve ral d ffer ent rules ,I 6now to loo6 for a 4om mon

al ty amo n-st the rules so I 4an en4a 3sula te them . But 3leas e 6ee3 learn n3atte rns. Read the l tera ture. Patte rns 3rov de the ba46 dro3 for

d s4u ss on s abou t lesso ns learn ed n anal ys s and des n. They - ve a team of deve lo3er sa 4om mon vo4a bular y for d s4u ss na 3robl em. They

enab le you to n4or 3orat e best) 3ra4t 4e a33r oa4h es nto your 4ode .
I l@ve RuBo ard

I l@ve RuBo ard

Dec

Com mon al ty@ var a b l ty anal

ys s dent f es my 4on4 e3tu al v ew <the 4om mon al ty= and my m3l eme ntat on v ew <ea4 h 3art 4ular var a t on= . If I 4ons der 5ust the 4om mon al t e

s and the ob5e 4ts that use them ,I 4an th n6 abou t the 3robl ems na d ffer ent way Ea de4o m3o sto n of res3 ons b l t es . In the Br de

3atte rn, for e2a m3le , the 3atte rn says to loo6 at my 3robl em dom an as be n 4om 3ose d of two d ffer ent ty3e s of ent t es <abst ra4t ons

and m3l eme ntat ons=. I there fore do not need to be lmt ed by 5ust do n ob5e 4t) or en ted de4o m3o sto n <that s, de4o m3o s nmy 3robl

em dom an nto ob5e 4ts=, I 4an also try de4o m3o s nmy 3robl em dom an nto res3 ons b l t es , f that s eas e r for me to do. I 4an then def n e the

ob5e 4ts that I reDu re to m3l eme nt thes e res3 ons b l t es <end nu3 w th ob5e 4t de4o m3o sto n=. Th s s 5ust an e2te ns on of the

rule I state d earl er that des ners shoul d not worr y abou t how to nsta nt at e ob5e 4ts unt l after they 6now all of the ob5e 4ts they need .

That rule 4an be v ew ed as a de4o m3o s nthe 3robl em dom an nto two 3arts !

"

>

S3e4 f4 3atte rns often - ve us

ass s tan4 e n th n6 nabou t how to de4o m3o se res3 ons b l t es . Bor e2a m3le , the #e4o rator 3atte rn - ves me a way to 4om b ne ob5e 4ts fle2 bly f

I de4o m3o se my 3robl em dom an nto the ma n set of res3 ons b l t es I alwa ys use <the

Con cre teC o'p one nt=


and the var a t ons I

o3t o nally have Emy de4o rator s. Strat e- es de4o m3o se my 3robl em nto an ob5e 4t that uses rules and the rules them selve s.
I l@ve RuBo ard

I l@ve

RuBo ard

+ela

I must adm t, n my 4our ses, I have som e fun w th a 4erta n Duot e from Ale2 ande r. After I have been tal6 nabou t

how -reat 3atte rns are for two) th rd s of a day, I 3 46 u3 Ale2 ande rLs !i'e less ,ay of $ ild ing, turn to the end, and say Th s boo6 s

+,% 3a-e s lon-. ?n 3a-e +,+, wh 4 h, I th n6 you w ll a-re e, s 3rett y 4lose to the end, Ale2 ande r says, 2At t"is final stag e# t"e patt erns are

no long er i'po rtant :Q2R'S

R'S

Ale2an der, C., Ish 6aw a, S., S lverst e n, 8., !"e !i'eles s ,ay of $ ildin g, (ew Nor6! ?2ford 7n vers ty Press, '%*%, 3. +,+.

I 3aus e to say, 1I w sh heLd have told me

th s at the benn n and I 4ould have save d mys elf som e t meO 1 Then I 4ont nue to Duot e from h m! 2!"e patt erns "ave ta g "t yo

to be rece ptive to w"at is real( 2R&S

R&S

b d,

3, +,+.

I fns h w th, 1If you read Ale2 ande rLs boo6 , you w ll 6now what s real Ethe relat onsh 3s

and for4e s des4 r bed by the 3atte rns.1 The 3atte rns - ve us a way to tal6 abou t thes e. >ow ever, t s not the 3atte rns them selve s that

are m3o rtant . Th s s true for soft ware 3atte rns as well. A 3atte rn des4 r bes the for4e s, mot vat o ns, and relat onsh 3s abou ta 3art

4ular 3robl em na 3art 4ular 4ont e2t and 3rov des us w th an a33r oa4h to addr ess n thes e ssue s. The Br de 3atte rn, for e2a m3le , s

abou t the relat onsh 3 betw een the der v ed 4lass es of an abstr a4t o n and the r 3oss ble m3l eme ntat ons. A Strat e-y 3atte rn s abou t the relat onsh

3s betw een

I l@ve RuBo ard

I l@ve RuBo ard

Patt

In the CA#@ CA8 3robl em earl er n th s boo6

,I show ed how des n 3atte rns 4an be used by fo4us non the r 4ont e2t w th ea4h other . #es -n 3atte rns wor6 nto-et her 4an ass s t n

the deve lo3m ent of an a33l 4at o nLs ar4h te4tu re. It s also usef ul to d st n-u sh how man y of the 3atte rns are m 4r o4os m4 e2a m3le s of des n by

4ont e2t. Bor e2a m3le ,

In fa4t, des n nto nter fa4es and 3oly mor3 h sm n -ene ral s a

6 nd of des n by 4ont e2t. 9oo6 at B -ur e &') ', wh 4 h s a re3r nt of B -ur e 0) ,. (ot 4 e how the abstr a4t 4lass L nter fa4e def n es the

4ont e2t w th n wh 4 h all of ts der v ed 4lass es must be m3l eme nted. -i!u

I l@ve RuBo ard

l@ve RuBo ard

iel

As you learn 3atte rns, t s usef ul to loo6 for the follo w nfor4e s and 4on4 e3ts!

I l@ve RuBo ard

I l@ve RuBo ard

)#!

In th s 4ha3 ter I sum mar Ged our new 3ers 3e4t ve on ob5e 4t) or en

ted des n. I des4 r bed how des n 3atte rns man fest th s. I su-este d that t s usef ul to loo6 at 3atte rns by see n -

"

>

>

>

>

I l@ve RuBo ard

I l@ve RuBo ard

Cha

Th s boo6 has been an ntro du4t on. An ntro du4t

on to des n 3atte rns, ob5e 4t or en tat o n, and to a more 3owe rful way to des n 4om 3ute r syste ms. >o3e fully, t has - ve n you som e

tools to -et start ed n th s r 4h and rewa rd nway of th n6 n-. "her e shoul d you turn ne2t n your stud yF I 4on4l ude th s boo6 w th an anno

tated l st of my 4urre nt re4o mme ndat ons. In th s 4ha3 ter,

) Bu rth er re ad nn de sn

3a tte rns . ) Ja va de vel o3 ers . ) C: : de vel o3 ers . ) C? B? 9 3r ora m me rs wh

o wa nt to lea rn ob5 e4t or en tat on . ) 9e ar nn th e 3o we rfu l de vel o3 me nt me th od

olo -y 4al led IP <e Itr em e Pr ora m m n=.

I l@ve RuBo ard

I l@ve RuBo ard

Desi

The "eb

s te for th s boo6 s lo4at ed at htt3! @@ww w.ne tob5e 4t ve s.4o m@d 3e23 la ne d At th s s te you w ll f nd add t onal nfor mat on on des n 3atte

rns, n4lu d n-

Nou w ll also f nd a form that you 4an use to send us your 4om ment

s and Dues t ons abou t th s boo6 . "e also 3ubl sh an e) G ne on des n 3atte rns and -ene ral ob5e 4t) or en ted des n. To subs 4r be , send

an e) ma l w th your nam e, 4om 3any nam e, and addr ess to nfo @net ob5e 4t ve s.4o m w th the word 1sub s4r b e1 n the sub5 e4t l ne.
I l@ve RuBo ard

I l@ve RuBo ard

+ec

I re4o mme nd the follo w nboo6 s and refer en4e s on ob5e 4t) or en ted 3roram m nand the 789! Bowl er,

8., 0efa ctori ng: -'pr ovin g t"e Desi gn of Exist ing Code , Read n-, 8ass .! Add son) "esl ey, &$$$ . !"e 'ost exte nsiv e treat 'en t of refac torin

g avail able( Bowl er, 8., S4ott , ;., 6%L Distil led &eco nd Editi on: A $rief + id e to t"e &tan dard Obje ct %od eling Lang age , Read n-,

8ass .! Add son) "esl ey, &$$$ . !"is is by far 'y favo rite so r ce for learn ing t"e 6%L( -t is bot" appr oac" able to begi n wit" and sef l as

a refer ence (find 'ys elf refer ring to it agai n and agai n( 8eye r, B., Obje ctOrie nted &oft ware Cons tr ct ion, 733e r Sadd le R ver

, (.J.! Prent 4e >all, '%%* . An incre dibly t"or o g" boo* by one of t"e brilli ant 'ind s in o r ind stry( The f eld of des n 3atte rns 4ont nues

to evolv e and dee3 en. ?ne 4an stud y the f eld on a var e ty of level s and from man y 3ers 3e4t ves. I re4o mme nd the follo w nboo6 s and

refer en4e s to hel3 you on your 5ourn ey! Ale2 ande r, C., Ish 6 awa, S., S lve rste n, 8., !"e !i'e less ,ay of $ ild ing, (ew Nor6! ?2fo rd 7n v

ers t y Pres s, '%*% . $ot" a pers onal and prof essio nal favo rite( -t is bot" ente rtain ing and insig "tf l ( -f yo read only one boo* fro' t"is list#

"ave it be t"is one( Ale2 ande r, C., Ish 6 awa, S., S lve rste n, 8., A Patt ern Lang age : !ow ns>$ ildi ngs> Cons tr ct ion, (ew Nor6! ?2fo rd 7n v

ers t y Pres s, '%** . Ale2 ande r, C., Ish 6 awa, S., S lve rste n, 8., Note s on &ynt "esis of 9or ', (ew Nor6! ?2fo rd 7n v ers t y Pres

s, '%*$ . Co3l en, J. % lti Para dig' Desi gn for C? ?, Read n-, 8ass .! Add son) "esl ey, '%%0 . C"ap ters .DE are a ' st

read even for nonC?? deve loper s( !"is boo* cont ains t"e best desc ripti on of co' 'on ality >vari abilit y anal ysis any w"er e( &ee o r boo* 3s ,eb

site for an online versi on of )i'3s doct oral disse rtati on# w"ic " is e1 i vale nt to "is boo* ( Jam ma, C., >elm , R., John son, R., Al ss des,

J., Desi gn Patt erns : Ele' ents of 0e s able Obje ctOrie nted &oft ware , Read n-, 8ass .! Add son) "esl ey, '%%+ . &till t"e best desi gn

patt erns boo* avail able( A ' st for C?? deve loper s( Jard ner, ;., Cogn itive Patt erns : Probl e'&olvi ng 9ra 'ew or*s for Obje ct !ec"

nolo gy, (ew Nor6! Cam br de 7n v ers t y Pres s, '%%0 . !"is appr oac" es patt erns fro' t"e pers pecti ve of cogn itive scien ce and artifi cial intell

igen ce( Dr( +ard ner was also "eav ily infl ence d by Alex ande r3s wor* ( S4h m dt, #., Stal, 8., Rohn ert, >., Buse hma nn, B., Patt ern-

Orie nted &oft ware Arc"i tect re# Fol( ., (ew Nor6! John " ley , &$$$ . !"e boo* to se for ' lti t"re aded and distri b te d envir on' ents( Al ss

des, J., Patt ern 4atc "ing, Read n-, 8ass .! Add son) "esl ey, '%%0 . !"is is a good adva nced boo* on desi gn patt erns( -ll st rates seve ral ways t"at

patt erns wor* toge t"er( $ot" o r boo* and t"e +o9 boo* s"o ld be read befo re readi ng t"is one(
I l@ve RuBo ard

I l@ve RuBo ard

+ec

"he n t

4om es to learn nJava, my favor te boo6 s are! C46el , B., !"in *ing in )ava, &nd Cd t on, 733e r Sadd le R ver , (.J.! Prent 4e >all, &$$$

. One of t"e best )ava boo* s on t"e 'ar *et( &ee "ttp: >>ww w(ec *elo bject s(co '>D ownl oad& ites for a dow nloa dabl e versi on of t"is boo* (

>ors tman n, C., Core )ava .8 Fol 'e @ 8 9 nd a'e ntals , Palo Alto! Pear son Cdu4 at on , '%%% . Anot "er good boo* for learn ing )ava (

Ca4h lanua-e has ts own set of ssue s whe n t 4om es to m3l eme nt ndes n 3atte rns. I re4o mme nd the follo w nboo6 s and refer en4e

s whe n t 4om es to Java ! Coad , P., )ava Desi gn, 733e r Sadd le R ver , (.J.! Prent 4e >all, &$$$ . -f yo are a )ava deve loper # t"is

boo* is a ' st read ( -t disc sse s 'ost of t"e princ iples and strat egie s we "ave fo n d sef l in sin g desi gn patt erns even t"o g" it does n3t

'en tion desi gn patt erns speci ficall y( Jran d, 8., Patt erns in )ava # Fol( @, (ew Nor6! John " ley , '%%0 . -f yo are a )ava deve

loper # yo 'ay find t"is boo* sef l( -t "as its exa 'ple s in )ava and it ses t"e 6%L( 4ow ever # t"e a t" ors belie ve t"e disc ssio ns on force s

and 'oti vatio ns in t"e +o9 boo* are 'or e sef l t"an is pres ente d in +ran d3s boo* ( 4ow ever # t"er e is a lot of val e by getti ng anot

"er set of exa 'ple s# parti c lar ly w"e n in t"e lang age of se <)av a=( See htt3! @@5av a.su n.4o m@5& se@'. .@do 4s@a 3@n de2. html for

nfor mat on on the Java API for ?bse rver and ?bse rvabl e. Ther e are s3e4 al 4ons derat ons whe n t 4om es to deal nw th threa ds n Java. I

re4o mme nd the follo w nreso ur4e s to hel3 learn abou t th s area ! >ollu b, A., !a'i ng )ava !"re ads, Ber6 eley! APre ss, &$$$ . >yde

, P., )ava !"re ad Prog ra' 'ing : !"e A t" orita tive &ol tion, Ind a na3o l s! SA8 S, '%%% . 9ea, #., Conc rre nt Prog ra' 'ing in )ava

: Desi gn Princ iples and Patt erns, Se4o nd Cd t on, Read n-, 8ass .! Add son) "esl ey, &$$$ .
I l@ve RuBo ard

I l@ve RuBo ard

+ec

have foun d the follo w nesse nt al for us n - C: : for 7(II ! Stev ens, "., Adva nced Prog ra' 'ing in t"e 6NG Envir on' ent, Read n-, 8ass

.! Add son) "esl ey, '%%& . !"is is a mus t reso rce for anyo ne doin g C? ? deve lop' ent on 6NG(
I l@ve RuBo ard

I l@ve RuBo ard

+ec

I have foun d the follo w nhel3f ul for C?B ?9 3roram mers who want to learn ob5e 4t) or en ted des n! 9eve y, R., 0een gine ering Cobo l

wit" Obje cts, (ew Nor6! 84Jr aw) > ll, '%%+ .A sef l boo* for CO$ OL prog ra' 'ers w"o are tryin g to learn obje ctorien ted desi gn(
I l@ve RuBo ard

I l@ve RuBo ard

+ec

"he n t 4om es to -a n n3rof 4 en4 y n eItr eme Proram m n<IP=, my two best re4o mme ndat ons are htt3! @@ww

w.ne tob5e 4t ve s.4o m@23 E ?ur own "eb s te on IP, n4lu d nart 4l es and 4our ses on IP. Be46 , ;., Extr e'e Prog ra' 'ing Expl aine d:

E'b race C"an ge, Read n-, 8ass .! Add son) "esl ey, &$$$ . !"is is wort "w"i le readi ng for anyo ne invol ved in soft ware deve lop' ent# even

if yo are not plan ning on sin g GP( "ave selec ted H/ or so page sconsi der esse ntial readi ng and list t"e ' on o r GP site( "e are

n the 3ro4 ess of def n nour own soft ware deve lo3m ent 3ro4 ess wh 4 h we 4all 9 -ht we ht Patte rn) A44el erate d Soft ware deve lo3m ent <9PA

=. Th s s an nterat o n of seve ral meth odol o- es , anal ys s te4h n Du es and des n te4h n Du es. See htt3! @@ww w.ne tob5e 4t ve s.4o m@l3 a for more

nfor mat on.

I l@ve RuBo ard

I l@ve RuBo ard

+ec

Th s boo6 m rr ors my 3h lo so3h y of be n ntro s3e4t ve and alwa ys loo6 n- to see

how I 4an m3r ove mys elf and my wor6 ! >unt , A., Tho mas, #., !"e Prag 'ati c Prog ra' 'er: 9ro ' )o r ney 'an to %ast er, Read

n-, 8ass .! Add son) "esl ey, &$$$ . !"is is one of t"os e lovel y boo* s t"at read a few page s of eac" day( ,"e nco' e acro

ss t"ing salrea dy do# ta*e t"e oppo rt ni ty to ac*n owle dge 'ys elf( ,"e nfind t"ing s -3' not doin g# ta*e t"e oppo rt ni ty to learn (
I l@ve RuBo

ard

I l@ve RuBo ard

Pers

It s my bel e f that the best des ners are not thos e who l ve and breat he 3roram m nand noth nelse. Rath

er, be n able to th n6 and to l ste n, hav n- a more 4om 3lete and dee3 3ers onal ty, and 6now ndeas are what ma6 e for -reat des ners. Nou 4an

4onn e4t bette r w th other 3eo3 le. Nou 4an -lea n deas from other d s4 3l ne s <for e2a m3le , as we dd from ar4h te4tu re and from anth ro3ol o-y=.

Nou w ll 4reat e syste ms that bette r ta6e nto a44o unt hum an be n -s, for who m our syste ms e2 st anyw ay. 8any of my stud ents as6

abou t what I l 6e to read, what has sha3 ed how I th n6 and hel3 ed me n my 5ourn ey. The follo w nare my re4o mme ndat ons. Alan re4o

mme nds the follo w n! Jr ev e, B., !"e $l e Day $oo* :A Less on in C"ee ring Io 6p, ;ans as C ty! Andr ews 848 eel Publ sh n -, &$$$ .

!"is is a f n and delig "tf l boo* ( 0ead it w"e neve r yo are feeli ng dow n <it3s s"or t=( > ll, (., !"in * and +ro w 0ic", (ew

Nor6! Balla nt ne Boo6 s, '%/$ . 20ic "2 does n3t only 'ea n in 'on ey8 it 'ea ns in w"at ever for' you want to be ric" in( !"is boo* "as "ad a

prof o nd i'pa ct on bot" 'y pers onal and b si ness s cc ess( ;und tG, #., &top ping : 4ow to $e &till ,"e n Io 4ave to Jeep +oin g,

Ber6 eley! Cona r Pres s, '%%0 . As a reco verin g wor* a"oli c# t"is boo* is a bea tif l re'i nder of "ow to slow dow n and enjo y life# b t

still get t"ing s done ( 8an d no, ?., !"e +rea test &ale s'a n in t"e ,orl d, (ew Nor6! Bant am Pres s, '%/0 .read and 2pra ctice d2

t"is boo* a few year s ago( -t "as "elp ed 'e live 'y life t"e way -3ve alwa ys want ed to( -f yo read it# stro ngly s gg est doin g w"at

t"e scrol ls tell 4afi d to do8 not j st read abo t it <yo 3 ll *no w w"at 'ea n w"e n yo read t"e boo* =( P lGer , P., 6nli 'ite

d ,eal t": !"e !"eo ry and Pract ice of Econ o'ic Alc" e'y, Crow n Publ sher s, '%%$ . !"is boo* pres ents bot" a new para dig' for reso rce

s and weal t"# and "ow to ta*e adva ntag e of it( A ' st read in t"e infor 'ati on age( Rem en, R., %y +ran dfat "er3s $les sing s:

&tori es of &tre ngt" # 0ef ge# and $elo ngin g, (ew Nor6! R ver head Boo6 s, &$$$ .A lovel y boo* to refle ct on one3 s bless ings( Jm re4o mme

nds the follo w n! BuGa n, T., and BuGa n, B., !"e %ind %ap $oo* : 4ow to 6se 0adi ant !"in *ing to %axi 'i7e Io r $rai n3s 6nta pped

Pote ntial, (ew Nor6! #utt on Boo6 s, '%%, . !"is "as revol tion i7ed "ow teac "# co' ' ni cate# t"in* # and ta*e note s( An incre dibly pow erf l tec"

ni1 e( se t"is daily ( Cah l l, T., 4ow t"e -ris" &ave d Civili 7atio n, (ew Nor6! #oub leday , '%%+ . -f yo "ave any -ris" bloo d in yo # t"is

will 'a* e yo pro d( Cann ibals t rn ed to t"e grea test force for civili 7atio n and resc e E ro pe( #aw son, C., 0elig ion and t"e

0ise of ,est ern C lt re, (ew Nor6! #oub leday , '%+$ . 4ow religi on s"ap ed t"e deve lop' ent of ,est ern civili 7atio n and *ept at bay t"e

2bar baria nis' t"at is alwa ys l r*i ng j st belo w t"e s rfa ce(2 -'p orta nt insig "ts into scien tific t"o g"t( Jens en, B., &i'p licity :

!"e New Co' petiti ve Adva ntag e in a ,orl d of %ore # $ett er# 9ast er, Cam br de, 8ass .! Pers eus Boo6 s, &$$$ .A revol tion in t"o g"t

and *no wled ge 'an age 'en t( Desi gnin g syst e's t"at are si'p ler for peop le to se# ta*in g " ' ans into acco nt in o r proc esse s

and tec" nolo gies( 9 nenfel ter, S., !ran sfor 'ing C lt re, Jran d Ra3 ds! Ba6e r Boo6 >ous e, '%%0 .A 'od el for nde rsta ndin g

c lt res t"ro g" socia l ga' e t"eo ry( S3ra dely, J. P., !"e Et"n ogra p"ic -nter view, (ew Nor6! >ar4 ourt Bra4 e Jova nov 4 h Colle -e Publ

sher s, '%*% .A ' st read for anyo ne w"o want s to beco 'e a bett er inter view er( !"e class ic text sed by all st d ents of ant" ropol

ogy( " -, ;., Jno wled ge %an age 'en t %et" ods, #alla s! S4he ma Pres s, '%%+ .A virt al ency clop edia of tec" ni1 es for "elpi

También podría gustarte