2004-Head First Design Patterns

Table of Contents Chapter 1. Welcome to Design Patterns....................................................... 1 Section 1.1. It started with a simple...

39 downloads 408 Views 39MB Size

Chapter 1. Welcome to Design Patterns

Table of Contents Chapter 1. Welcome to Design Patterns....................................................... 1 Section 1.1. It started with a simple SimUDuck app....................................................................................................................... 2 Section 1.2. But now we need the ducks to FLY............................................................................................................................. 3 Section 1.3. But something went horribly wrong........................................................................................................................... 4 Section 1.4. Joe thinks about inheritance....................................................................................................................................... 5 Section 1.5. Sharpen your pencil..................................................................................................................................................... 5 Section 1.6. How about an interface?............................................................................................................................................. 6 Section 1.7. What would you do if you were Joe?........................................................................................................................... 7 Section 1.8. The one constant in software development................................................................................................................ 8 Section 1.9. Sharpen your pencil.................................................................................................................................................... 8 Section 1.10. Zeroing in on the problem......................................................................................................................................... 9 Section 1.11. Separating what changes from what stays the same............................................................................................... 10 Section 1.12. Designing the Duck Behaviors.................................................................................................................................. 11 Section 1.13. Implementing the Duck Behaviors.......................................................................................................................... 13 Section 1.14. There are no Dumb Questions................................................................................................................................. 14 Section 1.15. Sharpen your pencil.................................................................................................................................................. 14 Section 1.16. Integrating the Duck Behavior................................................................................................................................. 15 Section 1.17. More Integration...................................................................................................................................................... 16 Section 1.18. Testing the Duck code.............................................................................................................................................. 18 Section 1.19. Setting behavior dynamically.................................................................................................................................. 20 Section 1.20. The Big Picture on encapsulated behaviors............................................................................................................ 22 Section 1.21. HAS-A can be better than IS-A................................................................................................................................ 23 Section 1.22. Speaking of Design Patterns................................................................................................................................... 24 Section 1.23. Design Puzzle........................................................................................................................................................... 25 Section 1.24. Overheard at the local diner.................................................................................................................................... 26 Section 1.25. Overheard in the next cubicle.................................................................................................................................. 27 Section 1.26. The power of a shared pattern vocabulary............................................................................................................. 28 Section 1.27. How do I use Design Patterns?............................................................................................................................... 29 Section 1.28. There are no Dumb Questions................................................................................................................................ 29 Section 1.29. Skeptical Developer-Friendly Patterns Guru......................................................................................................... 30 Section 1.30. Tools for your Design Toolbox................................................................................................................................ 32 Section 1.31. Design Puzzle Solution............................................................................................................................................ 34 Section 1.32. Solutions.................................................................................................................................................................. 35

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Page 1

Return to Table of Contents

Intro to Design Patterns

:HOFRPHWR 5 4 4 'HVLJQ3DWWHUQV 1RZWKDWZH·UHOLYLQJ LQ2EMHFWYLOOHZH·YHMXVWJRW WRJHWLQWR'HVLJQ3DWWHUQV HYHU\RQHLVGRLQJWKHP6RRQ ZH·OOEHWKHKLWRI-LPDQG %HWW\·V:HGQHVGD\QLJKW SDWWHUQVJURXS

6RPHRQHKDVDOUHDG\VROYHG\RXUSUREOHPV,QWKLVFKDSWHU\RX¶OOOHDUQ ZK\ DQGKRZ \RXFDQH[SORLWWKHZLVGRPDQGOHVVRQVOHDUQHGE\RWKHUGHYHORSHUVZKR¶YH EHHQGRZQWKHVDPHGHVLJQSUREOHPURDGDQGVXUYLYHGWKHWULS%HIRUHZH¶UHGRQHZH¶OO ORRNDWWKHXVHDQGEHQH¿WVRIGHVLJQSDWWHUQVORRNDWVRPHNH\22GHVLJQSULQFLSOHVDQG ZDONWKURXJKDQH[DPSOHRIKRZRQHSDWWHUQZRUNV7KHEHVWZD\WRXVHSDWWHUQVLVWRORDG \RXUEUDLQZLWKWKHPDQGWKHQUHFRJQL]HSODFHVLQ\RXUGHVLJQVDQGH[LVWLQJDSSOLFDWLRQV ZKHUH\RXFDQDSSO\WKHP,QVWHDGRIFRGHUHXVHZLWKSDWWHUQV\RXJHWH[SHULHQFHUHXVH

WKLVLVDQHZFKDSWHU



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 2

6LP8'XFN

1\[\IZ\ML_Q\PI[QUXTM;QU=,]KSIXX 2WM_WZS[NWZIKWUXIVa\PI\UISM[IPQOPTa[]KKM[[N]TL]KSXWVL [QU]TI\QWVOIUM;QU=,]KS
'XFN

$OOGXFNVTXDFNDQGVZLPWKH VXSHUFODVVWDNHVFDUHRIWKH LPSOHPHQWDWLRQFRGH

TXDFN VZLP GLVSOD\ 27+(5GXFNOLNHPHWKRGV

\SH VXEW RU N F X G (DFK SRQVLEOHI WVRZQ LVUHV PHQWLQJL YLRU LPSOH D\ EHKD NVRQ GLVSO RZLWORR IRUK FUHHQ WKHV

0DOODUG'XFN

5HGKHDG'XFN

GLVSOD\ ^

GLVSOD\ ^

ORRNVOLNHDPDOODUG`

ORRNVOLNHDUHGKHDG`

LV 7KHGLVSOD\ PHWKRG  XFN OG DO QFH DEVWUDFWVL W UHQ IH GLI RN OR SHV W\ VXE

IGXFNV KHUW\SHVR /RWVRIRW PWKH'XFNFODVV LQKHULWIUR

1V\PMTI[\aMIZ\PMKWUXIVaPI[JMMV]VLMZQVKZMI[QVOXZM[[]ZM NZWUKWUXM\Q\WZ[)N\MZI_MMSTWVOWNN[Q\MJZIQV[\WZUQVO [M[[QWVW^MZOWTN\PMKWUXIVaM`MK]\Q^M[\PQVSQ\¼[\QUMNWZIJQO QVVW^I\QWV


&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 3

LQWURWR'HVLJQ3DWWHUQV

*]\VW__MVMML\PML]KS[\W.4A
,MXVWQHHGWRDGGDÁ\  PHWKRGLQWKH'XFNFODVVDQG WKHQDOOWKHGXFNVZLOOLQKHULWLW 1RZ·VP\WLPHWRUHDOO\VKRZP\ WUXH22JHQLXV

-RH

:KDWZHZDQW 'XFN TXDFN VZLP GLVSOD\

VHV EFODV $OOVX LWIO\  LQKHU

0DOODUG'XFN

IO\

GHG

:KDW-RHDG

27+(5GXFNOLNHPHWKRGV

5HGKHDG'XFN

GLVSOD\ ^

GLVSOD\ ^

ORRNVOLNHDPDOODUG`

ORRNVOLNHDUHGKHDG`

W\SHV

2WKHU'XFN

\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 4

VRPHWKLQJZHQWZURQJ

*]\[WUM\PQVO_MV\PWZZQJTa_ZWVO -RH,·PDWWKH VKDUHKROGHU·VPHHWLQJ 7KH\MXVWJDYHDGHPRDQGWKHUH ZHUHUXEEHUGXFNLHVÁ\LQJDURXQG WKHVFUHHQ:DVWKLV\RXULGHDRI DMRNH"
:KDWKDSSHQHG" 2WMNIQTML\WVW\QKM\PI\VW\ITT []JKTI[[M[WN ,]KS[PW]TLÆa?PMV 2WMILLMLVM_JMPI^QWZ\W\PM ,]KS[]XMZKTI[[PM_I[IT[WILLQVO JMPI^QWZ\PI\_I[VW\IXXZWXZQI\M NWZ[WUM,]KS[]JKTI[[M[0MVW_ PI[ÆaQVOQVIVQUI\MWJRMK\[QV\PM ;QU=,]KSXZWOZIU

2.VRWKHUH·VDVOLJKW ÁDZLQP\GHVLJQ, GRQ·WVHHZK\WKH\FDQ·W MXVWFDOOLWD´IHDWXUHµ ,W·VNLQGRIFXWH

:KDWKHWKRXJKW ZDVDJUHDWXVH RILQKHULWDQFH IRUWKHSXUSRVH RIUHXVHKDVQ·W WXUQHGRXWVRZHOO ZKHQLWFRPHVWR PDLQWHQDQFH

)TWKITQbML]XLI\M\W\PMKWLMKI][MLIVWV TWKIT[QLMMNNMK\ÆaQVOZ]JJMZL]KS[ 'XFN

WKH \ LQ IO\LQJ O I  J JDYH  XWWLQ %\S UFODVVKH /GXFNV / H VXS \WR$ VHWKDW DELOLW GLQJWKR LQFOX OGQ·W VKRX



TXDFN VZLP GLVSOD\

IO\ 27+(5GXFNOLNHPHWKRGV

0DOODUG'XFN

5HGKHDG'XFN

5XEEHU'XFN

GLVSOD\ ^ ORRNVOLNHDPDOODUG `

GLVSOD\ ^ ORRNVOLNHDUHGKHDG `

TXDFN ^ RYHUULGGHQWR6TXHDN ` GLVSOD\ ^ ORRNVOLNHDUXEEHUGXFN `

GRQ·WTXDFN 5XEEHUGXFNVRYHUUULGGHQ VRTXDFN LV WR´6TXHDNµ

&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 5

LQWURWR'HVLJQ3DWWHUQV

2WM\PQVS[IJW]\QVPMZQ\IVKM ,FRXOGDOZD\VMXVW RYHUULGHWKHÁ\ PHWKRGLQ UXEEHUGXFNWKHZD\,DPZLWK WKHTXDFN PHWKRG

%XWWKHQZKDWKDSSHQVZKHQ ZHDGGZRRGHQGHFR\GXFNV WRWKHSURJUDP"7KH\DUHQ·W VXSSRVHGWRÁ\RUTXDFN

5XEEHU'XFN TXDFN ^VTXHDN` GLVSOD\ ^UXEEHUGXFN`

IO\ ^ RYHUULGHWRGRQRWKLQJ `

'HFR\'XFN

TXDFN ^ RYHUULGHWRGRQRWKLQJ ` GLVSOD\ ^GHFR\GXFN`

LQWKH WKHUFODVV +HUH·VDQR QRWLFHWKDWOLNH KLHUDUFK\ NLWGRHVQ·WIO\ 5XEEHU'XFRGRHVQ·WTXDFN EXWLWDOV

IO\ ^ RYHUULGHWRGRQRWKLQJ `

6KDUSHQ \RXU SHQFLO ?PQKPWN \PMNWTTW_QVOIZMLQ[IL^IV\IOM[WN ][QVOQVPMZQ\IVKM\W XZW^QLM,]KSJMPI^QWZ'+PWW[MITT\PI\IXXTa

R) +WLMQ[L]XTQKI\MLIKZW[[[]JKTI[[M[ R* :]V\QUMJMPI^QWZKPIVOM[IZMLQNÅK]T\ R+ ?MKIV¼\UISML]KS[LIVKM

R, 0IZL\WOIQVSVW_TMLOMWN ITTL]KSJMPI^QWZ[ R- ,]KS[KIV¼\ÆaIVLY]IKSI\\PM[IUM\QUM R. +PIVOM[KIV]VQV\MV\QWVITTaINNMK\W\PMZL]KS[

\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 6

LQKHULWDQFHLVQRWWKHDQVZHU

0W_IJW]\IVQV\MZNIKM' 2WMZMITQbML\PI\QVPMZQ\IVKMXZWJIJTa_I[V¼\\PM IV[_MZJMKI][MPMR][\OW\IUMUW\PI\[Ia[\PI\ \PMM`MK]\Q^M[VW__IV\\W]XLI\M\PMXZWL]K\M^MZa [Q`UWV\P[QV_Ia[\PMaPI^MV¼\aM\LMKQLMLWV2WM SVW_[\PM[XMK_QTTSMMXKPIVOQVOIVLPM¼TTJMNWZKML \WTWWSI\IVLXW[[QJTaW^MZZQLMÆaIVLY]IKSNWZ M^MZaVM_,]KS[]JKTI[[\PI\¼[M^MZILLML\W\PM XZWOZIUNWZM^MZ

,FRXOGWDNHWKHÁ\ RXWRIWKH 'XFNVXSHUFODVVDQGPDNHD )O\DEOH LQWHUIDFHZLWKDÁ\  PHWKRG7KDWZD\RQO\WKHGXFNVWKDW DUHVXSSRVHGWRÁ\ZLOOLPSOHPHQWWKDW LQWHUIDFHDQGKDYHDÁ\ PHWKRGDQG ,PLJKWDVZHOOPDNHD4XDFNDEOHWRR VLQFHQRWDOOGXFNVFDQTXDFN

;WPMVMML[IKTMIVMZ_Ia\WPI^MWVTa[WUMJ]\VW\ ITTWN \PML]KS\aXM[ÆaWZY]IKS

'XFN VZLP GLVSOD\

4XDFNDEOH

)O\DEOH

TXDFN

IO\

0DOODUG'XFN GLVSOD\  IO\ TXDFN

27+(5GXFNOLNHPHWKRGV

5HGKHDG'XFN GLVSOD\ IO\ TXDFN

5XEEHU'XFN GLVSOD\ TXDFN

'HFR\'XFN GLVSOD\

:KDWGR<28WKLQNDERXWWKLVGHVLJQ"



&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Page 7

Return to Table of Contents

LQWURWR'HVLJQ3DWWHUQV

7KDWLVOLNHWKHGXPEHVWLGHD \RX·YHFRPHXSZLWK&DQ\RXVD\ ´GXSOLFDWHFRGHµ",I\RXWKRXJKW KDYLQJWRRYHUULGHDIHZPHWKRGVZDVEDG KRZDUH\RXJRQQDIHHOZKHQ\RXQHHG WRPDNHDOLWWOHFKDQJHWRWKHÁ\LQJ EHKDYLRULQDOORIWKHÁ\LQJ 'XFNVXEFODVVHV"

?PI\_W]TLaW]LWQNaW]_MZM2WM' ?MSVW_\PI\VW\ITTWN \PM[]JKTI[[M[[PW]TLPI^MÆaQVOWZY]IKSQVO JMPI^QWZ[WQVPMZQ\IVKMQ[V¼\\PMZQOP\IV[_MZ*]\_PQTMPI^QVO \PM[]JKTI[[M[QUXTMUMV\.TaIJTMIVLWZ9]IKSIJTM[WT^M[XIZ\WN  \PMXZWJTMUVWQVIXXZWXZQI\MTaÆaQVOZ]JJMZL]KS[Q\KWUXTM\MTa LM[\ZWa[KWLMZM][MNWZ\PW[MJMPI^QWZ[[WQ\R][\KZMI\M[ILQNNMZMV\ UIQV\MVIVKMVQOP\UIZM)VLWN KW]Z[M\PMZMUQOP\JMUWZM\PIV WVMSQVLWN ÆaQVOJMPI^QWZM^MVIUWVO\PML]KS[\PI\LWÆa )\\PQ[XWQV\aW]UQOP\JM_IQ\QVONWZI,M[QOV8I\\MZV\WKWUM ZQLQVOQVWVI_PQ\MPWZ[MIVL[I^M\PMLIa*]\_PI\N]V_W]TL\PI\ JM'6W_M¼ZMOWQVO\WÅO]ZMW]\I[WT]\QWV\PMWTLNI[PQWVML_Ia¸ JaIXXTaQVOOWWL77[WN\_IZMLM[QOVXZQVKQXTM[

:RXOGQ·WLWEHGUHDP\LI RQO\WKHUHZHUHDZD\WREXLOG VRIWZDUHVRWKDWZKHQZHQHHGWR FKDQJHLWZHFRXOGGRVRZLWKWKHOHDVW SRVVLEOHLPSDFWRQWKHH[LVWLQJFRGH" :HFRXOGVSHQGOHVVWLPHUHZRUNLQJ FRGHDQGPRUHPDNLQJWKHSURJUDP GRFRROHUWKLQJV

\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Page 8

Return to Table of Contents

FKDQJHLVFRQVWDQW


(*1$+& ][MIUQZZWZ\W[MM\PMIV[_MZ 6WUI\\MZPW__MTTaW]LM[QOVIVIXXTQKI\QWVW^MZ\QUMIV IXXTQKI\QWVU][\OZW_IVLKPIVOMWZQ\_QTTLQM

6KDUSHQ \RXU SHQFLO /RWVRIWKLQJVFDQGULYHFKDQJH/LVWVRPHUHDVRQV \RX·YHKDGWRFKDQJHFRGHLQ\RXUDSSOLFDWLRQV ZHSXW LQDFRXSOHRIRXURZQWRJHW\RXVWDUWHG 

0\FXVWRPHUVRUXVHUVGHFLGHWKH\ZDQWVRPHWKLQJHOVHRUWKH\ZDQWQHZIXQFWLRQDOLW\ 0\FRPSDQ\GHFLGHGLWLVJRLQJZLWKDQRWKHUGDWDEDVHYHQGRUDQGLWLVDOVRSXUFKDVLQJ LWVGDWDIURPDQRWKHUVXSSOLHUWKDWXVHVDGLIIHUHQWGDWDIRUPDW$UJK



&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Page 9

Return to Table of Contents

LQWURWR'HVLJQ3DWWHUQV

BMZWQVOQVWV\PMXZWJTMU ;W_MSVW_][QVOQVPMZQ\IVKMPI[V¼\_WZSMLW]\^MZa_MTT[QVKM \PML]KSJMPI^QWZSMMX[KPIVOQVOIKZW[[\PM[]JKTI[[M[IVLQ\¼[ VW\IXXZWXZQI\MNWZITT[]JKTI[[M[\WPI^M\PW[MJMPI^QWZ[
2XUILUVWRIPDQ\GHVLJQ SULQFLSOHV:H·OOVSHQGPRUHWLPH RQWKHVHWKUXRJKRXWWKHERRN 1VW\PMZ_WZL[QN aW]¼^MOW\[WUMI[XMK\WN aW]ZKWLM\PI\Q[ KPIVOQVO[Ia_Q\PM^MZaVM_ZMY]QZMUMV\\PMVaW]SVW_aW]¼^M OW\IJMPI^QWZ\PI\VMML[\WJMX]TTMLW]\IVL[MXIZI\MLNZWUITT \PM[\]NN \PI\LWM[V¼\KPIVOM

7DNHZKDWYDULHVDQG ´HQFDSVXODWHµLWVRLWZRQ·W DIIHFWWKHUHVWRI\RXUFRGH 7KHUHVXOW")HZHU XQLQWHQGHGFRQVHTXHQFHV IURPFRGHFKDQJHVDQGPRUH IOH[LELOLW\LQ\RXUV\VWHPV

0MZM¼[IVW\PMZ_Ia\W\PQVSIJW]\\PQ[XZQVKQXTM"\ISM\PMXIZ\[ \PI\^IZaIVLMVKIX[]TI\M\PMU[W\PI\TI\MZaW]KIV IT\MZWZM`\MVL\PMXIZ\[\PI\^IZa_Q\PW]\INNMK\QVO \PW[M\PI\LWV¼\ )[[QUXTMI[\PQ[KWVKMX\Q[Q\NWZU[\PMJI[Q[NWZITUW[\M^MZa LM[QOVXI\\MZV)TTXI\\MZV[XZW^QLMI_Ia\WTM\[WUMXIZ\WN I[a[\MU ^IZaQVLMXMVLMV\TaWN ITTW\PMZXIZ\[ 7SIa\QUM\WX]TT\PML]KSJMPI^QWZW]\WN \PM,]KSKTI[[M[

\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 10

SXOORXWZKDWYDULHV

;MXIZI\QVO_PI\KPIVOM[NZWU_PI\[\Ia[\PM[IUM ?PMZMLW_M[\IZ\')[NIZI[_MKIV\MTTW\PMZ\PIV\PMXZWJTMU[_Q\PÆaIVLY]IKS\PM,]KS KTI[[Q[_WZSQVO_MTTIVL\PMZMIZMVWW\PMZXIZ\[WN Q\\PI\IXXMIZ\W^IZaWZKPIVOMNZMY]MV\Ta ;WW\PMZ\PIVINM_[TQOP\KPIVOM[_M¼ZMOWQVO\WXZM\\aU]KPTMI^M\PM,]KSKTI[[ITWVM 6W_\W[MXIZI\M\PM¹XIZ\[\PI\KPIVOMNZWU\PW[M\PI\[\Ia\PM[IUMº_MIZMOWQVO\WKZMI\M\_W [M\[WN KTI[[M[\W\ITTaIXIZ\NZWU,]KSWVMNWZÆaIVLWVMNWZY]IKS-IKP[M\WN KTI[[M[_QTTPWTL ITT\PMQUXTMUMV\I\QWV[WN \PMQZZM[XMK\Q^MJMPI^QWZ.WZQV[\IVKM_MUQOP\PI^MWVMKTI[[\PI\ QUXTMUMV\[Y]IKSQVOIVW\PMZ\PI\QUXTMUMV\[[Y]MISQVOIVLIVW\PMZ\PI\QUXTMUMV\[[QTMVKM

:HNQRZWKDWÁ\ DQGTXDFN DUHWKHSDUWVRIWKH 'XFNFODVVWKDWYDU\DFURVVGXFNV 7RVHSDUDWHWKHVHEHKDYLRUVIURPWKH'XFNFODVVZH·OO SXOOERWKPHWKRGVRXWRIWKH'XFNFODVVDQGFUHDWHD QHZVHWRIFODVVHVWRUHSUHVHQWHDFKEHKDYLRU

KHVXSHUFODVV 7KH'XFNFODVVLVVWLOOW SXOOLQJRXW UH HD RIDOOGXFNVEXWZ DYLRUVDQG EHK FN TXD QG \D IO WKH WKHUFODVV SXWWLQJWKHPLQWRDQR VWUXFWXUH

HDFKJHW 1RZIO\LQJDQGTXDFNLQJ WKHLURZQVHWRIFODVVHV

9DULRXVEHKDYLRU J LPSOHPHQWDWLRQVDUHJRLQ UH KH OLYH WR

3XOORXWZKDWYDULHV

' XF

NFODVV

)O\

U LQJ%HKDYLR

V 4X

RU DFNLQJ %HKDYL 

V

'XFN%HKDYLRUV



&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 11

LQWURWR'HVLJQ3DWWHUQV

,M[QOVQVO\PM,]KS*MPI^QWZ[ ;WPW_IZM_MOWQVO\WLM[QOV\PM[M\WN KTI[[M[\PI\ QUXTMUMV\\PMÆaIVLY]IKSJMPI^QWZ[' ?M¼LTQSM\WSMMX\PQVO[ÆM`QJTM#IN\MZITTQ\_I[\PMQVÆM`QJQTQ\aQV \PML]KSJMPI^QWZ[\PI\OW\][QV\W\ZW]JTMQV\PMÅZ[\XTIKM)VL_M SVW_\PI\_M_IV\\WI[[QOVJMPI^QWZ[\W\PMQV[\IVKM[WN ,]KS.WZ M`IUXTM_MUQOP\_IV\\WQV[\IV\QI\MIVM_5ITTIZL,]KSQV[\IVKM IVLQVQ\QITQbMQ\_Q\PI[XMKQÅK\aXMWN ÆaQVOJMPI^QWZ)VL_PQTM _M¼ZM\PMZM_PaVW\UISM[]ZM\PI\_MKIVKPIVOM\PMJMPI^QWZWN  IL]KSLaVIUQKITTa'1VW\PMZ_WZL[_M[PW]TLQVKT]LMJMPI^QWZ [M\\MZUM\PWL[QV\PM,]KSKTI[[M[[W\PI\_MKIV[IaKPIVOM\PM 5ITTIZL,]KS¼[ÆaQVOJMPI^QWZI\Z]V\QUM /Q^MV\PM[MOWIT[TM\¼[TWWSI\W]Z[MKWVLLM[QOVXZQVKQXTM"

'HVLJQ3ULQFLSOH 3URJUDPWRDQLQWHUIDFHQRWDQ LPSOHPHQWDWLRQ

?M¼TT][MIVQV\MZNIKM\WZMXZM[MV\MIKPJMPI^QWZ·NWZQV[\IVKM .Ta*MPI^QWZIVL9]IKS*MPI^QWZ·IVLMIKPQUXTMUMV\I\QWVWN I JMPI^QWZ_QTTQUXTMUMV\WVMWN \PW[MQV\MZNIKM[ ;W\PQ[\QUMQ\_WV¼\JM\PM,]KSKTI[[M[\PI\_QTTQUXTMUMV\\PM ÆaQVOIVLY]IKSQVOQV\MZNIKM[1V[\MIL_M¼TTUISMI[M\WN KTI[[M[ _PW[MMV\QZMZMI[WVNWZTQ^QVOQ[\WZMXZM[MV\IJMPI^QWZNWZM`IUXTM ¹[Y]MISQVOºIVLQ\¼[\PMJMPI^QWZKTI[[ZI\PMZ\PIV\PM,]KSKTI[[ \PI\_QTTQUXTMUMV\\PMJMPI^QWZQV\MZNIKM

)URPQRZRQWKH'XFN EHKDYLRUVZLOOOLYHLQD VHSDUDWHFODVV³DFODVVWKDW LPSOHPHQWVDSDUWLFXODU EHKDYLRULQWHUIDFH 7KDWZD\WKH'XFNFODVVHV ZRQ·WQHHGWRNQRZDQ\RI WKHLPSOHPHQWDWLRQGHWDLOV IRUWKHLURZQEHKDYLRUV


LQWHUIDFH!!

)O\%HKDYLRU IO\

)O\:LWK:LQJV

)O\1R:D\

IO\ ^

IO\ ^

LPSOHPHQWVGXFNIO\LQJ

GRQRWKLQJFDQ·WIO\

`

`

\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 12

SURJUDPWRDQLQWHUIDFH

,GRQ·WVHHZK\\RXKDYHWR XVHDQLQWHUIDFHIRU)O\%HKDYLRU
´3URJUDPWRDQLQWHUIDFHµUHDOO\PHDQV ´3URJUDPWRDVXSHUW\SHµ
DEVWUD EHDQDFEWVXSHUW\SH FR LQWHUIDF VWUDFWFODVV25XOG H 

'RJG QHZ'RJ  GEDUN 

$QLPDO

*]\XZWOZIUUQVO\WIVQV\MZNIKM[]XMZ\aXM_W]TLJM"

PDNH6RXQG

:HNQRZLW·V

$QLPDODQLPDO QHZ'RJ  ZHFDQQRZX D'RJEXW VHWKHDQLPDO DQLPDOPDNH6RXQG  UHIHUHQFHSRO\PRUSKLFDOO\

FRQFUHWH RQV LPSOHPHQWDWL

-^MVJM\\MZZI\PMZ\PIVPIZLKWLQVO\PMQV[\IV\QI\QWVWN \PM []J\aXMTQSMVM_,WOQV\W\PMKWLMI[[QOV\PMKWVKZM\M QUXTMUMV\I\QWVWJRMK\I\Z]V\QUM" &DW

'RJ PDNH6RXQG ^ EDUN  ` EDUN ^EDUNVRXQG`



'HFODULQJWKHYDULDEOH´Gµ \SH'RJ  DFRQFUHWHLPSOHPHQWDWLRQDVW RI$QLPDO  IRUFHVXVWRFRGHWRDFRQFUH WH LPSOHPHQWDWLRQ

PDNH6RXQG ^ PHRZ  ` PHRZ ^PHRZVRXQG`

:HGRQ·WNQRZ:+$7WKHDFWXDO D JHW$QLPDO  DQLPDOPDNH6RXQG  DQLPDOVXEW\SHLVDOOZHFDUHDERXW LVWKDWLWNQRZVKRZWRUHVSRQGWR PDNH6RXQG 

&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 13

LQWURWR'HVLJQ3DWWHUQV

1UXTMUMV\QVO\PM,]KS*MPI^QWZ[ 0MZM_MPI^M\PM\_WQV\MZNIKM[.Ta*MPI^QWZIVL9]IKS*MPI^QWZITWVO_Q\P \PMKWZZM[XWVLQVOKTI[[M[\PI\QUXTMUMV\MIKPKWVKZM\MJMPI^QWZ"

RUWKHTXDFN 6DPHWKLQJKHUHI DQLQWHUIDFH H EHKDYLRUZHKDY TXDFN  WKDWMXVWLQFOXGHVDVWREHLPSOH PHWKRGWKDWQHHG PHQWHG

WKDW HUIDFH$OO W LQ  Q D OHPHQW HKDYH +HUHZ JFODVVHVLPS VWQHHGWR LQ X M O\  V DOOI LQJFODVVH HWKRG QHZIO\ QWWKHIO\P LPSOHPH

LQWHUIDFH!!

LQWHUIDFH!!

4XDFN%HKDYLRU

)O\%HKDYLRU

TXDFN

IO\

)O\:LWK:LQJV

)O\1R:D\

4XDFN

6TXHDN

0XWH4XDFN

IO\ ^

IO\ ^

TXDFN ^

TXDFN ^

TXDFN ^

LPSOHPHQWVGXFNIO\LQJ

GRQRWKLQJFDQ·WIO\

LPSOHPHQWVGXFNTXDFNLQJ

UXEEHUGXFNLHVTXHDN

GRQRWKLQJFDQ·WTXDFN

`

`

`

`

`

4XDF UHDOO\ NVWKDW $QGKHUH TXDF DOOGXFNV ·VWKHLPSOHPHQWD N +HUH·VW WLRQRI WKDWFDQ·W K H  IO\ IO\LQJI LPSOHPHQW ZLQJV RUDOOGXFNVW DWLRQRI KDWKDY H

4XDFNVWKDWVTXHDN

:LWKWKLVGHVLJQRWKHUW\SHVRIREMHFWVFDQ U HXVHRXUÁ\DQGTXDFNEHKDYLRUVEHFDXVH WKHVHEHKDYLRUVDUHQRORQJHUKLGGHQDZD\ LQRXU'XFNFODVVHV

4XDFNVWKDWPDNH QRVRXQGDWDOO

 QHILWRI WWKHEH DOOWKHEDJ H J  H Z  R  6 ZLWKRXW ZLWK 5(86(DWFRPHVDORQJ K W JDJH QFH LQKHULWD

$QGZHFDQDGGQHZEHKDYLRUVZLWKRXW PRGLI\LQJDQ\RIRXUH[LVWLQJEHKDYLRU FODVVHVRUWRXFKLQJDQ\RIWKH'XFNFODVVHV WKDWXVHÁ\LQJEHKDYLRUV

\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 14

EHKDYLRULQDFODVV

WKHUH DUH QR

'XPE 4XHVWLRQV

Q:

Q:

'R,DOZD\VKDYHWRLPSOHPHQWP\DSSOLFDWLRQILUVWVHH ZKHUHWKLQJVDUHFKDQJLQJDQGWKHQJREDFNDQGVHSDUDWH  HQFDSVXODWHWKRVHWKLQJV"

,WIHHOVDOLWWOHZHLUGWRKDYHDFODVVWKDW·VMXVWD EHKDYLRU$UHQ·WFODVVHVVXSSRVHGWRUHSUHVHQWWKLQJV"$UHQ·W FODVVHVVXSSRVHGWRKDYHERWKVWDWH$1'EHKDYLRU"

A:

A:

1RWDOZD\VRIWHQZKHQ\RXDUHGHVLJQLQJDQDSSOLFDWLRQ \RXDQWLFLSDWHWKRVHDUHDVWKDWDUHJRLQJWRYDU\DQGWKHQJRDKHDG DQGEXLOGWKHIOH[LELOLW\WRGHDOZLWKLWLQWR\RXUFRGH
Q: A:

,QDQ22V\VWHP\HVFODVVHVUHSUHVHQWWKLQJVWKDW JHQHUDOO\KDYHERWKVWDWH LQVWDQFHYDULDEOHV DQGPHWKRGV$QGLQ WKLVFDVHWKHWKLQJKDSSHQVWREHDEHKDYLRU%XWHYHQDEHKDYLRU FDQVWLOOKDYHVWDWHDQGPHWKRGVDIO\LQJEHKDYLRUPLJKWKDYH LQVWDQFHYDULDEOHVUHSUHVHQWLQJWKHDWWULEXWHVIRUWKHIO\LQJ ZLQJ EHDWVSHUPLQXWHPD[DOWLWXGHDQGVSHHGHWF EHKDYLRU

6KRXOGZHPDNH'XFNDQLQWHUIDFHWRR"

1RWLQWKLVFDVH$V\RX·OOVHHRQFHZH·YHJRWHYHU\WKLQJ KRRNHGWRJHWKHUZHGREHQHILWE\KDYLQJ'XFNEHDFRQFUHWH FODVVDQGKDYLQJVSHFLILFGXFNVOLNH0DOODUG'XFNLQKHULWFRPPRQ SURSHUWLHVDQGPHWKRGV1RZWKDWZH·YHUHPRYHGZKDWYDULHVIURP WKH'XFNLQKHULWDQFHZHJHWWKHEHQHILWVRIWKLVVWUXFWXUHZLWKRXW WKHSUREOHPV

6KDUSHQ \RXU SHQFLO 8VLQJRXUQHZGHVLJQZKDWZRXOG\RXGRLI\RXQHHGHGWR DGGURFNHWSRZHUHGÀ\LQJWRWKH6LP8'XFNDSS"



&DQ\RXWKLQNRIDFODVVWKDWPLJKWZDQWWRXVHWKH4XDFN EHKDYLRUWKDWLVQ¶WDGXFN"

7VMM`IUXTMIL]KSKITTI LM^QKM\PI\UISM[L]KS[W]VL[



+ZMI\MI.Ta:WKSM\8W_MZMLKTI[[ \PI\QUXTMUMV\[\PM.Ta*MPI^QWZ QV\MZNIKM )V[_MZ["



&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 15

LQWURWR'HVLJQ3DWWHUQV

1V\MOZI\QVO\PM,]KS*MPI^QWZ 7KHNH\LVWKDWD'XFNZLOOQRZGHOHJDWHLWVÁ\LQJ DQGTXDFNLQJEHKDYLRULQVWHDGRIXVLQJTXDFNLQJDQG Á\LQJPHWKRGVGHÀQHGLQWKH'XFNFODVV RUVXEFODVV  +HUH·VKRZ 

.QZ[\_M¼TTILL\_WQV[\IVKM^IZQIJTM[\W\PM,]KSKTI[[KITTMLÆa*MPI^QWZIVL Y]IKS*MPI^QWZ\PI\IZMLMKTIZMLI[\PMQV\MZNIKM\aXMVW\IKWVKZM\MKTI[[QUXTMUMV\I\QWV \aXM-IKPL]KSWJRMK\_QTT[M\\PM[M^IZQIJTM[XWTaUWZXPQKITTa\WZMNMZMVKM\PM[XMKQÅK JMPI^QWZ\aXMQ\_W]TLTQSMI\Z]V\QUM.Ta?Q\P?QVO[;Y]MISM\K ?M¼TTIT[WZMUW^M\PMÆaIVLY]IKSUM\PWL[NZWU\PM,]KSKTI[[IVLIVa[]JKTI[[M[ JMKI][M_M¼^MUW^ML\PQ[JMPI^QWZW]\QV\W\PM.Ta*MPI^QWZIVL9]IKS*MPI^QWZKTI[[M[ ?M¼TTZMXTIKMÆaIVLY]IKSQV\PM,]KSKTI[[_Q\P\_W[QUQTIZUM\PWL[KITTML XMZNWZU.TaIVLXMZNWZU9]IKS#aW]¼TT[MMPW_\PMa_WZSVM`\

7KHEHKDYLRUYDULDEOHVDUH GHFODUHGDVWKHEHKDYLRU ,17(5)$&(W\SH

 7KHVHPHWKRGVUHSODFH  DFN IO\ DQGTX



,QVWDQFHYDULDEOHVKROGDUHIHUHQFHWR DVSHFLILFEHKDYLRUDWUXQWLPH 'XFN )O\%HKDYLRUIO\%HKDYLRU 4XDFN%HKDYLRUTXDFN%HKDYLRU SHUIRUP4XDFN VZLP GLVSOD\ SHUIRUP)O\ 27+(5GXFNOLNHPHWKRGV

6W__MQUXTMUMV\XMZNWZU9]IKS" SXEOLFFODVV'XFN^ 4XDFN%HKDYLRUTXDFN%HKDYLRU PRUH SXEOLFYRLGSHUIRUP4XDFN ^ TXDFN%HKDYLRUTXDFN  ` `

)O\

UV LQJ%HKDYLR 4X

RU DFNLQJ %HKDYL 

V

'XFN%HKDYLRUV

W VRPHWKLQJWKD DUHIHUHQFHWR (DFK'XFNKDV4XDFN%HKDYLRULQWHUIDFH LPSOHPHQWVWKH NEHKDYLRU QGOLQJWKHTXDF HVWKDW KD Q KD W HU WK 5D NREMHFWGHOHJDW LWVHOIWKH'XF REMHFWUHIHUHQFHGE\ KH W WR EHKDYLRU TXDFN%HKDYLRU

8ZM\\a[QUXTMP]P'


Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 16

LQWHJUDWLQJGXFNEHKDYLRU

5WZM1V\MOZI\QWV 

7SIa\QUM\W_WZZaIJW]\PW_\PMÆa*MPI^QWZIVL Y]IKS*MPI^QWZQV[\IVKM^IZQIJTM[IZM[M\4M\¼[\ISMITWWSI\ \PM5ITTIZL,]KSKTI[[" SXEOLFFODVV0DOODUG'XFNH[WHQGV'XFN^ SXEOLF0DOODUG'XFN ^ TXDFN%HKDYLRU QHZ4XDFN  À\%HKDYLRU QHZ)O\:LWK:LQJV  `

5HPHPEHU0DOODUG'XFNLQKHULWVWKHTXDFN %HKDYLRUDQGIO\%HKDYLRULQVWDQFHYDULDEOHV IURPFODVV'XFN

FODVVWR XVHVWKH4XDFN $0DOODUG'XFNNVRZKHQSHUIRUP4XDFN KDQGOHLWVTXDF HVSRQVLELOLW\IRUWKH LVFDOOHGWKHU WHGWRWKH4XDFNREMHFW TXDFNLVGHOHJD HDOTXDFN DQGZHJHWDU LWV O\:LWK:LQJVDV $QGLWXVHV) SH )O\%HKDYLRUW\

SXEOLFYRLGGLVSOD\ ^  6\VWHPRXWSULQWOQ ³,¶PDUHDO0DOODUGGXFN´  ` `

;W5ITTIZL,]KS¼[Y]IKSQ[IZMITTQ^ML]KSY]IKSVW\I[Y]MISIVL VW\IU]\MY]IKS;W_PI\PIXXMV[PMZM'?PMVI5ITTIZL,]KS Q[QV[\IV\QI\MLQ\[KWV[\Z]K\WZQVQ\QITQbM[\PM5ITTIZL,]KS¼[QVPMZQ\ML Y]IKS*MPI^QWZQV[\IVKM^IZQIJTM\WIVM_QV[\IVKMWN \aXM9]IKSI 9]IKS*MPI^QWZKWVKZM\MQUXTMUMV\I\QWVKTI[[ )VL\PM[IUMQ[\Z]MNWZ\PML]KS¼[ÆaQVOJMPI^QWZ¸\PM5ITTIZL,]KS¼[ KWV[\Z]K\WZQVQ\QITQbM[\PMÆa*MPI^QWZQV[\IVKM^IZQIJTM_Q\PIVQV[\IVKM WN \aXM.Ta?Q\P?QVO[I.Ta*MPI^QWZKWVKZM\MQUXTMUMV\I\QWVKTI[[



&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Page 17

Return to Table of Contents

LQWURWR'HVLJQ3DWWHUQV

:DLWDVHFRQGGLGQ·W\RX VD\ZHVKRXOG127SURJUDPWRDQ LPSOHPHQWDWLRQ"%XWZKDWDUHZHGRLQJ LQWKDWFRQVWUXFWRU":H·UHPDNLQJD QHZLQVWDQFHRIDFRQFUHWH4XDFN LPSOHPHQWDWLRQFODVV

/WWLKI\KP\PI\¼[M`IK\Ta_PI\_M¼ZMLWQVO NWZVW_ 4I\MZQV\PMJWWS_M¼TTPI^MUWZMXI\\MZV[QV W]Z\WWTJW`\PI\KIVPMTX][Å`Q\ ;\QTTVW\QKM\PI\_PQTM_MIZM[M\\QVO\PM JMPI^QWZ[\WKWVKZM\MKTI[[M[JaQV[\IV\QI\QVO IJMPI^QWZKTI[[TQSM9]IKSWZ.Ta?Q\P?QVO[ IVLI[[QOVQVOQ\\WW]ZJMPI^QWZZMNMZMVKM ^IZQIJTM_MKW]TLMI[QTaKPIVOM\PI\I\ Z]V\QUM ;W_M[\QTTPI^MITW\WN ÆM`QJQTQ\aPMZM J]\_M¼ZMLWQVOIXWWZRWJWN QVQ\QITQbQVO \PMQV[\IVKM^IZQIJTM[QVIÆM`QJTM_Ia*]\ \PQVSIJW]\Q\[QVKM\PMY]IKS*MPI^QWZ QV[\IVKM^IZQIJTMQ[IVQV\MZNIKM\aXM_M KW]TL\PZW]OP\PMUIOQKWN XWTaUWZXPQ[U LaVIUQKITTaI[[QOVILQNNMZMV\9]IKS*MPI^QWZ QUXTMUMV\I\QWVKTI[[I\Z]V\QUM
\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 18

WHVWLQJGXFNEHKDYLRUV


0DOODUG'XFNFODVVIURPWZRSDJHVEDFN 0DOODUG'XFNMDYD  SXEOLFDEVWUDFWFODVV'XFN^ )O\%HKDYLRUÀ\%HKDYLRU 4XDFN%HKDYLRUTXDFN%HKDYLRU SXEOLF'XFN ^ `   SXEOLFDEVWUDFWYRLGGLVSOD\  SXEOLFYRLGSHUIRUP)O\ ^ À\%HKDYLRUÀ\  `

YDULDEOHV 'HFODUHWZRUHIHUHQFH W\SHV DFH HUI LQW RU DYL EHK IRUWKH WKHVDPH $OOGXFNVXEFODVVHV LQ SDFNDJH LQKHULWWKHVH

'HOHJDWHWRWKHEHKDYLRUFODVV

SXEOLFYRLGSHUIRUP4XDFN ^ TXDFN%HKDYLRUTXDFN  ` SXEOLFYRLGVZLP ^ 6\VWHPRXWSULQWOQ ³$OOGXFNVÀRDWHYHQGHFR\V´  ` `  7\SHDQGFRPSLOHWKH)O\%HKDYLRULQWHUIDFH )O\%HKDYLRUMDYD DQG

WKHWZREHKDYLRULPSOHPHQWDWLRQFODVVHV )O\:LWK:LQJVMDYDDQG )O\1R:D\MDYD  SXEOLFLQWHUIDFH)O\%HKDYLRU^ SXEOLFYRLGÀ\  `

7KHLQWHUIDFHWKDWDOOIO\LQJ EHKDYLRUFODVVHVLPSOHPHQW

SXEOLFFODVV)O\:LWK:LQJVLPSOHPHQWV)O\%HKDYLRU^ SXEOLFYRLGÀ\ ^ LPSOHPHQWDWLRQ )O\LQJEHKDYLRU'2IO\  6\VWHPRXWSULQWOQ ³,¶PÀ\LQJ´  IRUGXFNVWKDW ` `

SXEOLFFODVV)O\1R:D\LPSOHPHQWV)O\%HKDYLRU^ SXEOLFYRLGÀ\ ^ 6\VWHPRXWSULQWOQ ³,FDQ¶WÀ\´  ` `



)O\LQJEHKDYLRU IRUGXFNVWKDW LPSOHPHQWDWLRQ UXEEHUGXFNVDQGGR127IO\ OLNH GHFR\GXFNV 

&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 19

LQWURWR'HVLJQ3DWWHUQV


4XDFN%HKDYLRUMDYD DQGWKHWKUHHEHKDYLRULPSOHPHQWDWLRQ FODVVHV 4XDFNMDYD0XWH4XDFNMDYDDQG6THDNMDYD  SXEOLFLQWHUIDFH4XDFN%HKDYLRU^ SXEOLFYRLGTXDFN  ` SXEOLFFODVV4XDFNLPSOHPHQWV4XDFN%HKDYLRU^ SXEOLFYRLGTXDFN ^ 6\VWHPRXWSULQWOQ ³4XDFN´  ` ` SXEOLFFODVV0XWH4XDFNLPSOHPHQWV4XDFN%HKDYLRU^ SXEOLFYRLGTXDFN ^  6\VWHPRXWSULQWOQ ³6LOHQFH!!´  ` ` SXEOLFFODVV6TXHDNLPSOHPHQWV4XDFN%HKDYLRU^ SXEOLFYRLGTXDFN ^  6\VWHPRXWSULQWOQ ³6TXHDN´  ` `  7\SHDQGFRPSLOHWKHWHVWFODVV

0LQL'XFN6LPXODWRUMDYD  SXEOLFFODVV0LQL'XFN6LPXODWRU^ SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ 'XFNPDOODUG QHZ0DOODUG'XFN  XFN·VLQKHULWHG 7KLVFDOOVWKH0DOODUG' LFKWKHQGHOHJDWHVWR PDOODUGSHUIRUP4XDFN  ZK G WKR PH  KH SHUIRUP4XDFN PDOODUGSHUIRUP)O\  U LHFDOOVTXDFN RQW YLR HKD N% XDF 4 W·V ` MHF WKHRE  FH UHQ YLRUUHIH ` N·VLQKHULWHGTXDFN%HKD



5XQWKHFRGH )LOH(GLW:LQGRZ+HOS
GXF ·V KLQJZLWK0DOODUG'XFN 7KHQZHGRWKHVDPHW WKRG PH LQKHULWHGSHUIRUP)O\ 

MDYD0LQL'XFN6LPXODWRU 4XDFN ,¶PÀ\LQJ \RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 20

GXFNVZLWKG\QDPLFEHKDYLRU

;M\\QVOJMPI^QWZLaVIUQKITTa ?PI\I[PIUM\WPI^MITT\PQ[LaVIUQK\ITMV\J]QT\QV\WW]ZL]KS[IVLVW\JM][QVO Q\ 1UIOQVMaW]_IV\\W[M\\PML]KS¼[JMPI^QWZ\aXM\PZW]OPI[M\\MZUM\PWLWV\PM L]KS[]JKTI[[ZI\PMZ\PIVJaQV[\IV\QI\QVOQ\QV\PML]KS¼[KWV[\Z]K\WZ 

$GGWZRQHZPHWKRGVWRWKH'XFNFODVV SXEOLFYRLGVHW)O\%HKDYLRU )O\%HKDYLRUIE ^ À\%HKDYLRU IE `

'XFN )O\%HKDYLRUIO\%HKDYLRU

SXEOLFYRLGVHW4XDFN%HKDYLRU 4XDFN%HKDYLRUTE ^ TXDFN%HKDYLRU TE `

4XDFN%HKDYLRUTXDFN%HKDYLRU VZLP GLVSOD\ SHUIRUP4XDFN SHUIRUP)O\ VHW)O\%HKDYLRU VHW4XDFN%HKDYLRU

?MKIVKITT\PM[MUM\PWL[IVa\QUM_M_IV\\WKPIVOM\PM JMPI^QWZWN IL]KSWV\PMÆa

27+(5GXFNOLNHPHWKRGV

HGLWRUQRWHJUDWXLWRXVSXQ¿ [



0DNHDQHZ'XFNW\SH 0RGHO'XFNMDYD  SXEOLFFODVV0RGHO'XFNH[WHQGV'XFN^ SXEOLF0RGHO'XFN ^ À\%HKDYLRU QHZ)O\1R:D\   TXDFN%HKDYLRU QHZ4XDFN  `

HG

RXQG NEHJLQVOLIHJU 2XUPRGHOGXF IO\ WR ZLWKRXWDZD\

SXEOLFYRLGGLVSOD\ ^ 6\VWHPRXWSULQWOQ ³,¶PDPRGHOGXFN´  ` `



0DNHDQHZ)O\%HKDYLRUW\SH )O\5RFNHW3RZHUHGMDYD 

7KDW·VRND\ZH·UHFUHDWLQJD URFNHWSRZHUHGIO\LQJEHKDYLRU

SXEOLFFODVV)O\5RFNHW3RZHUHGLPSOHPHQWV)O\%HKDYLRU^ SXEOLFYRLGÀ\ ^ 6\VWHPRXWSULQWOQ ³,¶PÀ\LQJZLWKDURFNHW´  ` `



&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 21

LQWURWR'HVLJQ3DWWHUQV



&KDQJHWKHWHVWFODVV 0LQL'XFN6LPXODWRUMDYD DGGWKH 0RGHO'XFNDQGPDNHWKH0RGHO'XFNURFNHWHQDEOHG

SXEOLFFODVV0LQL'XFN6LPXODWRU^ SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ 'XFNPDOODUG QHZ0DOODUG'XFN  PDOODUGSHUIRUP4XDFN  PDOODUGSHUIRUP)O\  

GHOHJDWHV RSHUIRUP)O\ 7KHILUVWFDOOWYLRUREMHFWVHWLQWKH WRWKHIO\%HKD QVWUXFWRUZKLFKLVD 0RGHO'XFN·VFR FH )O\1R:D\LQVWDQ

'XFNPRGHO QHZ0RGHO'XFN  PRGHOSHUIRUP)O\  PRGHOVHW)O\%HKDYLRU QHZ)O\5RFNHW3RZHUHG  PRGHOSHUIRUP)O\  ` ` ,ILWZRUNHGWKHPR

GHOGXFNG\QDPLFDOO\ FKDQJHGLWVIO\LQJEHKDYLRU< 7+$7LIWKHLPSOHPHQWDWLR RXFDQ·WGR QOLYHVLQVLGHWKH GXFNFODVV



5XQLW

EHIRUH

7KLVLQYRNHVWKHPRGHO·VLQK HULW EHKDYLRUVHWWHUPHWKRGDQGY HG PRGHOVXGGHQO\KDVURFNHWS RLOD7KH RZHUHG IO\LQJFDSDELOLW\

)LOH(GLW:LQGRZ+HOS
MDYD0LQL'XFN6LPXODWRU 4XDFN ,¶PÀ\LQJ ,FDQ¶WÀ\

DIWHU

,¶PÀ\LQJZLWKDURFNHW

7RFKDQJHDGXFN·V EHKDYLRUDWUXQWLPHMXVW FDOOWKHGXFN·VVHWWHU PHWKRGIRUWKDWEHKDYLRU \RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 22

WKHELJSLFWXUH


&OLHQWPDNHVXVHRIDQ HQFDSVXODWHGIDPLO\RIDOJRULWKPV IRUERWKIO\LQJDQGTXDFNLQJ &OLHQW

(QFDSVXODWHGÀ\EHKDYLRU LQWHUIDFH!!

)O\%HKDYLRU

K RIHDF 7KLQN EHKDYLRUV VHWRI DPLO\RI DVDI KPV DOJRULW

IO\

'XFN )O\:LWK:LQJV

)O\%HKDYLRUIO\%HKDYLRU 4XDFN%HKDYLRUTXDFN%HKDYLRU VZLP GLVSOD\

)O\1R:D\

IO\ ^

IO\ ^

LPSOHPHQWVGXFNIO\LQJ

GRQRWKLQJFDQ·WIO\

`

`

SHUIRUP4XDFN SHUIRUP)O\

(QFDSVXODWHGTXDFNEHKDYLRU

VHW)O\%HKDYLRU VHW4XDFN%HKDYLRU

LQWHUIDFH!!

27+(5GXFNOLNHPHWKRGV

4XDFN%HKDYLRU TXDFN

0DOODUG'XFN

5HGKHDG'XFN

5XEEHU'XFN

'HFR\'XFN

GLVSOD\ ^

GLVSOD\ ^

GLVSOD\ ^

GLVSOD\ ^

ORRNVOLNHDPDOODUG`

ORRNVOLNHDUHGKHDG`

ORRNVOLNHDUXEEHUGXFN`

ORRNVOLNHDGHFR\GXFN`

4XDFN

6TXHDN

0XWH4XDFN

TXDFN ^

TXDFN ^

TXDFN ^

LPSOHPHQWVGXFNTXDFNLQJ

UXEEHUGXFNLHVTXHDN

GRQRWKLQJFDQ·WTXDFN

`

`

`

V DYLRU HEHK VµDUH V H K 7 ULWKP DEOH ´DOJR HUFKDQJH LQW 

&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Page 23

Return to Table of Contents

LQWURWR'HVLJQ3DWWHUQV

0);)KIVJMJM\\MZ\PIV1;)
0DVWHUDQG6WXGHQW 0DVWHU*UDVVKRSSHU WHOOPHZKDW\RXKDYH OHDUQHGRIWKH2EMHFW 2ULHQWHGZD\V 6WXGHQW0DVWHU,KDYHOHDUQHGWKDW WKHSURPLVHRIWKHREMHFWRULHQWHGZD\ LVUHXVH

'HVLJQ3ULQFLSOH

0DVWHU*UDVVKRSSHUFRQWLQXH

)DYRUFRPSRVLWLRQRYHULQKHULWDQFH

6WXGHQW0DVWHUWKURXJKLQKHULWDQFH DOOJRRGWKLQJVPD\EHUHXVHGDQG VRZHZLOOFRPHWRGUDVWLFDOO\FXW GHYHORSPHQWWLPHOLNHZHVZLIWO\FXW EDPERRLQWKHZRRGV

)[aW]¼^M[MMVKZMI\QVO[a[\MU[][QVOKWUXW[Q\QWVOQ^M[aW] ITW\UWZMÆM`QJQTQ\a6W\WVTaLWM[Q\TM\aW]MVKIX[]TI\M INIUQTaWN ITOWZQ\PU[QV\W\PMQZW_V[M\WN KTI[[M[J]\Q\ IT[WTM\[aW]KPIVOMJMPI^QWZI\Z]V\QUMI[TWVOI[ \PMWJRMK\aW]¼ZMKWUXW[QVO_Q\PQUXTMUMV\[\PMKWZZMK\ JMPI^QWZQV\MZNIKM +WUXW[Q\QWVQ[][MLQVUIVaLM[QOVXI\\MZV[IVLaW]¼TT [MMITW\UWZMIJW]\Q\[IL^IV\IOM[IVLLQ[IL^IV\IOM[ \PZW]OPW]\\PMJWWS

0DVWHU*UDVVKRSSHULVPRUH WLPHVSHQWRQFRGHEHIRUHRUDIWHU GHYHORSPHQWLVFRPSOHWH" 6WXGHQW7KHDQVZHULVDIWHU 0DVWHU:HDOZD\VVSHQGPRUHWLPH PDLQWDLQLQJDQGFKDQJLQJVRIWZDUH WKDQLQLWLDOGHYHORSPHQW 0DVWHU6R*UDVVKRSSHUVKRXOGHIIRUW JRLQWRUHXVHDERYHPDLQWDLQWDELOLW\ DQGH[WHQVLELOLW\" 6WXGHQW0DVWHU,EHOLHYHWKDWWKHUHLV WUXWKLQWKLV

YiX`e gfn\i $GXFNFDOOLVDGHYLFHWKDWKXQWHUVXVHWRPLPLFWKH FDOOV TXDFNV RIGXFNV+RZZRXOG\RXLPSOHPHQW\RXU RZQGXFNFDOOWKDWGRHVQRWLQKHULWIURPWKH'XFNFODVV"

0DVWHU,FDQVHHWKDW\RXVWLOOKDYH PXFKWROHDUQ,ZRXOGOLNHIRU\RXWR JRDQGPHGLWDWHRQLQKHULWDQFHIXUWKHU $V\RX¶YHVHHQLQKHULWDQFHKDVLWV SUREOHPVDQGWKHUHDUHRWKHUZD\VRI DFKLHYLQJUHXVH

\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Page 24

Return to Table of Contents

WKHVWUDWHJ\SDWWHUQ

;XMISQVOWN,M[QOV8I\\MZV[

&RQJUDWXODWLRQVRQ \RXUILUVWSDWWHUQ





\RX QZKHQ QG LR LW LQ D I +,6GH ULHQGV 8VH7 RLPSUHVVI XWLYHV F W QHHG FHNH\H[H LQIOXHQ

&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 25

LQWURWR'HVLJQ3DWWHUQV

'HVLJQ3X]]OH *MTW_aW]¼TTÅVLIUM[[WN KTI[[M[IVLQV\MZNIKM[NWZIVIK\QWVIL^MV\]ZMOIUMAW]¼TT ÅVLKTI[[M[NWZOIUMKPIZIK\MZ[ITWVO_Q\PKTI[[M[NWZ_MIXWVJMPI^QWZ[\PMKPIZIK\MZ[ KIV][MQV\PMOIUM-IKPKPIZIK\MZKIVUISM][MWN WVM_MIXWVI\I\QUMJ]\KIV KPIVOM_MIXWV[I\IVa\QUML]ZQVO\PMOIUMAW]ZRWJQ[\W[WZ\Q\ITTW]\ )V[_MZ[IZMI\\PMMVLWN \PMKPIX\MZ AW]Z\I[S"  )ZZIVOM\PMKTI[[M[  1LMV\QNaWVMIJ[\ZIK\KTI[[WVMQV\MZNIKMIVLMQOP\KTI[[M[  ,ZI_IZZW_[JM\_MMVKTI[[M[



I,ZI_\PQ[SQVLWN IZZW_NWZQVPMZQ\IVKM¹M`\MVL[º



J,ZI_\PQ[SQVLWN IZZW_NWZQV\MZNIKM¹QUXTMUMV\[º



K,ZI_\PQ[SQVLWN IZZW_NWZ¹0);)º

 8]\\PMUM\PWL[M\?MIXWVQV\W\PMZQOP\KTI[[

&KDUDFWHU :HDSRQ%HKDYLRUZHDSRQ ILJKW 

%RZ$QG$UURZ%HKDYLRU

.QLIH%HKDYLRU XVH:HDSRQ ^LPSOHPHQWVFXWWLQJ ZLWKDNQLIH` LQWHUIDFH!!

4XHHQ

XVH:HDSRQ ^LPSOHPHQWVVKRRW LQJDQDUURZZLWKDERZ`

:HDSRQ%HKDYLRU

ILJKW ^`

XVH:HDSRQ 

.LQJ ILJKW ^`

7UROO ILJKW ^`

.QLJKW ILJKW ^`

$[H%HKDYLRU XVH:HDSRQ ^LPSOHPHQWVFKRS SLQJZLWKDQD[H`

6ZRUG%HKDYLRU XVH:HDSRQ ^LPSOHPHQWVVZLQJ LQJDVZRUG`

VHW:HDSRQ :HDSRQ%HKDYLRUZ ^ WKLVZHDSRQ Z `

\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Page 26

Return to Table of Contents

GLQHUWDON

7^MZPMIZLI\\PMTWKITLQVMZ $OLFH ,QHHGD&UHDPFKHHVH ZLWKMHOO\RQZKLWHEUHDGD FKRFRODWHVRGDZLWKYDQLOODLFHFUHDPD JULOOHGFKHHVHVDQGZLFKZLWKEDFRQDWXQD ÀVKVDODGRQWRDVWDEDQDQDVSOLWZLWK LFHFUHDP VOLFHGEDQDQDVDQGDFRIIHH ZLWKDFUHDPDQGWZRVXJDUVRK DQGSXWDKDPEXUJHURQWKHJULOO

)OR *LYHPHD&- :KLWHDEODFN ZKLWHD -DFN%HQQ\DUDGLRDKRXVH ERDWDFRIIHHUHJXODUDQG EXUQRQH

?PI\¼[\PMLQNNMZMVKMJM\_MMV\PM[M\_WWZLMZ['6W\I\PQVO 
&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Page 27

Return to Table of Contents

LQWURWR'HVLJQ3DWWHUQV

7^MZPMIZLQV\PMVM`\K]JQKTM 6R,FUHDWHGWKLVEURDGFDVW FODVV,WNHHSVWUDFNRIDOO WKHREMHFWVOLVWHQLQJWRLWDQGDQ\WLPH DQHZSLHFHRIGDWDFRPHVDORQJLWVHQGVD PHVVDJHWRHDFKOLVWHQHU:KDW·VFRROLVWKDW WKHOLVWHQHUVFDQMRLQWKHEURDGFDVWDWDQ\ WLPHRUWKH\FDQHYHQUHPRYHWKHPVHOYHV ,WLVUHDOO\G\QDPLFDQGORRVHO\FRXSOHG

5LFN

YiX`e gfn\i &DQ\RXWKLQNRIRWKHUVKDUHGYRFDEXODULHV WKDWDUHXVHGEH\RQG22GHVLJQDQGGLQHU WDON" +LQWKRZDERXWDXWRPHFKDQLFV FDUSHQWHUVJRXUPHWFKHIVDLUWUDI¿FFRQWURO  :KDWTXDOLWLHVDUHFRPPXQLFDWHGDORQJZLWK WKHOLQJR" &DQ\RXWKLQNRIDVSHFWVRI22GHVLJQ WKDWJHWFRPPXQLFDWHGDORQJZLWKSDWWHUQ QDPHV":KDWTXDOLWLHVJHWFRPPXQLFDWHG DORQJZLWKWKHQDPH³6WUDWHJ\3DWWHUQ´"

5LFNZK\ GLGQ·W\RXMXVWVD\ \RXZHUHXVLQJWKH 2EVHUYHU3DWWHUQ"

([DFWO\,I\RX FRPPXQLFDWHLQSDWWHUQV WKHQRWKHUGHYHORSHUVNQRZ LPPHGLDWHO\DQGSUHFLVHO\WKH GHVLJQ\RX·UHGHVFULELQJ-XVWGRQ·W JHW3DWWHUQ)HYHU\RX·OONQRZ \RXKDYHLWZKHQ\RXVWDUWXVLQJ SDWWHUQVIRU+HOOR :RUOG

\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 28

VKDUHGYRFDEXODU\


;PIZMLXI\\MZV^WKIJ]TIZQM[IZM87?-:.=4 ?PMVaW]KWUU]VQKI\M_Q\PIVW\PMZLM^MTWXMZWZaW]Z \MIU][QVOXI\\MZV[aW]IZMKWUU]VQKI\QVOVW\R][\I XI\\MZVVIUMJ]\I_PWTM[M\WN Y]ITQ\QM[KPIZIK\MZQ[\QK[ IVLKWV[\ZIQV\[\PI\\PMXI\\MZVZMXZM[MV\[

 UQWRLPSOH HJ\SDWWH XUGXFNVµ W D U W V H K QJW RIR ´:H·UHXVL YDULRXVEHKDYLRUV DYLRUKDVEHHQ PHQWWKH \RXWKHGXFNEHK WRIFODVVHV 7KLVWHOOV HGLQWRLWVRZQVH DQGFKDQJHG HQFDSVXODW HHDVLO\H[SDQGHG WKDWFDQEXQWLPHLIQHHGHG 8I\\MZV[ITTW_aW]\W[IaUWZM_Q\PTM[[?PMV HYHQDWU aW]][MIXI\\MZVQVILM[KZQX\QWVW\PMZLM^MTWXMZ[Y]QKSTa SVW_XZMKQ[MTa\PMLM[QOVaW]PI^MQVUQVL

\RX VKDYH WR J LQ W H
;PIZML^WKIJ]TIZQM[KIV\]ZJWKPIZOMaW]Z LM^MTWXUMV\\MIU)\MIU_MTT^MZ[MLQVLM[QOV XI\\MZV[KIVUW^MUWZMY]QKSTa_Q\PTM[[ZWWUNWZ UQ[]VLMZ[\IVLQVO ;PIZML^WKIJ]TIZQM[MVKW]ZIOMUWZMR]VQWZ LM^MTWXMZ[\WOM\]X\W[XMML2]VQWZLM^MTWXMZ[TWWS ]X\WM`XMZQMVKMLLM^MTWXMZ[?PMV[MVQWZLM^MTWXMZ[ UISM][MWN LM[QOVXI\\MZV[R]VQWZLM^MTWXMZ[IT[WJMKWUM UW\Q^I\ML\WTMIZV\PMU*]QTLIKWUU]VQ\aWN XI\\MZV ][MZ[I\aW]ZWZOIVQbI\QWV



RVKDUHGHVLJQ $V\RXUWHDPEHJLQVWLQWHUPVRI LGHDVDQGH[SHULHQFH DFRPPXQLW\ SDWWHUQV\RXZLOOEXLOG RISDWWHUQVXVHUV

7KLQNDERXWVWDUWLQJDSDWWHUQVVWXG\ JURXSDW\RXURUJDQL]DWLRQPD\EH\RX FDQHYHQJHWSDLGZKLOH\RX·UHOHDUQ LQJ

&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 29

LQWURWR'HVLJQ3DWWHUQV

0W_LW1][M,M[QOV8I\\MZV[' ?M¼^MITT][MLWNN\PM[PMTN TQJZIZQM[IVLNZIUM_WZS[?M\ISM\PMU_ZQ\M[WUMKWLMIOIQV[\\PMQZ)81[ KWUXQTM\PMUQV\WW]ZXZWOZIU[IVLJMVMÅ\NZWUITW\WN KWLM[WUMWVMMT[MPI[_ZQ\\MV
YLRU

\EHKD XODWHGÀ

(QFDSV

$,1


FH!! LQWHUID

)O\%HKDYLRU

IO\

)O\1R:D\

)O\:LWK:LQJV IO\ ^ LPSOH

'XFN

XDFNEH XODWHGT (QFDSV LQWHUIDFH!!

4XDFN% VZLP

HKDYLRU

4XDFN%

XDFN

SHUIRUP4

TXDFN

O\

SHUIRUP)

KDYLRU

VHW)O\%H

RU VHW4XDFN%HKDYL RGV LNHPHWK 27+(5GXFNO

0XWH4XDFN TXDFN ^

6TXHDN 4XDFN

'HFR\'XFN 5XEEHU'XFN 5HGKHDG'XFN KHDG`

TXDFN ^

GLVSOD\ ^ ` FR\GXFN ORRNVOLNHDGH

GLVSOD\ ^ EHUGXFN` ORRNVOLNHDUXE

FN

WKLQJFDQ·WTXD

`

`

2%6(59(5 

 LQW

GRQR UGXFNLHVTXHDN

UXEEH

`

ORRNVOLNHDUHG

6X EMH

TXDFN ^ FNLQJ

PHQWVGXFNTXD

LPSOH

FW2EMH

 

' R

J2EM

W HF

 F ' XFN2EMH

0R

W

W &D W2EMHF

HF XVH2EM

W

2EVHUYHUV

$XWRPDWLFXSGDWHQRWLILFDWLRQ

&RQW

U

UROOH

W2EMHFWV

GLVSOD\ ^ OODUG` ORRNVOLNHDPD

GLVSOD\ ^

09&

'HSHQGHQ

2EMHFWWKDW KROGVVWDWH 0DOODUG'XFN

FW

$%XQFKRI3DWWHUQV

`

KDYLRU

)O\%HKD

GLVSOD\

WKLQJFDQ·WIO\

GRQR

`

HKDYLRU YLRUIO\% HKDYLRU HKDYLRUTXDFN%

&OLHQW

IO\ ^ J

PHQWVGXFNIO\LQ

HO

0RG HVW 5HTX

9LHZ

QHZ
WKHUH DUH QR

'XPE 4XHVWLRQV

Q:

Q:

$UHQ·WOLEUDULHVDQGIUDPHZRUNV DOVRGHVLJQSDWWHUQV"

XQGHUVWDQG$3,VWKDWDUHVWUXFWXUHG DURXQGGHVLJQSDWWHUQV

A:

A:

Q:

,IGHVLJQSDWWHUQVDUHVRJUHDW ZK\FDQ·WVRPHRQHEXLOGDOLEUDU\RI WKHPVR,GRQ·WKDYHWR"

'HVLJQSDWWHUQVDUHKLJKHUOHYHO WKDQOLEUDULHV'HVLJQSDWWHUQVWHOOXV KRZWRVWUXFWXUHFODVVHVDQGREMHFWVWR VROYHFHUWDLQSUREOHPVDQGLWLVRXUMREWR DGDSWWKRVHGHVLJQVWRILWRXUSDUWLFXODU DSSOLFDWLRQ

)UDPHZRUNVDQGOLEUDULHVDUHQRW GHVLJQSDWWHUQVWKH\SURYLGHVSHFLILF LPSOHPHQWDWLRQVWKDWZHOLQNLQWRRXU FRGH6RPHWLPHVKRZHYHUOLEUDULHVDQG IUDPHZRUNVPDNHXVHRIGHVLJQSDWWHUQV LQWKHLULPSOHPHQWDWLRQV7KDW·VJUHDW EHFDXVHRQFH\RXXQGHUVWDQGGHVLJQ SDWWHUQV\RX·OOPRUHTXLFNO\

6RWKHUHDUHQROLEUDULHVRI GHVLJQSDWWHUQV"

A:

1REXW\RXZLOOOHDUQODWHUDERXW SDWWHUQFDWDORJVZLWKOLVWVRISDWWHUQVWKDW \RXFDQDSSO\WR\RXUDSSOLFDWLRQV

\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 30

ZK\GHVLJQSDWWHUQV"

3DWWHUQVDUH QRWKLQJPRUHWKDQXVLQJ 22GHVLJQSULQFLSOHV $FRPPRQPLVFRQFHSWLRQ *UDVVKRSSHUEXWLW·VPRUH VXEWOHWKDQWKDW
;SMX\QKIT,M^MTWXMZ

.ZQMVLTa 8I\\MZV[/]Z]

'HYHORSHU2ND\KPPEXWLVQ·WWKLVDOOMXVWJRRGREMHFWRULHQWHGGHVLJQ,PHDQ DVORQJDV,IROORZHQFDSVXODWLRQDQG,NQRZDERXWDEVWUDFWLRQLQKHULWDQFHDQG SRO\PRUSKLVPGR,UHDOO\QHHGWRWKLQNDERXW'HVLJQ3DWWHUQV",VQ·WLWSUHWW\ VWUDLJKWIRUZDUG",VQ·WWKLVZK\,WRRNDOOWKRVH22FRXUVHV",WKLQN'HVLJQ 3DWWHUQVDUHXVHIXOIRUSHRSOHZKRGRQ·WNQRZJRRG22GHVLJQ *XUX$KWKLVLVRQHRIWKHWUXHPLVXQGHUVWDQGLQJVRIREMHFWRULHQWHG GHYHORSPHQWWKDWE\NQRZLQJWKH22EDVLFVZHDUHDXWRPDWLFDOO\JRLQJWREHJRRGDW EXLOGLQJÁH[LEOHUHXVDEOHDQGPDLQWDLQDEOHV\VWHPV 'HYHORSHU1R" *XUX1R$VLWWXUQVRXWFRQVWUXFWLQJ22V\VWHPVWKDWKDYHWKHVHSURSHUWLHVLV QRWDOZD\VREYLRXVDQGKDVEHHQGLVFRYHUHGRQO\WKURXJKKDUGZRUN 'HYHORSHU,WKLQN,·PVWDUWLQJWRJHWLW7KHVHVRPHWLPHVQRQREYLRXVZD\VRI FRQVWUXFWLQJREMHFWRULHQWHGV\VWHPVKDYHEHHQFROOHFWHG *XUX\HVLQWRDVHWRISDWWHUQVFDOOHG'HVLJQ3DWWHUQV 'HYHORSHU6RE\NQRZLQJSDWWHUQV,FDQVNLSWKHKDUGZRUNDQGMXPSVWUDLJKWWR GHVLJQVWKDWDOZD\VZRUN" *XUX


&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 31

LQWURWR'HVLJQ3DWWHUQV

5HPHPEHUNQRZLQJ FRQFHSWVOLNHDEVWUDFWLRQ LQKHULWDQFHDQGSRO\PRUSKLVPGR QRWPDNH\RXDJRRGREMHFWRULHQWHG GHVLJQHU$GHVLJQJXUXWKLQNVDERXW KRZWRFUHDWHÁH[LEOHGHVLJQVWKDW DUHPDLQWDLQDEOHDQGWKDWFDQ FRSHZLWKFKDQJH

*XUX7KHUHDUHVRPHREMHFWRULHQWHGSULQFLSOHVWKDW XQGHUOLHWKHSDWWHUQVDQGNQRZLQJWKHVHZLOOKHOS\RX WRFRSHZKHQ\RXFDQ·WÀQGDSDWWHUQWKDWPDWFKHV\RXU SUREOHP 'HYHORSHU3ULQFLSOHV"
\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 32

\RXUGHVLJQWRROER[


22%DVLFV

$EVWUDFWLRQ Q (QFDSVXODWLR P LV 3RO\PRUSK ,QKHULWDQFH

V 223ULQFLSOH

FV H22EDVL NQRZWK SKLFDOO\ X R \ H P X U :HDVV ODVVHVSRO\PR VLJQE\ RIXVLQJFULWDQFHLVOLNHGH ODWLRQ KRZLQKH DQGKRZHQFDSVXUXVW\ FRQWUDFW \RXDUHDOLWWOH DG)LUVW ZRUNV,ISXOORXW\RXU+H WKLV RQWKHVH UHYLHZWKHQVNLP -DYDDQG JDLQ FKDSWHUD

ƒ .QRZLQJWKH22EDVLFV GRHVQRWPDNH\RXDJRRG 22GHVLJQHU

ƒ *RRG22GHVLJQVDUH UHXVDEOHH[WHQVLEOHDQG PDLQWDLQDEOH

ƒ 3DWWHUQVVKRZ\RXKRZWR EXLOGV\VWHPVZLWKJRRG 22GHVLJQTXDOLWLHV

ƒ 3DWWHUQVDUHSURYHQREMHFW RULHQWHGH[SHULHQFH

ƒ 3DWWHUQVGRQ·WJLYH\RX

V ZKDWYDULH (QFDSVXODWH  RVLWLRQRYHU )DYRUFRPS LQKHULWHQFH QRW LQWHUIDFHV R W P D U J R U 3 LRQV LPSOHPHQWDW

NDW JDFORVHUORR :H·OOEHWDNLQ HURDGDQGDOVR WKHVHGRZQWKPRUHWRWKHOLVW DGGLQJDIHZ

223DWWHUQV DPLO\RIDOJRULWKPV

HILQHVDI KHP 6WUDWHJDW\HVHGDFKRQHDQGPOHDNWHVVWWKHDOJRULWKP XO \ HQFDSV JHDEOH6WUDWHJ WKDWXVHLW LQWHUFKDQ HQGHQWO\IURPFOLHQWV HS YDU\LQG

2QHGRZQPDQ\WRJR



:MDD=LHGAFLK

FRGHWKH\JLYH\RX JHQHUDOVROXWLRQVWRGHVLJQ SUREOHPV
ƒ 3DWWHUQVDUHQ·WLQYHQWHG WKH\DUHGLVFRYHUHG

ƒ 0RVWSDWWHUQVDQG

7KURXJKRXWWKH ERRNWKLQNDERXW KRZSDWWHUQVUHO\ RQ22EDVLFVDQG SULQFLSOHV

SULQFLSOHVDGGUHVVLVVXHVRI FKDQJHLQVRIWZDUH

ƒ 0RVWSDWWHUQVDOORZVRPH SDUWRIDV\VWHPWRYDU\ LQGHSHQGHQWO\RIDOORWKHU SDUWV

ƒ :HRIWHQWU\WRWDNHZKDW YDULHVLQDV\VWHPDQG HQFDSVXODWHLW

ƒ 3DWWHUQVSURYLGHD VKDUHGODQJXDJHWKDWFDQ PD[LPL]HWKHYDOXHRI\RXU FRPPXQLFDWLRQZLWKRWKHU GHYHORSHUV

&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 33

LQWURWR'HVLJQ3DWWHUQV

4M\¼[OQ^MaW]ZZQOP\JZIQV[WUM\PQVO\WLW 1\¼[aW]Z[\IVLIZLKZW[[_WZL#ITTWN\PM[WT]\QWV_WZL[ IZMNZWU\PQ[KPIX\MZ

 

  













 













  2$/ 1-%!.  !.%#) +//!-). 

 1  !/2*-&%)# *0)

 0!- 0&. (&!   -/!) !- /$*0#$/ /$!4 2!-! ''!   -*#-( /* /$%. )*/ ) %(+'!(!)//%*)   //!-). #* %)/* 4*0-    !-) "-*( /$! */$!- #045.   !1!'*+(!)/ *)./)/  //!-). #%1! 0.  .$-! 



 //!-).  %) ()4 ++'%/%*).  1*- *1!- %)$!-%/)!  ) 2. /$-%''! 2%/$ /$%. +//!-)  *./ +//!-). "*''*2 "-*(    */ 4*0- *2)  %#$ '!1!' '%--%!.  *!5. "1*-%/! -%)&  //!-) /$/ "%3! /$! .%(0'/* 0& /$/ )5/ ,0&  -%''! $!!.! 2%/$ *)   0& !(* 2. '*/! 2$!-!

\RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 34

GHVLJQSX]]OHVROXWLRQ

'HVLJQ3X]]OH6ROXWLRQ +PIZIK\MZQ[\PMIJ[\ZIK\KTI[[NWZITT\PMW\PMZKPIZIK\MZ[3QVO9]MMV 3VQOP\IVL
DEVWUDFW &KDUDFWHU :HDSRQ%HKDYLRUZHDSRQ ILJKW  VHW:HDSRQ :HDSRQ%HKDYLRUZ ^ WKLVZHDSRQ Z `

.LQJ ILJKW ^`

$&KDUDFWHU+$6$ :HDSRQ%HKDYLRU

.QLJKW ILJKW ^`

7UROO

4XHHQ ILJKW ^`

ILJKW ^`

LQWHUIDFH!! :HDSRQ%HKDYLRU XVH:HDSRQ 

%RZ$QG$UURZ%HKDYLRU

6ZRUG%HKDYLRU XVH:HDSRQ ^LPSOHPHQWVVZLQJ LQJDVZRUG`

XVH:HDSRQ ^LPSOHPHQWVVKRRW LQJDQDUURZZLWKDERZ`

.QLIH%HKDYLRU

SOHPHQW FWFRXOGLP6D\D MH E R < 1 $ H 1RWHWKDW Q%HKDYLRULQWHUIDFVWHRUD WKH:HDSR DWXEHRIWRRWKSD SDSHUFOLS HDEDVV PXWDWHGV



XVH:HDSRQ ^LPSOHPHQWVFXWWLQJ ZLWKDNQLIH`

$[H%HKDYLRU

XVH:HDSRQ ^LPSOHPHQWVFKRS SLQJZLWKDQD[H`

&KDSWHU

Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 1. Welcome to Design Patterns

Return to Table of Contents

Page 35

LQWURWR'HVLJQ3DWWHUQV

6ROXWLRQV 6KDUSHQ \RXU SHQFLO ?PQKPWN \PMNWTTW_QVOIZMLQ[IL^IV\IOM[WN ][QVO[]JKTI[[QVO\WXZW^QLM[XMKQÅK ,]KSJMPI^QWZ'+PWW[MITT\PI\IXXTa

R+ 0IZL\WOIQVSVW_TMLOMWN ITTL]KSJMPI^QWZ[ R, ,]KS[KIV¼\ÆaIVLY]IKSI\\PM[IUM\QUM R- +PIVOM[KIV]VQV\MV\QWVITTaINNMK\W\PMZL]KS[

R) +WLMQ[L]XTQKI\MLIKZW[[[]JKTI[[M[ R* :]V\QUMJMPI^QWZKPIVOM[IZMLQNÅK]T\ R+ ?MKIV¼\UISML]KS¼[LIVKM

 













!

#





"









#



















!

















"













&





%













 

!

#

#

"

















!

 !

 







 !





!



 









 

$

6KDUSHQ \RXU SHQFLO





& 



 



!

 







 !





" 





"







$





"





!

















#



!  #









 





!









#





&

?PI\IZM[WUMNIK\WZ[\PI\LZQ^MKPIVOMQVaW]ZIXXTQKI\QWV['AW]UQOP\ PI^MI^MZaLQNNMZMV\TQ[\J]\PMZM¼[INM_WN W]Z[4WWSNIUQTQIZ'

0\FXVWRPHUVRUXVHUVGHFLGHWKH\ZDQWVRPHWKLQJHOVHRUWKH\ZDQWQHZIXQFWLRQDOLW\ 0\FRPSDQ\GHFLGHGLWLVJRLQJZLWKDQRWKHUGDWDEDVHYHQGRUDQGLWLVDOVRSXUFKDVLQJLWVGDWD IURPDQRWKHUVXSSOLHUWKDWXVHVDGLIIHUHQWGDWDIRUPDW$UJK :HOOWHFKQRORJ\FKDQJHVDQGZH·YHJRWWRXSGDWHRXUFRGHWRPDNHXVHRISURWRFROV :H·YHOHDUQHGHQRXJKEXLOGLQJRXUV\VWHPWKDWZH·GOLNHWRJREDFNDQGGRWKLQJVDOLWWOHEHWWHU \RXDUHKHUH



Chapter 1. Welcome to Design Patterns Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Table of Contents Chapter 2. Keeping your Objects in the know.............................................. 1 Section 2.1. The Weather Monitoring application overview.......................................................................................................... 3 Section 2.2. Unpacking the WeatherData class.............................................................................................................................. 4 Section 2.3. What do we know so far ?........................................................................................................................................... 5 Section 2.4. Taking a first, misguided SWAG at the Weather Station........................................................................................... 6 Section 2.5. Sharpen your pencil.................................................................................................................................................... 6 Section 2.6. What's wrong with our implementation?................................................................................................................... 7 Section 2.7. Meet the Observer Pattern.......................................................................................................................................... 8 Section 2.8. Publishers + Subscribers = Observer Pattern............................................................................................................ 9 Section 2.9. A day in the life of the Observer Pattern................................................................................................................... 10 Section 2.10. Five minute drama: a subject for observation........................................................................................................ 12 Section 2.11. Two weeks later........................................................................................................................................................ 14 Section 2.12. The Observer Pattern defined.................................................................................................................................. 15 Section 2.13. The Observer Pattern defined: the class diagram................................................................................................... 16 Section 2.14. The power of Loose Coupling.................................................................................................................................. 17 Section 2.15. Sharpen your pencil................................................................................................................................................. 18 Section 2.16. Cubicle conversation................................................................................................................................................ 19 Section 2.17. Designing the Weather Station............................................................................................................................... 20 Section 2.18. Implementing the Weather Station......................................................................................................................... 21 Section 2.19. Implementing the Subject interface in WeatherData............................................................................................. 22 Section 2.20. Now, let's build those display elements................................................................................................................. 23 Section 2.21. There are no Dumb Questions................................................................................................................................ 23 Section 2.22. Power up the Weather Station................................................................................................................................ 24 Section 2.23. Sharpen your pencil................................................................................................................................................ 25 Section 2.24. Using Java's built-in Observer Pattern.................................................................................................................. 28 Section 2.25. How Java's built-in Observer Pattern works.......................................................................................................... 29 Section 2.26. Reworking the Weather Station with the built-in support..................................................................................... 31 Section 2.27. Now, let's rework the CurrentConditionsDisplay................................................................................................... 32 Section 2.28. Exercise: Code Magnets.......................................................................................................................................... 33 Section 2.29. Running the new code............................................................................................................................................ 34 Section 2.30. The dark side of java.util.Observable..................................................................................................................... 35 Section 2.31. Other places you'll find the Observer Pattern in the JDK...................................................................................... 36 Section 2.32. And the code............................................................................................................................................................ 37 Section 2.33. Tools for your Design Toolbox................................................................................................................................ 38 Section 2.34. Exercise: Design Principle Challenge..................................................................................................................... 39 Section 2.35. Exercise solutions: Design Principle Challenge...................................................................................................... 41 Section 2.36. Code Magnets: Exercise solutions.......................................................................................................................... 42

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 1

the Observer Pattern

.HHSLQJ\RXU5 4 4 2EMHFWVLQWKHNQRZ +H\-HUU\,·P QRWLI\LQJHYHU\RQHWKDWWKH 3DWWHUQV*URXSPHHWLQJPRYHGWR 6DWXUGD\QLJKW:H·UHJRLQJWREH WDONLQJDERXWWKH2EVHUYHU3DWWHUQ 7KDWSDWWHUQLVWKHEHVW,W·VWKH %(67-HUU\

'RQ·WPLVVRXWZKHQVRPHWKLQJLQWHUHVWLQJKDSSHQV:H¶YHJRWD SDWWHUQWKDWNHHSV\RXUREMHFWVLQWKHNQRZZKHQVRPHWKLQJWKH\PLJKWFDUHDERXWKDSSHQV 2EMHFWVFDQHYHQGHFLGHDWUXQWLPHZKHWKHUWKH\ZDQWWREHNHSWLQIRUPHG7KH2EVHUYHU 3DWWHUQLVRQHRIWKHPRVWKHDYLO\XVHGSDWWHUQVLQWKH-'.DQGLW¶VLQFUHGLEO\XVHIXO%HIRUH ZH¶UHGRQHZH¶OODOVRORRNDWRQHWRPDQ\UHODWLRQVKLSVDQGORRVHFRXSOLQJ \HDKWKDW¶VULJKW ZHVDLGFRXSOLQJ :LWK2EVHUYHU\RX¶OOEHWKHOLIHRIWKH3DWWHUQV3DUW\

WKLVLVDQHZFKDSWHU



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 2

ZHDWKHUPRQLWRULQJVWDWLRQ

&RQJUDWXODWLRQV
)NC

/ 2AMA 7EATHER 3TREET -AIN  !LLEY /+ 4ORNADO

3TATEME

NTOF7O

RK

HUDWLRQ UQH[WJHQ X R G LO X E WHGWR HLQJVHOHF J6WDWLRQ WLRQVRQE OD ULQ R WX LW UD Q J R Q 0 &R HDWKHU SHQGLQJ : G VH D E W XUSDWHQW R Q R G GLWLRQV ,QWHUQH VH EHED DWKHUFRQ DWLRQZLOO XUUHQWZH :H·GOLNH VW F U V H N WK F D UD H W 7KHZ VVXUH ZKLFK PHWULFSUH WKUHH DWDREMHFW :HDWKHU' KXPLGLW\DQGEDUR WLQLWLDOO\SURYLGHV D UH K VDQGD W WX Q LF UD R VW WHPSH WKHUVWDWL DSSOLFDWL D Q H D  Z WH D V Q UH F RQGLWLR HDWKHU'DWD IRU\RXWR FXUUHQWF UHDOWLPHDVWKH: WV Q H P OH Q GLVSOD\H XSGDWHGL UHPHQWV UHFDVWDOO HQWPHDVX F H U VW U2 VLPSOHIR R P Q:HDWKH XLUHVWKH WKHUVWDWLR HORSHUVFDQ D H REMHFWDFT Z OH E D SDQG UGHY WKDWRWKH LVLVDQH[ LQ:H·G )XUWKHUWK RUHOHDVHDQ$3,VR GSOXJWKHPULJKW QWVW \VDQ 5DPDZD WKHUGLVSOD RZQZHD LU , H K 3 QFH W $ W WH D UL K Z W PRGHOR XWRVXSSO\ DWEXVLQHVV IRUHDFK UH J D H OLNHIRU\R Y D LQNVZHK KDUJHWKHP RSD\ 5DPDWK QWHQGWRF JW :HDWKHU2 DUHKRRNHGZHL HDUHJRLQ  HUV HVWSDUW Z P E R H VW WK X U F R H I WK RZ H\XVH1 GLVSOD\WK OLFDWLRQ V Q R WL S R DOSKDDSS N G F Q WR D V Q Q J L VL X \R XUGH VHHLQJ\R RUZDUGWR :HORRNI

Copyright Safari Books Online #1673621

 6LQFHUHO\ HVWR\RX

&(2 VRXUFHÀO XUULFDQH DWKHU'DWD H : H K -RKQQ\+ W J KWLQ UHRYHUQLJ 36:HD



&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 3

WKHREVHUYHUSDWWHUQ


LRQVLVRQHRI &XUUHQW&RQGLWWGLVSOD\V7KH WKUHHGLIIHUHQHWZHDWKHUVWDWV XVHUFDQDOVRJ  DQGDIRUHFDVW

GLVSOD\V

+XPLGLW\ VHQVRUGHYLFH

SXOOVGDWD

&XUUHQW &RQGLWLRQV 7HPSƒ +XPLGLW\ 3UHVVXUH

7HPSHUDWXUH VHQVRUGHYLFH

:HDWKHU'DWD REMHFW :HDWKHU6WDWLRQ

'LVSOD\GHYLFH

3UHVVXUH VHQVRUGHYLFH

:HDWKHU25DPDSURYLGHV

:KDWZHLPSOHPHQW


2XUMRELIZHFKRRVHWRDFFHSWLWLVWRFUHDWHDQDSSWKDW XVHVWKH:HDWKHU'DWDREMHFWWRXSGDWHWKUHHGLVSOD\VIRU FXUUHQWFRQGLWLRQVZHDWKHUVWDWVDQGDIRUHFDVW

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 4

ZHDWKHUGDWDFODVV

=VXIKSQVO\PM?MI\PMZ,I\IKTI[[ $VSURPLVHGWKHQH[WPRUQLQJWKH:HDWKHU'DWDVRXUFHÀOHVDUULYH 3HHNLQJLQVLGHWKHFRGHWKLQJVORRNSUHWW\VWUDLJKWIRUZDUG

:HDWKHU'DWD

JHW7HPSHUDWXUH JHW+XPLGLW\ JHW3UHVVXUH PHDVXUHPHQWV&KDQJHG RWKHUPHWKRGV

HFHQW KHPRVWUHKXPLGLW\ W Q U X W H U RGV DWXU UHHPHWK UWHPSHU 7KHVHWK HDVXUHPHQWVIR HVSHFWLYHO\ ZHDWKHUPPHWULFSUHVVXUHU HWWKH EOHVDUHV SGDWHG LD U D Y DQGEDUR VH H X :WK VKRZWRJHW FDUH+2 :HGRQ·W 'DWDREMHFWNQRZWDWLRQ :HDWKHU PWKH:HDWKHU6 LQIRIUR

'DWD RIWKH:HDWKHU 7KHGHYHORSHUVDFOXHDERXWZKDWZH REMHFWOHIWXV QHHGWRDGG

5HPHPEHUWKLV&XUUHQW&R QGLWLRQVLVMXVW 21(RIWKUHHGLIIHUHQWGLVS OD\VFUHHQV

&XUUHQW &RQGLWLRQV 7HPSƒ +XPLGLW\ 3UHVVXUH

   7KLVPHWKRGJHWVFDOOHG  ZKHQHYHUWKHZHDWKHUPHDVXUHPHQWV  KDYHEHHQXSGDWHG     SXEOLFYRLGPHDVXUHPHQWV&KDQJHG ^ 
:HDWKHU'DWDMDYD

2XUMRELVWRLPSOHPHQWPHDVXUHPHQWV&KDQJHG  VRWKDWLWXSGDWHVWKHWKUHHGLVSOD\VIRUFXUUHQW FRQGLWLRQVZHDWKHUVWDWVDQGIRUHFDVW

'LVSOD\GHYLFH



&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 5

WKHREVHUYHUSDWWHUQ

?PI\LW_MSVW_[WNIZ'
5 7KH:HDWKHU'DWDFODVVKDVJHWWHUPHWKRGVIRUWKUHH

JHW7HPSHUDWXUH JHW+XPLGLW\ JHW3UHVVXUH

5 7KHPHDVXUHPHQWV&KDQJHG PHWKRGLVFDOOHGDQ\

PHDVXUHPHQWV&KDQJHG

PHDVXUHPHQWYDOXHVWHPSHUDWXUHKXPLGLW\DQG EDURPHWULFSUHVVXUH

WLPHQHZZHDWKHUPHDVXUHPHQWGDWDLVDYDLODEOH :H GRQ¶WNQRZRUFDUHKRZWKLVPHWKRGLVFDOOHGZHMXVW NQRZWKDWLWLV

:HDWKHU 6WDWV $YJWHPSƒ 0LQWHPSƒ 0D[WHPSƒ

5 :HQHHGWRLPSOHPHQWWKUHHGLVSOD\HOHPHQWVWKDW XVHWKHZHDWKHUGDWDDFXUUHQWFRQGLWLRQVGLVSOD\D VWDWLVWLFVGLVSOD\DQGDIRUHFDVWGLVSOD\7KHVHGLVSOD\V PXVWEHXSGDWHGHDFKWLPH:HDWKHU'DWDKDVQHZ PHDVXUHPHQWV

&XUUHQW &RQGLWLRQV 7HPSƒ +XPLGLW\ 3UHVVXUH

'LVSOD\7ZR

)RUHFDVW

'LVSOD\2QH

77 7

'LVSOD\7KUHH

5 7KHV\VWHPPXVWEHH[SDQGDEOH²RWKHUGHYHORSHUV

FDQFUHDWHQHZFXVWRPGLVSOD\HOHPHQWVDQGXVHUV FDQDGGRUUHPRYHDVPDQ\GLVSOD\HOHPHQWVDVWKH\ ZDQWWRWKHDSSOLFDWLRQ&XUUHQWO\ZHNQRZDERXW RQO\WKHLQLWLDOWKUHHGLVSOD\W\SHV FXUUHQWFRQGLWLRQV VWDWLVWLFVDQGIRUHFDVW 

7 )XWXUHGLVSOD\V

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 6

¿UVWWU\ZLWKWKHZHDWKHUVWDWLRQ


SXEOLFFODVV:HDWKHU'DWD^ LQVWDQFHYDULDEOHGHFODUDWLRQV SXEOLFYRLGPHDVXUHPHQWV&KDQJHG ^ ÀRDWWHPS JHW7HPSHUDWXUH  ÀRDWKXPLGLW\ JHW+XPLGLW\  ÀRDWSUHVVXUH JHW3UHVVXUH 

*UDEWKHPRVWUHFHQWPHDVXUHPHWV E\FDOOLQJWKH:HDWKHU'DWD·VJHWWHU PHWKRGV DOUHDG\LPSOHPHQWHG 

FXUUHQW&RQGLWLRQV'LVSOD\XSGDWH WHPSKXPLGLW\SUHVVXUH  VWDWLVWLFV'LVSOD\XSGDWH WHPSKXPLGLW\SUHVVXUH  IRUHFDVW'LVSOD\XSGDWH WHPSKXPLGLW\SUHVVXUH  ` RWKHU:HDWKHU'DWDPHWKRGVKHUH `

1RZXSGDWH WKHGLVSOD\V

\HOHPHQWWR &DOOHDFKGLVSODOD\SDVVLQJLWWKH VS GL XSGDWHLWV DVXUHPHQWV PRVWUHFHQWPH

6KDUSHQ \RXU SHQFLO *I[MLWVW]ZÅZ[\QUXTMUMV\I\QWV_PQKPWN \PMNWTTW_QVOIXXTa' +PWW[MITT\PI\IXXTa

R) ?MIZMKWLQVO\WKWVKZM\M QUXTMUMV\I\QWV[VW\QV\MZNIKM[

R* .WZM^MZaVM_LQ[XTIaMTMUMV\_MVMML \WIT\MZKWLM

R+ ?MPI^MVW_Ia\WILLWZZMUW^M LQ[XTIaMTMUMV\[I\Z]V\QUM

R,
R- ?MPI^MV¼\MVKIX[]TI\ML\PMXIZ\\PI\ KPIVOM[

R. ?MIZM^QWTI\QVOMVKIX[]TI\QWVWN \PM ?MI\PMZ,I\IKTI[[

'HILQLWLRQRI6:$*6FLHQWLILF:LOG$

*XHVV



&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 7

WKHREVHUYHUSDWWHUQ

?PI\¼[_ZWVO_Q\PW]ZQUXTMUMV\I\QWV' 7KLQNEDFNWRDOOWKRVH&KDSWHUFRQFHSWVDQGSULQFLSOHV

SXEOLFYRLGPHDVXUHPHQWV&KDQJHG ^ ÀRDWWHPS JHW7HPSHUDWXUH  ÀRDWKXPLGLW\ JHW+XPLGLW\  ÀRDWSUHVVXUH JHW3UHVVXUH 

$UHDRIFKDQJHZHQHHG WRHQFDSVXODWHWKLV

FXUUHQW&RQGLWLRQV'LVSOD\XSGDWH WHPSKXPLGLW\SUHVVXUH  VWDWLVWLFV'LVSOD\XSGDWH WHPSKXPLGLW\SUHVVXUH  IRUHFDVW'LVSOD\XSGDWH WHPSKXPLGLW\SUHVVXUH  `

%\FRGLQJWRFRQFUHWHLPSOHPHQWDWLRQV ZHKDYHQRZD\WRDGGRUUHPRYH RWKHUGLVSOD\HOHPHQWVZLWKRXWPDNLQJ FKDQJHVWRWKHSURJUDP

$WOHDVWZHVHHPWREHXVLQJD FRPPRQLQWHUIDFHWRWDONWRWKH GLVSOD\HOHPHQWVWKH\DOOKDYHDQ XSGDWH PHWKRGWDNHVWKHWHPS KXPLGLW\DQGSUHVVXUHYDOXHV

8PP,NQRZ,·PQHZ KHUHEXWJLYHQWKDWZHDUHLQ WKH2EVHUYHU3DWWHUQFKDSWHU PD\EHZHVKRXOGVWDUWXVLQJLW"

:H·OOWDNHDORRNDW 2EVHUYHUWKHQFRPH EDFNDQGÀJXUHRXWKRZ WRDSSO\LWWRWKHZHDWKHU PRQLWRULQJDSS

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Page 8

Return to Table of Contents

PHHWWKHREVHUYHUSDWWHUQ

5MM\\PM7J[MZ^MZ8I\\MZV
)VM_[XIXMZX]JTQ[PMZOWM[QV\WJ][QVM[[IVLJMOQV[ X]JTQ[PQVOVM_[XIXMZ[



AW][]J[KZQJM\WIXIZ\QK]TIZX]JTQ[PMZIVLM^MZa\QUM \PMZM¼[IVM_MLQ\QWVQ\OM\[LMTQ^MZML\WaW])[TWVOI[ aW]ZMUIQVI[]J[KZQJMZaW]OM\VM_VM_[XIXMZ[



AW]]V[]J[KZQJM_PMVaW]LWV¼\_IV\XIXMZ[IVaUWZM IVL\PMa[\WXJMQVOLMTQ^MZML



?PQTM\PMX]JTQ[PMZZMUIQV[QVJ][QVM[[XMWXTMPW\MT[ IQZTQVM[IVLW\PMZJ][QVM[[M[KWV[\IV\Ta[]J[KZQJMIVL ]V[]J[KZQJM\W\PMVM_[XIXMZ

0LVVZKDW·VJRLQJRQ LQ2EMHFWYLOOH"1RZD\RI FRXUVHZHVXEVFULEH



&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 9

WKHREVHUYHUSDWWHUQ

8]JTQ[PMZ[;]J[KZQJMZ[%7J[MZ^MZ8I\\MZV ,I\RXXQGHUVWDQGQHZVSDSHUVXEVFULSWLRQV\RXSUHWW\PXFK XQGHUVWDQGWKH2EVHUYHU3DWWHUQRQO\ZHFDOOWKHSXEOLVKHU WKH68%-(&7DQGWKHVXEVFULEHUVWKH2%6(59(56 /HW·VWDNHDFORVHUORRN

7KHREVHUYHUVKDYHVXEVFULEHGWR UHJLVWHUHGZLWK WKH6XEMHFW WRUHFHLYHXSGDWHVZKHQWKH 6XEMHFW·VGDWDFKDQJHV

XEMHFWFKDQJHV :KHQGDWDLQWKH6RW LILHG HQ DU UV UYH WKHREVH WPDQDJHV 6XEMHFWREMHFGDWD RI VRPHELW

' R

  LQW

FW2EMH

1HZGDWDYDOXHVDUH FRPPXQLFDWHGWRWKH REVHUYHUVLQVRPHIRUP ZKHQWKH\FKDQJH

F ' XFN2EMH

W

W HF



FW

6X EMH

J2EM

W &D W2EMHF



0R

HF XVH2EM

W

2EVHUYHU2EMHFWV

Q 7KLVREMHFWLVQ·WDRHVQ·W G LW R V HU UY VH RE WKH JHWQRWLILHGZKHFKQDQJHV D DW 6XEMHFW·VG

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 10

DGD\LQWKHOLIHRIWKHREVHUYHUSDWWHUQ

)LIaQV\PMTQNMWN\PM7J[MZ^MZ8I\\MZV

'XFNUHDOO\ZDQWVLQRQWKH DFWLRQWKRVHLQWV6XEMHFWLV VHQGLQJRXWZKHQHYHULWVVWDWH FKDQJHVORRNSUHWW\LQWHUHVWLQJ

7KH'XFNREMHFWLVQRZDQ RIÀFLDOREVHUYHU 'XFNLVSV\FKHGKH·VRQWKH OLVWDQGLVZDLWLQJZLWKJUHDW DQWLFLSDWLRQIRUWKHQH[W QRWLÀFDWLRQVRKHFDQJHWDQLQW

FW

W HF J2EM

F ' XFN2EMH

W &D W2EMHF

0R

W

HF XVH2EM

W

2EVHUYHUV



6X EMH

LQW

' R

J2EM

W HF

FW

$'XFNREMHFWFRPHVDORQJ DQGWHOOVWKH6XEMHFWWKDW LWZDQWVWREHFRPHDQ REVHUYHU

' R

µ  PH H E L U F V LQW 6X XE EMH V FW2EMH HU W LV HJ ´U

FW2EMH

F ' XFN2EMH

0R

W

W &D W2EMHF

HF XVH2EM

W

2EVHUYHUV



1RZ'XFNDQGDOOWKHUHVWRIWKH REVHUYHUVJHWDQRWLÀFDWLRQWKDW WKH6XEMHFWKDVFKDQJHG

6X EMH

LQW

FW



7KH6XEMHFWJHWVDQHZ GDWDYDOXH

FW2EMH

 

' R

J2EM

W HF

 F ' XFN2EMH

0R

W

W &D W2EMHF

HF XVH2EM

W

2EVHUYHUV



&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 11

WKHREVHUYHUSDWWHUQ

 LQW

' R

J2EM

W HF

FW

7KH0RXVHREMHFWDVNVWREH UHPRYHGDVDQREVHUYHU

6X EMH

FW2EMH

7KH0RXVHREMHFWKDVEHHQ JHWWLQJLQWVIRUDJHVDQGLVWLUHG RILWVRLWGHFLGHVLW·VWLPHWR VWRSEHLQJDQREVHUYHU

´UH PR YH FW X ' QV XFN2EMH XE VF ULE HP Hµ 0R

W &D W2EMHF

HF XVH2EM

W

2EVHUYHUV

 ' R

LQW

FW2EMH

7KH6XEMHFWDFNQRZOHGJHVWKH 0RXVH·VUHTXHVWDQGUHPRYHVLW IURPWKHVHWRIREVHUYHUV

F ' XFN2EMH

0R

W HF J2EM

FW

0RXVHLVRXWWDKHUH

6X EMH

HF XVH2EM

W

W &D W2EMHF

W

2EVHUYHUV

7KH6XEMHFWKDVDQRWKHU QHZLQW 6X EMH

LQW

FW

$OOWKHREVHUYHUVJHWDQRWKHU QRWLÀFDWLRQH[FHSWIRUWKH 0RXVHZKRLVQRORQJHULQFOXGHG 'RQ·WWHOODQ\RQHEXWWKH0RXVH VHFUHWO\PLVVHVWKRVHLQWV PD\EHLW·OODVNWREHDQREVHUYHU DJDLQVRPHGD\





FW2EMH

 

' R

F ' XFN2EMH

0R

HF XVH2EM

W

J2EM

W HF

W &D W2EMHF

W 2EVHUYHUV

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 12

¿YHPLQXWHGUDPD

.Q^MUQV]\MLZIUI"I[]JRMK\NWZWJ[MZ^I\QWV ,QWRGD\·VVNLWWZRSRVWEXEEOHVRIWZDUHGHYHORSHUV HQFRXQWHUDUHDOOLYHKHDGKXQWHU 7KLVLV 5RQ,·PORRNLQJIRUD -DYDGHYHORSPHQWSRVLWLRQ,·YH JRWÀYH\HDUVH[SHULHQFHDQG

8K\HDK \RXDQGHYHU\ERG\ HOVHEDE\,·PSXWWLQJ \RXRQP\OLVWRI-DYD GHYHORSHUVGRQ·WFDOOPH ,·OOFDOO\RX

 +HDGKXQWHU6XEMHFW

 ,·OODGG\RXWRWKHOLVW \RX·OONQRZDORQJZLWK HYHU\RQHHOVH

6RIWZDUH 'HYHORSHU +L,·P-LOO,·YH ZULWWHQDORWRI(-% V\VWHPV,·PLQWHUHVWHG LQDQ\MRE\RX·YHJRWZLWK -DYDGHYHORSPHQW

  6RIWZDUH 'HYHORSHU 

6XEMHFW

&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 13

WKHREVHUYHUSDWWHUQ



0HDQZKLOHIRU5RQDQG-LOOOLIHJRHV RQLID-DYDMREFRPHVDORQJWKH\·OO JHWQRWLÀHGDIWHUDOOWKH\DUHRE VHUYHUV

7KDQNV,·OO VHQGP\UHVXPH ULJKWRYHU 7KLVJX\LVDUHDOMHUN ZKRQHHGVKLP,·P ORRNLQJIRUP\RZQMRE

+H\ REVHUYHUVWKHUH·V D-DYDRSHQLQJGRZQDW -DYD%HDQV58VMXPS RQLW'RQ·WEORZLW %ZDKDKDPRQH\ LQWKHEDQNEDE\

 2EVHUYHU 2EVHUYHU

 6XEMHFW $UJKKK0DUNP\ ZRUGV-LOO\RX·OOQHYHUZRUN LQWKLVWRZQDJDLQLI,KDYH DQ\WKLQJWRGRZLWKLW
-LOOODQGVKHURZQMRE
2EVHUYHU

!

6XEMHFW \RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Page 14

Return to Table of Contents

WKHREVHUYHUSDWWHUQGH¿QHG

<_W_MMS[TI\MZ

-LOO·VORYLQJOLIHDQGQRORQJHUDQREVHUYHU 6KH·VDOVRHQMR\LQJWKHQLFHIDWVLJQLQJ ERQXVWKDWVKHJRWEHFDXVHWKHFRPSDQ\ GLGQ·WKDYHWRSD\DKHDGKXQWHU

%XWZKDWKDVEHFRPHRIRXUGHDU5RQ":HKHDU KH·VEHDWLQJWKHKHDGKXQWHUDWKLVRZQJDPH +H·VQRWRQO\VWLOODQREVHUYHUKH·VJRWKLVRZQ FDOOOLVWQRZDQGKHLVQRWLI\LQJKLVRZQREVHUYHUV 5RQ·VDVXEMHFWDQGDQREVHUYHUDOOLQRQH



&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 15

WKHREVHUYHUSDWWHUQ



7KH2EVHUYHU3DWWHUQ GHILQHVDRQHWRPDQ\ UHODWLRQVKLSEHWZHHQDVHW RIREMHFWV

,ETSRELATETHISDEFINITIONTOHOWWEVEBEENTALKINGABOUTTHE PATTERN

21(720$1<5(/$7,216+,3

:KHQWKHVWDWHRIRQH REMHFWFKDQJHVDOORILWV GHSHQGHQWVDUHQRWLILHG

2EMHFWWKDW KROGVVWDWH   

' R

J2EM

W HF

 F ' XFN2EMH

0R

$XWRPDWLFXSGDWHQRWLILFDWLRQ

W

W &D W2EMHF

HF XVH2EM

W

2EVHUYHUV

W2EMHFW V

FW2EMH

'HSHQGHQ

LQW

FW



6X EMH

4HESUBJECTANDOBSERVERSDEFINETHEONE TO MANYRELATIONSHIP 4HEOBSERVERSAREDEPENDENTONTHESUBJECTSUCHTHATWHENTHE SUBJECTSSTATECHANGES THEOBSERVERSGETNOTIFIED$EPENDINGON THESTYLEOFNOTIFICATION THEOBSERVERMAYALSOBEUPDATEDWITH NEWVALUES !SYOULLDISCOVER THEREAREAFEWDIFFERENTWAYSTOIMPLEMENT THE/BSERVER0ATTERNBUTMOSTREVOLVEAROUNDACLASSDESIGNTHAT INCLUDES3UBJECTAND/BSERVERINTERFACES ,ETSTAKEALOOK \RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 16

ORRVHFRXSOLQJ


YHUVQHHG $OOSRWHQWLDOREVKHHU2EVHUYHU W QW PH WRLPSOH LQWHUIDFH LQWHUIDFH7KLVKR GXSGDWH  MXVWKDVRQHPHWGZKHQWKH OOH FD V WKDWJHW DQJHV 6XEMHFW·VVWDWHFK

(DFKVXEMHFW FDQKDYHPDQ\ REVHUYHUV OBSERVERS

LQWHUIDFH!!

LQWHUIDFH!!

6XEMHFW

2EVHUYHU

UHJLVWHU2EVHUYHU

XSGDWH

UHPRYH2EVHUYHU QRWLI\2EVHUYHUV

&RQFUHWH6XEMHFW

SUBJECT

XSGDWH

UHPRYH2EVHUYHU ^`

RWKHU2EVHUYHUVSHFLILF

ZD\V QRWLI\2EVHUYHUV ^` $FRQFUHWHVXEMHFWDO W LPSOHPHQWVWKH6XEMHF JHW6WDWH LRQWR LQWHUIDFH,QDGGLW VHW6WDWH YH PR UH G DQ WKHUHJLVWHU W MHF XE HV HW FU PHWKRGVWKHFRQ \2EVHUYHUV  LPSOHPHQWVDQRWLI GWRXSGDWH PHWKRGWKDWLVXVHVHUYHUV HFWPD\ 7KHFRQFUHWHVXRGEMVIRU DOOWKHFXUUHQWRE DQJHV WK PH DOVRKDYH ZKHQHYHUVWDWHFK WLQJLWVVWDWH VHWWLQJDQGJHWLV ODWHU  PRUHDERXWWK

Q:

Q:

A:

A:

7HATDOESTHISHAVETODO WITHONE TO MANYRELATIONSHIPS

7ITHTHE/BSERVERPATTERN THE 3UBJECTISTHEOBJECTTHATCONTAINSTHE STATEANDCONTROLSIT3O THEREIS/.% SUBJECTWITHSTATE4HEOBSERVERS ON THEOTHERHAND USETHESTATE EVEN IFTHEYDONTOWNIT4HEREAREMANY OBSERVERSANDTHEYRELYONTHE3UBJECT TOTELLTHEMWHENITSSTATECHANGES 3OTHEREISARELATIONSHIPBETWEENTHE /.%3UBJECTTOTHE-!.9/BSERVERS



&RQFUHWH2EVHUYHU

UHJLVWHU2EVHUYHU ^`

PHWKRGV

&RQFUHWHREVHUYHUVFDQEH DQ\FODVVWKDWLPSOHPHQWVWKH 2EVHUYHULQWHUIDFH(DFK REVHUYHUUHJLVWHUVZLWKDFRQFUHWH VXEMHFWWRUHFHLYHXSGDWHV

(OWDOESDEPENDENCECOME INTOTHIS

"ECAUSETHESUBJECTISTHESOLE OWNEROFTHATDATA THEOBSERVERSARE DEPENDENTONTHESUBJECTTOUPDATE THEMWHENTHEDATACHANGES4HIS LEADSTOACLEANER//DESIGNTHAN ALLOWINGMANYOBJECTSTOCONTROLTHE SAMEDATA

&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Page 17

Return to Table of Contents

WKHREVHUYHUSDWWHUQ


+RZPDQ\ V GLIIHUHQWNLQG \RX RIFKDQJHFDQ LGHQWLI\KHUH"

?MKIVZM][M[]JRMK\[WZWJ[MZ^MZ[QVLMXMVLMV\TaWN MIKPW\PMZ1N _M PI^MIVW\PMZ][MNWZI[]JRMK\WZIVWJ[MZ^MZ_MKIVMI[QTaZM][M\PMUJMKI][M\PM \_WIZMV¼\\QOP\TaKW]XTML +PIVOM[\WMQ\PMZ\PM[]JRMK\WZIVWJ[MZ^MZ_QTTVW\INNMK\\PMW\PMZ *MKI][M\PM\_WIZMTWW[MTaKW]XTML_MIZMNZMM\WUISMKPIVOM[\WMQ\PMZI[TWVOI[ \PMWJRMK\[[\QTTUMM\\PMQZWJTQOI\QWV[\WQUXTMUMV\\PM[]JRMK\WZWJ[MZ^MZQV\MZNIKM[

'HVLJQ3ULQFLSOH 6WULYHIRUORRVHO\FRXSOHGGHVLJQV EHWZHHQREMHFWVWKDWLQWHUDFW

/RRVHO\FRXSOHGGHVLJQVDOORZXVWREXLOGÁH[LEOH22 V\VWHPVWKDWFDQKDQGOHFKDQJHEHFDXVHWKH\PLQLPL]H WKHLQWHUGHSHQGHQF\EHWZHHQREMHFWV

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Page 18

Return to Table of Contents

SODQQLQJWKHZHDWKHUVWDWLRQ

6KDUSHQ \RXU SHQFLO %HIRUHPRYLQJRQWU\VNHWFKLQJRXWWKHFODVVHV\RX¶OOQHHGWRLPSOHPHQWWKH :HDWKHU6WDWLRQLQFOXGLQJWKH:HDWKHU'DWDFODVVDQGLWVGLVSOD\HOHPHQWV 0DNHVXUH\RXUGLDJUDPVKRZVKRZDOOWKHSLHFHV¿WWRJHWKHUDQGDOVRKRZ DQRWKHUGHYHORSHUPLJKWLPSOHPHQWKHURZQGLVSOD\HOHPHQW ,I\RXQHHGDOLWWOHKHOSUHDGWKHQH[WSDJH\RXUWHDPPDWHVDUHDOUHDG\ WDONLQJDERXWKRZWRGHVLJQWKH:HDWKHU6WDWLRQ



&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Page 19

Return to Table of Contents

WKHREVHUYHUSDWWHUQ

+]JQKTMKWV^MZ[I\QWV %DFNWRWKH:HDWKHU6WDWLRQSURMHFW\RXUWHDPPDWHVKDYH DOUHDG\VWDUWHGWKLQNLQJWKURXJKWKHSUREOHP

6XH

6RKRZDUHZH JRLQJWREXLOGWKLVWKLQJ"

0DU\"?MTTQ\PMTX[\WSVW__M¼ZM][QVO\PM7J[MZ^MZ8I\\MZV 6XH":QOP\J]\PW_LW_MIXXTaQ\' 0DU\"0UU4M\¼[TWWSI\\PMLMÅVQ\QWVIOIQV" 4HE/BSERVER0ATTERNDEFINESAONE TO MANYDEPENDENCYBETWEENOBJECTSSOTHATWHENONE OBJECTCHANGESSTATE ALLITSDEPENDENTSARENOTIFIEDANDUPDATEDAUTOMATICALLY

0DU\"


Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 20

GHVLJQLQJWKHZHDWKHUVWDWLRQ

,M[QOVQVO\PM?MI\PMZ;\I\QWV 0W_LWM[\PQ[LQIOZIUKWUXIZM_Q\PaW]Z['

/HW·VDOVRFUHDWHDQ LQWHUIDFHIRUDOOGLVSOD\ HOHPHQWVWRLPSOHPHQW7KH GLVSOD\HOHPHQWVMXVWQHHGWR LPSOHPHQWDGLVSOD\ PHWKRG

RPSRQHQWV $OORXUZHDWKHUFEVHUYHU 2 KH W QW LPSOHPH JLYHVWKH LQWHUIDFH7KLVRQ LQWHUIDFH PP FR D W HF 6XEM FRPHVWLPH LW Q KH Z WR WRWDON UYHUV VH RE H WRXSGDWHWK

FH LQWHUID  W F H M E LOLDU XUVX +HUH·VRRXOGORRNIDP K V  WKLV

OBSERVERS

LQWHUIDFH!!

LQWHUIDFH!!

LQWHUIDFH!!

'LVSOD\(OHPHQW

2EVHUYHU

6XEMHFW UHJLVWHU2EVHUYHU

GLVSOD\

XSGDWH

UHPRYH2EVHUYHU QRWLI\2EVHUYHUV

&XUUHQW&RQGLWLRQV SU

T BJEC

:HDWKHU'DWD

XSGDWH

7KLUG3DUW\'LVSOD\

GLVSOD\ ^GLVSOD\FXUUHQW

XSGDWH

PHDVXUHPHQWV`

UHJLVWHU2EVHUYHU 

GLVSOD\ ^GLVSOD\

UHPRYH2EVHUYHU 

VRPHWKLQJHOVHEDVHGRQ PHDVXUHPHQWV`

QRWLI\2EVHUYHUV JHW7HPSHUDWXUH JHW+XPLGLW\ JHW3UHVVXUH PHDVXUHPHQWV&KDQJHG

7KLVGLVSOD\HOHPHQW VKRZVWKHFXUUHQW PHDVXUHPHQWVIURPWK :HDWKHU'DWDREMHFW H

:HDWKHU'DWDQRZ LPSOHPHQWVWKH 6XEMHFWLQWHUIDFH

6WDWLVWLFV'LVSOD\ XSGDWH  GLVSOD\ ^GLVSOD\WKHDYHU DJHPLQDQGPD[PHDVXUH PHQWV`

7KLVRQHNHHSVWUDFN RIWKHPLQDYJPD[ PHDVXUHPHQWVDQG GLVSOD\VWKHP

)RUHFDVW'LVSOD\ XSGDWH GLVSOD\ ^GLVSOD\WKH

'HYHORSHUV FDQLPSOHPHQW WKH2EVHUYHU DQG'LVSOD\ LQWHUIDFHVWR FUHDWHWKHLURZQ GLVSOD\HOHPHQW

IRUHFDVW`

HU 7KLVGLVSOD\VKRZVWKHZHDWK HWHU URP ED WKH RQ IRUHFDVWEDVHG 7KHVHWKUHHGLVSOD\HOHPHQWVVKRXOGKDYHDSRLQWHUWR :HDWKHU'DWDODEHOHG´VXEMHFWµWRREXWER\ZRXOG WKLVGLDJUDPVWDUWWRORRNOLNHVSDJKHWWLLIWKH\GLG 

&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 21

WKHREVHUYHUSDWWHUQ

1UXTMUMV\QVO\PM?MI\PMZ;\I\QWV ?M¼ZMOWQVO\W[\IZ\W]ZQUXTMUMV\I\QWV][QVO\PMKTI[[LQIOZIUIVLNWTTW_QVO5IZa IVL;]M¼[TMILNZWUINM_XIOM[JIKSAW]¼TT[MMTI\MZQV\PQ[KPIX\MZ\PI\2I^I XZW^QLM[[WUMJ]QT\QV[]XXWZ\NWZ\PM7J[MZ^MZXI\\MZVPW_M^MZ_M¼ZMOWQVO\WOM\ W]ZPIVL[LQZ\aIVLZWTTW]ZW_VNWZVW_?PQTMQV[WUMKI[M[aW]KIVUISM][MWN  2I^I¼[J]QT\QV[]XXWZ\QVITW\WN KI[M[Q\¼[UWZMÆM`QJTM\WJ]QTLaW]ZW_VIVLQ\¼[ VW\ITT\PI\PIZL;WTM\¼[OM\[\IZ\ML_Q\P\PMQV\MZNIKM["

%RWKRIWKHVHPHWKRGVWDNHDQ

SXEOLFLQWHUIDFH6XEMHFW^ 2EVHUYHUDVDQDUJXPHQWWKDWLVWKH SXEOLFYRLGUHJLVWHU2EVHUYHU 2EVHUYHUR  2EVHUYHUWREHUHJLVWHUHGRUUHPRYHG SXEOLFYRLGUHPRYH2EVHUYHU 2EVHUYHUR  SXEOLFYRLGQRWLI\2EVHUYHUV  7KLVPHWKRGLVFDOOHGWRQRWLI\DOOREVHUYHUV `

ZKHQWKH6XEMHFW·VVWDWHKDVFKDQJHG

SXEOLFLQWHUIDFH2EVHUYHU^ SXEOLFYRLGXSGDWH ÀRDWWHPSÀRDWKXPLGLW\ÀRDWSUHVVXUH  `

7KHVHDUHWKHVWDWHYDOXHVWKH2EVHUYHUVJHWIURP WKH6XEMHFWZKHQDZHDWKHUPHDVXUHPHQWFKDQJHV SXEOLFLQWHUIDFH'LVSOD\(OHPHQW^ SXEOLFYRLGGLVSOD\  `

7KH2EVHUYHULQWHUIDFHLV LPSOHPHQWHGE\DOOREVHUYHUV VRWKH\DOOKDYHWRLPSOHPHQW WKHXSGDWH PHWKRG+HUH ZH·UHIROORZLQJ0DU\DQG 6XH·VOHDGDQGSDVVLQJWKH PHDVXUHPHQWVWRWKHREVHUYHUV

7KH'LVSOD\(OHPHQWLQWHUIDFHMXVWLQFOXGHV RQHPHWKRGGLVSOD\ WKDWZHZLOOFDOOZKHQ WKHGLVSOD\HOHPHQWQHHGVWREHGLVSOD\HG

YiX`e gfn\i 0DU\DQG6XHWKRXJKWWKDWSDVVLQJWKHPHDVXUHPHQWVGLUHFWO\WRWKH REVHUYHUVZDVWKHPRVWVWUDLJKWIRUZDUGPHWKRGRIXSGDWLQJVWDWH'R \RXWKLQNWKLVLVZLVH"+LQWLVWKLVDQDUHDRIWKHDSSOLFDWLRQWKDW PLJKWFKDQJHLQWKHIXWXUH",ILWGLGFKDQJHZRXOGWKHFKDQJHEHZHOO HQFDSVXODWHGRUZRXOGLWUHTXLUHFKDQJHVLQPDQ\SDUWVRIWKHFRGH" &DQ\RXWKLQNRIRWKHUZD\VWRDSSURDFKWKHSUREOHPRISDVVLQJWKH XSGDWHGVWDWHWRWKHREVHUYHUV" 'RQ¶WZRUU\ZH¶OOFRPHEDFNWRWKLVGHVLJQGHFLVLRQDIWHUZH¿QLVKWKH LQLWLDOLPSOHPHQWDWLRQ \RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 22

LPSOHPHQWLQJWKHZHDWKHUVWDWLRQ

1UXTMUMV\QVO\PM;]JRMK\QV\MZNIKMQV ?MI\PMZ,I\I

+HUHZHLPSOHPHQWWKH6XEMHFW,QWHUIDFH

:MUMUJMZW]ZÅZ[\I\\MUX\I\QUXTMUMV\QVO\PM?MI\PMZ,I\IKTI[[I\\PM JMOQVVQVOWN \PMKPIX\MZ'AW]UQOP\_IV\\WZMNZM[PaW]ZUMUWZa6W_ Q\¼[\QUM\WOWJIKSIVLLW\PQVO[_Q\P\PM7J[MZ^MZ8I\\MZVQVUQVL

5(0(0%(5ZHGRQ·WSURYLGH LPSRUWDQGSDFNDJHVWDWHPHQWVLQ WKHFRGHOLVWLQJV*HWWKHFRPSOHWH VRXUFHFRGHIURPWKHZLFNHGO\VPDUW ZHEVLWH
SXEOLFFODVV:HDWKHU'DWDLPSOHPHQWV6XEMHFW^ :HDWKHU'DWDQRZLPSOHPHQWV SULYDWH$UUD\/LVWREVHUYHUV WKH6XEMHFWLQWHUIDFH SULYDWHÀRDWWHPSHUDWXUH SULYDWHÀRDWKXPLGLW\ WR :H·YHDGGHGDQ$UUD\/LVW SULYDWHÀRDWSUHVVXUH ZH DQG UV UYH EVH KROGWKH2  RU FUHDWHLWLQWKHFRQVWUXFW SXEOLF:HDWKHU'DWD ^ REVHUYHUV QHZ$UUD\/LVW  `  :KHQDQREVHUYHUUHJLVWHUVZHMXVW SXEOLFYRLGUHJLVWHU2EVHUYHU 2EVHUYHUR ^ DGGLWWRWKHHQGRIWKHOLVW REVHUYHUVDGG R  ` /LNHZLVHZKHQDQREVHUYHUZDQWVWRXQUHJLVWHU  ZHMXVWWDNHLWRIIWKHOLVW SXEOLFYRLGUHPRYH2EVHUYHU 2EVHUYHUR ^ LQWL REVHUYHUVLQGH[2I R  LI L!  ^ +HUH·VWKHIXQSDUWWKLVLVZKHUHZH REVHUYHUVUHPRYH L  KHREVHUYHUVDERXWWKHVWDWH WHOODOOW ` WKH\DUHDOO2EVHUYHUVZH %HFDXVH ` NQRZWKH\DOOLPSOHPHQWXSGDWH VR  ZHNQRZKRZWRQRWLI\WKHP SXEOLFYRLGQRWLI\2EVHUYHUV ^ IRU LQWL LREVHUYHUVVL]H L ^ 2EVHUYHUREVHUYHU  2EVHUYHU REVHUYHUVJHW L  REVHUYHUXSGDWH WHPSHUDWXUHKXPLGLW\SUHVVXUH  ` UVZKHQ ` I\WKH2EVHUVXYHUHPHQWV WL QR H :  GPHD ZHJHWXSGDWH SXEOLFYRLGPHDVXUHPHQWV&KDQJHG ^ WKHU6WDWLRQ HD : KH W RP IU QRWLI\2EVHUYHUV  `  SXEOLFYRLGVHW0HDVXUHPHQWV ÀRDWWHPSHUDWXUHÀRDWKXPLGLW\ÀRDWSUHVVXUH ^ WKLVWHPSHUDWXUH WHPSHUDWXUH WKLVKXPLGLW\ KXPLGLW\ 2ND\ZKLOHZHZDQWHGWRVKLSDQLFHOLWWOH WKLVSUHVVXUH SUHVVXUH ZHDWKHUVWDWLRQZLWKHDFKERRNWKHSXEOLVKHU PHDVXUHPHQWV&KDQJHG  ZRXOGQ·WJRIRULW6RUDWKHUWKDQUHDGLQJ ` DFWXDOZHDWKHUGDWDRIIDGHYLFHZH·UH  JRLQJWRXVHWKLVPHWKRGWRWHVWRXUGLVSOD\ RWKHU:HDWKHU'DWDPHWKRGVKHUH HOHPHQWV2UIRUIXQ\RXFRXOGZULWHFRGH `

WRJUDEPHDVXUHPHQWVRIIWKHZHE



&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 23

WKHREVHUYHUSDWWHUQ

6W_TM\¼[J]QTL\PW[MLQ[XTIaMTMUMV\[ 6W_\PI\_M¼^MOW\W]Z?MI\PMZ,I\IKTI[[[\ZIQOP\MVMLW]\Q\¼[\QUM\WJ]QTL\PM ,Q[XTIa-TMUMV\[?MI\PMZ7:IUIWZLMZML\PZMM"\PMK]ZZMV\KWVLQ\QWV[LQ[XTIa\PM [\I\Q[\QK[LQ[XTIaIVL\PMNWZMKI[\LQ[XTIa4M\¼[\ISMITWWSI\\PMK]ZZMV\KWVLQ\QWV[ LQ[XTIa#WVKMaW]PI^MIOWWLNMMTNWZ\PQ[LQ[XTIaMTMUMV\KPMKSW]\\PM[\I\Q[\QK[IVL NWZMKI[\LQ[XTIa[QV\PMPMILÅZ[\KWLMLQZMK\WZaAW]¼TT[MM\PMaIZM^MZa[QUQTIZ

2EVHUYHU PSOHPHQWVIURPWKH L \ OD S LV G 7KLV QJHWFKDQJHV VRLWFD 'DWDREMHFW :HDWKHU

,WDOVRLPSO HQWV'LVSOD\( EHFDXVHRXUHP OHPHQW UHTXLUHDOOG $3,LVJRLQJWR LPSOHPHQWWKLLVSOD\HOHPHQWVWR VLQWHUIDFH

SXEOLFFODVV&XUUHQW&RQGLWLRQV'LVSOD\LPSOHPHQWV2EVHUYHU'LVSOD\(OHPHQW^ SULYDWHÀRDWWHPSHUDWXUH SULYDWHÀRDWKXPLGLW\ SULYDWH6XEMHFWZHDWKHU'DWD 7KHFRQVWUXFWRULVSDVVHGWKH  ZHDWKHU'DWDREMHFW WKH6XEMHFW  SXEOLF&XUUHQW&RQGLWLRQV'LVSOD\ 6XEMHFWZHDWKHU'DWD ^ DQGZHXVHLWWRUHJLVWHUWKH WKLVZHDWKHU'DWD ZHDWKHU'DWD GLVSOD\DVDQREVHUYHU ZHDWKHU'DWDUHJLVWHU2EVHUYHU WKLV  `  SXEOLFYRLGXSGDWH ÀRDWWHPSHUDWXUHÀRDWKXPLGLW\ÀRDWSUHVVXUH ^ WKLVWHPSHUDWXUH WHPSHUDWXUH WKLVKXPLGLW\ KXPLGLW\ :KHQXSGDWH LVFDOOHGZH\ GLVSOD\  VDYHWKHWHPSDQGKXPLGLW ` DQGFDOOGLVSOD\   SXEOLFYRLGGLVSOD\ ^ 6\VWHPRXWSULQWOQ ³&XUUHQWFRQGLWLRQV³WHPSHUDWXUH ³)GHJUHHVDQG³KXPLGLW\³KXPLGLW\´  7KHGLVSOD\ PHWKRG ` MXVWSULQWVRXWWKHPRVW `

UHFHQWWHPSDQGKXPLGLW\

WKHUH DUH QR

'XPE 4XHVWLRQV

Q:

)SUPDATE THEBESTPLACETO CALLDISPLAY

THEWAYTHEDATAGETSDISPLAYED7E AREGOINGTOSEETHISWHENWEGETTO THEMODEL VIEW CONTROLLERPATTERN

A:

Q:

)NTHISSIMPLEEXAMPLEITMADE SENSETOCALLDISPLAY WHENTHEVALUES CHANGED(OWEVER YOUARERIGHT THEREAREMUCHBETTERWAYSTODESIGN

7HYDIDYOUSTOREAREFERENCE TOTHE3UBJECT)TDOESNTLOOK LIKEYOUUSEITAGAINAFTERTHE CONSTRUCTOR

A:

4RUE BUTINTHEFUTUREWE MAYWANTTOUN REGISTEROURSELVESAS ANOBSERVERANDITWOULDBEHANDY TOALREADYHAVEAREFERENCETOTHE SUBJECT

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 24

WHVWLQJWKHZHDWKHUVWDWLRQ

8W_MZ]X\PM?MI\PMZ;\I\QWV 

)LUVWOHW·VFUHDWHDWHVWKDUQHVV
WHWKH )LUVWFUHDDW D :HDWKHU'  FW REMH

SXEOLFFODVV:HDWKHU6WDWLRQ^ SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ :HDWKHU'DWDZHDWKHU'DWD QHZ:HDWKHU'DWD   &XUUHQW&RQGLWLRQV'LVSOD\FXUUHQW'LVSOD\   ,I\RXGRQ·W ZDQWWRQHZ&XUUHQW&RQGLWLRQV'LVSOD\ ZHDWKHU'DWD  6WDWLVWLFV'LVSOD\VWDWLVWLFV'LVSOD\ QHZ6WDWLVWLFV'LVSOD\ ZHDWKHU'DWD  KH GRZQORDGW )RUHFDVW'LVSOD\IRUHFDVW'LVSOD\ QHZ)RUHFDVW'LVSOD\ ZHDWKHU'DWD  DQ

FRGH\RXF W FRPPHQWRX ZHDWKHU'DWDVHW0HDVXUHPHQWV I  OLQHV WKHVHWZR ZHDWKHU'DWDVHW0HDVXUHPHQWV I  DQGUXQLW ZHDWKHU'DWDVHW0HDVXUHPHQWV I  ` `



6LPXODWHQHZZHDWKHU PHDVXUHPHQWV

&UHDWHWKHWKUHH GLVSOD\VDQG SDVVWKHPWKH :HDWKHU'DWDREMHFW

5XQWKHFRGHDQGOHWWKH2EVHUYHU3DWWHUQGRLWVPDJLF )LOH(GLW:LQGRZ+HOS6WRUP\:HDWKHU

MDYD:HDWKHU6WDWLRQ &XUUHQWFRQGLWLRQV)GHJUHHVDQGKXPLGLW\ $YJ0D[0LQWHPSHUDWXUH  )RUHFDVW,PSURYLQJZHDWKHURQWKHZD\ &XUUHQWFRQGLWLRQV)GHJUHHVDQGKXPLGLW\ $YJ0D[0LQWHPSHUDWXUH  )RUHFDVW:DWFKRXWIRUFRROHUUDLQ\ZHDWKHU &XUUHQWFRQGLWLRQV)GHJUHHVDQGKXPLGLW\ $YJ0D[0LQWHPSHUDWXUH  )RUHFDVW0RUHRIWKHVDPH 



&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Page 25

Return to Table of Contents

WKHREVHUYHUSDWWHUQ

6KDUSHQ \RXU SHQFLO -RKQQ\+XUULFDQH:HDWKHU25DPD¶V&(2MXVWFDOOHGWKH\FDQ¶WSRVVLEO\VKLSZLWKRXWD+HDW,QGH[ GLVSOD\HOHPHQW+HUHDUHWKHGHWDLOV 7KHKHDWLQGH[LVDQLQGH[WKDWFRPELQHVWHPSHUDWXUHDQGKXPLGLW\WRGHWHUPLQHWKHDSSDUHQW WHPSHUDWXUH KRZKRWLVDFWXDOO\IHHOV 7RFRPSXWHWKHKHDWLQGH[\RXWDNHWKHWHPSHUDWXUH7DQGWKH UHODWLYHKXPLGLW\5+DQGXVHWKLVIRUPXOD

KHDWLQGH[  

  7 5+  7

5+  7  5+ 

7 5+  7 5+  7 5+   7  5+  7 5+   7 5+  7 5+   7 5+  7 5+

6RJHWW\SLQJ -XVWNLGGLQJ'RQ¶WZRUU\\RXZRQ¶WKDYHWRW\SHWKDWIRUPXODLQMXVWFUHDWH\RXURZQ+HDW,QGH['LVSOD\ MDYD¿OHDQGFRS\WKHIRUPXODIURPKHDWLQGH[W[WLQWRLW


G

DQJH WFK  D K Z ·V SXW +HUH KLVRXW QL W

MDYD:HDWKHU6WDWLRQ &XUUHQWFRQGLWLRQV)GHJUHHVDQGKXPLGLW\ $YJ0D[0LQWHPSHUDWXUH  )RUHFDVW,PSURYLQJZHDWKHURQWKHZD\ +HDWLQGH[LV &XUUHQWFRQGLWLRQV)GHJUHHVDQGKXPLGLW\ $YJ0D[0LQWHPSHUDWXUH  )RUHFDVW:DWFKRXWIRUFRROHUUDLQ\ZHDWKHU +HDWLQGH[LV &XUUHQWFRQGLWLRQV)GHJUHHVDQGKXPLGLW\ $YJ0D[0LQWHPSHUDWXUH  )RUHFDVW0RUHRIWKHVDPH +HDWLQGH[LV 

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 26

¿UHVLGHFKDWVXEMHFWDQGREVHUYHU


;]JRMK\

7J[MZ^MZ

1¼UOTIL_M¼ZMÅVITTaOM\\QVOIKPIVKM\WKPI\QV XMZ[WV ?MTT1LWUaRWJLWV¼\1'1IT_Ia[\MTTaW]_PI\¼[ OWQVOWV2][\JMKI][M1LWV¼\ZMITTaSVW__PW aW]IZMLWM[V¼\UMIV1LWV¼\KIZM)VLJM[QLM[1 LWSVW_\PMUW[\QUXWZ\IV\\PQVOIJW]\aW]¸ aW]QUXTMUMV\\PM7J[MZ^MZQV\MZNIKM

:MITTa'1\PW]OP\aW]LQLV¼\KIZMU]KPIJW]\ ][7J[MZ^MZ[

?MTTaMIPJ]\\PI\¼[R][\I[UITTXIZ\WN _PW1 IU)Va_Ia1SVW_ITW\UWZMIJW]\aW]

7PaMIPTQSM_PI\' ?MTTaW]¼ZMIT_Ia[XI[[QVOaW]Z[\I\MIZW]VL \W][7J[MZ^MZ[[W_MKIV[MM_PI\¼[OWQVO WVQV[QLMaW]?PQKPOM\[ITQ\\TMIVVWaQVOI\ \QUM[ ?MTTM`K]]][MUM1PI^M\W[MVLUa[\I\M_Q\PUa VW\QÅKI\QWV[[WITTaW]TIba7J[MZ^MZ[_QTTSVW_ _PI\PIXXMVML 7S_IQ\R][\IUQV]\MPMZM#ÅZ[\_M¼ZMVW\TIba _MR][\PI^MW\PMZ[\]NN \WLWQVJM\_MMVaW]Z WP[WQUXWZ\IV\VW\QÅKI\QWV[5Z;]JRMK\IVL [MKWVL_PaLWV¼\aW]TM\][KWUM\WaW]NWZ \PM[\I\M_M_IV\ZI\PMZ\PIVX][PQVOQ\W]\\W R][\M^MZaWVM' ?MTT1O]M[[\PI\UQOP\_WZS1¼LPI^M\WWXMV Ua[MTN ]XM^MVUWZM\PW]OP\WTM\ITTaW]7J[MZ^MZ[ KWUMQVIVLOM\\PM[\I\M\PI\aW]VMML


&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 27

WKHREVHUYHUSDWWHUQ

;]JRMK\

AM[1KW]TLTM\aW]X]TTUa[\I\M*]\_WV¼\\PI\JM TM[[KWV^MVQMV\NWZaW]'1N aW]PI^M\WKWUM\WUM M^MZa\QUMaW]_IV\[WUM\PQVOaW]UQOP\PI^M\W UISMU]T\QXTMUM\PWLKITT[\WOM\ITT\PM[\I\MaW] _IV\
?MTT1KIV[MM\PMIL^IV\IOM[\WLWQVOQ\JW\P_Ia[ 1PI^MVW\QKML\PI\\PMZMQ[IJ]QT\QV2I^I7J[MZ^MZ 8I\\MZV\PI\ITTW_[aW]\W][MMQ\PMZX][PWZX]TT

7J[MZ^MZ ?PaLWV¼\aW]R][\_ZQ\M[WUMX]JTQKOM\\MZ UM\PWL[\PI\_QTTTM\][X]TTW]\\PM[\I\M_M VMML'

,WV¼\JM[WX][Pa 
7PZMITTa'1\PQVS_M¼ZMOWQVO\WTWWSI\\PI\ VM`\ /ZMI\UIaJM1¼TTOM\\W[MMIOWWLM`IUXTMWN  X]TTIVLKPIVOMUaUQVL ?PI\][IOZMMWV[WUM\PQVO'1O]M[[\PMZM¼[ IT_Ia[PWXM

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 28

MDYD¶VEXLOWLQREVHUYHUSDWWHUQ

=[QVO2I^I¼[J]QT\QV 7J[MZ^MZ8I\\MZV

:LWK-DYD·VEXLOWLQ VXSSRUWDOO\RXKDYHWRGRLV H[WHQG2EVHUYDEOHDQGWHOOLW ZKHQWRQRWLI\WKH2EVHUYHUV 7KH$3,GRHVWKHUHVWIRU\RX

;WNIZ_M¼^MZWTTMLW]ZW_VKWLMNWZ\PM 7J[MZ^MZ8I\\MZVJ]\2I^IPI[J]QT\QV[]XXWZ\ QV[M^MZITWN Q\[)81[
HFODVVNHHSV 7KH2EVHUYDOOEO\RXUREVHUYHUV WUDFNRID VWKHPIRU\RX DQGQRWLILH 2EVHUYDEOH

2EVHUYDEOHLVD DGG2EVHUYHU &/$66QRWDQ GHOHWH2EVHUYHU QRWLI\2EVHUYHUV LQWHUIDFHVR :HDWKHU'DWD VHW&KDQJHG H[WHQGV2EVHUYDEOH

NIDPLOLDU,QDV 7KLVVKRXOGORR WO\WKHVDPH DF IDFWLW·VH[ ODVVGLDJUDP  RXUSUHYLRXVF LQWHUIDFH!!

OBSERVERS

2EVHUYHU XSGDWH

SUB

JEC

T

:HDWKHU'DWD

ORRN JHW+XPLGLW\ RHVQ·WOG G  LV K 7 LDU+R WR JHW3UHVVXUH IDPLO ZH·OOJHW WLJKW DVHF LFKZHFDQ WKLVLQ +HUH·VRXU6XEMHFWEVZKHUYDEOH:H 2 KH QRZDOVRFDOOW JLVWHU UHPRYH  GRQ·WQHHGWKHUH UV PHWKRGV DQGQRWLI\2EVHUYH WKDWEHKDYLRU DQ\PRUHZHLQKHULWV IURPWKHVXSHUFODV

*HQHUDO'LVSOD\ XSGDWH GLVSOD\ 

6WDWLVWLFV'LVSOD\ XSGDWH  GLVSOD\

:HOHIWRXWWKH 'LVSOD\(OHPHQW LQWHUIDFHEXWDOO WKHGLVSOD\VVWLOO LPSOHPHQWLWWRR )RUHFDVW'LVSOD\ XSGDWH GLVSOD\

JHW7HPSHUDWXUH



DNHWRWKHXSGDWH  7KHUHZLOOEHDIHZFKDQJHVWHUYRP EXWEDVLFDOO\LW·V HUV 2EV WH FUH PHWKRGLQWKHFRQ VHUYHULQWHUIDFH 2E PRQ FRP WKHVDPHLGHDZHKDYHD DW·VFDOOHGE\WKH6XEMHFW ZLWKDQXSGDWH PHWKRGWK

&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 29

WKHREVHUYHUSDWWHUQ

0W_2I^I¼[J]QT\QV7J[MZ^MZ8I\\MZV_WZS[
)RUDQ2EMHFWWREHFRPHDQREVHUYHU )[][]ITQUXTMUMV\\PM7J[MZ^MZQV\MZNIKM\PQ[\QUM\PMRI^I]\QT7J[MZ^MZ QV\MZNIKMIVLKITTILL7J[MZ^MZWVIVa7J[MZ^IJTMWJRMK\4QSM_Q[M\WZMUW^M aW]Z[MTN I[IVWJ[MZ^MZR][\KITTLMTM\M7J[MZ^MZ

)RUWKH2EVHUYDEOHWRVHQGQRWLÀFDWLRQV .QZ[\WN ITTaW]VMML\WJM7J[MZ^IJTMJaM`\MVLQVO\PMRI^I]\QT7J[MZ^IJTM []XMZKTI[[.ZWU\PMZMQ\Q[I\_W[\MXXZWKM[[" 
WKDWWKHVWDWHKDVFKDQJHGLQ\RXUREMHFW  7KHQFDOORQHRIWZRQRWLI\2EVHUYHUV PHWKRGV

HLWKHUQRWLI\2EVHUYHUV RUQRWLI\2EVHUYHUV 2EMHFWDUJ

WDNHVDQ 7KLVYHUVLRQGDW DREMHFW \ DU DUELWU HGWR VV SD WKDWJHWV YHUZKHQLW HU EV 2 HDFK LVQRWLILHG

)RUDQ2EVHUYHUWRUHFHLYHQRWLÀFDWLRQV 1\QUXTMUMV\[\PM]XLI\MUM\PWLI[JMNWZMJ]\\PM[QOVI\]ZMWN \PM UM\PWLQ[IJQ\LQNNMZMV\"

GDWDREMHFW

XSGDWH 2EVHUYDEOHR2EMHFWDUJ

7KH6XEMHFWWKDWVHQW WKHQRWLILFDWLRQLVSDVVHG 7KLVZLOOEHWKHGDWDREMHFWWKDWZDV SDVVHGWRQRWLI\2EVHUYHUV RUQXOOLI LQDVWKLVDUJXPHQW DGDWDREMHFWZDVQ·WVSHFLILHG 1N aW]_IV\\W¹X][PºLI\I\W\PMWJ[MZ^MZ[aW]KIVXI[[\PMLI\II[ILI\IWJRMK\ \W\PMVW\QNa7J[MZ^MZIZOUM\PWL1N VW\\PMV\PM7J[MZ^MZPI[\W¹X]TTº\PMLI\I Q\_IV\[NZWU\PM7J[MZ^IJTMWJRMK\XI[[ML\WQ\0W_'4M\¼[ZM_WZS\PM?MI\PMZ ;\I\QWVIVLaW]¼TT[MM

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Page 30

Return to Table of Contents

EHKLQGWKHVFHQHV

:DLWEHIRUHZHJHW WRWKDWZK\GRZHQHHGWKLV VHW&KDQJHG PHWKRG":HGLGQ·W QHHGWKDWEHIRUH


%HKLQG WKH6FHQHV VHW&KDQJHG ^ FKDQJHG WUXH `

WKH IRUV H G R F V R OD 3VHXGHUYDEOH& 2EV

QRWLI\2EVHUYHUV 2EMHFWDUJ ^ LI FKDQJHG ^ IRUHYHU\REVHUYHURQWKHOLVW^ FDOOXSGDWH WKLVDUJ ` FKDQJHG IDOVH ` `

G 7KHVHW&KDQJHG PHWKR  VHWVDFKDQJHGIODJWRWUXH QRWLI\2EVHUYHUV RQO\LI QRWLILHVLWVREVHUYHUV58( WKHFKDQJHGIODJLV7

$QGDIWHULWQRWLILHV WKHREVHUYHUVLWVHWVWKH FKDQJHGIODJEDFNWRIDOVH

QRWLI\2EVHUYHUV ^ QRWLI\2EVHUYHUV QXOO `

?PaQ[\PQ[VMKM[[IZa'
&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 31

WKHREVHUYHUSDWWHUQ

:M_WZSQVO\PM?MI\PMZ;\I\QWV_Q\P\PMJ]QT\QV[]XXWZ\ )LUVWOHW·VUHZRUN:HDWKHU'DWDWRXVH MDYDXWLO2EVHUYDEOH



0DNHVXUHZHDUHLPSRUWLQJWKH ULJKW2EVHUYHU2EVHUYDEOH





:HDUHQRZ VXEFODVVLQJ2EVHUYDEOH

:HGRQ·WQHHGWRNHHSWUDFNRI RXUREVHUYHUVDQ\PRUHRUPDQDJH WKHLUUHJLVWUDWLRQDQGUHPRYDO WKHVXSHUFODVVZLOOKDQGOHWKDW  VRZH·YHUHPRYHGWKHFRGHIRU UHJLVWHUDGGDQGQRWLI\

LPSRUWMDYDXWLO2EVHUYDEOH LPSRUWMDYDXWLO2EVHUYHU  SXEOLFFODVV:HDWKHU'DWDH[WHQGV2EVHUYDEOH^ SULYDWHÀRDWWHPSHUDWXUH  2XUFRQVWUXFWRUQRORQJHU SULYDWHÀRDWKXPLGLW\ QHHGVWRFUHDWHDGDWD SULYDWHÀRDWSUHVVXUH VWUXFWXUHWRKROG2EVHUYHUV  SXEOLF:HDWKHU'DWD ^` 1RWLFHZHDUHQ·WVHQGLQJDGDWDREMHFWZLWK  WKHQRWLI\2EVHUYHUV FDOO7KDWPHDQV SXEOLFYRLGPHDVXUHPHQWV&KDQJHG ^ VHW&KDQJHG  ZH·UHXVLQJWKH38//PRGHO QRWLI\2EVHUYHUV  `  SXEOLFYRLGVHW0HDVXUHPHQWV ÀRDWWHPSHUDWXUHÀRDWKXPLGLW\ÀRDWSUHVVXUH ^ WKLVWHPSHUDWXUH WHPSHUDWXUH WKLVKXPLGLW\ KXPLGLW\ WKLVSUHVVXUH SUHVVXUH  :HQRZILUVWFDOOVHW&KDQJHG WR PHDVXUHPHQWV&KDQJHG  LQGLFDWHWKHVWDWHKDVFKDQJHG ` EHIRUHFDOOLQJQRWLI\2EVHUYHUV   SXEOLFÀRDWJHW7HPSHUDWXUH ^ UHWXUQWHPSHUDWXUH `  SXEOLFÀRDWJHW+XPLGLW\ ^ UHWXUQKXPLGLW\ ` 7KHVHPHWKRGVDUHQ·WQHZEXW   EHFDXVHZHDUHJRLQJWRXVH´SXOOµ SXEOLFÀRDWJHW3UHVVXUH ^ ZHWKRXJKWZH·GUHPLQG\RX UHWXUQSUHVVXUH ` WKH\DUHKHUH7KH2EVHUYHUV ` ZLOOXVHWKHPWRJHWDWWKH



:HDWKHU'DWDREMHFW·VVWDWH

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 32

FXUUHQWFRQGLWLRQVUHZRUN

1RZOHW·VUHZRUNWKH&XUUHQW&RQGLWLRQV'LVSOD\



$JDLQPDNHVXUHZHDUHLPSRUWLQJ WKHULJKW2EVHUYHU2EVHUYDEOH 

:HQRZDUHLPSOHPHQWLQJWKH2EVHUYHULQWHUIDFHIURPMDYDXWLO

LPSRUWMDYDXWLO2EVHUYDEOH LPSRUWMDYDXWLO2EVHUYHU  SXEOLFFODVV&XUUHQW&RQGLWLRQV'LVSOD\LPSOHPHQWV2EVHUYHU'LVSOD\(OHPHQW^ 2EVHUYDEOHREVHUYDEOH SULYDWHÀRDWWHPSHUDWXUH  2XUFRQVWUXFWRUQRZWDNHVDQ SULYDWHÀRDWKXPLGLW\ 2EVHUYDEOHDQGZHXVHWKLVWR  DGGWKHFXUUHQWFRQGLWLRQV SXEOLF&XUUHQW&RQGLWLRQV'LVSOD\ 2EVHUYDEOHREVHUYDEOH ^ REMHFWDVDQ2EVHUYHU WKLVREVHUYDEOH REVHUYDEOH REVHUYDEOHDGG2EVHUYHU WKLV  `   :H·YHFKDQJHGWKH SXEOLFYRLGXSGDWH 2EVHUYDEOHREV2EMHFWDUJ ^ XSGDWH PHWKRG LI REVLQVWDQFHRI:HDWKHU'DWD ^ WRW DNHERWKDQ :HDWKHU'DWDZHDWKHU'DWD  :HDWKHU'DWD REV 2EVHUYDEOHDQGWKH WKLVWHPSHUDWXUH ZHDWKHU'DWDJHW7HPSHUDWXUH  RSWLRQDOGDWDDUJXPHQW WKLVKXPLGLW\ ZHDWKHU'DWDJHW+XPLGLW\  GLVSOD\  ` `  SXEOLFYRLGGLVSOD\ ^  ,QXSGDWH ZHILUVW 6\VWHPRXWSULQWOQ ³&XUUHQWFRQGLWLRQV³WHPSHUDWXUH PDNHVXUHWKHREVHUYDEOH ³)GHJUHHVDQG³KXPLGLW\³KXPLGLW\´  LVRIW\SH:HDWKHU'DWD ` DQGWKHQZHXVHLWV `

JHWWHUPHWKRGVWR REWDLQWKHWHPSHUDWXUH DQGKXPLGLW\ PHDVXUHPHQWV$IWHU WKDWZHFDOOGLVSOD\ 



&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 33

WKHREVHUYHUSDWWHUQ

&RGH0DJQHWV ([HUFLVH

4HE&ORECAST$ISPLAYCLASSISALLSCRAMBLEDUPONTHEFRIDGE#ANYOU RECONSTRUCTTHECODESNIPPETSTOMAKEITWORK3OMEOFTHECURLY BRACESFELLONTHEFLOORANDTHEYWERETOOSMALLTOPICKUP SOFEEL FREETOADDASMANYOFTHOSEASYOUNEED  EOH UYD EVH 2 D\ VSO W'L FDV H U )R ^ OLF GLVSOD\  SXE UYDEOH H REV

REVHUY DEOHDGG2EVHUY HU WKLV 

LI REVHUYDEOHLQVWDQFHRI:HDWKHU'DWD ^

PSOHPHQWV SXEOLFFODVV)RUHFDVW'LVSOD\L ^ PHQW D\(OH 'LVSO YHU 2EVHU ^ GGLVSOD\ SXEOLFYRL RGHKHUH F D\ SO LV G `

 VXUH  VXUH JHW3UHV V H U QW3 U'DWD H XUUH  F ZHDWK H U X  V H V U H X U V 3 ODVW HQW3UHV FXUU

SULYDWHÀRD WFXUUHQW3UH VVXUH  SULYDWHÀRD I WODVW3UHVVX UH :HDWKHU'DWDZH DWKHU'DWD  :HDWKHU'DWD R EVHUYDEOH

DEOH YDEOHREVHUY XSGDWH 2EVHU SXEOLFYRLG UJ ^ 2EMHFWD

EVHUYDEOH LPSRUWMDYDXWLO2 EVHUYHU LPSRUWMDYDXWLO2 \RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Page 34

Return to Table of Contents

WHVWGULYH

:]VVQVO\PMVM_KWLM -XVWWREHVXUHOHW·VUXQWKHQHZFRGH )LOH(GLW:LQGRZ+HOS7U\7LKLV$W+RPH

MDYD:HDWKHU6WDWLRQ )RUHFDVW,PSURYLQJZHDWKHURQWKHZD\ $YJ0D[0LQWHPSHUDWXUH  &XUUHQWFRQGLWLRQV)GHJUHHVDQGKXPLGLW\ )RUHFDVW:DWFKRXWIRUFRROHUUDLQ\ZHDWKHU $YJ0D[0LQWHPSHUDWXUH  &XUUHQWFRQGLWLRQV)GHJUHHVDQGKXPLGLW\ )RUHFDVW0RUHRIWKHVDPH $YJ0D[0LQWHPSHUDWXUH  &XUUHQWFRQGLWLRQV)GHJUHHVDQGKXPLGLW\ 

+PPGR\RXQRWLFHDQ\WKLQJGLIIHUHQW"/RRNDJDLQ AW]¼TT[MMITT\PM[IUMKITK]TI\QWV[J]\Ua[\MZQW][Ta\PMWZLMZWN \PM\M`\W]\X]\Q[ LQNNMZMV\?PaUQOP\\PQ[PIXXMV'
1HYHUGHSHQGRQRUGHURIHYDOXDWLRQRIWKH 2EVHUYHUQRWLÀFDWLRQV


&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Page 35

Return to Table of Contents

WKHREVHUYHUSDWWHUQ

'RHVQ·W MDYDXWLO2EVHUYDEOH YLRODWHRXU22GHVLJQSULQFLSOH RISURJUDPPLQJWRLQWHUIDFHV QRWLPSOHPHQWDWLRQV"


2EVHUYDEOHLVDFODVV AW]ITZMILaSVW_NZWUW]ZXZQVKQXTM[\PQ[Q[IJILQLMIJ]\_PI\PIZULWM[Q\ZMITTa KI][M' .QZ[\JMKI][M7J[MZ^IJTMQ[IKTI[[aW]PI^M\W[]JKTI[[Q\
2EVHUYDEOHSURWHFWVFUXFLDOPHWKRGV 1N aW]TWWSI\\PM7J[MZ^IJTM)81\PM[M\+PIVOMLUM\PWLQ[XZW\MK\ML;W_PI\'?MTT \PQ[UMIV[aW]KIV¼\KITT[M\+PIVOML]VTM[[aW]¼^M[]JKTI[[ML7J[MZ^IJTM
:KDWWRGR" 7J[MZ^IJTMUIa[MZ^MaW]ZVMML[QN aW]KIVM`\MVLRI^I]\QT7J[MZ^IJTM7V\PMW\PMZ PIVLaW]UIaVMML\WZWTTaW]ZW_VQUXTMUMV\I\QWVI[_MLQLI\\PMJMOQVVQVOWN \PM KPIX\MZ1VMQ\PMZKI[MaW]SVW_\PM7J[MZ^MZ8I\\MZV_MTTIVLaW]¼ZMQVIOWWLXW[Q\QWV \W_WZS_Q\PIVa)81\PI\UISM[][MWN \PMXI\\MZV

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 36

REVHUYHUDQGVZLQJ

7\PMZXTIKM[aW]¼TTNQVL\PM7J[MZ^MZ8I\\MZV QV\PM2,3
$OLWWOHEDFNJURXQG

ERXW ,I\RX·UHFXULRXVDWH UQLQ DW U3 YH HU EV WKH2 WKH W RX FN KH VF -DYD%HDQ HQHU 3URSHUW\&KDQJH/LVW LQWHUIDFH

4M\¼[\ISMITWWSI\I[QUXTMXIZ\WN \PM;_QVO)81\PM2*]\\WV1N aW]TWWS]VLMZ \PMPWWLI\2*]\\WV¼[[]XMZKTI[[)J[\ZIK\*]\\WVaW]¼TT[MM\PI\Q\PI[ITW\WN ILL ZMUW^MTQ[\MVMZUM\PWL[
$OLWWOHOLIHFKDQJLQJDSSOLFDWLRQ 7SIaW]ZIXXTQKI\QWVQ[XZM\\a[QUXTMAW]¼^MOW\IJ]\\WV\PI\[Ia[¹;PW]TL1LW Q\'ºIVL_PMVaW]KTQKSWV\PI\J]\\WV\PMTQ[\MVMZ[WJ[MZ^MZ[OM\\WIV[_MZ\PM Y]M[\QWVQVIVa_Ia\PMa_IV\?M¼ZMQUXTMUMV\QVO\_W[]KPTQ[\MVMZ[KITTML\PM )VOMT4Q[\MVMZIVL\PM,M^QT4Q[\MVMZ0MZM¼[PW_\PMIXXTQKI\QWVJMPI^M["

UIDFH +HUH·VRXUIDQF\LQWH

WZKHQ $QGKHUH·VWKHRXWXWSXWRQ E KH ZHFOLFNRQW )LOH(GLW:LQGRZ+HOS+H0DGH0H'R,W

'HYLODQVZHU $QJHODQVZHU 

MDYD6ZLQJ2EVHUYHU([DPSOH &RPHRQGRLW 'RQ¶WGRLW\RXPLJKWUHJUHWLW 

&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 37

WKHREVHUYHUSDWWHUQ

)VL\PMKWLM
SSOLFDWLRQWKDW 6LPSOH6ZLQJDI UDPHDQG MXVWFUHDWHVD QLQLW WR XW E WKURZVD

SXEOLFFODVV6ZLQJ2EVHUYHU([DPSOH^ -)UDPHIUDPH  SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ 6ZLQJ2EVHUYHU([DPSOHH[DPSOH QHZ6ZLQJ2EVHUYHU([DPSOH  H[DPSOHJR  `  SXEOLFYRLGJR ^ 0DNHVWKHGHYLODQG IUDPH QHZ-)UDPH  -%XWWRQEXWWRQ QHZ-%XWWRQ ³6KRXOG,GRLW"´  DQJHOREMHFWVOLVWHQHUV EXWWRQDGG$FWLRQ/LVWHQHU QHZ$QJHO/LVWHQHU  REVHUYHUV RIWKHEXWWRQ EXWWRQDGG$FWLRQ/LVWHQHU QHZ'HYLO/LVWHQHU  IUDPHJHW&RQWHQW3DQH DGG %RUGHU/D\RXW&(17(5EXWWRQ  6HWIUDPHSURSHUWLHVKHUH `  FODVV$QJHO/LVWHQHULPSOHPHQWV$FWLRQ/LVWHQHU^ SXEOLFYRLGDFWLRQ3HUIRUPHG $FWLRQ(YHQWHYHQW ^ 6\VWHPRXWSULQWOQ ³'RQ¶WGRLW\RXPLJKWUHJUHWLW´  ` +HUHDUHW `

WKHREVHUYHUKHFODVVGHILQLWLRQVIRU

FODVV'HYLO/LVWHQHULPSOHPHQWV$FWLRQ/LVWHQHU^ FODVVHV EXWWVGHILQHGDVLQQHU  KH\GRQ·WKDY SXEOLFYRLGDFWLRQ3HUIRUPHG $FWLRQ(YHQWHYHQW ^ HWREH  6\VWHPRXWSULQWOQ ³&RPHRQGRLW´  ` ` ` XSGDWH WKH

5DWKHUWKDQ DFWLRQ3HUIRUPHG PHWKRG JHWVFDOOHGZKHQWKHVWDWH LQWKHVXEMHFW LQWKLVFDVH WKHEXWWRQ FKDQJHV

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 38

\RXUGHVLJQWRROER[


:MDD=LHGAFLK

ƒ 7KH2EVHUYHU3DWWHUQGHILQHV DRQHWRPDQ\UHODWLRQVKLS EHWZHHQREMHFWV

ƒ 6XEMHFWVRUDVZHDOVRNQRZ WKHP2EVHUYDEOHVXSGDWH 2EVHUYHUVXVLQJDFRPPRQ LQWHUIDFH

22%DVLFV V

223ULQFLSOH

ƒ 2EVHUYHUVDUHORRVHO\FRXSOHG

$EVWUDFWLRQ Q (QFDSVXODWLR P LV SK 3RO\PRU FH V ,QKHULWHQ

ZKDWYDULH (QFDSVXODWH  RVLWLRQRYHU )DYRUFRPS LQKHULWDQFH RW QWHUIDFHVQ L R W P D U J R 3U LRQV LPSOHPHQWDW G RRVHO\FRXSOH 6WULYHIRUOZHHQREMHFWVWKDW GHVLJQVEHW LQWHUDFW

LQWKDWWKH2EVHUYDEOHNQRZV QRWKLQJDERXWWKHPRWKHU WKDQWKDWWKH\LPSOHPHQWWKH 2EVHUYHU,QWHUIDFH

ƒ
HVW +HUH·V\RXUQHZHPEHU SULQFLSOH5HPGGHVLJQVDUH ORRVHO\FRXSOH H[LEOHDQG PXFKPRUHIOFKDQJH UHVLOLHQWWR

223DWWHUQDVIDPLO\RIDOJRULWKPV PDQ\

HILQHV WR HIDNLQHVHVWDKHRPQHRUVLWVRKPW KDW 6WUDWHJ\GHDFUKYRQHUH GP DQG W V H OJ F E HV D MH 2 E DW WKH HQFDSVXO \EWHJHW\ZOHHWHVQR DOOLWV DEHOHQGH6QWFUD WJKDHVWXVWVHDLWWH WHG Q V D K QW F OLH LQWHUFKDQGJHHS WQO\HR W F F P MH E DQGXSGD ZKHQHGQHQR IUR YDU\LQGHS DUHQRWLILHG V W Q H G Q H S H G \ DXWRPDWLFDOO

$QHZSDWWHUQIRUFRPPXQLFDWLQJVWDWHWRD VHWRIREMHFWVLQDORRVHO\FRXSOHGPDQQHU:H KDYHQ·WVHHQWKHODVWRIWKH2EVHUYHU3DWWHUQ MXVWZDLWXQWLOZHWDONDERXW09& 

ƒ 'RQ·WGHSHQGRQDVSHFLILF RUGHURIQRWLILFDWLRQIRU\RXU 2EVHUYHUV

ƒ -DYDKDVVHYHUDO LPSOHPHQWDWLRQVRIWKH 2EVHUYHU3DWWHUQLQFOXGLQJ WKHJHQHUDOSXUSRVHMDYDXWLO 2EVHUYDEOH

ƒ :DWFKRXWIRULVVXHVZLWK WKHMDYDXWLO2EVHUYDEOH LPSOHPHQWDWLRQ

ƒ 'RQ·WEHDIUDLGWRFUHDWH \RXURZQ2EVHUYDEOH LPSOHPHQWDWLRQLIQHHGHG

ƒ 6ZLQJPDNHVKHDY\XVHRIWKH 2EVHUYHU3DWWHUQDVGRPDQ\ *8,IUDPHZRUNV

ƒ
&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 39

WKHREVHUYHUSDWWHUQ

([HUFLVH

'HVLJQ3ULQFLSOH&KDOOHQJH &OREACHDESIGNPRINCIPLE DESCRIBEHOWTHE/BSERVER0ATTERN MAKESUSEOFTHEPRINCIPLE

'HVLJQ3ULQFLSOH ,GHQWLI\WKHDVSHFWVRI\RXUDSSOLFDWLRQWKDWYDU\ DQGVHSDUDWHWKHPIURPZKDWVWD\VWKHVDPH

'HVLJQ3ULQFLSOH 3URJUDPWRDQLQWHUIDFHQRWDQLPSOHPHQWDWLRQ

7KLVLVDKDUGRQHKLQWWKLQNDERXWKRZREVHUYHUV DQGVXEMHFWVZRUNWRJHWKHU 'HVLJQ3ULQFLSOH )DYRUFRPSRVLWLRQRYHULQKHULWDQFH

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 40

FURVVZRUGSX]]OH






















 







 

















  0#/3 *# '0   ,-1 , ',1#/$!#  #3'* ," ,%#* /#  1- 1&# 211-,

 +.*#+#,1 1&'0 +#1&-" 1- %#1 ,-1'$'#"  '** %-1 -,# -$ &#/ -4,  2//#,1-,"'1'-,0'0.*6 '+.*#+#,10 1&'0 ',1#/$!#  -4 1- %#1 6-2/0#*$ -$$ 1&#  0#/3#/ *'01  -2 $-/%-1 1&'0 '$ 6-2/# ,-1 %#11',% ,-1'$'#" 4&#, 6-2 1&',) 6-2 0&-2*" #   ,# 2 (#!1 *')#0 1- 1*) 1-  - 0#/3#/0  -,1 !-2,1 -, 1&'0 $-/ ,-1'$'!1'-,  #+.#/12/# &2+'"'16 ,"    0#/3#/0 /#  -, 1&# 2 (#!1  /-%/+ 1- ,  ,-1 , '+.*#+#,11'-,   2 (#!1 '0 0'+'*/ 1-  

 -, 40 -1& ,  0#/3#/ ,"    -2 4,1 1- )##. 6-2/ !-2.*',% 

 # 060 6-2 0&-2*" %- $-/ '1   !, +,%# 6-2/ - 0#/3#/0 $-/ 6-2  3 $/+#4-/) 4'1& *-10 -$  0#/3#/0  #1&#/+0  ,+#" $1#/ 1&'0 )'," -$ 01-/+    0#/3#/0 *')# 1- #  4&#, 0-+#1&',% ,#4 &..#,0   &# #1&#/1 !*00  1&# 2 (#!1 ',1#/$!#   # "'",1 4,1 ,6 +-/# ',10 0- &# /#+-3#" &'+0#*$    *+-01 $-/%-1 1&#  ',"#5 "'0.*6  2 (#!1 ','1'**6 4,1#" 1-  ** 1&# "1 1-  0#/3#/

&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 41

WKHREVHUYHUSDWWHUQ

([HUFLVH VROXWLRQV

6KDUSHQ \RXU SHQFLO *I[MLWVW]ZÅZ[\QUXTMUMV\I\QWV_PQKPWN \PMNWTTW_QVOIXXTa' +PWW[MITT\PI\IXXTa

R) ?MIZMKWLQVO\WKWVKZM\M

R,
QUXTMUMV\I\QWV[VW\QV\MZNIKM[

R* .WZM^MZaVM_LQ[XTIaMTMUMV\_MVMML

R- ?MPI^MV¼\MVKIX[]TI\ML_PI\KPIVOM[ 

\WIT\MZKWLM

R+ ?MPI^MVW_Ia\WILLLQ[XTIa

R. ?MIZM^QWTI\QVOMVKIX[]TI\QWVWN \PM

MTMUMV\[I\Z]V\QUM

?MI\PMZ,I\IKTI[[

'HVLJQ 3ULQFLSOH &KDOOHQJH 'HVLJQ3ULQFLSOH ,GHQWLI\WKHDVSHFWVRI\RXUDSSOLFDWLRQWKDW YDU\DQGVHSDUDWHWKHPIURPZKDWVWD\VWKH VDPH

'HVLJQ3ULQFLSOH 3URJUDPWRDQLQWHUIDFHQRWDQLPSOHPHQWDWLRQ

'HVLJQ3ULQFLSOH )DYRUFRPSRVLWLRQRYHULQKHULWDQFH

7KHWKLQJWKDWYDULHVLQWKH2EVHUYHU3DWWHUQ LVWKHVWDWHRIWKH6XEMHFWDQGWKHQXPEHUDQG W\SHVRI2EVHUYHUV:LWKWKLVSDWWHUQ\RXFDQ YDU\WKHREMHFWVWKDWDUHGHSHQGHQWRQWKHVWDWH RIWKH6XEMHFWZLWKRXWKDYLQJWRFKDQJHWKDW 6XEMHFW7KDW·VFDOOHGSODQQLQJDKHDG

%RWKWKH6XEMHFWDQG2EVHUYHUXVHLQWHUIDFHV 7KH6XEMHFWNHHSVWUDFNRIREMHFWVLPSOHPHQW LQJWKH2EVHUYHULQWHUIDFHZKLOHWKHREVHUYHUV UHJLVWHUZLWKDQGJHWQRWLILHGE\WKH6XEMHFW LQWHUIDFH$VZH·YHVHHQWKLVNHHSVWKLQJVQLFH DQGORRVHO\FRXSOHG

7KH2EVHUYHU3DWWHUQXVHVFRPSRVLWLRQWRFRPSRVH DQ\QXPEHURI2EVHUYHUVZLWKWKHLU6XEMHFWV 7KHVHUHODWLRQVKLSVDUHQ·WVHWXSE\VRPHNLQGRI LQKHULWDQFHKLHUDUFK\1RWKH\DUHVHWXSDW UXQWLPHE\FRPSRVLWLRQ

\RXDUHKHUH



Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 2. Keeping your Objects in the know

Return to Table of Contents

Page 42

H[HUFLVHVROXWLRQV

&RGH0DJQHWV

([HUFLVH VROXWLRQV

HUYDEOH LPSRUWMDYDXWLO2EV HUYHU LPSRUWMDYDXWLO2EV

HQWV SXEOLFFODVV)RUHFDVW'LVSOD\LPSOHP 2EVHUYHU'LVSOD\(OHPHQW^ SULYDWHÀRDW FXUUHQW3UHV VXUH  SULYDWHÀRDW I ODVW3UHVVXU H

VHUYDEOH SXEOLF)RUHFDVW'LVSOD\ 2E REVHUYDEOH ^

:HDWKHU'DWDZHD WKHU'DWD  :HDWKHU'DWD RE VHUYDEOH REVHUYD EOHDGG2EVHUYHU WKLV  ` DEOH YDEOHREVHUY XSGDWH 2EVHU SXEOLFYRLG ^ UJ WD 2EMHF LI REVHUYDEOHLQVWDQFHRI:HDWKHU'DWD ^

ODVW3UHVVXUH FXUUHQW3UHVVXUH FXUUHQW3UHVVXUH ZHDWKHU'DWDJHW3UHVVXUH 

GLVSOD\ 

`

`

^ GGLVSOD\ SXEOLFYRL KHUH SOD\FRGH GLV



`











`  









#

"









!







#

 















!











!





#

!



 



!





















#









#































!

"























 

 









"



%







 "





 





 

 







 





!

$ 













 



















"













! 



"













&KDSWHU

Chapter 2. Keeping your Objects in the know Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

3 the DecoratorPattern

g Decorating

h

Objects g

I used to think real men subclassed everything. That was until I learned the power of extension at runtime, rather than at compile time. Now look at me!

Just call this chapter “Design Eye for the Inheritance Guy.” We’ll re-examine the typical overuse of inheritance and you’ll learn how to decorate your classes at runtime using a form of object composition. Why? Once you know the techniques of decorating, you’ll be able to give your (or someone else’s) objects new responsibilities without making any code changes to the underlying classes.

this is a new chapter

79

the starbuzz story

Welcome to Starbuzz Coffee Starbuzz Coffee has made a name for itself as the fastest growing coffee shop around. If you’ve seen one on your local corner, look across the street; you’ll see another one. Because they’ve grown so quickly, they’re scrambling to update their ordering systems to match their beverage offerings. When they first went into business they designed their classes like this...

stract class, Beverage is an abbe verages subclassed by all ff ee shop. offered in the co

The description instance variable is set in each subclass and holds a description of the beverage, like “Most Excellent Dark Roast”. The getDescription() method returns the description.

Beverage description

The cost() method is abstract; subclassses need to define their own implementation.

getDescription() cost() // Other useful methods...

DarkRoast

HouseBlend cost()

cost()

Decaf cost()

Espresso cost()

Each subclass implements cost() to return the cost of the beverage.

80

Chapter 3

the decorator pattern

In addition to your coffee, you can also ask for several condiments like steamed milk, soy, and mocha (otherwise known as chocolate), and have it all topped off with whipped milk. Starbuzz charges a bit for each of these, so they really need to get them built into their order system. Here’s their first attempt... Beverage description getDescription() cost() // Other useful methods...

DarkRoastWithSteamedMilk andMocha

HouseBlendWithSteamedMilk andMocha HouseBlendWithSteamedMilk cost() andCaramel HouseBlendWithWhipandMocha

cost()

cost()

cost()

DecafWithSteamedMilk andCaramel

DarkRoastWithSteamedMilk andCaramel

cost()

HouseBlendWithMocha cost() HouseBlendWithSoyandMocha

EspressoWithSteamedMilk andMocha

DecafWithSteamedMilk andMocha

cost() DarkRoastWithWhipandMocha

cost()

EspressoWithSteamedMilk andCaramel

EspressoWithWhipandMocha

cost()

DecafWithWhipandMocha cost()

cost()

HouseBlendWithSteamedMilk andSoycost() cost()

HouseBlendWithWhip

cost()

cost()

HouseBlendWithSoy cost()

DarkRoastWithSteamedMilk andSoy

HouseBlendWithSteamedMilk cost() cost()

cost() DarkRoastWithSteamedMilk

cost() HouseBlendWithSteamedMilk andWhip

cost() HouseBlendWithWhipandSoy

cost()

EspressoWithSteamedMilk DecafWithSoy andSoy DecafWithSteamedMilk cost() andSoy EspressoWithSteamedMilk cost()

DarkRoastWithSoy cost() DecafWithSteamedMilk cost() DecafWithSoyandMocha DarkRoastWithSoy DarkRoastWithSoyandMocha cost() DecafWithSoy cost() DecafWithSoyandMocha cost() EspressoWhip cost() cost()

cost()

DecafWithWhip cost()

DarkRoastWithWhip

cost()

cost()

EspressoWithSteamedMilk andWhip DecafWithSteamedMilk DarkRoastWithSteamedMilk cost() andWhip andWhip EspressoWithWhipandSoy DecafWithWhipandSoy cost() DarkRoastWithWhipandSoy cost() cost()

cost()

EspressoWithMocha

cost() DecafWithMocha

cost()

DarkRoastWithMocha

cost() cost()

Whoa! Can you say “class explosion?”

cost()

mputes the Each cost method coalo ng with the cost of the coffee the order. other condiments in

you are here 4

81

violating design principles

A

brain power

It’s pretty obvious that Starbuzz has created a maintenance nightmare for themselves. What happens when the price of milk goes up? What do they do when they add a new caramel topping? Thinking beyond the maintenance problem, which of the design principles that we’ve covered so far are they violating? Hint: they’re violating two of them in a big way! This is stupid; why do we need all these classes? Can’t we just use instance variables and inheritance in the superclass to keep track of the condiments?

Well, let’s give it a try. Let’s start with the Beverage base class and add instance variables to represent whether or not each beverage has milk, soy, mocha and whip...

Beverage description milk soy mocha whip getDescription() cost() hasMilk() setMilk() hasSoy() setSoy() hasMocha() setMocha() hasWhip() setWhip() // Other useful methods..

82

Chapter 3

New boolean values for each condiment. Now we’ll implement cost() in Beverage (instead of keeping it abstract), so that it can calculate the costs associated with the condiments for a particular beverage instance. Subclasses will still override cost(), but they will also invoke the super version so that they can calculate the total cost of the basic beverage plus the costs of the added condiments.

e boolean These get and setndthiments. values for the co

the decorator pattern Beverage

Now let’s add in the subclasses, one for each beverage on the menu:

e st() will calculate th The superclass coth ile wh , condiments costs for all of ste() in the subclasses the overridden cofunctionality to will extend that that specific include costs for beverage type. needs to compute Each cost() methobedverage and then the cost of the ents by calling the add in the condim tation of cost(). superclass implemen

description milk soy mocha whip getDescription() cost() hasMilk() setMilk() hasSoy() setSoy() hasMocha() setMocha() hasWhip() setWhip() // Other useful methods..

DarkRoast

HouseBlend

Decaf

cost()

cost()

Sharpen your pencil

cost()

Espresso cost()

Write the cost( ) methods for the following classes (pseudo-Java is okay):

public class Beverage { public double cost() {

public class DarkRoast extends Beverage { public DarkRoast() { description = “Most Excellent Dark Roast”; } public double cost() {

}

}

}

}

you are here 4

83

impact of change

See, five classes total. This is definitely the way to go.

I’m not so sure; I can see some potential problems with this approach by thinking about how the design might need to change in the future.

Sharpen your pencil What requirements or other factors might change that will impact this design?

Price changes for condiments will force us to alter existing code. New condiments will force us to add new methods and alter the cost method in the superclass. We may have new beverages. For some of these beverages (iced tea?), the condiments may not be appropriate, yet the Tea subclass will still inherit methods like hasWhip(). What if a customer wants a double mocha?

rn:

Your tu

84

Chapter 3

aw in this is s e As w pter 1, idea! Cha ery bad av

the decorator pattern

Master and Student... Master: Grasshopper, it has been some time since our last meeting. Have you been deep in meditation on inheritance? Student: Yes, Master. While inheritance is powerful, I have learned that it doesn’t always lead to the most flexible or maintainable designs. Master: Ah yes, you have made some progress. So, tell me my student, how then will you achieve reuse if not through inheritance? Student: Master, I have learned there are ways of “inheriting” behavior at runtime through composition and delegation. Master: Please, go on... Student: When I inherit behavior by subclassing, that behavior is set statically at compile time. In addition, all subclasses must inherit the same behavior. If however, I can extend an object’s behavior through composition, then I can do this dynamically at runtime. Master: Very good, Grasshopper, you are beginning to see the power of composition. Student: Yes, it is possible for me to add multiple new responsibilities to objects through this technique, including responsibilities that were not even thought of by the designer of the superclass. And, I don’t have to touch their code! Master: What have you learned about the effect of composition on maintaining your code? Student: Well, that is what I was getting at. By dynamically composing objects, I can add new functionality by writing new code rather than altering existing code. Because I’m not changing existing code, the chances of introducing bugs or causing unintended side effects in pre-existing code are much reduced. Master: Very good. Enough for today, Grasshopper. I would like for you to go and meditate further on this topic... Remember, code should be closed (to change) like the lotus flower in the evening, yet open (to extension) like the lotus flower in the morning.

you are here 4

85

the open-closed principle

The Open-Closed Principle Grasshopper is on to one of the most important design principles:

Design Principle Classes should be open for extension, but closed for modification.

Come on in; we’re open. Feel free to extend our classes with any new behavior you like. If your needs or requirements change (and we know they will), just go ahead and make your own extensions. Sorry, we’re closed. That’s right, we spent a lot of time getting this code correct and bug free, so we can’t let you alter the existing code. It must remain closed to modification. If you don’t like it, you can speak to the manager.

Our goal is to allow classes to be easily extended to incorporate new behavior without modifying existing code. What do we get if we accomplish this? Designs that are resilient to change and flexible enough to take on new functionality to meet changing requirements.

86

Chapter 3

the decorator pattern

there are no

Dumb Questions

Q:

Open for extension and closed for modification? That sounds very contradictory. How can a design be both?

A:

That’s a very good question. It certainly sounds contradictory at first. After all, the less modifiable something is, the harder it is to extend, right? As it turns out, though, there are some clever OO techniques for allowing systems to be extended, even if we can’t change the underlying code. Think about the Observer Pattern (in Chapter 2)... by adding new Observers, we can extend the Subject at any time, without adding code to the Subject. You’ll see quite a few more ways of extending behavior with other OO design techniques.

Q:

Okay, I understand Observable, but how do I generally design something to be extensible, yet closed for modification?

A:

Usually, you can’t. Making OO design flexible and open to extension without the modification of existing code takes time and effort. In general, we don’t have the luxury of tying down every part of our designs (and it would probably be wastefu). Following the Open-Closed Principle usually introduces new levels of abstraction, which adds complexity to our code. You want to concentrate on those areas that are most likely to change in your designs and apply the principles there.

Q:

How do I know which areas of change are more important?

A:

That is partly a matter of experience in designing OO systems and also a matter of the knowing the domain you are working in. Looking at other examples will help you learn to identify areas of change in your own designs.

A:

While it may seem like a contradiction, there are techniques for allowing code to be extended without direct modification.

Q:

Be careful when choosing the areas of code that need to be extended; applying the Open-Closed Principle EVERYWHERE is wasteful, unnecessary, and can lead to complex, hard to understand code.

Many of the patterns give us time tested designs that protect your code from being modified by supplying a means of extension. In this chapter you’ll see a good example of using the Decorator pattern to follow the OpenClosed principle.

How can I make every part of my design follow the Open-Closed Principle?

you are here 4

87

meet the decorator pattern

Meet the Decorator Pattern

Okay, enough of the “Object Oriented Design Club.” We have real problems here! Remember us? Starbuzz Coffee? Do you think you could use some of those design principles to actually help us?

Okay, we’ve seen that representing our beverage plus condiment pricing scheme with inheritance has not worked out very well – we get class explosions, rigid designs, or we add functionality to the base class that isn’t appropriate for some of the subclasses. So, here’s what we’ll do instead: we’ll start with a beverage and “decorate” it with the condiments at runtime. For example, if the customer wants a Dark Roast with Mocha and Whip, then we’ll:

1

Take a DarkRoast object

2

Decorate it with a Mocha object

3

Decorate it with a Whip object

4

Call the cost() method and rely on delegation to add on the condiment costs

Okay, but how do you “decorate” an object, and how does delegation come into this? A hint: think of decorator objects as “wrappers.” Let’s see how this works...

88

Chapter 3

the decorator pattern

Constructing a drink order with Decorators 1

We start with our DarkRoast object.

cost()

DarkRoast

2

t DarkRoaasnd has t a h t r e b Remem from Beverage mputes inherits method that co a cost() of the drink. the cost

The customer wants Mocha, so we create a Mocha object and wrap it around the DarkRoast.

or. Its ject is a decorisatdecorating, The Mocha obth object it type mirrors Beeverage. (By “mirror”, in this case, a the same type..) we mean it is

cost()

cost()

DarkRoast

Mocha

3

oo, st() method tn treat co a s a h a ch o e ca So, M olymorphism w and through p wrapped in Mocha as any Beveragetoo (because Mocha is a a Beverage, Beverage). subtype of

The customer also wants Whip, so we create a Whip decorator and wrap Mocha with it.

cost()

cost()

Whip

cost()

DarkRoast

Mocha

Whip is a decorator, so it also mirrors DarkRoast’s type and includes a cost() method.

So, a DarkRoast wrapped in Mocha and Whip is still a Beverage and we can do anything with it we can do with a DarkRoast, including call its cost() method. you are here 4

89

decorator characteristics

4

Now it’s time to compute the cost for the customer. We do this by calling cost() on the outermost decorator, Whip, and Whip is going to delegate computing the cost to the objects it decorates. Once it gets a cost, it will add on the cost of the Whip.

in (You’ll see how ) s. ge a few pa

Mocha. 2 Whip calls cost() on t() on the 1 First, we call cos , Whip. tor ora dec st mo out

$1.29

.10

3

cost()

.20

cost()

Whip 5

Whip adds its total, 10 cents, to the result from Mocha, and returns the final result—$1.29.

5

Mocha calls cost() on DarkRoast.

cost()

t .99 D ar k Ro as

Mocha

4

Dar kRoast returns its cost, 99 cents.

Mocha adds its cost, 20 cents, to the result from Dar kRoast, and returns the new total, $1.19.

Okay, here’s what we know so far... ß Decorators have the same supertype as the objects they decorate. ß You can use one or more decorators to wrap an object. ß Given that the decorator has the same supertype as the object it decorates, we can pass around a decorated object in place of the original (wrapped) object.

ß The decorator adds its own behavior either before and/or after delegating to the object it decorates to do the rest of the job.

ß Objects can be decorated at any time, so we can decorate objects dynamically at runtime with as many decorators as we like.

Now let’s see how this all really works by looking at the Decorator Pattern definition and writing some code. 90

Chapter 3

t Key Poin

!

the decorator pattern

The Decorator Pattern defined Let’s first take a look at the Decorator Pattern description: The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

While that describes the role of the Decorator Pattern, it doesn’t give us a lot of insight into how we’d apply the pattern to our own implementation. Let’s take a look at the class diagram, which is a little more revealing (on the next page we’ll look at the same structure applied to the beverage problem).

Each component can be used on its own, or wrapped by a decorator.

Component

component

methodA() methodB()

The ConcreteComponent is the object we’re going to dynamically add new behavior to. It extends Component.

// other methods

Decorator

ConcreteComponent methodA()

methodA()

methodB()

methodB()

// other methods

// other methods

ConcereteDecoratorA Component wrappedObj

s an The ConcreteDecoratorehathing th r fo ble instance varia t it decorates (the Componen s). ap wr r the Decorato

methodA() methodB() newBehavior() // other methods

Each decorator HAS-A (wraps) a component, which means the decorator has an instance variable that holds a reference to a component.

ent the Decorators implem abstract same interfacemorponent they class as the co corate. are going to de ConcereteDecoratorB Component wrappedObj Object newState

methodA() methodB() // other methods

e Decorators can extendt.th en on mp state of the co

Decorators can add new methods; however, new behavior is typically added by doing computation before or after an existing method in the component. you are here 4

91

decorating beverages

Decorating our Beverages Okay, let’s work our Starbuzz beverages into this framework...

r Beverage acts asneou class. nt po m abstract co

component

Beverage description

getDescription() cost() // other useful methods

CondimentDecorator

DarkRoast

HouseBlend cost()

cost()

getDescription()

Decaf

Espresso cost()

cost()

e concretper r u o f e e Th nents, on compo e type. coffe

Milk

Whip

Soy

Mocha

Beverage beverage

Beverage beverage

Beverage beverage

Beverage beverage

cost()

cost()

cost()

cost()

getDescription()

getDescription()

getDescription()

getDescription()

And here are our condiment decorators; notice they need to implement not only cost() but also getDescription(). We’ll see why in a moment...

A

brain power

Before going further, think about how you’d implement the cost() method of the coffees and the condiments. Also think about how you’d implement the getDescription() method of the condiments.

92

Chapter 3

the decorator pattern

Cubicle Conversation Some confusion over Inheritance versus Composition

ry

Ma

Okay, I’m a little confused...I thought we weren’t going to use inheritance in this pattern, but rather we were going to rely on composition instead.

Sue: What do you mean? Mary: Look at the class diagram. The CondimentDecorator is extending the Beverage class. That’s inheritance, right? Sue: True. I think the point is that it’s vital that the decorators have the same type as the objects they are going to decorate. So here we’re using inheritance to achieve the type matching, but we aren’t using inheritance to get behavior. Mary: Okay, I can see how decorators need the same “interface” as the components they wrap because they need to stand in place of the component. But where does the behavior come in? Sue: When we compose a decorator with a component, we are adding new behavior. We are acquiring new behavior not by inheriting it from a superclass, but by composing objects together. Mary: Okay, so we’re subclassing the abstract class Beverage in order to have the correct type, not to inherit its behavior. The behavior comes in through the composition of decorators with the base components as well as other decorators. Sue: That’s right. Mary: Ooooh, I see. And because we are using object composition, we get a whole lot more flexibility about how to mix and match condiments and beverages. Very smooth. Sue: Yes, if we rely on inheritance, then our behavior can only be determined statically at compile time. In other words, we get only whatever behavior the superclass gives us or that we override. With composition, we can mix and match decorators any way we like... at runtime. Mary: And as I understand it, we can implement new decorators at any time to add new behavior. If we relied on inheritance, we’d have to go in and change existing code any time we wanted new behavior. Sue: Exactly. Mary: I just have one more question. If all we need to inherit is the type of the component, how come we didn’t use an interface instead of an abstract class for the Beverage class? Sue: Well, remember, when we got this code, Starbuzz already had an abstract Beverage class. Traditionally the Decorator Pattern does specify an abstract component, but in Java, obviously, we could use an interface. But we always try to avoid altering existing code, so don’t “fix” it if the abstract class will work just fine. you are here 4

93

decorator training

New barista training

Okay, I need for you to make me a double mocha, soy latte with whip.

Make a picture for what happens when the order is for a “double mocha soy lotte with whip” beverage. Use the menu to get the correct prices, and draw your picture using the same format we used earlier (from a few pages back): Mocha. 2 Whip calls cost() on

3

) on the 1 First, we call cost( outmost decorator, Whip.

$1.29

.10

cost()

.20

cost()

Whip 5

Whip adds its total, 10 cents, to the result from Mocha, and returns the final result—$1.29.

5

Mocha calls cost() on DarkRoast.

cost()

t .99 D ar k Ro as

Mocha

4

was for This picturoeast mocha a “dark r rage. whip” beve

DarkRoast returns its cost, 99 cents.

Mocha adds its cost, 20 cents, to the result from DarkRoast, and returns the new total, $1.19.

Sharpen your pencil

Draw your picture here.

Starbuzz Coffee

Coffees House Blend .89 Dark Roast .99 Decaf 1.05 Espresso 1.99 Condiments Steamed Milk .10 Mocha .20 Soy .15 Whip .10

bu tar zz

u tarb zz

ffee S Co

fe Cof e S

le “doub ke a whip” a m n h ca oy, : you te wit nd, S ! HINTha soy lat HouseBle and Whip moc ombining Mocha by c shots of two

94

Chapter 3

the decorator pattern

Writing the Starbuzz code It’s time to whip this design into some real code. Let’s start with the Beverage class, which doesn’t need to change from Starbuzz’s original design. Let’s take a look:

public abstract class Beverage { String description = “Unknown Beverage”; public String getDescription() { return description; } public abstract double cost(); }

abstract Beverage is an two methods class with the n() and cost(). getDescriptio

getDescription is already implemented for us, but we need to implement cost() in the subclasses.

Beverage is simple enough. Let’s implement the abstract class for the Condiments (Decorator) as well:

to be First, we needle with a Beverage, interchangeab the Beverage class. so we extend public abstract class CondimentDecorator extends Beverage { public abstract String getDescription(); }

We’re also going to require that the condiment decorators all reimplement the getDescription() method. Again, we’ll see why in a sec...

you are here 4

95

implementing the beverages

Coding beverages Now that we’ve got our base classes out of the way, let’s implement some beverages. We’ll start with Espresso. Remember, we need to set a description for the specific beverage and also implement the cost() method.

public class Espresso extends Beverage { public Espresso() { description = “Espresso”; } public double cost() { return 1.99; } }

public double cost() { return .89; }

Okay, here’s another Beverage. All we do is set the appropriate description, “House Blend Coffee,” and then return the correct cost: 89¢. You can create the other two Beverage classses (DarkRoast and Decaf) in exactly the same way.

96

Chapter 3

we To take care of the description,the for or uct str con set this in the instance class. Remember the description ge. variable is inherited from Bevera

’t spresso. We djuonst E an of st co e to compute th this class, we Finally, we needabout adding in condiments in.99. need to worry the price of an Espresso: $1 need to return

public class HouseBlend extends Beverage { public HouseBlend() { description = “House Blend Coffee”; }

}

d the Beverage First we exthiens is a beverage. class, since t

Coffee Starbuzz s Coffee Blend House oast R Dark Decaf so Espres

.89 .99 1.05 1.99

ents Condim d Milk Steame Mocha Soy Whip

.10 .20 .15 .10

the decorator pattern

Coding condiments If you look back at the Decorator Pattern class diagram, you’ll see we’ve now written our abstract component (Beverage), we have our concrete components (HouseBlend), and we have our abstract decorator (CondimentDecorator). Now it’s time to implement the concrete decorators. Here’s Mocha:

tor tDecora n e im d n o Mocha is a decorator, so we e Mocha with at ti an mber, Cverage. st e in m to e g in R We’re go extend CondimentDecorator. Be Beverage using: extends a reference to a e variable to hold th (1) An instancewrapping. public class Mocha extends CondimentDecorator { beverage we are Beverage beverage; t this instance (2) A way to seject we are wrapping. public Mocha(Beverage beverage) { variable to theinob pass the beverage this.beverage = beverage; ere, we’re go g tothe decorator’s H } we’re wrapping to constructor. public String getDescription() { return beverage.getDescription() + “, Mocha”; } public double cost() { return .20 + beverage.cost(); } }

beverage ute the cost of oullr to mp co to ed e ne we w No delegate the ca mpth we t, rs Fi . ha e the oc ut M th co wi ng, so that it can to the result. ti ra co de ’re we ct je ob e cost of Mocha cost; then, we add th

We want our description to not only include the beverage - say “Dark Roast” - but also to include each item decorating the beverage, for instance, “Dark Roast, Mocha”. So we first delegate to the object we are decorating to get its description, then append “, Mocha” to that description.

On the next page we’ll actually instantiate the beverage and wrap it with all its condiments (decorators), but first... Sharpen your pencil

Write and compile the code for the other Soy and Whip condiments. You’ll need them to finish and test the application.

you are here 4

97

testing the beverages

Serving some coffees Congratulations. It’s time to sit back, order a few coffees and marvel at the flexible design you created with the Decorator Pattern.

Here’s some test code to make orders: public class StarbuzzCoffee { public static void main(String args[]) { Beverage beverage = new Espresso(); System.out.println(beverage.getDescription() + “ $” + beverage.cost());

s condimentt. o n , o s s e nd cos an espr Order upt its description a and prin

oast object. Make a DarkR Beverage beverage2 = new DarkRoast(); Wrap it with a Mocha. beverage2 = new Mocha(beverage2); Wrap it in a second Mocha. beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); Wrap it in a Whip. System.out.println(beverage2.getDescription() + “ $” + beverage2.cost()); Beverage beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); System.out.println(beverage3.getDescription() + “ $” + beverage3.cost());

Finally, give us a HouseBlend with Soy, Mocha, and Whip.

} }

Now, let’s get those orders in:

We’re going to see a much better way of creating decorated objects when we cover the Factory and Builder Design Patterns.

File Edit Window Help CloudsInMyCoffee

% java StarbuzzCoffee Espresso $1.99 Dark Roast Coffee, Mocha, Mocha, Whip $1.49 File Edit Window Help CloudsInMyCoffee House Blend Coffee, Soy, Mocha, Whip $1.34 %

98

Chapter 3

the decorator pattern

there are no

Dumb Questions

Q:

I’m a little worried about code that might test for a specfic concrete component – say, HouseBlend – and do something, like issue a discount. Once I’ve wrapped the HouseBlend with decorators, this isn’t going to work anymore.

A:

That is exactly right. If you have code that relies on the concrete component’s type, decorators will break that code. As long as you only write code against the abstract component type, the use of decorators will remain transparent to your code. However, once you start writing code against concrete components, you’ll want to rethink your application design and your use of decorators.

Q:

Wouldn’t it be easy for some client of a beverage to end up with a decorator that isn’t the outermost decorator? Like if I had a DarkRoast with Mocha, Soy, and Whip, it would be easy to write code that somehow ended up with a reference to Soy instead of Whip, which means it would not including Whip in the order.

A:

You could certainly argue that you have to manage more objects with the Decorator Pattern and so there is an increased chance that coding errors will introduce the kinds of problems you suggest. However, decorators are typically created by using other patterns like Factory and Builder. Once we’ve covered these patterns, you’ll see that the creation of the concrete component with its decorator is “well encapsulated” and doesn’t lead to these kinds of problems.

Q:

Can decorators know about the other decorations in the chain? Say, I wanted my getDecription() method to print “Whip, Double Mocha” instead of “Mocha, Whip, Mocha”? That would require that my outermost decorator know all the decorators it is wrapping.

A:

Decorators are meant to add behavior to the object they wrap. When you need to peek at multiple layers into the decorator chain, you are starting to push the decorator beyond its true intent. Nevertheless, such things are possible. Imagine a CondimentPrettyPrint decorator that parses the final decription and can print “Mocha, Whip, Mocha” as “Whip, Double Mocha.” Note that getDecription() could return an ArrayList of descriptions to make this easier.

Sharpen your pencil Our friends at Starbuzz have introduced sizes to their menu. You can now order a coffee in tall, grande, and venti sizes (translation: small, medium, and large). Starbuzz saw this as an intrinsic part of the coffee class, so they’ve added two methods to the Beverage class: setSize() and getSize(). They’d also like for the condiments to be charged according to size, so for instance, Soy costs 10¢, 15¢ and 20¢ respectively for tall, grande, and venti coffees. How would you alter the decorator classes to handle this change in requirements?

you are here 4

99

decorators in java i/o

Real World Decorators: Java I/O The large number of classes in the java.io package is... overwhelming. Don’t feel alone if you said “whoa” the first (and second and third) time you looked at this API. But now that you know the Decorator Pattern, the I/O classes should make more sense since the java.io package is largely based on Decorator. Here’s a typical set of objects that use decorators to add functionality to reading data from a file:

A text file for reading.

ered

e InputStr

a

ff

m

ne Nu mberInputStrea

LineNumberInputStream is also a concrete decorator. It adds the ability to count the line numbers as it reads data.

m

Li

Bu

FileInputStream

BufferedInputStream is a concrete decorator. BufferedInputStream adds behavior in two ways: it buffers input to improve performance, and also augments the interface with a new method readLine() for reading character-based input, a line at a time.

BufferedInputStream and LineNumberInputStream both extend FilterInputStream, which acts as the abstract decorator class.

100

Chapter 3

hat’s component tary e h t is m a e r FileInputStr ted. The Java I/O lib g a in r d o c lu e c nts, in being d ral compone BufferInputStream, e v se s e li p p su others. eam, String FileInputStr putStream and a fewent from ByteArrayIn give us a base compon All of thesead bytes. which to re

the decorator pattern

Decorating the java.io classes ent. ract compon st b a r u o ’s Here

FilterInputStream is an abstract decorator.

InputStream

FileInputStream

StringBufferInputStream

PushbackInputStream

These InputStreams act as the concrete components that we will wrap with decorators. There are a few more we didn’t show, like ObjectInputStream.

FilterInputStream

ByteArrayInputStream

BufferedInputStream

DataInputStream

LineNumberInputStream

crete decorators.

And finally, here are all our con

You can see that this isn’t so different from the Starbuzz design. You should now be in a good position to look over the java.io API docs and compose decorators on the various input streams. You’ll see that the output streams have the same design. And you’ve probably already found that the Reader/Writer streams (for character-based data) closely mirror the design of the streams classes (with a few differences and inconsistencies, but close enough to figure out what’s going on). Java I/O also points out one of the downsides of the Decorator Pattern: designs using this pattern often result in a large number of small classes that can be overwhelming to a developer trying to use the Decorator-based API. But now that you know how Decorator works, you can keep things in perspective and when you’re using someone else’s Decorator-heavy API, you can work through how their classes are organized so that you can easily use wrapping to get the behavior you’re after.

you are here 4

101

write your own i/o decorator

Writing your own Java I/O Decorator Okay, you know the Decorator Pattern, you’ve seen the I/O class diagram. You should be ready to write your own input decorator. How about this: write a decorator that converts all uppercase characters to lowercase in the input stream. In other words, if we read in “I know the Decorator Pattern therefore I RULE!” then your decorator converts this to “i know the decorator pattern therefore i rule!”

t to import e g r o f ’t n o D t shown) java.io... (no

No problem. I just have to extend the FilterInputStream class and override the read() methods.

First, extend the FilterInputStream, the abstract decorator for all InputStreams.

public class LowerCaseInputStream extends FilterInputStream { public LowerCaseInputStream(InputStream in) { super(in); } public int read() throws IOException { int c = super.read(); return (c == -1 ? c : Character.toLowerCase((char)c)); }

}

public int read(byte[] b, int offset, int len) throws IOException { int result = super.read(b, offset, len); for (int i = offset; i < offset+result; i++) { b[i] = (byte)Character.toLowerCase((char)b[i]); Now we need to implement two } read methods. They take a return result; byte (or an array of bytes) } convert each byte (that

REMEMBER: we don’t provide import and package statements in the code listings. Get the complete source code from the wickedlysmart web site. You’ll find the URL on page xxxiii in the Intro. 102

Chapter 3

and represents a character) to lowercase if it’s an uppercase character.

the decorator pattern

Test out your new Java I/O Decorator Write some quick code to test the I/O decorator: public class InputTest { public static void main(String[] args) throws IOException { int c; try { m InputStream in = ileInputStrea new LowerCaseInputStream( Set up the tFe it, first with new BufferedInputStream( and decoradInputStream new FileInputStream(“test.txt”))); Buffere while((c = in.read()) >= 0) { System.out.print((char)c); } in.close(); } catch (IOException e) { e.printStackTrace(); } } }

Just use the stream to read characters until the end of file and print as we go.

Give it a spin:

a brand new and then ouInr putStream filter. LowerCase

I know the Decorator Pattern therefore I RULE!

test.txt file

You need tfoile. make this

File Edit Window Help DecoratorsRule

% java InputTest i know the decorator pattern therefore i rule! %

you are here 4

103

decorator interview

Patterns Exposed This week’s interview:

Confessions of a Decorator HeadFirst: Welcome Decorator Pattern. We’ve heard that you’ve been a bit down on yourself lately? Decorator: Yes, I know the world sees me as the glamorous design pattern, but you know, I’ve got my share of problems just like everyone. HeadFirst: Can you perhaps share some of your troubles with us? Decorator: Sure. Well, you know I’ve got the power to add flexibility to designs, that much is for sure, but I also have a dark side. You see, I can sometimes add a lot of small classes to a design and this occasionally results in a design that’s less than straightforward for others to understand. HeadFirst: Can you give us an example? Decorator: Take the Java I/O libraries. These are notoriously difficult for people to understand at first. But if they just saw the classes as a set of wrappers around an InputStream, life would be much easier. HeadFirst: That doesn’t sound so bad. You’re still a great pattern, and improving this is just a matter of public education, right? Decorator: There’s more, I’m afraid. I’ve got typing problems: you see, people sometimes take a piece of client code that relies on specific types and introduce decorators without thinking through everything. Now, one great thing about me is that you can usually insert decorators transparently and the client never has to know it’s dealing with a decorator. But like I said, some code is dependent on specific types and when you start introducing decorators, boom! Bad things happen. HeadFirst: Well, I think everyone understands that you have to be careful when inserting decorators, I don’t think this is a reason to be too down on yourself. Decorator: I know, I try not to be. I also have the problem that introducing decorators can increase the complexity of the code needed to instantiate the component. Once you’ve got decorators, you’ve got to not only instantiate the component, but also wrap it with who knows how many decorators. HeadFirst: I’ll be interviewing the Factory and Builder patterns next week – I hear they can be very helpful with this? Decorator: That’s true; I should talk to those guys more often. HeadFirst: Well, we all think you’re a great pattern for creating flexible designs and staying true to the Open-Closed Principle, so keep your chin up and think positively! Decorator: I’ll do my best, thank you. 104

Chapter 3

the decorator pattern

Tools for your Design Toolbox

BULLET POINTS

You’ve got another chapter under your belt and a new principle and pattern in the toolbox.

ß Inheritance is one form of extension, but not necessarily the best way to achieve flexibility in our designs.

ß In our designs we should allow behavior to be extended without the need to modify existing code.

OO Basincs Abstractio tion s OO Principle Encapsulaphism Polymor hat varies. w e t la su p a Enc nchee. ritance eritaIn h in r e v o n ositio Favor comp not interfaces, Program to s. ion implementat gns coupled desi ly se o lo r o Strive f eract. ts that int c je b o n e e w bet for

ld be open r u o h s s e s s la o C ut closed f extension b n. modificatio

ß Composition and delegation can often be used to add new behaviors at runtime.

ß The Decorator Pattern provides an alternative to subclassing for extending behavior.

ß The Decorator Pattern involves a set of decorator classes that are used to wrap concrete components.

ß We now have the Open-Closed g Principle to guide us. We’re goin to strive to design our system so that the closed parts are . isolated from our new extensions

OO Patterr-ndsefineysofa aolgneor-itthom-s,tmhaanty

bys-erdvefeinyesbaetfwameilenkeosbjethcetms soddaitllioitnsal O g e t a r St depenedaecnhcone, andrm-aanAgtets ascthaotareit, hm o ch lg pddaytneadmically. encapsuwlahteensDoenc.e oSortbrajeattcetgy slettsoietahdne aaonbdjecutit able ilritme ienclioetnifts thaat fulesexib. le a interchdanegpeernedseptnolytnsib r tending nden f o s provide vary indaeupteoDmeactoicralltyor o subclassing for ex t alternative y. t functionali designs n for creatingOr was it er t t pa st ir f r . And here’s ou the Open-Closed Principle ern we’ve that satisfy st? Is there another patt really the fir lows this principle as well? used that fol

Decorator classes mirror the type of the components they decorate. (In fact, they are the same type as the components they decorate, either through inheritance or interface implementation.)

ß Decorators change the behavior of their components by adding new functionality before and/or after (or even in place of) method calls to the component.

ß You can wrap a component with any number of decorators.

ß Decorators are typically transparent to the client of the component; that is, unless the client is relying on the component’s concrete type.

ß Decorators can result in many small objects in our design, and overuse can be complex. you are here 4

105

exercise solutions

Exercise solutions public class Beverage { // declare instance variables for milkCost, // soyCost, mochaCost, and whipCost, and // getters and setters for milk, soy, mocha // and whip.

public class DarkRoast extends Beverage { public DarkRoast() { description = “Most Excellent Dark Roast”; }

public float cost() { float condimentCost = 0.0; if (hasMilk()) { condimentCost += milkCost; } if (hasSoy()) { condimentCost += soyCost; } if (hasMocha()) { condimentCost += mochaCost; } if (hasWhip()) { condimentCost += whipCost; } return condimentCost;

public float cost() { return 1.99 + super.cost();

}

}

} }

New barista training

“double mocha soy lotte with whip” on Mocha 2 Whip calls cost() another Mocha. 3 Mocha calls cost() on the Mocha calls cost() on Soy. First, we call cost() on 4 Next, 1 outmost decorator, Whip. Soy calls 5 Last topping! cost() on HouseBlend.

6

offee

tarbu

zz C

$1.54

.10

cost()

.20

S

W

hip

Whip’s cost(), which adds .10 and we have a final cost of $1.54.

Chapter 3

.15

.20

M

11 Finally, the result returns to

106

cost() cost()

Mo

cha

oc

ha

cost()

So

y

cost()

.89 nd HouseBle

7

8

9

HouseBlend’s cost() method returns .89 cents and pops off the stack. Soy’s cost() method adds .15 and returns the result, and pops off the stack.

The second Mocha’s cost() method adds .20 and returns the result, and pops off the stack.

method The first Mocha’s cost() result, adds .20 and returns the and pops off the stack.

the decorator pattern

Exercise solutions Our friends at Starbuzz have introduced sizes to their menu. You can now order a coffee in tall, grande, and venti sizes (for us normal folk: small, medium, and large). Starbuzz saw this as an intrinsic part of the coffee class, so they’ve added two methods to the Beverage class: setSize() and getSize(). They’d also like for the condiments to be charged according to size, so for instance, Soy costs 10¢, 15¢, and 20¢ respectively for tall, grande, and venti coffees. How would you alter the decorator classes to handle this change in requirements?

public class Soy extends CondimentDecorator { Beverage beverage; public Soy(Beverage beverage) { this.beverage = beverage; } public getSize() { return beverage.getSize(); }

propagate the Now we need thoo d to the wrapped getSize() mete should also move this beverage. W he abstract class since method to t l condiment decorators. it’s used in al

public String getDescription() { return beverage.getDescription() + “, Soy”; } public double cost() { double cost = beverage.cost(); if (getSize() == Beverage.TALL) { cost += .10; } else if (getSize() == Beverage.GRANDE) { cost += .15; } else if (getSize() == Beverage.VENTI) { cost += .20; } return cost; }

Here we get the size (which propagates all the way to the concrete beverage) and then add the appropriate cost.

}

you are here 4

107

Chapter 4. Baking with OO Goodness

Table of Contents Chapter 4. Baking with OO Goodness.......................................................... 1 Section 4.1. Brain Power................................................................................................................................................................. 3 Section 4.2. Identifying the aspects that vary................................................................................................................................. 4 Section 4.3. But the pressure is on to add more pizza types.......................................................................................................... 5 Section 4.4. Encapsulating object creation..................................................................................................................................... 6 Section 4.5. Building a simple pizza factory................................................................................................................................... 7 Section 4.6. There are no Dumb Questions.................................................................................................................................... 7 Section 4.7. Reworking the PizzaStore class.................................................................................................................................. 8 Section 4.8. The Simple Factory defined........................................................................................................................................ 9 Section 4.9. Franchising the pizza store....................................................................................................................................... 10 Section 4.10. A framework for the pizza store.............................................................................................................................. 12 Section 4.11. Allowing the subclasses to decide............................................................................................................................ 13 Section 4.12. Let's make a PizzaStore............................................................................................................................................ 15 Section 4.13. Declaring a factory method...................................................................................................................................... 17 Section 4.14. Let's see how it works: ordering pizzas with the pizza factory method.................................................................. 18 Section 4.15. Let's check out how these pizzas are really made to order... Behind the Scenes.................................................... 19 Section 4.16. We're just missing one thing: PIZZA!..................................................................................................................... 20 Section 4.17. Now we just need some concrete subclasses... how about defining New York and Chicago style cheese pizzas?..... 21 Section 4.18. It's finally time to meet the Factory Method Pattern............................................................................................. 23 Section 4.19. Another perspective: parallel class hierarchies...................................................................................................... 24 Section 4.20. Design Puzzle.......................................................................................................................................................... 25 Section 4.21. Factory Method Pattern defined............................................................................................................................. 26 Section 4.22. There are no Dumb Questions................................................................................................................................ 27 Section 4.23. A very dependent PizzaStore.................................................................................................................................. 29 Section 4.24. Looking at object dependencies............................................................................................................................. 30 Section 4.25. The Dependency Inversion Principle...................................................................................................................... 31 Section 4.26. Applying the Principle............................................................................................................................................ 32 Section 4.27. Inverting your thinking........................................................................................................................................... 34 Section 4.28. A few guidelines to help you follow the Principle.................................................................................................. 35 Section 4.29. Meanwhile, back at the PizzaStore......................................................................................................................... 36 Section 4.30. Families of ingredients........................................................................................................................................... 37 Section 4.31. Building the ingredient factories............................................................................................................................ 38 Section 4.32. Building the New York ingredient factory.............................................................................................................. 39 Section 4.33. Sharpen your pencil................................................................................................................................................ 40 Section 4.34. Reworking the pizzas............................................................................................................................................... 41 Section 4.35. Reworking the pizzas, continued............................................................................................................................ 42 Section 4.36. Code Up Close......................................................................................................................................................... 43 Section 4.37. Revisiting our pizza stores...................................................................................................................................... 44 Section 4.38. What have we done?............................................................................................................................................... 45 Section 4.39. More pizza for Ethan and Joel................................................................................................................................ 46 Section 4.40. Abstract Factory Pattern defined........................................................................................................................... 48 Section 4.41. Patterns Exposed..................................................................................................................................................... 50 Section 4.42. Factory Method and Abstract Factory compared................................................................................................... 52 Section 4.43. Tools for your Design Toolbox................................................................................................................................ 54 Section 4.44. Exercise solutions................................................................................................................................................... 56 Section 4.45. Sharpen your pencil................................................................................................................................................ 56 Section 4.46. Design Puzzle Solution............................................................................................................................................ 57 Section 4.47. A very dependent PizzaStore.................................................................................................................................. 58 Section 4.48. Sharpen your pencil................................................................................................................................................ 58 Section 4.49. Sharpen your pencil................................................................................................................................................ 59 Section 4.50. Puzzle Solution....................................................................................................................................................... 60

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

the Factory Pattern

4

Return to Table of Contents

Page 1

5

%DNLQJZLWK22*RRGQHVV

4

*HWUHDG\WREDNHVRPHORRVHO\FRXSOHG22GHVLJQV7KHUHLVPRUHWR PDNLQJREMHFWVWKDQMXVWXVLQJWKHQHZRSHUDWRU
WKLVLVDQHZFKDSWHU



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 2

WKLQNLQJDERXW³QHZ´

2ND\LW·VEHHQWKUHHFKDSWHUV DQG\RXVWLOOKDYHQ·WDQVZHUHGP\ TXHVWLRQDERXWQHZ:HDUHQ·WVXSSRVHG WRSURJUDPWRDQLPSOHPHQWDWLRQEXW HYHU\WLPH,XVHQHZWKDW·VH[DFWO\ ZKDW,·PGRLQJULJKW"

:KHQ\RXVHH´QHZµWKLQN´FRQFUHWHµ AM[_PMVaW]][MVM_aW]IZMKMZ\IQVTaQV[\IV\QI\QVOIKWVKZM\M KTI[[[W\PI\¼[LMÅVQ\MTaIVQUXTMUMV\I\QWVVW\IVQV\MZNIKM)VL Q\¼[IOWWLY]M[\QWV#aW]¼^MTMIZVML\PI\\aQVOaW]ZKWLM\WI KWVKZM\MKTI[[KIVUISMQ\UWZMNZIOQTMIVLTM[[ÆM`QJTM 'XFNGXFN QHZ0DOODUG'XFN 

:HZDQWWRXVHLQWHUI WRNHHSFRGHIOH[LEO DFHV H

HDQ %XWZHKDYHWRFUHDW FODVV WH FUH FRQ D RI QFH WD LQV

?PMVaW]PI^MI_PWTM[M\WN ZMTI\MLKWVKZM\MKTI[[M[WN\MVaW]¼ZM NWZKML\W_ZQ\MKWLMTQSM\PQ[" 'XFNGXFN LI SLFQLF ^ GXFN QHZ0DOODUG'XFN  `HOVHLI KXQWLQJ ^ GXFN QHZ'HFR\'XFN  `HOVHLI LQ%DWK7XE ^ GXFN QHZ5XEEHU'XFN  `

LIIHUHQW :HKDYHDEXQFKRIG RQ·WNQRZ HG Z DQG GXFNFODVVHV QHZHQHHG XQWLOUXQWLPHZKLFKR H LDW QW WRLQVWD

0MZM_M¼^MOW\[M^MZITKWVKZM\MKTI[[M[JMQVOQV[\IV\QI\MLIVL\PM LMKQ[QWVWN _PQKP\WQV[\IV\QI\MQ[UILMI\Z]V\QUMLMXMVLQVOWV [WUM[M\WN KWVLQ\QWV[ ?PMVaW][MMKWLMTQSM\PQ[aW]SVW_\PI\_PMVQ\KWUM[\QUMNWZ KPIVOM[WZM`\MV[QWV[aW]¼TTPI^M\WZMWXMV\PQ[KWLMIVLM`IUQVM _PI\VMML[\WJMILLMLWZLMTM\ML7N\MV\PQ[SQVLWN KWLMMVL[ ]XQV[M^MZITXIZ\[WN \PMIXXTQKI\QWVUISQVOUIQV\MVIVKMIVL ]XLI\M[UWZMLQNÅK]T\IVLMZZWZXZWVM 

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 3

WKHIDFWRU\SDWWHUQ

%XW\RXKDYHWRFUHDWH DQREMHFWDWVRPHSRLQWDQG -DYDRQO\JLYHVXVRQHZD\WR FUHDWHDQREMHFWULJKW"6R ZKDWJLYHV"

:KDW·VZURQJZLWK´QHZµ"
5HPHPEHUWKDW EH´RSHQIRUH GHVLJQVVKRXOG FORVHGIRUPRG[WHQVLRQEXW VHH&KDSWHUILILFDWLRQµ RUDUHYLHZ

YiX`e gfn\i +RZPLJKW\RXWDNHDOOWKHSDUWVRI\RXUDSSOLFDWLRQWKDWLQVWDQWLDWHFRQFUHWHFODVVHVDQG VHSDUDWHRUHQFDSVXODWHWKHPIURPWKHUHVWRI\RXUDSSOLFDWLRQ"

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 4

LGHQWLI\ZKDWYDULHV

1LMV\QNaQVO\PMI[XMK\[\PI\^IZa 4M\¼[[IaaW]PI^MIXQbbI[PWXIVLI[IK]\\QVOMLOMXQbbI[\WZM W_VMZQV7JRMK\^QTTMaW]UQOP\MVL]X_ZQ\QVO[WUMKWLMTQSM\PQ[" 3L]]DRUGHU3L]]D ^ 

3L]]DSL]]D QHZ3L]]D 



SL]]DSUHSDUH 



SL]]DEDNH 



SL]]DFXW 



SL]]DER[ 



UHWXUQSL]]D

)RUIOH[LELOLW\ZHUHDOO\ZDQW WKLVWREHDQDEVWUDFWFODVVRU LQWHUIDFHEXWZHFDQ·WGLUHFWO\ LQVWDQWLDWHHLWKHURIWKRVH

`

%XW\RXQHHGPRUHWKDQRQHW\SHRISL]]D

;W\PMVaW]¼LILL[WUMKWLM\PI\LM\MZUQVM[\PMIXXZWXZQI\M\aXMWN XQbbI IVL\PMVOWM[IJW]\UISQVO\PMXQbbI"

:H·UHQRZSDVVLQJLQ WKHW\SHRISL]]DWR RUGHU3L]]D

3L]]DRUGHU3L]]D 6WULQJW\SH ^ 

3L]]DSL]]D

LI W\SHHTXDOV ³FKHHVH´ ^ SL]]D QHZ&KHHVH3L]]D  `HOVHLI W\SHHTXDOV ³JUHHN´ ^ SL]]D QHZ*UHHN3L]]D  

`HOVHLI W\SHHTXDOV ³SHSSHURQL´ ^

SL]]D QHZ3HSSHURQL3L]]D  ` 

SL]]DSUHSDUH 



SL]]DEDNH 



SL]]DFXW 



SL]]DER[ 



UHWXUQSL]]D

`



%DVHGRQWKHW\SHRISL]]DZH LQVWDQWLDWHWKHFRUUHFWFRQFUHW DQGDVVLJQLWWRWKHSL]]DLQVWDQHFODVV YDULDEOH1RWHWKDWHDFKSL]]D FH KDVWRLPSOHPHQWWKH3L]]DLQW KHUH HUIDFH

2QFHZHKDYHD3L]]DZHSUHSDUHLW \RXNQRZUROOWKHGRXJKSXWRQWKH VDXFHDQGDGGWKHWRSSLQJV FKHHVH  WKHQZHEDNHLWFXWLWDQGER[LW (DFK3L]]DVXEW\SH &KHHVH3L]]D 9HJJLH3L]]DHWF NQRZVKRZWR SUHSDUHLWVHOI

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 5

WKHIDFWRU\SDWWHUQ

*]\\PMXZM[[]ZMQ[WV\WILLUWZMXQbbI\aXM[ AW]ZMITQbM\PI\ITTWN aW]ZKWUXM\Q\WZ[PI^MILLMLIKW]XTMWN \ZMVLaXQbbI[\W \PMQZUMV]["\PM+TIU8QbbIIVL\PM>MOOQM8QbbI7J^QW][TaaW]VMML\WSMMX ]X_Q\P\PMKWUXM\Q\QWV[WaW]¼TTILL\PM[MQ\MU[\WaW]ZUMV])VLaW]PI^MV¼\ JMMV[MTTQVOUIVa/ZMMS8QbbI[TI\MTa[WaW]LMKQLM\W\ISM\PI\WNN \PMUMV]"

3L]]DRUGHU3L]]D 6WULQJW\SH ^

FORVHG HLV127Q,I G R F  LV K 7 LILFDWLR V IRUPRG]D6KRSFKDQJHH L] WKH3 DRIIHULQJVZ LWVSL]] JHWLQWRWKLV KDYHWR GPRGLI\LW FRGHDQ



3L]]DSL]]D

LI W\SHHTXDOV ³FKHHVH´ ^

LHV 7KLVLVZKDWYDU $VWKHSL]]D HV VHOHFWLRQFKDQJ O RYHUWLPH\RX·O WKLV KDYHWRPRGLI\ HU FRGHRYHUDQGRY

SL]]D QHZ&KHHVH3L]]D  `HOVHLI W\SHHTXDOV ³JUHHN´ ^ SL]]D QHZ*UHHN3L]]D  

`HOVHLI W\SHHTXDOV ³SHSSHURQL´ ^

SL]]D QHZ3HSSHURQL3L]]D  

`HOVHLI W\SHHTXDOV ³FODP´ ^

SL]]D QHZ&ODP3L]]D  

`HOVHLI W\SHHTXDOV ³YHJJLH´ ^

SL]]D QHZHJJLH3L]]D  ` 

SL]]DSUHSDUH 



SL]]DEDNH 



SL]]DFXW 



SL]]DER[ 



UHWXUQSL]]D

`

7KLVLVZKDWZHH[SHFWWRVWD\ WKHVDPH)RUWKHPRVWSDUW SUHSDULQJFRRNLQJDQGSDFNDJLQJ DSL]]DKDVUHPDLQHGWKHVDPH IRU\HDUVDQG\HDUV6RZH GRQ·WH[SHFWWKLVFRGHWRFKDQJH MXVWWKHSL]]DVLWRSHUDWHVRQ

+TMIZTaLMITQVO_Q\P_PQKPKWVKZM\MKTI[[Q[QV[\IV\QI\MLQ[ZMITTaUM[[QVO]XW]Z WZLMZ8QbbIUM\PWLIVLXZM^MV\QVOQ\NZWUJMQVOKTW[MLNWZUWLQÅKI\QWV*]\VW_ \PI\_MSVW__PI\Q[^IZaQVOIVL_PI\Q[V¼\Q\¼[XZWJIJTa\QUM\WMVKIX[]TI\MQ\

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 6

HQFDSVXODWHREMHFWFUHDWLRQ

-VKIX[]TI\QVOWJRMK\KZMI\QWV ;WVW__MSVW__M¼LJMJM\\MZWNN UW^QVO\PMWJRMK\KZMI\QWV W]\WN \PMWZLMZ8QbbIUM\PWL*]\PW_'?MTT_PI\ _M¼ZMOWQVO\WLWQ[\ISM\PMKZMI\QWVKWLMIVLUW^MQ\W]\ QV\WIVW\PMZWJRMK\\PI\Q[WVTaOWQVO\WJMKWVKMZVML_Q\P KZMI\QVOXQbbI[

LI W\SHHTXDOV ³FKHHVH´ ^ SL]]D QHZ&KHHVH3L]]D  

`HOVHLI W\SHHTXDOV ³SHSSHURQL´ ^

SL]]D QHZ3HSSHURQL3L]]D  

`HOVHLI W\SHHTXDOV ³FODP´ ^

SL]]D QHZ&ODP3L]]D  

`HOVHLI W\SHHTXDOV ³YHJJLH´ ^

SL]]D QHZ9HJJLH3L]]D  `

3L]]DRUGHU3L]]D 6WULQJW\SH ^ 

3L]]DSL]]D



SL]]DSUHSDUH 



SL]]DEDNH 



SL]]DFXW 



SL]]DER[ 



UHWXUQSL]]D

 )LUVWZHSXOOWKHREMHFW H WK RI XW HR FRG FUHDWLRQ RUGHU3L]]D0HWKRG

:KDW·V JRLQJ

WRJRK

`

HUH"

HLQDQREMHFWWKDW 7KHQZHSODFHWKDWFRG RXWKRZWRFUHDWH DE LVRQO\JRLQJWRZRUU\ MHFWQHHGVDSL]]D SL]]DV,IDQ\RWKHUREHFWWRFRPHWR FUHDWHGWKLVLVWKHREM

LP S

6

.IK\WZQM[PIVLTM\PMLM\IQT[WN WJRMK\KZMI\QWV7VKM_MPI^M I;QUXTM8QbbI.IK\WZaW]ZWZLMZ8QbbIUM\PWLR][\JMKWUM[ IKTQMV\WN \PI\WJRMK\)Va\QUMQ\VMML[IXQbbIUQ\I[S[ \PMXQbbINIK\WZa\WUISMWVM/WVMIZM\PMLIa[_PMV\PM WZLMZ8QbbIUM\PWLVMML[\WSVW_IJW]\/ZMMS^MZ[][+TIU XQbbI[6W_\PMWZLMZ8QbbIUM\PWLR][\KIZM[\PI\Q\OM\[I XQbbI_PQKPQUXTMUMV\[\PM8QbbIQV\MZNIKM[W\PI\Q\KIVKITT XZMXIZMJISMK]\IVLJW`

WR U\

:H·YHJRWDQDPHIRUWKLVQHZREMHFWZH FDOOLWD)DFWRU\

OH3L]]D)DF

?M¼^M[\QTTOW\INM_LM\IQT[\WÅTTQVPMZM#NWZQV[\IVKM_PI\LWM[ \PMWZLMZ8QbbIUM\PWLZMXTIKMQ\[KZMI\QWVKWLM_Q\P'4M\¼[ QUXTMUMV\I[QUXTMNIK\WZaNWZ\PMXQbbI[\WZMIVLÅVLW]\ 

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 7

WKHIDFWRU\SDWWHUQ

*]QTLQVOI[QUXTMXQbbINIK\WZa ?M¼TT[\IZ\_Q\P\PMNIK\WZaQ\[MTN?PI\_M¼ZMOWQVO\WLWQ[LMÅVMIKTI[[\PI\MVKIX[]TI\M[\PMWJRMK\ KZMI\QWVNWZITTXQbbI[0MZMQ\Q[

SOH3L]]D)DFWRU\,WKDV +HUH·VRXUQHZFODVVWKH6LP DVIRULWVFOLHQWV RQHMRELQOLIHFUHDWLQJSL]]

SXEOLFFODVV6LPSOH3L]]D)DFWRU\^ SXEOLF3L]]DFUHDWH3L]]D 6WULQJW\SH ^ 3L]]DSL]]D QXOO

 LQHD GLQ HGHI PHWKR WKH R Z  W )LUV WH3L]]D 7KLVLV LOOXVHW FUHD IDFWRU\ OLHQWVZ FWV WKH KRGDOOF QHZREMH PHW QWLDWH LQVWD

LI W\SHHTXDOV ³FKHHVH´ ^ SL]]D QHZ&KHHVH3L]]D  `HOVHLI W\SHHTXDOV ³SHSSHURQL´ ^ SL]]D QHZ3HSSHURQL3L]]D  `HOVHLI W\SHHTXDOV ³FODP´ ^ SL]]D QHZ&ODP3L]]D  `HOVHLI W\SHHTXDOV ³YHJJLH´ ^ SL]]D QHZ9HJJLH3L]]D  ` UHWXUQSL]]D ` `

+HUH·VWKHFRGHZH SOXFNHGRXWRIWKH RUGHU3L]]D PHWKRG

7KLVFRGHLVVWLOOSDUDPHWHUL]HGE\ WKHW\SHRIWKHSL]]DMXVWOLNHRXU RULJLQDORUGHU3L]]D PHWKRGZDV

Q:

:KDW·VWKHDGYDQWDJHRIWKLV" ,WORRNVOLNHZHDUHMXVWSXVKLQJWKH SUREOHPRIIWRDQRWKHUREMHFW

A:

2QHWKLQJWRUHPHPEHULVWKDWWKH 6LPSOH3L]]D)DFWRU\PD\KDYHPDQ\FOLHQWV :H·YHRQO\VHHQWKHRUGHU3L]]D PHWKRG KRZHYHUWKHUHPD\EHD3L]]D6KRS0HQX FODVVWKDWXVHVWKHIDFWRU\WRJHWSL]]DV IRUWKHLUFXUUHQWGHVFULSWLRQDQGSULFH:H PLJKWDOVRKDYHD+RPH'HOLYHU\FODVVWKDW KDQGOHVSL]]DVLQDGLIIHUHQWZD\WKDQRXU

there are no Dumb Questions 3L]]D6KRSFODVVEXWLVDOVRDFOLHQWRIWKH IDFWRU\ 6RE\HQFDSVXODWLQJWKHSL]]DFUHDWLQJ LQRQHFODVVZHQRZKDYHRQO\RQH SODFHWRPDNHPRGLILFDWLRQVZKHQWKH LPSOHPHQWDWLRQFKDQJHV 'RQ·WIRUJHWZHDUHDOVRMXVWDERXWWR UHPRYHWKHFRQFUHWHLQVWDQWLDWLRQVIURPRXU FOLHQWFRGH

Q:

,·YHVHHQDVLPLODUGHVLJQZKHUH DIDFWRU\OLNHWKLVLVGHILQHGDVDVWDWLF PHWKRG:KDWLVWKHGLIIHUHQFH"

A:

'HILQLQJDVLPSOHIDFWRU\DVD VWDWLFPHWKRGLVDFRPPRQWHFKQLTXHDQG LVRIWHQFDOOHGDVWDWLFIDFWRU\:K\XVHD VWDWLFPHWKRG"%HFDXVH\RXGRQ·WQHHG WRLQVWDQWLDWHDQREMHFWWRPDNHXVHRIWKH FUHDWHPHWKRG%XWUHPHPEHULWDOVRKDV WKHGLVDGYDQDJHWKDW\RXFDQ·WVXEFODVVDQG FKDQJHWKHEHKDYLRURIWKHFUHDWHPHWKRG \RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 8

VLPSOHIDFWRU\

:M_WZSQVO\PM8QbbI;\WZMKTI[[ 6W_Q\¼[\QUM\WÅ`]XW]ZKTQMV\KWLM?PI\_M_IV\\WLWQ[ZMTaWV\PM NIK\WZa\WKZMI\M\PMXQbbI[NWZ][0MZMIZM\PMKPIVOM["

1RZZHJLYH3L]]D6WRUHDUHIHUHQFH WRD6LPSOH3L]]D)DFWRU\ SXEOLFFODVV3L]]D6WRUH^ 6LPSOH3L]]D)DFWRU\IDFWRU\  3L]]D6WRUHJHWVWKHIDFWRU\S SXEOLF3L]]D6WRUH 6LPSOH3L]]D)DFWRU\IDFWRU\ ^ DVVHGWR LWLQWKHFRQVWUXFWRU WKLVIDFWRU\ IDFWRU\ `  SXEOLF3L]]DRUGHU3L]]D 6WULQJW\SH ^ 3L]]DSL]]D  SL]]D IDFWRU\FUHDWH3L]]D W\SH   SL]]DSUHSDUH  $QGWKHRUGHU3L]]D PHWKRGXVHVWKH SL]]DEDNH  IDFWRU\WRFUHDWHLWVSL]]DVE\VLPSO\ SL]]DFXW  SDVVLQJRQWKHW\SHRIWKHRUGHU SL]]DER[  UHWXUQSL]]D ` RWKHUPHWKRGVKHUH `

1RWLFHWKDWZH·YHUHSODFHGWKHQHZ RSHUDWRUZLWKDFUHDWHPHWKRGRQWKH IDFWRU\REMHFW1RPRUHFRQFUHWH LQVWDQWLDWLRQVKHUH

YiX`e gfn\i :HNQRZWKDWREMHFWFRPSRVLWLRQDOORZVXVWRFKDQJHEHKDYLRUG\QDPLFDOO\DWUXQWLPH DPRQJ RWKHUWKLQJV EHFDXVHZHFDQVZDSLQDQGRXWLPSOHPHQWDWLRQV+RZPLJKWZHEHDEOHWRXVH WKDWLQWKH3L]]D6WRUH":KDWIDFWRU\LPSOHPHQWDWLRQVPLJKWZHEHDEOHWRVZDSLQDQGRXW"

:HGRQ¶WNQRZDERXW\RXEXWZH¶UHWKLQNLQJ1HZ


&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 9

WKHIDFWRU\SDWWHUQ


)LUVW +HDG DEOH RU +RQ WLRQ 0HQ


3DWWHUQ +RQRUDEOH 0HQWLRQ

2][\JMKI][M;QUXTM.IK\WZaQ[V¼\I:-)4XI\\MZVLWM[V¼\UMIV_M[PW]TLV¼\KPMKSW]\ PW_Q\¼[X]\\WOM\PMZ4M\¼[\ISMITWWSI\\PMKTI[[LQIOZIUWN W]ZVM_8QbbI;\WZM"

7KLVLVWKHIDFWRU\ZKHUHZHFUHDWH SL]]DVLWVKRXOGEHWKHRQO\SDUW RIRXUDSSOLFDWLRQWKDWUHIHUVWR FRQFUHWH3L]]DFODVVHV

3L]]D6WRUH RUGHU3L]]D

RI 7KLVLVWKHSURGXFWD L]] S U\ WR DF WKHI D :H·YHGHILQHG3L]]V ODV F FW UD DVDQDEVW ZLWKVRPHKHOSIXO W LPSOHPHQWDWLRQVWKD FDQEHRYHUULGGHQ

3L]]D

6LPSOH3L]]D)DFWRU\ FUHDWH3L]]D

SUHSDUH EDNH FXW

QWRIWKH 7KLVLVWKHFOLH 6WRUH ]D L] 3 \ IDFWRU XJKWKH QRZJRHVWKURDFWRU\WRJHW ) ]D L] H3 SO 6LP L]]D LQVWDQFHVRIS

GLVRIWHQ 7KHFUHDWHPHWKR  OO\ LFD GHFODUHGVWDW

7KHVHDUHRXUFRQFUHWHSURGXFWV SURGXFWQHHGVWRLPSOHPHQWWKH3(DFK LQWHUIDFH  ZKLFKLQWKLVFDVHPHDL]]D ´H[WHQGWKHDEVWUDFW3L]]DFODVVµ QV EHFRQFUHWH$VORQJDVWKDW·VW DQG LWFDQEHFUHDWHGE\WKHIDFWRU\KHFDVH DQG KDQGHGEDFNWRWKHFOLHQW

ER[

3HSSHURQL3L]]D

&KHHVH3L]]D

9HJJLH3L]]D

&ODP3L]]D


-XVWDQRWKHUUHPLQGHULQGHVLJQSDWWHUQVWKHSKUDVH´LPSOHPHQWDQLQWHUIDFHµGRHV127DOZD\VPHDQ ´ZULWHDFODVVWKHLPSOHPHQWVD-DYDLQWHUIDFHE\XVLQJWKH´LPSOHPHQWVµNH\ZRUGLQWKHFODVVGHFODUDWLRQµ,Q WKHJHQHUDOXVHRIWKHSKUDVHDFRQFUHWHFODVVLPSOHPHQWLQJDPHWKRGIURPDVXSHUW\SH ZKLFKFRXOGEHD FODVV25LQWHUIDFH LVVWLOOFRQVLGHUHGWREH´LPSOHPHQWLQJWKHLQWHUIDFHµRIWKDWVXSHUW\SH \RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 10

SL]]DIUDQFKLVH

.ZIVKPQ[QVO\PMXQbbI[\WZM AW]Z7JRMK\^QTTM8QbbI;\WZMPI[LWVM[W_MTT\PI\aW]¼^M\ZW]VKML \PMKWUXM\Q\QWVIVLVW_M^MZaWVM_IV\[I8QbbI;\WZMQV\PMQZ W_VVMQOPJWZPWWL)[\PMNZIVKPQ[MZaW]_IV\\WMV[]ZM\PM Y]ITQ\aWN \PMNZIVKPQ[MWXMZI\QWV[IVL[WaW]_IV\\PMU\W][M aW]Z\QUM\M[\MLKWLM *]\_PI\IJW]\ZMOQWVITLQNNMZMVKM['-IKPNZIVKPQ[MUQOP\ _IV\\WWNNMZLQNNMZMV\[\aTM[WN XQbbI[6M_AWZS+PQKIOWIVL +ITQNWZVQI\WVIUMINM_LMXMVLQVOWV_PMZM\PMNZIVKPQ[M [\WZMQ[TWKI\MLIVL\PM\I[\M[WN \PMTWKITXQbbIKWVVWQ[[M]Z[



1

L]]D)DFWR

2QHIUDQFKLVHZDQWVDIDFWRU\ WKDWPDNHV1<VW\OHSL]]DV WKLQFUXVWWDVW\VDXFHDQG MXVWDOLWWOHFKHHVH



  3L ]

<3

U\


&

KL FDJ

FW RU\

]D6WRUH

D R3L]]D)

$QRWKHUIUDQFKLVH ZDQWVDIDFWRU\WKDW PDNHV&KLFDJRVW\OH SL]]DVWKHLUFXVWRPHUV OLNHSL]]DVZLWKWKLFN FUXVWULFKVDXFHDQG WRQVRIFKHHVH

?M¼^M[MMVWVMIXXZWIKP 1N _M\ISMW]\;QUXTM8QbbI.IK\WZaIVLKZMI\M\PZMMLQNNMZMV\ NIK\WZQM[6A8QbbI.IK\WZa+PQKIOW8QbbI.IK\WZaIVL +ITQNWZVQI8QbbI.IK\WZa\PMV_MKIVR][\KWUXW[M\PM8QbbI;\WZM _Q\P\PMIXXZWXZQI\MNIK\WZaIVLINZIVKPQ[MQ[OWWL\WOW


&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 11

WKHIDFWRU\SDWWHUQ

1<3L]]D)DFWRU\Q\)DFWRU\ QHZ1<3L]]D)DFWRU\  3L]]D6WRUHQ\6WRUH QHZ3L]]D6WRUH Q\)DFWRU\  Q\6WRUHRUGHU ³9HJJLH´ 

+HUHZHFUHDWHDIDFWRU\ IRUPDNLQJ1<VW\OHSL]]DV

7KHQZHFUHDWHD3L]]D6WRUHDQGSDVVLW DUHIHUHQFHWRWKH1<IDFWRU\ DQGZKHQZHPDNHSL]]DVZH JHW1<VW\OHGSL]]DV

&KLFDJR3L]]D)DFWRU\FKLFDJR)DFWRU\ QHZ&KLFDJR3L]]D)DFWRU\  3L]]D6WRUHFKLFDJR6WRUH QHZ3L]]D6WRUH FKLFDJR)DFWRU\  FKLFDJR6WRUHRUGHU ³9HJJLH´ 

/LNHZLVHIRUWKH&KLFDJRSL]]DVWRUHVZHFUHDWH DIDFWRU\IRU&KLFDJRSL]]DVDQGFUHDWHDVWRUH WKDWLVFRPSRVHGZLWKD&KLFDJRIDFWRU\:KHQ ZHPDNHSL]]DVZHJHWWKH&KLFDJRIODYRUHG RQHV

*]\aW]¼LTQSMITQ\\TMUWZMY]ITQ\aKWV\ZWT ;WaW]\M[\UIZSM\ML\PM;QUXTM.IK\WZaQLMIIVL_PI\aW] NW]VL_I[\PI\\PMNZIVKPQ[M[_MZM][QVOaW]ZNIK\WZa\W KZMI\MXQbbI[J]\[\IZ\QVO\WMUXTWa\PMQZW_VPWUMOZW_V XZWKML]ZM[NWZ\PMZM[\WN \PMXZWKM[["\PMa¼LJISM\PQVO[I TQ\\TMLQNNMZMV\Ta\PMa¼LNWZOM\\WK]\\PMXQbbIIVL\PMa¼L][M \PQZLXIZ\aJW`M[

,·YHEHHQPDNLQJSL]]DIRU \HDUVVR,WKRXJKW,·GDGGP\ RZQ´LPSURYHPHQWVµWRWKH 3L]]D6WRUHSURFHGXUHV

:M\PQVSQVO\PMXZWJTMUIJQ\aW][MM\PI\_PI\aW]¼LZMITTa TQSM\WLWQ[KZMI\MINZIUM_WZS\PI\\QM[\PM[\WZMIVL\PM XQbbIKZMI\QWV\WOM\PMZaM\[\QTTITTW_[\PQVO[\WZMUIQV ÆM`QJTM 1VW]ZMIZTaKWLMJMNWZM\PM;QUXTM8QbbI.IK\WZa_MPIL \PMXQbbIUISQVOKWLM\QML\W\PM8QbbI;\WZMJ]\Q\_I[V¼\ ÆM`QJTM;WPW_KIV_MPI^MW]ZXQbbIIVLMI\Q\\WW'

JRRG 1RWZKDW\RXZDQWLQDZDQWWRNQRZ 7 12 GR IUDQFKLVH
\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 12

OHWWKHVXEFODVVHVGHFLGH

)NZIUM_WZSNWZ\PMXQbbI[\WZM
3L]]D6WRUHLVQRZDEVWUDFW VHHZK\EHORZ  SXEOLFDEVWUDFWFODVV3L]]D6WRUH^

 

SXEOLF3L]]DRUGHU3L]]D 6WULQJW\SH ^  3L]]DSL]]D





SL]]D FUHDWH3L]]D W\SH 

   

   

SL]]DSUHSDUH  SL]]DEDNH  SL]]DFXW  SL]]DER[ 

  

 `

UHWXUQSL]]D

 `

DEVWUDFWFUHDWH3L]]D 6WULQJW\SH 

1RZFUHDWH3L]]DLVEDFNWREHLQJD FDOOWRDPHWKRGLQWKH3L]]D6WRUH UDWKHUWKDQRQDIDFWRU\REMHFW

$OOWKLVORRNVMXVWWKHVDPH

1RZZH·YHPRYHGRXUIDFWRU\ REMHFWWRWKLVPHWKRG

2XU´IDFWRU\PHWKRGµLVQRZ DEVWUDFWLQ3L]]D6WRUH

6W__M¼^MOW\I[\WZM_IQ\QVONWZ[]JKTI[[M[#_M¼ZMOWQVO\WPI^MI []JKTI[[NWZMIKPZMOQWVIT\aXM6A8QbbI;\WZM+PQKIOW8QbbI;\WZM +ITQNWZVQI8QbbI;\WZMIVLMIKP[]JKTI[[Q[OWQVO\WUISM\PMLMKQ[QWVIJW]\ _PI\UISM[]XIXQbbI4M\¼[\ISMITWWSI\PW_\PQ[Q[OWQVO\W_WZS



&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 13

WKHIDFWRU\SDWWHUQ

)TTW_QVO\PM[]JKTI[[M[\WLMKQLM :MUMUJMZ\PM8QbbI;\WZMITZMILaPI[I_MTTPWVMLWZLMZ[a[\MUQV\PMWZLMZ8QbbI UM\PWLIVLaW]_IV\\WMV[]ZM\PI\Q\¼[KWV[Q[\MV\IKZW[[ITTNZIVKPQ[M[ ?PI\^IZQM[IUWVO\PMZMOQWVIT8QbbI;\WZM[Q[\PM[\aTMWN XQbbI[\PMaUISM·6M_AWZS 8QbbIPI[\PQVKZ][\+PQKIOW8QbbIPI[\PQKSIVL[WWV·IVL_MIZMOWQVO\WX][PITT \PM[M^IZQI\QWV[QV\W\PMKZMI\M8QbbIUM\PWLIVLUISMQ\ZM[XWV[QJTMNWZKZMI\QVO\PM ZQOP\SQVLWN XQbbI
(DFKVXEFODVVRYHUULGHVWKHFUHDWH3L]]D  PHWKRGZKLOHDOOVXEFODVVHVPDNHXVH RIWKHRUGHU3L]]D PHWKRGGHILQHG LQ3L]]D6WRUH:HFRXOGPDNHWKH RUGHU3L]]D PHWKRGILQDOLIZHUHDOO\ ZDQWHGWRHQIRUFHWKLV

3L]]D6WRUH FUHDWH3L]]D RUGHU3L]]D

,IDIUDQFKLVHZDQWV1<VW\OH SL]]DVIRULWVFXVWRPHUVLW XVHVWKH1<VXEFODVVZKLFKKDV LWVRZQFUHDWH3L]]D PHWKRG FUHDWLQJ1<VW\OHSL]]DV

1<6W\OH3L]]D6WRUH

&KLFDJR6W\OH3L]]D6WRUH FUHDWH3L]]D

FUHDWH3L]]D

5HPHPEHUFUHDWH3L]]D LV DEVWUDFWLQ3L]]D6WRUHVRDOO SL]]DVWRUHVXEW\SHV0867 LPSOHPHQWWKHPHWKRG

6LPLODUO\E\XVLQJWKH &KLFDJRVXEFODVVZHJHWDQ LPSOHPHQWDWLRQRIFUHDWH3L]]D  ZLWK&KLFDJRLQJUHGLHQWV

SXEOLF3L]]DFUHDWH3L]]D W\SH ^

SXEOLF3L]]DFUHDWH3L]]D W\SH ^

LI W\SHHTXDOV ³FKHHVH´ ^

LI W\SHHTXDOV ³FKHHVH´ ^

SL]]D QHZ&KLFDJR6W\OH&KHHVH3L]]D 

SL]]D QHZ1<6W\OH&KHHVH3L]]D 

`HOVHLI W\SHHTXDOV ³SHSSHURQL´ ^

`HOVHLI W\SHHTXDOV ³SHSSHURQL´ ^

SL]]D QHZ&KLFDJR6W\OH3HSSHURQL3L]]D 

SL]]D QHZ1<6W\OH3HSSHURQL3L]]D 

`HOVHLI W\SHHTXDOV ³FODP´ ^

`HOVHLI W\SHHTXDOV ³FODP´ ^

SL]]D QHZ&KLFDJR6W\OH&ODP3L]]D 

SL]]D QHZ1<6W\OH&ODP3L]]D 

`HOVHLI W\SHHTXDOV ³YHJJLH´ ^

`HOVHLI W\SHHTXDOV ³YHJJLH´ ^

SL]]D QHZ&KLFDJR6W\OH9HJJLH3L]]D 

SL]]D QHZ1<6W\OH9HJJLH3L]]D 

`

`

`

`

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 14

KRZGRVXEFODVVHVGHFLGH"

,GRQ·WJHWLW7KH 3L]]D6WRUHVXEFODVVHVDUHMXVW VXEFODVVHV+RZDUHWKH\GHFLGLQJ DQ\WKLQJ",GRQ·WVHHDQ\ORJLFDOGHFLVLRQ PDNLQJFRGHLQ1<6W\OH3L]]D6WRUH

?MTT\PQVSIJW]\Q\NZWU\PMXWQV\WN ^QM_WN \PM8QbbI;\WZM¼[WZLMZ8QbbIUM\PWL"Q\Q[ LMÅVMLQV\PMIJ[\ZIK\8QbbI;\WZMJ]\KWVKZM\M\aXM[IZMWVTaKZMI\MLQV\PM[]JKTI[[M[

3L]]D6WRUH FUHDWH3L]]D RUGHU3L]]D

QWKHDEVWUDFW RUGHU3L]]D LVGHILQHGL DVVHV6RWKH EFO VX WKH 3L]]D6WRUHQRW VXEFODVVLVDFWXDOO\ PHWKRGKDVQRLGHDZKLFKNLQJWKHSL]]DV PD QG HD FRG UXQQLQJWKH

6W_\W\ISM\PQ[ITQ\\TMN]Z\PMZ\PMWZLMZ8QbbIUM\PWLLWM[ITW\WN \PQVO[_Q\PI 8QbbIWJRMK\TQSMXZMXIZMJISMK]\JW`J]\JMKI][M8QbbIQ[IJ[\ZIK\WZLMZ8QbbIPI[ VWQLMI_PI\ZMITKWVKZM\MKTI[[M[IZMQV^WT^ML1VW\PMZ_WZL[Q\¼[LMKW]XTML 3L]]D6WRUH

SL]]D FUHDWH3L]]D  SL]]DSUHSDUH 

FUHDWH3L]]D

SL]]DEDNH 

RUGHU3L]]D

SL]]DFXW  SL]]DER[ 

3L]]D WRDFWXDOO\JHW RUGHU3L]]D FDOOVFUHDWHLFKNLQGRISL]]DZLOOLW DSL]]DREMHFW%XWZK PHWKRGFDQ·WGHFLGHLW JHW"7KHRUGHU3L]]D GRHVGHFLGH" GRHVQ·WNQRZKRZ6RZKR ?PMVWZLMZ8QbbIKITT[KZMI\M8QbbIWVMWN aW]Z[]JKTI[[M[_QTTJMKITTMLQV\WIK\QWV\W KZMI\MIXQbbI?PQKPSQVLWN XQbbI_QTTJMUILM'?MTT\PI\¼[LMKQLMLJa\PMKPWQKMWN  XQbbI[\WZMaW]WZLMZNZWU6A;\aTM8QbbI;\WZMWZ+PQKIOW;\aTM8QbbI;\WZM

1<6W\OH3L]]D6WRUH FUHDWH3L]]D

&KLFDJR6W\OH3L]]D6WRUH FUHDWH3L]]D

;WQ[\PMZMIZMIT\QUMLMKQ[QWV\PI\[]JKTI[[M[UISM'6WJ]\NZWU\PMXMZ[XMK\Q^MWN  WZLMZ8QbbIQN aW]KPW[MI6A;\aTM8QbbI;\WZM\PI\[]JKTI[[OM\[\WLM\MZUQVM_PQKP XQbbIQ[UILM;W\PM[]JKTI[[M[IZMV¼\ZMITTa¹LMKQLQVOº·Q\_I[aW]_PWLMKQLMLJa KPWW[QVO_PQKP[\WZMaW]_IV\ML·J]\\PMaLWLM\MZUQVM_PQKPSQVLWN XQbbIOM\[UILM



&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 15

WKHIDFWRU\SDWWHUQ

4M\¼[UISMI8QbbI;\WZM *MQVOINZIVKPQ[MPI[Q\[JMVMÅ\[AW]OM\ITT\PM8QbbI;\WZM N]VK\QWVITQ\aNWZNZMM)TT\PMZMOQWVIT[\WZM[VMML\WLWQ[[]JKTI[[ 8QbbI;\WZMIVL[]XXTaIKZMI\M8QbbIUM\PWL\PI\QUXTMUMV\[ \PMQZ[\aTMWN 8QbbI?M¼TT\ISMKIZMWN \PMJQO\PZMMXQbbI[\aTM[NWZ \PMNZIVKPQ[MM[ 0MZM¼[\PM6M_AWZSZMOQWVIT[\aTM"

3L]]DDQGWKH FUHDWH3L]]D UHWXUQVDOHIRUZKLFK QVLE VSR VXEFODVVLVIXOO\UH LDWHV FRQFUHWH3L]]DLWLQVWDQW

SXEOLFFODVV1<3L]]D6WRUHH[WHQGV3L]]D6WRUH^ 3L]]DFUHDWH3L]]D 6WULQJLWHP ^ LI LWHPHTXDOV ³FKHHVH´ ^ UHWXUQQHZ1<6W\OH&KHHVH3L]]D  `HOVHLI LWHPHTXDOV ³YHJJLH´ ^ UHWXUQQHZ1<6W\OH9HJJLH3L]]D  `HOVHLI LWHPHTXDOV ³FODP´ ^ UHWXUQQHZ1<6W\OH&ODP3L]]D  `HOVHLI LWHPHTXDOV ³SHSSHURQL´ ^ UHWXUQQHZ1<6W\OH3HSSHURQL3L]]D  `HOVHUHWXUQQXOO ` `

7KH1<3L]]D6WRUHH[WHQGV 3L]]D6WRUHVRLWLQKHULWVWKH RUGHU3L]]D PHWKRG DPRQJRWKHUV 

:H·YHJRWWRLPSOHPHQW FUHDWH3L]]D VLQFHLWLV DEVWUDFWLQ3L]]D6WRUH

+HUH·VZKHUHZHFUHDWHRXU FRQFUHWHFODVVHV)RUHDFKW\SH RI3L]]DZHFUHDWHWKH1<VW\OH

1RWHWKDWWKHRUGHU3L]]D PHWKRGLQWKH VXSHUFODVVKDVQRFOXHZKLFK3L]]DZHDUHFUHDWLQJLW MXVWNQRZVLWFDQSUHSDUHEDNHFXWDQGER[LW 7VKM_M¼^MOW\W]Z8QbbI;\WZM[]JKTI[[M[J]QT\Q\_QTTJM\QUM \W[MMIJW]\WZLMZQVO]XIXQbbIWZ\_W*]\JMNWZM_MLW\PI\ _PaLWV¼\aW]\ISMIKZIKSI\J]QTLQVO\PM+PQKIOW;\aTMIVL +ITQNWZVQI;\aTMXQbbI[\WZM[WV\PMVM`\XIOM

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Page 16

Return to Table of Contents

IDFWRU\PHWKRG

6KDUSHQ \RXU SHQFLO ?M¼^MSVWKSMLW]\\PM6A8QbbI;\WZMR][\\_WUWZM\WOWIVL_M¼TTJMZMILa\WNZIVKPQ[M  ?ZQ\M\PM+PQKIOWIVL+ITQNWZVQI8QbbI;\WZMQUXTMUMV\I\QWV[PMZM"



&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 17

WKHIDFWRU\SDWWHUQ

,MKTIZQVOINIK\WZaUM\PWL ?Q\PR][\IKW]XTMWN \ZIV[NWZUI\QWV[\W\PM8QbbI;\WZM_M¼^MOWVMNZWU PI^QVOIVWJRMK\PIVLTM\PMQV[\IV\QI\QWVWN W]ZKWVKZM\MKTI[[M[\WI[M\WN  []JKTI[[M[\PI\IZMVW_\ISQVOWV\PI\ZM[XWV[QJQTQ\a4M\¼[\ISMIKTW[MZTWWS"

 7KHVXEFODVVHVRIGOHREMHFW DQ K UH WR D6 ]] 3L XVLQWKH LQVWDQWLDWLRQIRU WKRG PH  D FUHDWH3L]]

SXEOLFDEVWUDFWFODVV3L]]D6WRUH^

SXEOLF3L]]DRUGHU3L]]D 6WULQJW\SH ^ 3L]]DSL]]D

1<6W\OH3L]]D6WRUH FUHDWH3L]]D

SL]]D FUHDWH3L]]D W\SH  SL]]DSUHSDUH  SL]]DEDNH  SL]]DFXW  SL]]DER[ 

&KLFDJR6W\OH3L]]D6WRUH FUHDWH3L]]D

UHWXUQSL]]D ` SURWHFWHGDEVWUDFW3L]]DFUHDWH3L]]D 6WULQJW\SH  RWKHUPHWKRGVKHUH `

$OOWKHUHVSRQVLELOLW\IRU LQVWDQWLDWLQJ3L]]DVKDVEHHQ PRYHGLQWRDPHWKRGWKDW DFWVDVDIDFWRU\

&RGH8S&ORVH )NIK\WZaUM\PWLPIVLTM[WJRMK\KZMI\QWVIVLMVKIX[]TI\M[Q\QV I[]JKTI[[
KRGPD\EH $IDFWRU\PHW RUQRW  G ]H UL SDUDPHWH VHYHUDO WRVHOHFWDPRQJSURGXFW D I R QV LR YDULDW

DEVWUDFW3URGXFWIDFWRU\0HWKRG 6WULQJW\SH

$IDFWRU\PHWKRG DEVWUDFWVRWKHVX LV $IDFWRU\PHWKRGUHWXUQV DUHFRXQWHGRQWREFODVVHV D3URGXFWWKDWLVW\SLFDOO\ REMHFWFUHDWLRQ KDQGOH XVHGZLWKLQPHWKRGVGHILQHG LQWKHVXSHUFODVV

ODWHVWKHFOLHQW WKH $IDFWRU\PHWKRGLVR LNHRUGHU3L]]D  FRGHLQWKHVXSHUFODVVO QGRIFRQFUHWH IURPNQRZLQJZKDWNL DWHG FUH O\ 3URGXFWLVDFWXDO

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 18

RUGHULQJDSL]]D

4M\¼[[MMPW_Q\_WZS["WZLMZQVOXQbbI[_Q\P \PMXQbbINIK\WZaUM\PWL

,OLNH1<6W\OHSL]]D\RX NQRZWKLQFULVS\FUXVW ZLWKDOLWWOHFKHHVHDQG UHDOO\JRRGVDXFH

(WKDQ

(WKDQQHHGVWRRUGHU KLVSL]]DIURPD1< SL]]DVWRUH

,OLNH&KLFDJRVW\OHGHHSGLVK SL]]DZLWKWKLFNFUXVWDQG WRQVRIFKHHVH

-RHO

-RHOQHHGVWRRUGHUKLV SL]]DIURPD&KLFDJR SL]]DVWRUH6DPHSL]]D RUGHULQJPHWKRGEXW GLIIHUHQWNLQGRISL]]D

;WPW_LW\PMaWZLMZ'





.QZ[\2WMTIVL-\PIVVMMLIVQV[\IVKMWN I8QbbI;\WZM2WMTVMML[\WQV[\IV\QI\MI +PQKIOW8QbbI;\WZMIVL-\PIVVMML[I6A8QbbI;\WZM



?Q\PI8QbbI;\WZMQVPIVLJW\P-\PIVIVL2WMTKITT\PMWZLMZ8QbbIUM\PWLIVLXI[[ QV\PM\aXMWN XQbbI\PMa_IV\KPMM[M^MOOQMIVL[WWV







&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 19

WKHIDFWRU\SDWWHUQ

4M\¼[KPMKSW]\PW_\PM[MXQbbI[IZM ZMITTaUILM\WWZLMZ



%HKLQG WKH6FHQHV

/HW·VIROORZ(WKDQ·VRUGHUÀUVWZHQHHGD1<3L]]D6WRUH 3L]]D6WRUHQ\3L]]D6WRUH QHZ1<3L]]D6WRUH 

&UHDWHVDLQVWDQFHRI 1<3L]]D6WRUH   Q \3

1RZWKDWZHKDYHDVWRUHZHFDQWDNHDQRUGHU

GLVFDOOHGRQ 7KHRUGHU3L]]D PHWKR  WKHPHWKRG QFH VWD HLQ WKHQ\3L]]D6WRU UXQV  GHILQHGLQVLGH3L]]D6WRUH



L]]D6WR

U

FUHDWH3L]]D ³FKHHVH´

Q\3L]]D6WRUHRUGHU3L]]D ³FKHHVH´ 

H





7KHRUGHU3L]]D PHWKRGWKHQFDOOVWKHFUHDWH3L]]D  PHWKRG 3L]]DSL]]D FUHDWH3L]]D ³FKHHVH´ 

5HPHPEHUFUHDWH3L]]D WKHIDFWRU\ PHWKRGLVLPSOHPHQWHGLQWKHVXEFODVV,Q WKLVFDVHLWUHWXUQVD1<&KHHVH3L]]D





   3L]]D

)LQDOO\ZHKDYHWKHXQSUHSDUHGSL]]DLQKDQGDQGWKH RUGHU3L]]D PHWKRGÀQLVKHVSUHSDULQJLW SL]]DSUHSDUH  SL]]DEDNH  SL]]DFXW  SL]]DER[ 

GJHWV 7KHRUGHU3L]]D PHWKRQRZLQJ WN KRX ZLW D ]] 3L EDFND DVVLWLV H[DFWO\ZKDWFRQFUHWHFO

GHILQHG $OORIWKHVHPHWKRGVDUH UQHG HWX DU ]] SL ILF LQWKHVSHFL RG IURPWKHIDFWRU\PHWK WKH LQ QHG HIL G ]D 3L] DWH FUH 1<3L]]D6WRUH

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 20

WKHSL]]DFODVVHV

?M¼ZMR][\UQ[[QVOWVM\PQVO"81BB) 7]Z8QbbI;\WZMQ[V¼\OWQVO\WJM^MZaXWX]TIZ _Q\PW]\[WUMXQbbI[[WTM\¼[QUXTMUMV\\PMU" UDFW LWKDQDEVW :H·OOVWDUWZDQGDOOWKHFRQFUHWH 3L]]DFODVV HULYHIURPWKLV SL]]DVZLOOG

HDW\SHRIGRXJKD

(DFK3L]]DKDVDQDP SXEOLFDEVWUDFWFODVV3L]]D^ 6WULQJQDPH W\SHRIVDXFHDQGDVHWRIWRSSLQJV 6WULQJGRXJK 6WULQJVDXFH $UUD\/LVWWRSSLQJV QHZ$UUD\/LVW   YRLGSUHSDUH ^ 7KHDEVWUDFWFODVVSURYLGHV 6\VWHPRXWSULQWOQ ³3UHSDULQJ³QDPH  VRPHEDVLFGHIDXOWVIRUEDNLQJ 6\VWHPRXWSULQWOQ ³7RVVLQJGRXJK´  FXWWLQJDQGER[LQJ 6\VWHPRXWSULQWOQ ³$GGLQJVDXFH´  6\VWHPRXWSULQWOQ ³$GGLQJWRSSLQJV³  IRU LQWL LWRSSLQJVVL]H L ^ 6\VWHPRXWSULQWOQ ³³WRSSLQJVJHW L  ` 3UHSDUDWLRQIROORZVD ` QXPEHURIVWHSVLQD  SDUWLFXODUVHTXHQFH YRLGEDNH ^ 6\VWHPRXWSULQWOQ ³%DNHIRUPLQXWHVDW´  `  YRLGFXW ^ 6\VWHPRXWSULQWOQ ³&XWWLQJWKHSL]]DLQWRGLDJRQDOVOLFHV´  `  YRLGER[ ^ 6\VWHPRXWSULQWOQ ³3ODFHSL]]DLQRI¿FLDO3L]]D6WRUHER[´  `  SXEOLF6WULQJJHW1DPH ^ UHWXUQQDPH ` `

5(0(0%(5ZHGRQ·WSURYLGHLPSRUWDQGSDFNDJHVWDWHPHQWVLQWKH FRGHOLVWLQJV*HWWKHFRPSOHWHVRXUFHFRGHIURPWKHZLFNHGO\VPDUW ZHEVLWH
&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 21

WKHIDFWRU\SDWWHUQ

6W__MR][\VMML[WUMKWVKZM\M[]JKTI[[M[PW_IJW]\LMNQVQVO 6M_AWZSIVL+PQKIOW[\aTMKPMM[MXQbbI['

SXEOLFFODVV1<6W\OH&KHHVH3L]]DH[WHQGV3L]]D^ SXEOLF1<6W\OH&KHHVH3L]]D ^ QDPH ³1<6W\OH6DXFHDQG&KHHVH3L]]D´ GRXJK ³7KLQ&UXVW'RXJK´ VDXFH ³0DULQDUD6DXFH´  WRSSLQJVDGG ³*UDWHG5HJJLDQR&KHHVH´  ` `

7KH1<3L]]DKDVLWVRZQ PDULQDUDVW\OHVDXFHDQGWKLQFUXVW

$QGRQHWRSSLQJUHJJLDQRF

KHHVH

7KH&KLFDJR3L]]DXVHVSOXP WRPDWRHVDVDVDXFHDORQJ ZLWKH[WUDWKLFNFUXVW

SXEOLFFODVV&KLFDJR6W\OH&KHHVH3L]]DH[WHQGV3L]]D^ SXEOLF&KLFDJR6W\OH&KHHVH3L]]D ^ QDPH ³&KLFDJR6W\OH'HHS'LVK&KHHVH3L]]D´ GRXJK ³([WUD7KLFN&UXVW'RXJK´ VDXFH ³3OXP7RPDWR6DXFH´  WRSSLQJVDGG ³6KUHGGHG0R]]DUHOOD&KHHVH´  7KH&KLFDJRVW\OHGHHS ` GLVKSL]]DKDVORWVRI  PR]]DUHOODFKHHVH YRLGFXW ^ 6\VWHPRXWSULQWOQ ³&XWWLQJWKHSL]]DLQWRVTXDUHVOLFHV´  ` `

7KH&KLFDJRVW\OHSL]]DDOVRRYHUULGHVWKHFXW  PHWKRGVRWKDWWKHSLHFHVDUHFXWLQWRVTXDUHV

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 22

PDNHVRPHSL]]DV

AW]¼^M_IQ\MLTWVOMVW]OP\QUMNWZ[WUMXQbbI[ WZR

ZHFUHDWH

)LUVW SXEOLFFODVV3L]]D7HVW'ULYH^ WRUHV GLIIHUHQWV  SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ 7KHQXVHRQHRQHVWRUH 3L]]D6WRUHQ\6WRUH QHZ1<3L]]D6WRUH  WRPDNH(WKDQ·VRUGHU 3L]]D6WRUHFKLFDJR6WRUH QHZ&KLFDJR3L]]D6WRUH   3L]]DSL]]D Q\6WRUHRUGHU3L]]D ³FKHHVH´  6\VWHPRXWSULQWOQ ³(WKDQRUGHUHGD³SL]]DJHW1DPH ³?Q´   SL]]D FKLFDJR6WRUHRUGHU3L]]D ³FKHHVH´  6\VWHPRXWSULQWOQ ³-RHORUGHUHGD³SL]]DJHW1DPH ³?Q´  ` `

$QGWKHRWKHUIRU-RHO·V

)LOH(GLW:LQGRZ+HOS
MDYD3L]]D7HVW'ULYH 3UHSDULQJ1<6W\OH6DXFHDQG&KHHVH3L]]D 7RVVLQJGRXJK $GGLQJVDXFH $GGLQJWRSSLQJV *UDWHG5HJLDQRFKHHVH %DNHIRUPLQXWHVDW &XWWLQJWKHSL]]DLQWRGLDJRQDOVOLFHV 3ODFHSL]]DLQRI¿FLDO3L]]D6WRUHER[ (WKDQRUGHUHGD1<6W\OH6DXFHDQG&KHHVH3L]]D 3UHSDULQJ&KLFDJR6W\OH'HHS'LVK&KHHVH3L]]D 7RVVLQJGRXJK $GGLQJVDXFH $GGLQJWRSSLQJV 6KUHGGHG0R]]DUHOOD&KHHVH %DNHIRUPLQXWHVDW &XWWLQJWKHSL]]DLQWRVTXDUHVOLFHV 3ODFHSL]]DLQRI¿FLDO3L]]D6WRUHER[ -RHORUGHUHGD&KLFDJR6W\OH'HHS'LVK&KHHVH3L]]D



%RWKSL]]DVJHWSUHSDUHG  WKHWRSSLQJVDGGHGDQGWKH  SL]]DVEDNHGFXWDQGER[HG 2XUVXSHUFODVVQHYHUKDGWRVV NQRZWKHGHWDLOVWKHVXEFOD KDQGOHGDOOWKDWMXVWE\ LQVWDQWLDWLQJWKHULJKWSL]]D

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 23

WKHIDFWRU\SDWWHUQ

1\¼[NQVITTa\QUM\WUMM\\PM.IK\WZa5M\PWL8I\\MZV )TTNIK\WZaXI\\MZV[MVKIX[]TI\MWJRMK\KZMI\QWV

2IWHQWKHFUHDWRUFRQWDLQVFRGHWKDW GHSHQGVRQDQDEVWUDFWSURGXFWZKLFK LVSURGXFHGE\DVXEFODVV7KHFUHDWRU QHYHUUHDOO\NQRZVZKLFKFRQFUHWH SURGXFWZDVSURGXFHG

3L]]D6WRUH FUHDWH3L]]D RUGHU3L]]D

&KLFDJR3L]]D6WRUH

1<3L]]D6WRUH

FUHDWH3L]]D

FUHDWH3L]]D

7KHFUH LVRXUIDDWH3L]]D PHWK SURGXFHVFWRU\PHWKRG RG ,W SURGXFW V

6LQFHHDFKIUDQFKLVHJHWVLWV RZQVXEFODVVRI3L]]D6WRUH LW·VIUHHWRFUHDWHLWV RZQVW\OHRISL]]DE\ LPSOHPHQWLQJFUHDWH3L]]D 

GXFH &ODVVHVWKDWSUR OHG DO F UH D WV XF SURG V FRQFUHWHFUHDWRU


7KHVHDUHWKHFRQFUHWH SURGXFWVDOOWKHSL]]DVWKDW DUHSURGXFHGE\RXUVWRUHV 1<6W\OH&KHHVH3L]]D 1<6W\OH3HSSHURQL3L]]D

1<6W\OH&ODP3L]]D 1<6W\OH9HJJLH3L]]D

)DFWRULHVSURGXFHSURGXFWV DQGLQWKH3L]]D6WRUHRXU SURGXFWLVD3L]]D

&KLFDJR6W\OH&KHHVH3L]]D &KLFDJR6W\OH3HSSHURQL3L]]D &KLFDJR6W\OH&ODP3L]]D

&KLFDJR6W\OH9HJJLH3L]]D

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 24

FUHDWRUVDQGSURGXFWV

)VW\PMZXMZ[XMK\Q^M"XIZITTMTKTI[[PQMZIZKPQM[ ?M¼^M[MMV\PI\\PMNIK\WZaUM\PWLXZW^QLM[INZIUM_WZSJa[]XXTaQVOIV WZLMZ8QbbIUM\PWL\PI\Q[KWUJQVML_Q\PINIK\WZaUM\PWL)VW\PMZ_Ia\WTWWS I\\PQ[XI\\MZVI[INZIUM_WZSQ[QV\PM_IaQ\MVKIX[]TI\M[XZWL]K\SVW_TMLOMQV\W MIKPKZMI\WZ 4M\¼[TWWSI\\PM\_WXIZITTMTKTI[[PQMZIZKPQM[IVL[MMPW_\PMaZMTI\M"


1RWLFHKRZWKHVH FODVVKLHUDUFKLHVDUH SDUDOOHOERWKKDYH DEVWUDFWFODVVHVWKDW DUHH[WHQGHGE\ FRQFUHWHFODVVHVZKLFK
1<6W\OH&KHHVH3L]]D 1<6W\OH3HSSHURQL3L]]D

1<6W\OH&ODP3L]]D 1<6W\OH9HJJLH3L]]D

&KLFDJR6W\OH&KHHVH3L]]D

&KLFDJR3L]]D6WRUH

1<3L]]D6WRUH

FUHDWH3L]]D

FUHDWH3L]]D

&KLFDJR6W\OH3HSSHURQL3L]]D &KLFDJR6W\OH&ODP3L]]D

&KLFDJR6W\OH9HJJLH3L]]D

 ODWHV DSVX ZWR RUH F Q H ]D6W  UH XWKR ] R L W 3 6 R H R K  DJ L]]D HDE &KLF DOOW ZWR 1<3 OHGJ ]DV 7KH DSVXODWHV DERXWKR SL]]DV 7KHWKHNQRZ W\OHSL] HQF OHGJH JRVW\OH DOO H1<V NQRZ H&KLFD PDN PDN

NQRZOHGJH DSVXODWLQJWKLV

HNH\WRHQF U\PHWKRGLVWK

7KHIDFWR



&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 25

WKHIDFWRU\SDWWHUQ

'HVLJQ3X]]OH ?MVMMLIVW\PMZSQVLWN XQbbINWZ\PW[MKZIba+ITQNWZVQIV[KZIbaQVIOWWL_Ia WN KW]Z[M,ZI_IVW\PMZXIZITTMT[M\WN KTI[[M[\PI\aW]¼LVMML\WILLIVM_ +ITQNWZVQIZMOQWV\WW]Z8QbbI;\WZM

3L]]D6WRUH FUHDWH3L]]D RUGHU3L]]D

UH

ZLQJKH

UD
1<6W\OH&KHHVH3L]]D 1<6W\OH3HSSHURQL3L]]D

1<6W\OH&ODP3L]]D 1<6W\OH9HJJLH3L]]D

&KLFDJR3L]]D6WRUH FUHDWH3L]]D

&KLFDJR6W\OH&KHHVH3L]]D &KLFDJR6W\OH3HSSHURQL3L]]D

&KLFDJR6W\OH&ODP3L]]D

&KLFDJR6W\OH9HJJLH3L]]D

7SIaVW__ZQ\M\PMÅ^MUW[\JQbIZZM\PQVO[aW]KIV\PQVSWN \WX]\WVIXQbbI
\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 26

IDFWRU\PHWKRGGH¿QHG

.IK\WZa5M\PWL8I\\MZVLMNQVML 1\¼[\QUM\WZWTTW]\\PMWNÅKQITLMÅVQ\QWVWN \PM.IK\WZa5M\PWL8I\\MZV"


)[_Q\PM^MZaNIK\WZa\PM.IK\WZa5M\PWL8I\\MZVOQ^M[][I_Ia\WMVKIX[]TI\M\PM QV[\IV\QI\QWV[WN KWVKZM\M\aXM[4WWSQVOI\\PMKTI[[LQIOZIUJMTW_aW]KIV[MM\PI\\PM IJ[\ZIK\+ZMI\WZOQ^M[aW]IVQV\MZNIKM_Q\PIUM\PWLNWZKZMI\QVOWJRMK\[IT[WSVW_VI[\PM ¹NIK\WZaUM\PWLº)VaW\PMZUM\PWL[QUXTMUMV\MLQV\PMIJ[\ZIK\+ZMI\WZIZM_ZQ\\MV\W WXMZI\MWVXZWL]K\[XZWL]KMLJa\PMNIK\WZaUM\PWL7VTa[]JKTI[[M[IK\]ITTaQUXTMUMV\ \PMNIK\WZaUM\PWLIVLKZMI\MXZWL]K\[ )[QV\PMWNÅKQITLMÅVQ\QWVaW]¼TTWN\MVPMIZLM^MTWXMZ[[Ia\PI\\PM.IK\WZa5M\PWLTM\[ []JKTI[[M[LMKQLM_PQKPKTI[[\WQV[\IV\QI\M
KDW WKHPZZHEHW  N V D  XOG W
QWDLQV DFODVVWKDWFR 7KH&UHDWRULV WLRQVIRUDOORIWKH WKHLPSOHPHQWD LSXODWHSURGXFWV PHWKRGVWRPDQIDFWRU\PHWKRG H[FHSWIRUWKH 3URGXFW

WLPSOHPHQW $OOSURGXFWVPXVDFHVRWKDWWKH UI WH WKHVDPHLQ HWKHSURGXFWV FODVVHVZKLFKXV KHLQWHUIDFH FDQUHIHUWRW WHFODVV QRWWKHFRQFUH

&UHDWRU IDFWRU\0HWKRG DQ2SHUDWLRQ

&RQFUHWH3URGXFW

&RQFUHWH&UHDWRU IDFWRU\0HWKRG

7KH&RQFUHWH&UHDWRULVUHVSRQVLEOHIRU FUHDWLQJRQHRUPRUHFRQFUHWHSURGXFWV,W LVWKHRQO\FODVVWKDWKDVWKHNQRZOHGJHRI KRZWRFUHDWHWKHVHSURGXFWV 

7KHDEVWUDFWIDFWRU\0HWKRG  LVZKDWDOO&UHDWRUVXEFODVVHV PXVWLPSOHPHQW

7KH&RQFUHWH&UHDWRU LPSOHPHQWVWKH IDFWRU\0HWKRG ZKLFKLV WKHPHWKRGWKDWDFWXDOO\ SURGXFHVSURGXFWV

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 27

WKHIDFWRU\SDWWHUQ

Q:

there are no Dumb Questions

:KDW·VWKHDGYDQWDJHRIWKH)DFWRU\0HWKRG 3DWWHUQZKHQ\RXRQO\KDYHRQH&RQFUHWH&UHDWRU"

A:

A:

:HLPSOHPHQWHGZKDWLVNQRZQDVWKH SDUDPHWHUL]HGIDFWRU\PHWKRG,WFDQPDNHPRUHWKDQ RQHREMHFWEDVHGRQDSDUDPHWHUSDVVHGLQDV\RX QRWLFHG2IWHQKRZHYHUDIDFWRU\MXVWSURGXFHVRQH REMHFWDQGLVQRWSDUDPHWHUL]HG%RWKDUHYDOLGIRUPV RIWKHSDWWHUQ

7KH)DFWRU\0HWKRG3DWWHUQLVXVHIXOLI \RX·YHRQO\JRWRQHFRQFUHWHFUHDWRUEHFDXVH\RXDUH GHFRXSOLQJWKHLPSOHPHQWDWLRQRIWKHSURGXFWIURP LWVXVH,I\RXDGGDGGLWLRQDOSURGXFWVRUFKDQJHD SURGXFW·VLPSOHPHQWDWLRQLWZLOOQRWDIIHFW\RXU&UHDWRU EHFDXVHWKH&UHDWRULVQRWWLJKWO\FRXSOHGWRDQ\ &RQFUHWH3URGXFW 

Q:

Q:

A:

:RXOGLWEHFRUUHFWWRVD\WKDWRXU1<DQG &KLFDJRVWRUHVDUHLPSOHPHQWHGXVLQJ6LPSOH )DFWRU\"7KH\ORRNMXVWOLNHLW

A:

7KH\·UHVLPLODUEXWXVHGLQGLIIHUHQWZD\V(YHQ WKRXJKWKHLPSOHPHQWDWLRQRIHDFKFRQFUHWHVWRUHORRNV DORWOLNHWKH6LPSOH3L]]D)DFWRU\UHPHPEHUWKDWWKH FRQFUHWHVWRUHVDUHH[WHQGLQJDFODVVZKLFKKDVGHILQHG FUHDWH3L]]D DVDQDEVWUDFWPHWKRG,WLVXSWRHDFK VWRUHWRGHILQHWKHEHKDYLRURIWKHFUHDWH3L]]D PHWKRG ,Q6LPSOH)DFWRU\WKHIDFWRU\LVDQRWKHUREMHFWWKDWLV FRPSRVHGZLWKWKH3L]]D6WRUH

Q:

$UHWKHIDFWRU\PHWKRGDQGWKH&UHDWRU DOZD\VDEVWUDFW"

A:

1R\RXFDQGHILQHDGHIDXOWIDFWRU\PHWKRG WRSURGXFHVRPHFRQFUHWHSURGXFW7KHQ\RXDOZD\V KDYHDPHDQVRIFUHDWLQJSURGXFWVHYHQLIWKHUHDUHQR VXEFODVVHVRIWKH&UHDWRU

Q:

(DFKVWRUHFDQPDNHIRXUGLIIHUHQWNLQGV RISL]]DVEDVHGRQWKHW\SHSDVVHGLQ'RDOO FRQFUHWHFUHDWRUVPDNHPXOWLSOHSURGXFWVRUGRWKH\ VRPHWLPHVMXVWPDNHRQH"



Q:

,·PVWLOODELWFRQIXVHGDERXWWKHGLIIHUHQFH EHWZHHQ6LPSOH)DFWRU\DQG)DFWRU\0HWKRG7KH\ ORRNYHU\VLPLODUH[FHSWWKDWLQ)DFWRU\0HWKRGWKH FODVVWKDWUHWXUQVWKHSL]]DLVDVXEFODVV&DQ\RX H[SODLQ"

A:


\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Page 28

Return to Table of Contents

PDVWHUDQGVWXGHQW

0DVWHUDQG6WXGHQW 0DVWHU*UDVVKRSSHUWHOOPHKRZ\RXUWUDLQLQJLVJRLQJ" 6WXGHQW0DVWHU,KDYHWDNHQP\VWXG\RI³HQFDSVXODWHZKDW YDULHV´IXUWKHU 0DVWHU*RRQ 6WXGHQW,KDYHOHDUQHGWKDWRQHFDQHQFDSVXODWHWKHFRGHWKDW FUHDWHVREMHFWV:KHQ\RXKDYHFRGHWKDWLQVWDQWLDWHVFRQFUHWH FODVVHVWKLVLVDQDUHDRIIUHTXHQWFKDQJH,¶YHOHDUQHGD WHFKQLTXHFDOOHG³IDFWRULHV´WKDWDOORZV\RXWRHQFDSVXODWHWKLV EHKDYLRURILQVWDQWLDWLRQ 0DVWHU$QGWKHVH³IDFWRULHV´RIZKDWEHQH¿WDUHWKH\" 6WXGHQW7KHUHDUHPDQ\%\SODFLQJDOOP\FUHDWLRQFRGHLQRQH REMHFWRUPHWKRG,DYRLGGXSOLFDWLRQLQP\FRGHDQGSURYLGHRQH SODFHWRSHUIRUPPDLQWHQDQFH7KDWDOVRPHDQVFOLHQWVGHSHQG RQO\XSRQLQWHUIDFHVUDWKHUWKDQWKHFRQFUHWHFODVVHVUHTXLUHGWR LQVWDQWLDWHREMHFWV$V,KDYHOHDUQHGLQP\VWXGLHVWKLVDOORZVPH WRSURJUDPWRDQLQWHUIDFHQRWDQLPSOHPHQWDWLRQDQGWKDWPDNHV P\FRGHPRUHÀH[LEOHDQGH[WHQVLEOHLQWKHIXWXUH 0DVWHU


&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 29

WKHIDFWRU\SDWWHUQ

)^MZaLMXMVLMV\8QbbI;\WZM 6KDUSHQ \RXU SHQFLO 4M\¼[XZM\MVLaW]¼^MVM^MZPMIZLWN IV77NIK\WZa0MZM¼[I^MZ[QWVWN \PM8QbbI;\WZM\PI\ LWM[V¼\][MINIK\WZa#UISMIKW]V\WN \PMV]UJMZWN KWVKZM\MXQbbIWJRMK\[\PQ[KTI[[Q[ LMXMVLMV\WV1N aW]ILLML+ITQNWZVQI[\aTMXQbbI[\W\PQ[8QbbI;\WZMPW_UIVaWJRMK\[_W]TLQ\ JMLMXMVLMV\WV\PMV' SXEOLFFODVV'HSHQGHQW3L]]D6WRUH^  SXEOLF3L]]DFUHDWH3L]]D 6WULQJVW\OH6WULQJW\SH ^ 3L]]DSL]]D QXOO LI VW\OHHTXDOV ³1<´ ^ LI W\SHHTXDOV ³FKHHVH´ ^ SL]]D QHZ1<6W\OH&KHHVH3L]]D  `HOVHLI W\SHHTXDOV ³YHJJLH´ ^ +DQGOHVDOOWKH1< SL]]D QHZ1<6W\OH9HJJLH3L]]D  VW\OHSL]]DV `HOVHLI W\SHHTXDOV ³FODP´ ^ SL]]D QHZ1<6W\OH&ODP3L]]D  `HOVHLI W\SHHTXDOV ³SHSSHURQL´ ^ SL]]D QHZ1<6W\OH3HSSHURQL3L]]D  ` `HOVHLI VW\OHHTXDOV ³&KLFDJR´ ^ LI W\SHHTXDOV ³FKHHVH´ ^ SL]]D QHZ&KLFDJR6W\OH&KHHVH3L]]D  +DQGOHVDOOWKH `HOVHLI W\SHHTXDOV ³YHJJLH´ ^ &KLFDJRVW\OH SL]]D QHZ&KLFDJR6W\OH9HJJLH3L]]D  SL]]DV `HOVHLI W\SHHTXDOV ³FODP´ ^ SL]]D QHZ&KLFDJR6W\OH&ODP3L]]D  `HOVHLI W\SHHTXDOV ³SHSSHURQL´ ^ SL]]D QHZ&KLFDJR6W\OH3HSSHURQL3L]]D  ` `HOVH^ 6\VWHPRXWSULQWOQ ³(UURULQYDOLGW\SHRISL]]D´  UHWXUQQXOO ` SL]]DSUHSDUH  SL]]DEDNH  SL]]DFXW  SL]]DER[  UHWXUQSL]]D ` `


QXPEHU

OLIRUQLDWRR

QXPEHUZLWK&D

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 30

REMHFWGHSHQGHQFLHV

4WWSQVOI\WJRMK\LMXMVLMVKQM[ ?PMVaW]LQZMK\TaQV[\IV\QI\MIVWJRMK\aW]IZMLMXMVLQVOWVQ\[ KWVKZM\MKTI[[
%HFDXVHDQ\FKDQJHVWRWKHFRQFUHWH LPSOHPHQWDWLRQVRISL]]DVDIIHFWVWKH 3L]]D6WRUHZHVD\WKDWWKH3L]]D6WRUH ´GHSHQGVRQµWKHSL]]DLPSOHPHQWDWLRQV 3  

UH

,IWKHLPSOHPHQ FODVVHVFKDQJHWWDWLRQRIWKHVH KDYHWRPRGLI\ KHQZHPD\ LQ3L]]D6WRUH

7KLVYHUVLRQRIWKH 3L]]D6WRUHGHSHQGVRQDOO WKRVHSL]]DREMHFWVEHFDXVH LW·VFUHDWLQJWKHPGLUHFWO\



H3L]]D

KH HV

]] D

3L

RQL 3 ] D L ]

3L]]D

LH

3 L ]] D

ODP

L3L]]D

RQ

&ODP

J LFD &K

6 1<

W\ OH

W\ OH3HSS

6

\O 6W

R6 W\OH&

H3 H S SH U

(YHU\QHZNLQGRISL]]D ZHDGGFUHDWHVDQRWKHU GHSHQGHQF\IRU3L]]D6WRUH

JR

9H J J

J LFD &K

6 1<

1<

W\ OH

HU

R6 W\OH9

&KH H

L FD &K

VH 3L]]D

J LFD &K

6 1<

W\ OH

HJ JLH3L]]D

L]]D6WR

R6 W\OH&

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 31

WKHIDFWRU\SDWWHUQ


'HVLJQ3ULQFLSOH

VWKH XVHWRLPSUHV RXUUDLVHZLOO <  P RR U W WKH IVHWWKHFRV PRUHWKDQRIDQG\RX·OOJDLQ RIWKLVERRN LRQRI\RXU WKHDGPLUDW RSHUV IHOORZGHYHO

'HSHQGXSRQDEVWUDFWLRQV'RQRW GHSHQGXSRQFRQFUHWHFODVVHV

)\ÅZ[\\PQ[XZQVKQXTM[W]VL[ITW\TQSM¹8ZWOZIU\WIV QV\MZNIKMVW\IVQUXTMUMV\I\QWVºZQOP\'1\Q[[QUQTIZ# PW_M^MZ\PM,MXMVLMVKa1V^MZ[QWV8ZQVKQXTMUISM[IVM^MV [\ZWVOMZ[\I\MUMV\IJW]\IJ[\ZIK\QWV1\[]OOM[\[\PI\W]Z PQOPTM^MTKWUXWVMV\[[PW]TLVW\LMXMVLWVW]ZTW_TM^MT KWUXWVMV\[#ZI\PMZ\PMa[PW]TLJW\PLMXMVLWVIJ[\ZIK\QWV[ *]\_PI\\PMPMKSLWM[\PI\UMIV' ?MTTTM\¼[[\IZ\JaTWWSQVOIOIQVI\\PMXQbbI[\WZMLQIOZIU WV\PMXZM^QW][XIOM8QbbI;\WZMQ[W]Z¹PQOPTM^MT KWUXWVMV\ºIVL\PMXQbbIQUXTMUMV\I\QWV[IZMW]Z¹TW_ TM^MTKWUXWVMV\[ºIVLKTMIZTa\PM8QbbI;\WZMQ[LMXMVLMV\ WV\PMKWVKZM\MXQbbIKTI[[M[ 6W_\PQ[XZQVKQXTM\MTT[][_M[PW]TLQV[\MIL_ZQ\MW]ZKWLM [W\PI\_MIZMLMXMVLQVOWVIJ[\ZIK\QWV[VW\KWVKZM\M KTI[[M[
$´KLJKOHYHOµFRPSRQHQWLVDFODVV ZLWKEHKDYLRUGHILQHGLQWHUPVRI RWKHU´ORZOHYHOµFRPSRQHQWV )RUH[DPSOH3L]]D6WRUHLVD KLJKOHYHOFRPSRQHQWEHFDXVH LWVEHKDYLRULVGHILQHGLQWHUPV RISL]]DVLWFUHDWHVDOOWKH GLIIHUHQWSL]]DREMHFWVSUHSDUHV EDNHVFXWVDQGER[HVWKHPZKLOH WKHSL]]DVLWXVHVDUHORZOHYHO FRPSRQHQWV

*]\PW_LW_MLW\PQ['4M\¼[\PQVSIJW]\PW__M¼L IXXTa\PQ[XZQVKQXTM\WW]Z>MZa,MXMVLMV\8QbbI;\WZM QUXTMUMV\I\QWV

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 32

GHSHQGHQF\LQYHUVLRQSULQFLSOH

)XXTaQVO\PM8ZQVKQXTM 6W_\PMUIQVXZWJTMU_Q\P\PM>MZa,MXMVLMV\8QbbI;\WZMQ[\PI\Q\LMXMVL[ WVM^MZa\aXMWN XQbbIJMKI][MQ\IK\]ITTaQV[\IV\QI\M[KWVKZM\M\aXM[QVQ\[ WZLMZ8QbbIUM\PWL ?PQTM_M¼^MKZMI\MLIVIJ[\ZIK\QWV8QbbI_M¼ZMVM^MZ\PMTM[[KZMI\QVOKWVKZM\M 8QbbI[QV\PQ[KWLM[W_MLWV¼\OM\ITW\WN TM^MZIOMW]\WN \PQ[IJ[\ZIK\QWV 0W_KIV_MOM\\PW[MQV[\IV\QI\QWV[W]\WN \PMWZLMZ8QbbIUM\PWL'?MTTI[ _MSVW_\PM.IK\WZa5M\PWLITTW_[][\WLWR][\\PI\ ;WIN\MZ_M¼^MIXXTQML\PM.IK\WZa5M\PWLW]ZLQIOZIUTWWS[TQSM\PQ["

GVRQO\ 3L]]D6WRUHQRZGHSHQ FODVV DFW VWU RQ3L]]DWKHDE

H

 

3 L]

]D6WRU

3L]]DLVDQDEVWUDFW FODVVDQDEVWUDFWLRQ

HU

H3L]]D

KH HV

3 L ]] D

ODP

W\ OH3HSS

6

J LFD &K

J LFD &K

R6 W\OH&

RQL3 ] L ]D

3L]]D

LH

L3L]]D

RQ

9H J J

R6 W\OH9

JR

HJ JLH3L]]D

W\ OH

&ODP

LFD &K

\O 6W

6 1<

1<

J LFD &K

H3 HS SH U

3L

W\ OH

&KH H

]] D

6 1<

6 1<

W\ OH

3L]]D

VH 3L]]D

 

 

ODVVHVGHSHQGRQ 7KHFRQFUHWHSL]]DFQWRREHFDXVHWKH\ WLR UDF EVW WKH3L]]DD QWHUIDFH UHPHPEHU LPSOHPHQWWKH3L]]DL µLQWKHJHQHUDO ZH·UHXVLQJ´LQWHUIDFHEVWUDFWFODVV VHQVH LQWKH3L]]DD

R6 W\OH&

)N\MZIXXTaQVO\PM.IK\WZa5M\PWLaW]¼TTVW\QKM\PI\W]ZPQOPTM^MTKWUXWVMV\ \PM8QbbI;\WZMIVLW]ZTW_TM^MTKWUXWVMV\[\PMXQbbI[JW\PLMXMVLWV8QbbI \PMIJ[\ZIK\QWV.IK\WZa5M\PWLQ[VW\\PMWVTa\MKPVQY]MNWZILPMZQVO\W\PM ,MXMVLMVKa1V^MZ[QWV8ZQVKQXTMJ]\Q\Q[WVMWN \PMUWZMXW_MZN]TWVM[



&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Page 33

Return to Table of Contents

WKHIDFWRU\SDWWHUQ

2ND\,JHWWKHGHSHQGHQF\ SDUWEXWZK\LVLWFDOOHG GHSHQGHQF\LQYHUVLRQ"

:KHUH·VWKH´LQYHUVLRQµLQ'HSHQGHQF\ ,QYHUVLRQ3ULQFLSOH"
\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Page 34

Return to Table of Contents

LQYHUW\RXUWKLQNLQJ

1V^MZ\QVOaW]Z\PQVSQVO +PPP3L]]D6WRUHVSUHSDUHEDNHDQG ER[SL]]DV6RP\VWRUHQHHGVWREH DEOHWRPDNHDEXQFKRIGLIIHUHQW SL]]DV&KHHVH3L]]D9HJJLH3L]]D &ODP3L]]DDQGVRRQ

:HOOD&KHHVH3L]]DDQGD 9HJJLH3L]]DDQGD&ODP3L]]D DUHDOOMXVW3L]]DVVRWKH\ VKRXOGVKDUHD3L]]DLQWHUIDFH

7SIa[WaW]VMML\WQUXTMUMV\I8QbbI;\WZM ?PI\¼[\PMÅZ[\\PW]OP\\PI\XWX[QV\WaW]ZPMIL'

:QOP\aW][\IZ\I\\WXIVLNWTTW_\PQVO[LW_V\W \PMKWVKZM\MKTI[[M[*]\I[aW]¼^M[MMVaW]LWV¼\ _IV\aW]Z[\WZM\WSVW_IJW]\\PMKWVKZM\MXQbbI \aXM[JMKI][M\PMVQ\¼TTJMLMXMVLMV\WVITT\PW[M KWVKZM\MKTI[[M[ 6W_TM\¼[¹QV^MZ\ºaW]Z\PQVSQVOQV[\MILWN  [\IZ\QVOI\\PM\WX[\IZ\I\\PM8QbbI[IVL\PQVS IJW]\_PI\aW]KIVIJ[\ZIK\

:QOP\ AW]IZM\PQVSQVOIJW]\\PMIJ[\ZIK\QWV 8QbbI;WVW_OWJIKSIVL\PQVSIJW]\\PMLM[QOV WN \PM8QbbI;\WZMIOIQV 6LQFH,QRZKDYHD3L]]D DEVWUDFWLRQ,FDQGHVLJQP\ 3L]]D6WRUHDQGQRWZRUU\DERXW WKHFRQFUHWHSL]]DFODVVHV

+TW[M*]\\WLW\PI\aW]¼TTPI^M\WZMTaWVI NIK\WZa\WOM\\PW[MKWVKZM\MKTI[[M[W]\WN  aW]Z8QbbI;\WZM7VKMaW]¼^MLWVM\PI\aW]Z LQNNMZMV\KWVKZM\MXQbbI\aXM[LMXMVLWVTaWVIV IJ[\ZIK\QWVIVL[WLWM[aW]Z[\WZM?M¼^M\ISMV ILM[QOV_PMZM\PM[\WZMLMXMVLMLWVKWVKZM\M KTI[[M[IVLQV^MZ\ML\PW[MLMXMVLMVKQM[ITWVO _Q\PaW]Z\PQVSQVO 

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 35

WKHIDFWRU\SDWWHUQ

)NM_O]QLMTQVM[\WPMTXaW]NWTTW_\PM 8ZQVKQXTM
HKROGLQJ ,I\RXXVHQHZ\RX·OOEFUHWHFODVV FRQ D DUHIHUHQFHWR URXQGWKDW 8VHDIDFWRU\WRJHWD

ƒ 6W^IZQIJTM[PW]TLPWTLIZMNMZMVKM\WIKWVKZM\MKTI[[

ƒ 6WKTI[[[PW]TLLMZQ^MNZWUIKWVKZM\MKTI[[

,I\RXGHULYHIURPDFRQFUHWHFODVV \RX·UHGHSHQGLQJRQDFRQFUHWHFODVV 'HULYHIURPDQDEVWUDFWLRQOLNHDQ LQWHUIDFHRUDQDEVWUDFWFODVV

ƒ 6WUM\PWL[PW]TLW^MZZQLMIVQUXTMUMV\MLUM\PWLWN  IVaWN Q\[JI[MKTI[[M[

,I\RXRYHUULGHDQLP WKHQ\RXUEDVHFODVV SOHPHQWHGPHWKRG DEVWUDFWLRQWRVWD ZDVQ·WUHDOO\DQ PHWKRGVLPSOHPHQWHUWZLWK7KRVH PHDQWWREHVKDUHGGLQWKHEDVHFODVVDUH E\DOO\RXUVXEFODVVH V

%XWZDLWDUHQ·WWKHVH JXLGHOLQHVLPSRVVLEOHWRIROORZ" ,I,IROORZWKHVH,·OOQHYHUEH DEOHWRZULWHDVLQJOHSURJUDP

AW]¼ZMM`IK\TaZQOP\ 4QSMUIVaWN W]ZXZQVKQXTM[\PQ[Q[IO]QLMTQVM aW][PW]TL[\ZQ^MNWZZI\PMZ\PIVIZ]TMaW][PW]TLNWTTW_ITT\PM\QUM +TMIZTaM^MZa[QVOTM2I^IXZWOZIUM^MZ_ZQ\\MV^QWTI\M[\PM[MO]QLMTQVM[ *]\QN aW]QV\MZVITQbM\PM[MO]QLMTQVM[IVLPI^M\PMUQV\PMJIKSWN  aW]ZUQVL_PMVaW]LM[QOVaW]¼TTSVW__PMVaW]IZM^QWTI\QVO\PM XZQVKQXTMIVLaW]¼TTPI^MIOWWLZMI[WVNWZLWQVO[W.WZQV[\IVKMQN aW] PI^MIKTI[[\PI\Q[V¼\TQSMTa\WKPIVOMIVLaW]SVW_Q\\PMVQ\¼[VW\\PM MVLWN \PM_WZTLQN aW]QV[\IV\QI\MIKWVKZM\MKTI[[QVaW]ZKWLM
\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 36

IDPLOLHVRILQJUHGLHQWV

5MIV_PQTMJIKSI\\PM8QbbI;\WZM
'RXJK

3HSSHURQL

6W_\PMSMa\W7JRMK\^QTTM8QbbI¼[[]KKM[[PI[ IT_Ia[JMMVNZM[PY]ITQ\aQVOZMLQMV\[IVL _PI\aW]¼^MLQ[KW^MZMLQ[\PI\_Q\P\PM VM_NZIUM_WZSaW]ZNZIVKPQ[M[PI^MJMMV NWTTW_QVOaW]ZXZWKML]ZM[J]\INM_NZIVKPQ[M[ PI^MJMMV[]J[\Q\]\QVOQVNMZQWZQVOZMLQMV\[ QV\PMQZXQM[\WTW_MZKW[\[IVLQVKZMI[M \PMQZUIZOQV[AW]SVW_aW]¼^MOW\\WLW [WUM\PQVOJMKI][MQV\PMTWVO\MZU\PQ[Q[ OWQVO\WP]Z\\PM7JRMK\^QTTMJZIVL

-V[]ZQVOKWV[Q[\MVKaQVaW]Z QVOZMLQMV\[

&KHHVH

9HJJLHV

6DXFH

;WPW_IZMaW]OWQVO\WMV[]ZMMIKPNZIVKPQ[MQ[][QVO Y]ITQ\aQVOZMLQMV\['AW]¼ZMOWQVO\WJ]QTLINIK\WZa\PI\ XZWL]KM[\PMUIVL[PQX[\PMU\WaW]ZNZIVKPQ[M[ 6W_\PMZMQ[WVTaWVMXZWJTMU_Q\P\PQ[XTIV"\PMNZIVKPQ[M[IZMTWKI\MLQV LQNNMZMV\ZMOQWV[IVL_PI\Q[ZML[I]KMQV6M_AWZSQ[VW\ZML[I]KMQV+PQKIOW ;WaW]PI^MWVM[M\WN QVOZMLQMV\[\PI\VMML\WJM[PQXXML\W6M_AWZSIVLI LQNNMZMV\[M\\PI\VMML[\W[PQXXML\W+PQKIOW4M\¼[\ISMIKTW[MZTWWS"

%JKECIQ 2K\\C/GPW &KHHVH3L]]D 3OXP7RPDWR6DXFH0R]]DUHOOD3DUPHVDQ 2UHJDQR

9HJJLH3L]]D 3OXP7RPDWR6DXFH0R]]DUHOOD3DUPHVDQ (JJSODQW6SLQDFK%ODFN2OLYHV

&ODP3L]]D Q&ODPV 3OXP7RPDWR6DXFH0R]]DUHOOD3DUPHVD 3HSSHURQL3L]]D 3OXP7RPDWR6DXFH0R]]DUHOOD3DUPHVDQ HSSHURQL (JJSODQW6SLQDFK%ODFN2OLYHV3



:H·YHJRWWKH VDPHSURGXFW IDPLOLHV GRXJK VDXFHFKHHVH YHJJLHVPHDWV  EXWGLIIHUHQW LPSOHPHQWDWLRQV EDVHGRQUHJLRQ

0GY;QTM 2K\\C/GPW &KHHVH3L]]D 0DULQDUD6DXFH5HJJLDQR*DUOLF 9HJJLH3L]]D 0DULQDUD6DXFH5HJJLDQR0XVKURRPV 2QLRQV5HG3HSSHUV

&ODP3L]]D 0DULQDUD6DXFH5HJJLDQR)UHVK&ODPV 3HSSHURQL3L]]D 0DULQDUD6DXFH5HJJLDQR0XVKURRPV 2QLRQV5HG3HSSHUV3HSSHURQL

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 37

WKHIDFWRU\SDWWHUQ

.IUQTQM[WNQVOZMLQMV\[ 1HZ
+PQKIOW )UR]HQ&ODPV

0R]]DUHOOD&KHHVH

)RUWKLVWRZRUN\RXDUHJRLQJWR KDYHWRÀJXUHRXWKRZWRKDQGOH IDPLOLHVRILQJUHGLHQWV

6M_AWZS

7KLFN&UXVW'RXJK

3OXP7RPDWR6DXFH

H HIURPWKHVDP 3L]]DVDUHPDG $OO2EMHFWYLOOH·V HDFKUHJLRQKDVDGLIIHUHQW FRPSRQHQWVEXW IWKRVHFRPSRQHQWV LPSOHPHQWDWLRQR

)UHVK&ODPV

7KLQ&UXVW'RXJK

0DULQDUD6DXFH

+ITQNWZVQI 5HJJLDQR&KHHVH &DPDUL

9HU\7KLQ&UXVW

%UXVFKHWWD6DXFH

*RDW&KHHVH

(DFKIDPLO\FRQVLVWVRIDW\SHRIGRXJK DW\SHRIVDXFHDW\SHRIFKHHVHDQGD VHDIRRGWRSSLQJ DORQJZLWKDIHZPRUHZH KDYHQ·WVKRZQOLNHYHJJLHVDQGVSLFHV  LHQWIDPLOLHVZLWK UHJLRQVPDNHXSLQJUHG ,QWRWDOWKHVHWKUHH LQJDFRPSOHWHIDPLO\RILQJUHGLHQWV HDFKUHJLRQLPSOHPHQW \RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 38

LQJUHGLHQWIDFWRULHV

*]QTLQVO\PMQVOZMLQMV\NIK\WZQM[ 1RZZH·UHJRLQJWREXLOGDIDFWRU\WRFUHDWHRXULQJUHGLHQWVWKH IDFWRU\ZLOOEHUHVSRQVLEOHIRUFUHDWLQJHDFKLQJUHGLHQWLQWKH LQJUHGLHQWIDPLO\,QRWKHUZRUGVWKHIDFWRU\ZLOOQHHGWRFUHDWH GRXJKVDXFHFKHHVHDQGVRRQ
SXEOLFLQWHUIDFH3L]]D,QJUHGLHQW)DFWRU\^  SXEOLF'RXJKFUHDWH'RXJK  SXEOLF6DXFHFUHDWH6DXFH  SXEOLF&KHHVHFUHDWH&KHHVH  SXEOLF9HJJLHV>@FUHDWH9HJJLHV  SXEOLF3HSSHURQLFUHDWH3HSSHURQL  SXEOLF&ODPVFUHDWH&ODP   `

/RWVRIQHZFODVVHVKHUH RQHSHULQJUHGLHQW

QHD HGLHQWZHGHIL )RUHDFKLQJU GLQRXULQWHUIDFH FUHDWHPHWKR

QHU\µ ,IZH·GKDGVRPHFRPPRQ´PDFKL I FHR VWDQ KLQ WRLPSOHPHQWLQHDF DQ IDFWRU\ZHFRXOGKDYHPDGHWKLV G VWHD VVLQ DEVWUDFWFOD

+HUH·VZKDWZH·UHJRLQJWRGR





*]QTLINIK\WZaNWZMIKPZMOQWV


1UXTMUMV\I[M\WN QVOZMLQMV\KTI[[M[\WJM][ML_Q\P\PMNIK\WZaTQSM :MOOQIVW+PMM[M:ML8MXXMZ[IVL



&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 39

WKHIDFWRU\SDWWHUQ

*]QTLQVO\PM6M_AWZSQVOZMLQMV\NIK\WZa 2ND\KHUH·VWKHLPSOHPHQWDWLRQIRU WKH1HZ
QWV 7KH1<LQJUHGLHQWIDFWRU\LPSOHPH WKHLQWHUIDFHIRUDOOLQJUHGLHQW IDFWRULHV

SXEOLFFODVV1<3L]]D,QJUHGLHQW)DFWRU\LPSOHPHQWV3L]]D,QJUHGLHQW)DFWRU\^  SXEOLF'RXJKFUHDWH'RXJK ^ UHWXUQQHZ7KLQ&UXVW'RXJK  LHQWLQWKH ` )RUHDFKLQJUHGO\ZHFUHDWH PL  ID W LQJUHGLHQ SXEOLF6DXFHFUHDWH6DXFH ^ YHUVLRQ WKH1HZ@FUHDWH9HJJLHV ^ 9HJJLHVYHJJLHV>@ ^QHZ*DUOLF QHZ2QLRQ QHZ0XVKURRP QHZ5HG3HSSHU ` UHWXUQYHJJLHV )RUYHJJLHVZHUHWXUQDQDUUD\RI ` 9HJJLHV+HUHZH·YHKDUGFRGHGWKH  SXEOLF3HSSHURQLFUHDWH3HSSHURQL ^ YHJJLHV:HFRXOGPDNHWKLVPRUH UHWXUQQHZ6OLFHG3HSSHURQL  VRSKLVWLFDWHGEXWWKDWGRHVQ·WUHDOO\ ` DGGDQ\WKLQJWROHDUQLQJWKHIDFWRU\ SXEOLF&ODPVFUHDWH&ODP ^ SDWWHUQVRZH·OONHHSLWVLPSOH UHWXUQQHZ)UHVK&ODPV  ` `

1HZ
7KHEHVWVOLFHGSHSSHURQL7KLV LVVKDUHGEHWZHHQ1HZ
\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 40

EXLOGDIDFWRU\

6KDUSHQ \RXU SHQFLO ?ZQ\M\PM+PQKIOW8QbbI1VOZMLQMV\.IK\WZaAW]KIV ZMNMZMVKM\PMKTI[[M[JMTW_QVaW]ZQUXTMUMV\I\QWV"

(JJ3ODQW 6SLQDFK

7KLFN&UXVW'RXJK

6OLFHG3HSSHURQL

%ODFN2OLYHV

3OXP7RPDWR6DXFH

)UR]HQ&ODP 0R]]DUHOOD



&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 41

WKHIDFWRU\SDWWHUQ

:M_WZSQVO\PMXQbbI[ ?M¼^MOW\W]ZNIK\WZQM[ITTÅZML]XIVLZMILa\WXZWL]KMY]ITQ\aQVOZMLQMV\[#VW__M R][\VMML\WZM_WZSW]Z8QbbI[[W\PMaWVTa][MNIK\WZaXZWL]KMLQVOZMLQMV\[?M¼TT [\IZ\_Q\PW]ZIJ[\ZIK\8QbbIKTI[["

SXEOLFDEVWUDFWFODVV3L]]D^ 6WULQJQDPH 'RXJKGRXJK 6DXFHVDXFH 9HJJLHVYHJJLHV>@ &KHHVHFKHHVH 3HSSHURQLSHSSHURQL &ODPVFODP DEVWUDFWYRLGSUHSDUH 

(DFKSL]]DKROGVDVHWRILQJUHGLHQWV WKDWDUHXVHGLQLWVSUHSDUDWLRQ

:H·YHQRZPDGHWKHSUHSDUHPHWKRGDEVWUDFW 7KLVLVZKHUHZHDUHJRLQJWRFROOHFWWKH LQJUHGLHQWVQHHGHGIRUWKHSL]]DZKLFKRI FRXUVHZLOOFRPHIURPWKHLQJUHGLHQWIDFWRU\

YRLGEDNH ^ 6\VWHPRXWSULQWOQ ³%DNHIRUPLQXWHVDW´  ` YRLGFXW ^ 6\VWHPRXWSULQWOQ ³&XWWLQJWKHSL]]DLQWRGLDJRQDOVOLFHV´  ` YRLGER[ ^ 6\VWHPRXWSULQWOQ ³3ODFHSL]]DLQRI¿FLDO3L]]D6WRUHER[´  ` YRLGVHW1DPH 6WULQJQDPH ^ WKLVQDPH QDPH ` 6WULQJJHW1DPH ^ UHWXUQQDPH `

HPDLQWKHVDPHZLWK 2XURWKHUPHWKRGVUHSUHSDUHPHWKRG WK WKHH[FHSWLRQRI

SXEOLF6WULQJWR6WULQJ ^ FRGHWRSULQWSL]]DKHUH ` `

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 42

GHFRXSOLQJLQJUHGLHQWV

:M_WZSQVO\PMXQbbI[KWV\QV]ML 6W_\PI\aW]¼^MOW\IVIJ[\ZIK\8QbbI\W_WZSNZWUQ\¼[\QUM\WKZMI\M \PM6M_AWZSIVL+PQKIOW[\aTM8QbbI[·WVTa\PQ[\QUMIZW]VL\PMa_QTT OM\\PMQZQVOZMLQMV\[[\ZIQOP\NZWU\PMNIK\WZaMOOQM+TIUIVL[WWV
ZHQHHG

SXEOLFFODVV&KHHVH3L]]DH[WHQGV3L]]D^ 7RPDNHDSL]]DQRZ WKH 3L]]D,QJUHGLHQW)DFWRU\LQJUHGLHQW)DFWRU\ DIDFWRU\WRSURYLGH L]]D  LQJUHGLHQWV6RHDFK3 SXEOLF&KHHVH3L]]D 3L]]D,QJUHGLHQW)DFWRU\LQJUHGLHQW)DFWRU\ ^ VVJHWVDIDFWRU\SDVVHG FOD DQGLW·V WKLVLQJUHGLHQW)DFWRU\ LQJUHGLHQW)DFWRU\ LQWRLWVFRQVWUXFWRUHYDULDEOH ` QF VWD LQ DQ LQ VWRUHG  YRLGSUHSDUH ^ 6\VWHPRXWSULQWOQ ³3UHSDULQJ³QDPH  GRXJK LQJUHGLHQW)DFWRU\FUHDWH'RXJK  VDXFH LQJUHGLHQW)DFWRU\FUHDWH6DXFH  +HUH·VZKHUHWKHPDJLFKDSSHQV FKHHVH LQJUHGLHQW)DFWRU\FUHDWH&KHHVH  ` `

 7KHSUHSDUH PHWKRGVWHSVWKURXJKFUHDWLQJ Q HGVD LWQH WLPH DFKHHVHSL]]DDQGHDFK LQJUHGLHQWLWDVNVWKHIDFWRU\WRSURGXFHLW



&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 43

WKHIDFWRU\SDWWHUQ

&RGH8S&ORVH
VDXFH LQJUHGLHQW)DFWRU\FUHDWH6DXFH 

:H·UHVHWWLQJWKH 3L]]DLQVWDQFH  YDULDEOHWRUHIHU WKHVSHFLILFVDXFHWR XVHGLQWKLVSL]]D

7KLVLVRXULQJUHGLHQWIDFWRU\ 7KH3L]]DGRHVQ·WFDUHZKLFK IDFWRU\LVXVHGDVORQJDVLWLV DQLQJUHGLHQWIDFWRU\

RGUHWXUQVWKHVDXFH 7KHFUHDWH6DXFH PHWK LRQ,IWKLVLVD1< WKDWLVXVHGLQLWVUHJKHQZHJHWPDULQDUDVDXFH W RU\ DFW LQJUHGLHQWI

4M\¼[KPMKSW]\\PM+TIU8QbbII[_MTT"

SXEOLFFODVV&ODP3L]]DH[WHQGV3L]]D^ 3L]]D,QJUHGLHQW)DFWRU\LQJUHGLHQW)DFWRU\  SXEOLF&ODP3L]]D 3L]]D,QJUHGLHQW)DFWRU\LQJUHGLHQW)DFWRU\ ^ WKLVLQJUHGLHQW)DFWRU\ LQJUHGLHQW)DFWRU\ &ODP3L]]DDOVRVWDVKHVDQ ` LQJUHGLHQWIDFWRU\  YRLGSUHSDUH ^ 6\VWHPRXWSULQWOQ ³3UHSDULQJ³QDPH  GRXJK LQJUHGLHQW)DFWRU\FUHDWH'RXJK  VDXFH LQJUHGLHQW)DFWRU\FUHDWH6DXFH  7RPDNHDFODPSL]]DWKH FKHHVH LQJUHGLHQW)DFWRU\FUHDWH&KHHVH  FODP LQJUHGLHQW)DFWRU\FUHDWH&ODP  SUHSDUHPHWKRGFROOHFWVWKHULJKW ` LQJUHGLHQWVIURPLWVORFDOIDFWRU\ `

,ILW·VD1HZ
\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 44

XVHWKHULJKWLQJUHGLHQWIDFWRU\

:M^Q[Q\QVOW]ZXQbbI[\WZM[ :H·UHDOPRVWWKHUHZHMXVWQHHGWRPDNHDTXLFNWULSWRRXU IUDQFKLVHVWRUHVWRPDNHVXUHWKH\DUHXVLQJWKHFRUUHFW 3L]]DV:HDOVRQHHGWRJLYHWKHPDUHIHUHQFHWRWKHLUORFDO LQJUHGLHQWIDFWRULHV

SXEOLFFODVV1<3L]]D6WRUHH[WHQGV3L]]D6WRUH^  SURWHFWHG3L]]DFUHDWH3L]]D 6WULQJLWHP ^ 3L]]DSL]]D QXOO 3L]]D,QJUHGLHQW)DFWRU\LQJUHGLHQW)DFWRU\  QHZ1<3L]]D,QJUHGLHQW)DFWRU\   LI LWHPHTXDOV ³FKHHVH´ ^  SL]]D QHZ&KHHVH3L]]D LQJUHGLHQW)DFWRU\  SL]]DVHW1DPH ³1HZ
KD1< RPSRVHGZLW VZLOO F LV H RU W 6 \7KL 7KH1< LHQWIDFWRU V SL]]DLQJUHGSURGXFHWKHLQJUHGLHQW R W G VH EHX W\OHSL]]DV IRUDOO1<V

:HQRZSDVVHDFKSL]]DWKH IDFWRU\WKDWVKRXOGEHXVHGWR SURGXFHLWVLQJUHGLHQWV /RRNEDFNRQHSDJHDQGPDNHVXUH \RXXQGHUVWDQGKRZWKHSL]]DDQG WKHIDFWRU\ZRUNWRJHWKHU

)RUHDFKW\SHRI3L]]DZH LQVWDQWLDWHDQHZ3L]]DDQGJLYH LWWKHIDFWRU\LWQHHGVWRJHW LWVLQJUHGLHQWV

YiX`e gfn\i &RPSDUHWKLVYHUVLRQRIWKHFUHDWH3L]]D PHWKRG WRWKHRQHLQWKH)DFWRU\0HWKRGLPSOHPHQWDWLRQ HDUOLHULQWKHFKDSWHU



&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 45

WKHIDFWRU\SDWWHUQ

?PI\PI^M_MLWVM' 7KDWZDVTXLWHDVHULHVRI FRGHFKDQJHVZKDWH[DFWO\ GLGZHGR" :HSURYLGHGDPHDQV  IFUHDWLQJDIDPLO\RI R LQJUHGLHQWVIRUSL]]DVE\ LQWURGXFLQJDQHZW\SHRI IDFWRU\FDOOHGDQ$EVWUDFW )DFWRU\ $Q$EVWUDFW)DFWRU\JLYHV XVDQLQWHUIDFHIRUFUHDWLQJ DIDPLO\RISURGXFWV%\ ZULWLQJFRGHWKDWXVHVWKLV LQWHUIDFHZHGHFRXSOHRXU FRGHIURPWKHDFWXDOIDFWRU\ WKDWFUHDWHVWKHSURGXFWV 7KDWDOORZVXVWRLPSOHPHQW DYDULHW\RIIDFWRULHVWKDW SURGXFHSURGXFWVPHDQWIRU GLIIHUHQWFRQWH[WV²VXFKDV GLIIHUHQWUHJLRQVGLIIHUHQW RSHUDWLQJV\VWHPVRU GLIIHUHQWORRNDQGIHHOV

 'HILQHVWKH LQWHUIDFH

1DLGEVXKNNG#DUVTCEV+PITGFKGPV(CEVQT[ 3URYLGHV LRQV LPSOHPHQWDW V IRUSURGXFW

%JKECIQ

0GY;QTM

&ROMTHEABSTRACTFACTORY WE DERIVEONEORMORECONCRETE FACTORIESTHATPRODUCETHESAME PRODUCTS BUTWITHDIFFERENT IMPLEMENTATIONS

  3L] ]D6WRUH



%HFDXVHRXUFRGHLV GHFRXSOHGIURPWKHDFWXDO SURGXFWVZHFDQVXEVWLWXWH GLIIHUHQWIDFWRULHVWRJHW GLIIHUHQWEHKDYLRUV OLNH JHWWLQJPDULQDUDLQVWHDGRI SOXPWRPDWRHV 

!N!BSTRACT&ACTORYPROVIDESANINTERFACEFOR AFAMILYOFPRODUCTS7HATSAFAMILY)NOUR CASEITSALLTHETHINGSWENEEDTOMAKEAPIZZA DOUGH SAUCE CHEESE MEATSANDVEGGIES

ZLWK 3L]]DPDGHSURGXFHGE\ V QW LH HG JU LQ WRU\ FRQFUHWHIDF

7ETHENWRITEOURCODESOTHATITUSESTHE FACTORYTOCREATEPRODUCTS"YPASSINGIN AVARIETYOFFACTORIES WEGETAVARIETYOF IMPLEMENTATIONSOFTHOSEPRODUCTS"UT OURCLIENTCODESTAYSTHESAME

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 46

RUGHUVRPHPRUHSL]]D

5WZMXQbbINWZ-\PIVIVL2WMT %HKLQG WKH6FHQHV

-\PIVIVL2WMTKIV¼\OM\MVW]OP7JRMK\ ^QTTM8QbbI ?PI\\PMa LWV¼\SVW_Q[\PI\VW_\PMQZWZLMZ[IZMUISQVO][MWN\PM VM_QVOZMLQMV\NIK\WZQM[;WVW__PMV\PMaWZLMZ

,·PVWLFNLQ· ZLWK&KLFDJR

,·PVWLOOORYLQ·1<6W\OH




)LUVWZHQHHGD1<3L]]D6WRUH



1RZWKDWZHKDYHDVWRUHZHFDQWDNHDQRUGHU

  Q \3



&UHDWHVDQLQVWDQFHRI 1<3L]]D6WRUH

H

3L]]D6WRUHQ\3L]]D6WRUH QHZ1<3L]]D6WRUH 

L]]D6WR

U

Q\3L]]D6WRUHRUGHU3L]]D ³FKHHVH´ 

LVFDOOHGRQ WKHRUGHU3L]]D PHWKRG  QFH VWD WKHQ\3L]]D6WRUHLQ



7KHRUGHU3L]]D PHWKRGÀUVWFDOOVWKHFUH DWH3L]]D PHWKRG

HVH´ ]]D ³FKH FUHDWH3L

3L]]DSL]]D FUHDWH3L]]D ³FKHHVH´ 



&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 47

WKHIDFWRU\SDWWHUQ

.ZWUPMZM\PQVO[KPIVOMJMKI][M_M IZM][QVOIVQVOZMLQMV\NIK\WZa



%HKLQG WKH6FHQHV

:KHQWKHFUHDWH3L]]D PHWKRGLVFDOOHGWKDW·V ZKHQRXULQJUHGLHQWIDFWRU\JHWVLQYROYHG

U\LVFKRVHQDQG 7KHLQJUHGLHQWIDFWR L]]D6WRUHDQGWKHQ H3 WK D LQVWDQWLDWHGLQ UXFWRURIHDFKSL]] SDVVHGLQWRWKHFRQVW KROGV

&UHDWHVDLQVWDQFHRI 3L]]DWKDWLVFRPSRVHG ZLWKWKH1HZ
DF W RU \

3L]]DSL]]D QHZ&KHHVH3L]]D Q\,QJUHGLHQW)DFWRU\ 

Q

\, QJU HGLHQW)



1H[WZHQHHGWRSUHSDUHWKHSL]]D2QFHWKH SUHSDUH PHWKRGLVFDOOHGWKHIDFWRU\LVDVNHG WRSUHSDUHLQJUHGLHQWV YRLGSUHSDUH ^ GRXJK IDFWRU\FUHDWH'RXJK  VDXFH IDFWRU\FUHDWH6DXFH  FKHHVH IDFWRU\FUHDWH&KHHVH  `

SUHSDUH



   3L]]D

XVW 7KLQFU 0DULQDUD

5HJJLDQR

Z


)LQDOO\ZHKDYHWKHSUHSDUHGSL]]DLQKDQGDQGWKH RUGHU3L]]D PHWKRGEDNHVFXWVDQGER[HVWKHSL]]D \RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 48

DEVWUDFWIDFWRU\GH¿QHG

)J[\ZIK\.IK\WZa8I\\MZVLMNQVML ?M¼ZMILLQVOaM\IVW\PMZNIK\WZaXI\\MZV\WW]ZXI\\MZVNIUQTaWVM\PI\TM\[][KZMI\MNIUQTQM[ WN XZWL]K\[4M\¼[KPMKSW]\\PMWNÅKQITLMÅVQ\QWVNWZ\PQ[XI\\MZV"
?M¼^MKMZ\IQVTa[MMV\PI\)J[\ZIK\.IK\WZaITTW_[IKTQMV\\W][MIVIJ[\ZIK\QV\MZNIKM\W KZMI\MI[M\WN ZMTI\MLXZWL]K\[_Q\PW]\SVW_QVOWZKIZQVOIJW]\\PMKWVKZM\MXZWL]K\[\PI\ IZMIK\]ITTaXZWL]KML1V\PQ[_Ia\PMKTQMV\Q[LMKW]XTMLNZWUIVaWN \PM[XMKQÅK[WN \PM KWVKZM\MXZWL]K\[4M\¼[TWWSI\\PMKTI[[LQIOZIU\W[MMPW_\PQ[ITTPWTL[\WOM\PMZ"

7KH&OLHQWLVZULWWHQDJDLQVWWKH DEVWUDFWIDFWRU\DQGWKHQFRPSRVHGDW UXQWLPHZLWKDQDFWXDOIDFWRU\ 7KH$EVWUDFW)DFWRU\GHILQHVWKH LQWHUIDFHWKDWDOO&RQFUHWHIDFWRULHV PXVWLPSOHPHQWZKLFKFRQVLVWVRIDVHW RIPHWKRGVIRUSURGXFLQJSURGXFWV LQWHUIDFH!! $EVWUDFW)DFWRU\

&OLHQW

7KLVLVWKHSURGXFW IDPLO\(DFKFRQFUHWH IDFWRU\FDQSURGXFHDQ HQWLUHVHWRISURGXFWV

LQWHUIDFH!! $EVWUDFW3URGXFW$

&UHDWH3URGXFW$ &UHDWH3URGXFW%

3URGXFW$

3URGXFW$

&RQFUHWH)DFWRU\

&RQFUHWH)DFWRU\

&UHDWH3URGXFW$

&UHDWH3URGXFW$

&UHDWH3URGXFW%

&UHDWH3URGXFW%

7KHFRQFUHWHIDFWRULHVLPSOHPHQWWKH GLIIHUHQWSURGXFWIDPLOLHV7RFUHDWHD SURGXFWWKHFOLHQWXVHVRQHRIWKHVHIDFWRULHV VRLWQHYHUKDVWRLQVWDQWLDWHDSURGXFWREMHFW



LQWHUIDFH!! $EVWUDFW3URGXFW%

3URGXFW%

3URGXFW%

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 49

WKHIDFWRU\SDWWHUQ

7KDW·VDIDLUO\FRPSOLFDWHGFODVV GLDJUDPOHW·VORRNDWLWDOOLQWHUPVRI RXU3L]]D6WRUH

7KHFOLHQWVRIWKH$EVWUDFW )DFWRU\DUHWKHWZR LQVWDQFHVRIRXU3L]]D6WRUH 1<3L]]D6WRUHDQG &KLFDJR6W\OH3L]]D6WRUH 1<3L]]D6WRUH FUHDWH3L]]D

7KHDEVWUDFW3L]]D,QJUHGLHQW)DFWRU\ LVWKHLQWHUIDFHWKDWGHILQHVKRZWR PDNHDIDPLO\RIUHODWHGSURGXFWV HYHU\WKLQJZHQHHGWRPDNHDSL]]D

LQWHUIDFH!! 'RXJK

7KLFN&UXVW'RXJK

7KLQ&UXVW'RXJK

LQWHUIDFH!! 3L]]D,QJUHGLHQW)DFWRU\ FUHDWH'RXJK

LQWHUIDFH!! 6DXFH

FUHDWH6DXFH FUHDWH&KHHVH FUHDWH9HJJLHV FUHDWH3HSSHURQL

3OXP7RPDWR6DXFH

FUHDWH&ODP

&KLFDJR3L]]D,QJUHGLHQW)DFWRU\

1<3L]]D,QJUHGLHQW)DFWRU\ FUHDWH'RXJK

FUHDWH'RXJK

FUHDWH6DXFH

FUHDWH6DXFH

FUHDWH&KHHVH

FUHDWH&KHHVH

FUHDWH9HJJLHV

FUHDWH9HJJLHV

FUHDWH3HSSHURQL

FUHDWH3HSSHURQL

FUHDWH&ODP

FUHDWH&ODP

0DULQDUD6DXFH

LQWHUIDFH!! &KHHVH

0R]]DUHOOD&KHHVH

5HJJLDQR&KHHVH

LQWHUIDFH!! &ODPV

7KHMRERIWKHFRQFUHWH SL]]DIDFWRULHVLVWR PDNHSL]]DLQJUHGLHQWV (DFKIDFWRU\NQRZV KRZWRFUHDWHWKHULJKW REMHFWVIRUWKHLUUHJLRQ

)UR]HQ&ODPV

)UHVK&ODPV

(DFKIDFWRU\SURGXFHVDGLIIHUHQW LPSOHPHQWDWLRQIRUWKHIDPLO\RISURGXFWV \RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Page 50

Return to Table of Contents

LQWHUYLHZZLWKIDFWRU\SDWWHUQV

,QRWLFHGWKDWHDFKPHWKRGLQWKH $EVWUDFW)DFWRU\DFWXDOO\ORRNVOLNHD)DFWRU\ 0HWKRG FUHDWH'RXJK FUHDWH6DXFH HWF  (DFKPHWKRGLVGHFODUHGDEVWUDFWDQGWKH VXEFODVVHVRYHUULGHLWWRFUHDWHVRPH REMHFW,VQ·WWKDW)DFWRU\0HWKRG"

,VWKDWD)DFWRU\0HWKRGOXUNLQJLQVLGHWKH $EVWUDFW)DFWRU\" /WWLKI\KP AM[WN\MV\PMUM\PWL[WN IV)J[\ZIK\.IK\WZaIZM QUXTMUMV\MLI[NIK\WZaUM\PWL[1\UISM[[MV[MZQOP\'
3DWWHUQV([SRVHG 7KLVZHHN·VLQWHUYLHZ

)DFWRU\0HWKRGDQG$EVWUDFW)DFWRU\RQHDFKRWKHU +HDG)LUVW?W_IVQV\MZ^QM__Q\P\_WXI\\MZV[I\WVKM 
&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Page 51

Return to Table of Contents

WKHIDFWRU\SDWWHUQ +HDG)LUVW+IVaW]M`XTIQVUWZMIJW]\\PI\.IK\WZa 5M\PWL' )DFWRU\0HWKRG;]ZM*W\P)J[\ZIK\.IK\WZaIVL 1KZMI\MWJRMK\[·\PI\¼[W]ZRWJ[*]\1LWQ\\PZW]OP QVPMZQ\IVKM $EVWUDFW)DFWRU\IVL1LWQ\\PZW]OPWJRMK\ KWUXW[Q\QWV )DFWRU\0HWKRG:QOP\;W\PI\UMIV[\WKZMI\M WJRMK\[][QVO.IK\WZa5M\PWLaW]VMML\WM`\MVLIKTI[[ IVLW^MZZQLMINIK\WZaUM\PWL

)DFWRU\0HWKRG7PKWUMWV\PI\¼[IJQOLMIT  +PIVOQVOaW]ZQV\MZNIKMUMIV[aW]PI^M\WOWQVIVL KPIVOM\PMQV\MZNIKMWN M^MZa[]JKTI[[ 
$EVWUDFW)DFWRU\AM[1¼TTILUQ\Q\UaKWVKZM\M NIK\WZQM[WN\MVQUXTMUMV\INIK\WZaUM\PWL\WKZMI\M )DFWRU\0HWKRG1\KZMI\M[WJRMK\[WN KW]Z[M 1UMIV \PMQZXZWL]K\[1VUaKI[M\PMaIZM][MLX]ZMTa\WKZMI\M \PM_PWTMXWQV\WN \PM.IK\WZa5M\PWL8I\\MZVQ[\PI\ XZWL]K\[ aW]¼ZM][QVOI[]JKTI[[\WLWaW]ZKZMI\QWVNWZaW]1V \PI\_IaKTQMV\[WVTaVMML\WSVW_\PMIJ[\ZIK\\aXM\PMa )DFWRU\0HWKRG_PQTMQVUaKI[M1][]ITTa QUXTMUMV\KWLMQV\PMIJ[\ZIK\KZMI\WZ\PI\UISM[][MWN  IZM][QVO\PM[]JKTI[[_WZZQM[IJW]\\PMKWVKZM\M\aXM \PMKWVKZM\M\aXM[\PM[]JKTI[[M[KZMI\M ;WQVW\PMZ_WZL[1SMMXKTQMV\[LMKW]XTMLNZWU\PM KWVKZM\M\aXM[ +HDG)LUVW1\[W]VL[TQSMaW]JW\PIZMOWWLI\_PI\ aW]LW1¼U[]ZMXMWXTMTQSMPI^QVOIKPWQKM#IN\MZITT $EVWUDFW)DFWRU\)VL1LW\WWWVTa1LWQ\QVI NIK\WZQM[IZM[W][MN]T\PMa¼TT_IV\\W][M\PMUQVITT LQNNMZMV\_Ia SQVL[WN LQNNMZMV\[Q\]I\QWV[AW]JW\PMVKIX[]TI\M +HDG)LUVW/WWV)J[\ZIK\.IK\WZaaW][IQL WJRMK\KZMI\QWV\WSMMXIXXTQKI\QWV[TWW[MTaKW]XTML [WUM\PQVOIJW]\WJRMK\KWUXW[Q\QWV' IVLTM[[LMXMVLMV\WVQUXTMUMV\I\QWV[_PQKPQ[ZMITTa OZMI\_PM\PMZaW]¼ZM][QVO.IK\WZa5M\PWLWZ)J[\ZIK\ $EVWUDFW)DFWRU\1XZW^QLMIVIJ[\ZIK\\aXMNWZ .IK\WZa5Ia1ITTW_aW]MIKPIXIZ\QVO_WZL' KZMI\QVOINIUQTaWN XZWL]K\[;]JKTI[[M[WN \PQ[\aXM LMÅVMPW_\PW[MXZWL]K\[IZMXZWL]KML
$EVWUDFW)DFWRU\


Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 52

SDWWHUQVFRPSDUHG

.IK\WZa5M\PWLIVL)J[\ZIK\.IK\WZaKWUXIZML 3URYLGHVDQDEVWUDFW LQWHUIDFHIRU FUHDWLQJRQHSURGXFW

3L]]D6WRUH FUHDWH3L]]D

VZKLFK (DFKVXEFODVVGHFLGH DQWLDWH QVW RL VW ODV HF HW QFU FR

1HZ
 3L]]D6WRUHLVLPSOHPHQWHGDVD)DFWRU\ 0HWKRGEHFDXVHZHZDQWWREHDEOHWR FUHDWHDSURGXFWWKDWYDULHVE\UHJLRQ :LWKWKH)DFWRU\0HWKRGHDFKUHJLRQ JHWVLWVRZQFRQFUHWHIDFWRU\WKDW NQRZVKRZWRPDNHSL]]DVZKLFKDUH DSSURSULDWHIRUWKHDUHD

&KLFDJR3L]]D6WRUH

1<3L]]D6WRUH

7KH)DFWRU\0HWKRG

&KLFDJR6WRUH

FUHDWH3L]]D

FUHDWH3L]]D

7KH)DFWRU\0HWKRG

7KH1<3L]]D6WRUHVXEFODVVRQO\ LQVWDQWLDWHV1<VW\OHSL]]DV

7KLVLVWKHSURGXFWRIWKH 3L]]D6WRUH&OLHQWVRQO\ UHO\RQWKLVDEVWUDFWW\SH

7KH&KLFDJDR3L]]D6WRUH VXEFODVVLQVWDQWLDWHVRQO\ &KLFDJRVW\OHSL]]DV

3L]]D

1<6W\OH&KHHVH3L]]D

1<6W\OH3HSSHURQL3L]]D

1<6W\OH&ODP3L]]D

6XEFODVVHVDUHLQVWDQLDWHG E\WKH)DFWRU\0HWKRGV

&KLFDJR6W\OH&KHHVH3L]]D &KLFDJR6W\OH3HSSHURQL3L]]D

&KLFDJR6W\OH&ODP3L]]D

1<6W\OH9HJJLH3L]]D

&KLFDJR6W\OH9HJJLH3L]]D

1HZ
&KLFDJR

7KHFUHDWH3L]]D PHWKRGLVSDUDPHWHUL]HGE\SL]]D W\SHVRZHFDQUHWXUQPDQ\W\SHVRISL]]DSURGXFWV 

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 53

WKHIDFWRU\SDWWHUQ

DQ RU\LVLPSOHPHQWHGDV 3L]]D,QJUHGLHQW)DFW FDXVHZHQHHGWRFUHDWH $EVWUDFW)DFWRU\EH WKHLQJUHGLHQWV (DFK IDPLOLHVRISURGXFWV KHLQJUHGLHQWVXVLQJLWVRZQ VXEFODVVLPSOHPHQWVW UHJLRQDOVXSSOLHUV

LQWHUIDFH!! 3L]]D,QJUHGLHQW)DFWRU\ FUHDWH'RXJK

3URYLGHVDQDEVWUDFW  LQWHUIDFHIRUFUHDWLQJD IDPLO\RISURGXFWV

FUHDWH6DXFH FUHDWH&KHHVH FUHDWH9HJJLHV FUHDWH3HSSHURQL

(DFKFRQFUHWHVXEFODVV FUHDWHVDIDPLO\RISURGXFWV

FUHDWH&ODP

1HZ
1<3L]]D,QJUHGLHQW)DFWRU\

FUHDWH'RXJK

FUHDWH6DXFH

FUHDWH6DXFH

FUHDWH&KHHVH

FUHDWH&KHHVH

FUHDWH9HJJLHV

FUHDWH9HJJLHV

FUHDWH3HSSHURQL

FUHDWH3HSSHURQL

FUHDWH&ODP

FUHDWH&ODP

0HWKRGVWRFUHDWHSURGXFWV LQDQ$EVWUDFW)DFWRU\DUH RIWHQLPSOHPHQWHGZLWKD )DFWRU\0HWKRG

RUWKHW\SHRIFODPV

IRULQVWDQFHWKHVXEFODVV GHFLGHVWKHW\SHRIGRXJK

LQWHUIDFH!! &ODPV

LQWHUIDFH!! 'RXJK

7KLFN&UXVW'RXJK

&KLFDJR

&KLFDJR3L]]D,QJUHGLHQW)DFWRU\

FUHDWH'RXJK

)UR]HQ&ODPV

7KLQ&UXVW'RXJK

LQWHUIDFH!! 6DXFH

3OXP7RPDWR6DXFH

0DULQDUD6DXFH

(DFKLQJUHGLHQW UHSUHVHQWVD  SURGXFWWKDWLV SURGXFHGE\D G )DFWRU\0HWKR  LQWKH$EVWUDFW )DFWRU\

)UHVK&ODPV

LQWHUIDFH!! &KHHVH

0R]]DUHOOD&KHHVH

5HJJLDQR&KHHVH

7KHSURGXFWVXEFODVVHVFUHDWHSDUDOOHOVHWVRISURGXFWIDPLOLHV +HUHZHKDYHD1HZ


Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 54

\RXUGHVLJQWRROER[


 :MDD=LHGAFLK

ƒ $OOIDFWRULHVHQFDSVXODWHREMHFW FUHDWLRQ

ƒ 6LPSOH)DFWRU\ZKLOHQRWDERQD ILGHGHVLJQSDWWHUQLVDVLPSOH ZD\WRGHFRXSOH\RXUFOLHQWV IURPFRQFUHWHFODVVHV

22%DVLFV

$EVWUDFWLRQ Q (QFDSVXODWLR YDULHV W KD P Z LV WH SK OD (QFDSVX 3RO\PRU QKHULWDQFH VLWLRQRYHUL )DYRUFRPSR ,QKHULWDQFH

223ULQFLSOH

V

RW LQWHUIDFHVQ 3URJUDPWRWLRQV LPSOHPHQWD HGGHVLJQV RRVHO\FRXSO 6WULYHIRUOMHFWVWKDWLQWHUDFW EHWZHHQRE UH[WHQVLRQ GEHRSHQIR &ODVVHVVKRXOIRUPRGLILFDWLRQ EXWFORVHG 'RQRW EVWUDFWLRQV 'HSHQGRQDFRQFUHWHFODVVHV GHSHQGRQ

223DWWHLQUHVQDVILQDPHVDLO\RRIQHDOJRWRULWKPPQDDQVO\

ƒ )DFWRU\0HWKRGUHOLHVRQ LQKHULWDQFHREMHFWFUHDWLRQLV GHOHJDWHGWRVXEFODVVHVZKLFK LPSOHPHQWWKHIDFWRU\PHWKRGWR FUHDWHREMHFWV

ƒ $EVWUDFW)DFWRU\UHOLHVRQREMHFW

:HKDYHDQHZSULQFLSOHWKDW JXLGHVXVWRNHHSWKLQJV DEVWUDFWZKHQHYHUSRVVLEOH

ƒ $OOIDFWRU\SDWWHUQVSURPRWH ORRVHFRXSOLQJE\UHGXFLQJWKH GHSHQGHQF\RI\RXUDSSOLFDWLRQ RQFRQFUHWHFODVVHV

ZSDWWHUQV %RWKRIWKHVHQHWFUHDWLRQ HF EM HQFDSVXODWHR HGHFRXSOHG DQGOHDGWRPRU IOH[LEOHGHVLJQV

LR3KD W GVR PLWW WDGKH YLLFGDOHO\DQ DFMHWHVKFW URKP VH\URYHHUGUDFDHIWKERRQHWGUHI PREDN 6WU2DWEHJ $QWW RU\Q QG UKHVFW\DWH H R F ZWDHH LWODP D LW G OJ ) HV F DO F\ H DW  F MH XO HQ D ' HV W RE G SV U LOL  VRI QUHHVWVDW VWD HQFDGHSHQ$EVW J\DOH WR WH VF LQ[LJDWEOIXDP 6WLHHUD HOLW VHLW H MH RUKDLGFFQJ RQHDHVLEOELUI DW IRY RE SG WKQG UHKDQVSQJRQ X VIDOH DFV\FW D VZHGLWKRXW QW H FW OLH LQWHZUFKH G MH LQRUWH LH RE RP LI SU RUWO UHIUQRWHSHGDVHQVLW H[WHQGLQJ QGDWHQ 'GHHF HQSHGUHHQ FOFRQQJFUIHWRUHFODVVHV YDU\GLQ ODDWWVWHLYDGHRWRUVGXE HS O\ KHLU DXDOWRWHPUQ VSDWHFLFLIDO\LQJ\W LQHDQ IXQFWLRQDOLW HWKRGDQ'HI HFWEXW EM R )DFWRUH\0 J RUFUHDWLQ FKFODVVWR I DF UI WH LQ GHFLGHZKL V OHWVXEFODVVHV)DFWRU\0HWKRGOHWH H DW WL WRWK RQ WL LQVWDQ LD QW WD LQV DFODVVGHIHU VXEFODVVHV



FRPSRVLWLRQREMHFWFUHDWLRQ LVLPSOHPHQWHGLQPHWKRGV H[SRVHGLQWKHIDFWRU\LQWHUIDFH

ƒ 7KHLQWHQWRI)DFWRU\0HWKRG LVWRDOORZDFODVVWRGHIHU LQVWDQWLDWLRQWRLWVVXEFODVVHV

ƒ 7KHLQWHQWRI$EVWUDFW)DFWRU\ LVWRFUHDWHIDPLOLHVRIUHODWHG REMHFWVZLWKRXWKDYLQJWRGHSHQG RQWKHLUFRQFUHWHFODVVHV

ƒ 7KH'HSHQGHQF\,QYHUVLRQ 3ULQFLSOHJXLGHVXVWRDYRLG GHSHQGHQFLHVRQFRQFUHWHW\SHV DQGWRVWULYHIRUDEVWUDFWLRQV

ƒ )DFWRULHVDUHDSRZHUIXO WHFKQLTXHIRUFRGLQJWR DEVWUDFWLRQVQRWFRQFUHWH FODVVHV

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 55

WKHIDFWRU\SDWWHUQ

1\¼[JMMVITWVOKPIX\MZ/ZIJI[TQKMWN8QbbIIVLZMTI`_PQTMLWQVO \PQ[KZW[[_WZL#ITTWN\PM[WT]\QWV_WZL[IZMNZWU\PQ[KPIX\MZ



  



























 -  "2.05 $2'.# $ "' %0 -"'(1$ (1   -  "2.05 $2'.# 4'. #$"(#$1 4'("' "+ 11 2. (-12 -2( 2$

.+$.%(66 2.0$(- "2.05$2'.# 22$0++$4.0*25+$(66 131$2'(1*(-#.% "'$$1$ -!120 "2 "2.05$ "'(-&0$#($-2% "2.05(1   '$- 5.3 31$ -$4 5.3 0$ /0.&0 ,,(-& 2. -   "0$ 2$(66  (1  24. 4.0#1   .$+ +(*$1 2'(1 *(-# .% /(66  - "2.05$2'.#2'$(66 2.0$ -#2'$ ".-"0$2$ (66 1 ++ #$/$-# .- 2'(1 !120 "2(.  '$- "+ 11 (-12 -2( 2$1 - .!)$"2 %0., ".-"0$2$ "+ 11 (21  .- 2' 2 .!)$"2   ++ % "2.05 / 22$0-1 ++.4 31 2.  .!)$"2 "0$ 2(.-

 $ 31$#  (- (,/+$  "2.05 -# !120 "2  "2.05 -# (-'$0(2 -"$ (-  "2.05 $2'.#

 !120 "2  "2.05 "0$ 2$1  .% /0.#3"21 .2 % "2.05/ 22$0-!32' -#5 -.-$2'$+$11  2' - +(*$1 2'(1 *(-# .% /(66

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Page 56

Return to Table of Contents

H[HUFLVHVROXWLRQV

([HUFLVHVROXWLRQV 6KDUSHQ \RXU SHQFLO ?M¼^MSVWKSMLW]\\PM6A8QbbI;\WZM#R][\\_WUWZM\WOWIVL_M¼TTJMZMILa\WNZIVKPQ[M  ?ZQ\M\PM+PQKIOWIVL+ITQNWZVQI8QbbI;\WZMQUXTMUMV\I\QWV[PMZM"

H[DFWO\OLNHWKH1HZ
SXEOLFFODVV&DOLIRUQLD3L]]D6WRUHH[WHQGV3L]]D6WRUH^ SURWHFWHG3L]]DFUHDWH3L]]D 6WULQJLWHP ^ LI LWHPHTXDOV ³FKHHVH´ ^ UHWXUQQHZ&DOLIRUQLD6W\OH&KHHVH3L]]D  `HOVHLI LWHPHTXDOV ³YHJJLH´ ^ UHWXUQQHZ&DOLIRUQLD6W\OH9HJJLH3L]]D  `HOVHLI LWHPHTXDOV ³FODP´ ^ UHWXUQQHZ&DOLIRUQLD6W\OH&ODP3L]]D  `HOVHLI LWHPHTXDOV ³SHSSHURQL´ ^ UHWXUQQHZ&DOLIRUQLD6W\OH3HSSHURQL3L]]D  `HOVHUHWXUQQXOO ` `



SL]]D )RUWKH&KLFDJRDYHWR K VW MX VWRUHZH DWH PDNHVXUHZHFUH ]DV SL] OH W\ V JR LFD &K

RUQLD DQGIRUWKH&DOLIFUHDWH H SL]]DVWRUHZ SL]]DV &DOLIRUQLDVW\OH

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 57

WKHIDFWRU\SDWWHUQ

'HVLJQ3X]]OH6ROXWLRQ ?MVMMLIVW\PMZSQVLWN XQbbINWZ\PW[MKZIba+ITQNWZVQIV[KZIbaQVI/77,_IaWN  KW]Z[M,ZI_IVW\PMZXIZITTMT[M\WN KTI[[M[\PI\aW]¼LVMML\WILLIVM_+ITQNWZVQI ZMOQWV\WW]Z8QbbI;\WZM

3L]]D6WRUH

\RXQHHGWR +HUH·VHYHU\WKLQJDSL]]DVWRUH DGGD&DOLIRUQL ]DVWRUHFODVV WKHFRQFUHWHSL]QLDVW\OHSL]]DV DQGWKH&DOLIRU

FUHDWH3L]]D RUGHU3L]]D

1<3L]]D6WRUH FUHDWH3L]]D

1<6W\OH&KHHVH3L]]D

1<6W\OH3HSSHURQL3L]]D 1<6W\OH&ODP3L]]D

1<6W\OH9HJJLH3L]]D

&KLFDJR3L]]D6WRUH FUHDWH3L]]D

&KLFDJR6W\OH&KHHVH3L]]D &KLFDJR6W\OH3HSSHURQL3L]]D &KLFDJR6W\OH&ODP3L]]D

&KLFDJR6W\OH9HJJLH3L]]D

&DOLIRUQLD3L]]D6WRUH FUHDWH3L]]D

&DOLIRUQLD6W\OH&KHHVH3L]]D &DOLIRUQLD6W\OH3HSSHURQL3L]]D

&DOLIRUQLD6W\OH&ODP3L]]D

&DOLIRUQLD6W\OH9HJJLH3L]]D

7SIaVW__ZQ\M\PMÅ^M[QTTQM[\\PQVO[aW]KIV\PQVSWN \WX]\WVIXQbbI
+HUHDWLUHRQRVXU  0DVKHG3RWDWRHVZLWK5RDVWHG*DUOLF VXJJHV %%46DXFH $UWLFKRNH+HDUWV 0 0·V 3HDQXWV \RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 58

H[HUFLVHVROXWLRQV

)^MZaLMXMVLMV\8QbbI;\WZM 6KDUSHQ \RXU SHQFLO 4M\¼[XZM\MVLaW]¼^MVM^MZPMIZLWN IV77NIK\WZa0MZM¼[I^MZ[QWVWN \PM8QbbI;\WZM \PI\LWM[V¼\][MINIK\WZa#UISMIKW]V\WN \PMV]UJMZWN KWVKZM\MXQbbIWJRMK\[\PQ[ KTI[[Q[LMXMVLMV\WV1N aW]ILLML+ITQNWZVQI[\aTMXQbbI[\W\PQ[8QbbI;\WZMPW_UIVa WJRMK\[_W]TLQ\JMLMXMVLMV\WV\PMV' SXEOLFFODVV'HSHQGHQW3L]]D6WRUH^  SXEOLF3L]]DFUHDWH3L]]D 6WULQJVW\OH6WULQJW\SH ^ 3L]]DSL]]D QXOO LI VW\OHHTXDOV ³1<´ ^ LI W\SHHTXDOV ³FKHHVH´ ^ SL]]D QHZ1<6W\OH&KHHVH3L]]D  `HOVHLI W\SHHTXDOV ³YHJJLH´ ^ +DQGOHVDOOWKH1< SL]]D QHZ1<6W\OH9HJJLH3L]]D  VW\OHSL]]DV `HOVHLI W\SHHTXDOV ³FODP´ ^ SL]]D QHZ1<6W\OH&ODP3L]]D  `HOVHLI W\SHHTXDOV ³SHSSHURQL´ ^ SL]]D QHZ1<6W\OH3HSSHURQL3L]]D  ` `HOVHLI VW\OHHTXDOV ³&KLFDJR´ ^ LI W\SHHTXDOV ³FKHHVH´ ^ SL]]D QHZ&KLFDJR6W\OH&KHHVH3L]]D  +DQGOHVDOOWKH `HOVHLI W\SHHTXDOV ³YHJJLH´ ^ &KLFDJRVW\OH SL]]D QHZ&KLFDJR6W\OH9HJJLH3L]]D  SL]]DV `HOVHLI W\SHHTXDOV ³FODP´ ^ SL]]D QHZ&KLFDJR6W\OH&ODP3L]]D  `HOVHLI W\SHHTXDOV ³SHSSHURQL´ ^ SL]]D QHZ&KLFDJR6W\OH3HSSHURQL3L]]D  ` `HOVH^ 6\VWHPRXWSULQWOQ ³(UURULQYDOLGW\SHRISL]]D´  UHWXUQQXOO ` SL]]DSUHSDUH  SL]]DEDNH  SL]]DFXW  SL]]DER[  UHWXUQSL]]D ` `




QXPEHU



OLIRUQLDWRR

QXPEHUZLWK&D

&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 59

WKHIDFWRU\SDWWHUQ

6KDUSHQ \RXU SHQFLO /WIPMILIVL_ZQ\M\PM+PQKIOW8QbbI1VOZMLQMV\.IK\WZa#aW]KIVZMNMZMVKM\PM KTI[[M[JMTW_QVaW]ZQUXTMUMV\I\QWV" SXEOLFFODVV&KLFDJR3L]]D,QJUHGLHQW)DFWRU\ LPSOHPHQWV3L]]D,QJUHGLHQW)DFWRU\ ^ SXEOLF'RXJKFUHDWH'RXJK ^ UHWXUQQHZ7KLFN&UXVW'RXJK  ` SXEOLF6DXFHFUHDWH6DXFH ^ UHWXUQQHZ3OXP7RPDWR6DXFH  ` SXEOLF&KHHVHFUHDWH&KHHVH ^ UHWXUQQHZ0R]]DUHOOD&KHHVH  ` SXEOLF9HJJLHV>@FUHDWH9HJJLHV ^ 9HJJLHVYHJJLHV>@ ^QHZ%ODFN2OLYHV  QHZ6SLQDFK  QHZ(JJSODQW ` UHWXUQYHJJLHV ` SXEOLF3HSSHURQLFUHDWH3HSSHURQL ^ UHWXUQQHZ6OLFHG3HSSHURQL  ` SXEOLF&ODPVFUHDWH&ODP ^ UHWXUQQHZ)UR]HQ&ODPV  ` ` (JJ3ODQW 6SLQDFK

7KLFN&UXVW'RXJK

6OLFHG3HSSHURQL

%ODFN2OLYHV

3OXP7RPDWR6DXFH

)UR]HQ&ODPV 0R]]DUHOOD&KHHVH

\RXDUHKHUH



Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 4. Baking with OO Goodness

Return to Table of Contents

Page 60

FURVVZRUGSX]]OHVROXWLRQ

3X]]OH6ROXWLRQ













!



#





!





#



! 

 



"

$









"



!





#





!

!







#









"

"











#



 







 





!



#









#











#



#





!

&





#















"

#

&





!

&













#



 













#

&



# 

! &

"

" 

# 

 &











 '

'















 





"

$





#



&KDSWHU

Chapter 4. Baking with OO Goodness Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Table of Contents Chapter 5. One of a Kind Objects................................................................. 1 Section 5.1. The Little Singleton: A small Socratic exercise in the style of The Little Lisper........................................................ 3 Section 5.2. Dissecting the classic Singleton Pattern implementation.......................................................................................... 5 Section 5.3. Patterns Exposed......................................................................................................................................................... 6 Section 5.4. The Chocolate Factory................................................................................................................................................. 7 Section 5.5. Sharpen your pencil.................................................................................................................................................... 8 Section 5.6. Singleton Pattern defined........................................................................................................................................... 9 Section 5.7. Hershey, PA: Houston, we have a problem............................................................................................................... 10 Section 5.8. BE the JVM................................................................................................................................................................ 11 Section 5.9. Dealing with multithreading..................................................................................................................................... 12 Section 5.10. Can we improve multithreading?............................................................................................................................ 13 Section 5.11. Meanwhile, back at the Chocolate Factory.............................................................................................................. 15 Section 5.12. Congratulations!....................................................................................................................................................... 15 Section 5.13. there are no Dumb Questions.................................................................................................................................. 16 Section 5.14. Tools for your Design Toolbox................................................................................................................................ 18 Section 5.15. Exercise solutions.................................................................................................................................................... 20 Section 5.16. Exercise Solutions.................................................................................................................................................... 21 Section 5.17. Exercise solutions.................................................................................................................................................... 22

Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Page 1

the Singleton Pattern

Return to Table of Contents

5

4 2QHRID.LQG2EMHFWV 4 ,WHOO\DVKH·V21(2)$ .,1'/RRNDWWKHOLQHV WKHFXUYHVWKHERG\ WKHKHDGOLJKWV


2XUQH[WVWRSLVWKH6LQJOHWRQ3DWWHUQRXUWLFNHWWRFUHDWLQJRQH RIDNLQGREMHFWVIRUZKLFKWKHUHLVRQO\RQHLQVWDQFH
WKLVLVDQHZFKDSWHU



Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 2

RQHDQGRQO\RQH

:KDWLVWKLV"$Q HQWLUHFKDSWHUDERXWKRZWR LQVWDQWLDWHMXVW 21(2%-(&7 7KDW·VRQHDQG21/< 21(REMHFW

Copyright Safari Books Online #1673621 'HYHORSHU:KDWXVHLVWKDW" *XUX7KHUHDUHPDQ\REMHFWVZHRQO\QHHGRQHRIWKUHDGSRROVFDFKHVGLDORJER[HVREMHFWVWKDWKDQGOH SUHIHUHQFHVDQGUHJLVWU\VHWWLQJVREMHFWVXVHGIRUORJJLQJDQGREMHFWVWKDWDFWDVGHYLFHGULYHUVWRGHYLFHV OLNHSULQWHUVDQGJUDSKLFVFDUGV,QIDFWIRUPDQ\RIWKHVHW\SHVRIREMHFWVLIZHZHUHWRLQVWDQWLDWH PRUHWKDQRQHZH·GUXQLQWRDOOVRUWVRISUREOHPVOLNHLQFRUUHFWSURJUDPEHKDYLRURYHUXVHRIUHVRXUFHVRU LQFRQVLVWHQWUHVXOWV 'HYHORSHU2ND\VRPD\EHWKHUHDUHFODVVHVWKDWVKRXOGRQO\EHLQVWDQWLDWHGRQFHEXWGR,QHHGDZKROH FKDSWHUIRUWKLV"&DQ·W,MXVWGRWKLVE\FRQYHQWLRQRUE\JOREDOYDULDEOHV"
4HISISACTUALLYIMPLEMENTATIONDEPENDENT3OME*6-SWILLCREATETHESEOBJECTSLAZILY



&KDSWHU

Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 3

WKHVLQJOHWRQSDWWHUQ

7KH/LWWOH6LQJOHWRQ )[UITT;WKZI\QKM`MZKQ[MQV\PM[\aTMWN
0W__W]TLaW]KZMI\MI[QVOTMWJRMK\'

QHZ0\2EMHFW 

)VL_PI\QN IVW\PMZWJRMK\_IV\ML\WKZMI\MI 5a7JRMK\'+W]TLQ\KITTVM_WV5a7JRMK\IOIQV'

AM[WN KW]Z[M

;WI[TWVOI[_MPI^MIKTI[[KIV_MIT_Ia[ QV[\IV\QI\MQ\WVMWZUWZM\QUM['

AM[?MTTWVTaQN Q\¼[IX]JTQKKTI[[

)VLQN VW\'

?MTTQN Q\¼[VW\IX]JTQKKTI[[WVTaKTI[[M[QV\PM[IUM XIKSIOMKIVQV[\IV\QI\MQ\*]\\PMaKIV[\QTTQV[\IV\QI\M Q\UWZM\PIVWVKM

0UUQV\MZM[\QVO

6W1¼LVM^MZ\PW]OP\WN Q\J]\1O]M[[Q\UISM[ [MV[MJMKI][MQ\Q[ITMOITLMÅVQ\QWV

,QLaW]SVW_aW]KW]TLLW\PQ['

SXEOLF0\&ODVV^ SULYDWH0\&ODVV ^` `

?PI\LWM[Q\UMIV'

1[]XXW[MQ\Q[IKTI[[\PI\KIV¼\JMQV[\IV\QI\ML JMKI][MQ\PI[IXZQ^I\MKWV[\Z]K\WZ

?MTTQ[\PMZM)6AWJRMK\\PI\KW]TL][M \PMXZQ^I\MKWV[\Z]K\WZ'

0UU1\PQVS\PMKWLMQV5a+TI[[Q[\PMWVTa KWLM\PI\KW]TLKITTQ\*]\\PI\LWM[V¼\UISM U]KP[MV[M

\RXDUHKHUH



Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 4

FUHDWLQJDVLQJOHWRQ

?PaVW\'

*MKI][M1¼LPI^M\WPI^MIVQV[\IVKMWN \PM KTI[[\WKITTQ\J]\1KIV¼\PI^MIVQV[\IVKM JMKI][MVWW\PMZKTI[[KIVQV[\IV\QI\MQ\1\¼[ IKPQKSMVIVLMOOXZWJTMU"1KIV][M\PM KWV[\Z]K\WZNZWUIVWJRMK\WN \aXM5a+TI[[ J]\1KIVVM^MZQV[\IV\QI\M\PI\WJRMK\JMKI][M VWW\PMZWJRMK\KIV][M¹VM_5a+TI[[º

7SIa1\_I[R][\I\PW]OP\

5a+TI[[Q[IKTI[[_Q\PI[\I\QKUM\PWL?MKIV KITT\PM[\I\QKUM\PWLTQSM\PQ["

?PI\LWM[\PQ[UMIV'

0\&ODVVJHW,QVWDQFH  SXEOLF0\&ODVV^ SXEOLFVWDWLF0\&ODVVJHW,QVWDQFH ^ ` `

?PaLQLaW]][M5a+TI[[QV[\MILWN  [WUMWJRMK\VIUM'

?MTTOM\1V[\IVKMQ[I[\I\QKUM\PWL#QVW\PMZ _WZL[Q\Q[I+4);;UM\PWLAW]VMML\W][M \PMKTI[[VIUM\WZMNMZMVKMI[\I\QKUM\PWL

>MZaQV\MZM[\QVO?PI\QN _MX]\\PQVO[\WOM\PMZ

?W_aW][]ZMKIV

6W_KIV1QV[\IV\QI\MI5a+TI[[' SXEOLF0\&ODVV^ SULYDWH0\&ODVV ^` SXEOLFVWDWLF0\&ODVVJHW,QVWDQFH ^ UHWXUQQHZ0\&ODVV  ` `

;WVW_KIVaW]\PQVSWN I[MKWVL_Ia\WQV[\IV\QI\M IVWJRMK\'

0\&ODVVJHW,QVWDQFH 

+IVaW]ÅVQ[P\PMKWLM[W\PI\WVTa76-QV[\IVKM WN 5a+TI[[Q[M^MZKZMI\ML'

AM[1\PQVS[W AW]¼TTÅVL\PMKWLMWV\PMVM`\XIOM



&KDSWHU

Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 5

WKHVLQJOHWRQSDWWHUQ

,Q[[MK\QVO\PMKTI[[QK;QVOTM\WV 8I\\MZVQUXTMUMV\I\QWV /HW·VUHQDPH0\&ODVV WR6LQJOHWRQ SXEOLFFODVV6LQJOHWRQ^ SULYDWHVWDWLF6LQJOHWRQXQLTXH,QVWDQFH  RWKHUXVHIXOLQVWDQFHYDULDEOHVKHUH  SULYDWH6LQJOHWRQ ^`  SXEOLFVWDWLF6LQJOHWRQJHW,QVWDQFH ^ LI XQLTXH,QVWDQFH QXOO ^ XQLTXH,QVWDQFH QHZ6LQJOHWRQ  ` UHWXUQXQLTXH,QVWDQFH `  RWKHUXVHIXOPHWKRGVKHUH `

:DWFKLW

WLF HDVWD :HKDY WRKROGRXU YDULDEOHWDQFHRIWKH RQHLQV LQJOHWRQ FODVV6

)FYOUREJUST FLIPPINGTHROUGH THEBOOK DONT BLINDLYTYPEINTHIS CODE YOULLSEEA ITHASAFEWISSUES LATERINTHECHAPTER

2XUFRQVWUXFWRULV GHFODUHGSULYDWH RQO\6LQJOHWRQFDQ LQVWDQWLDWHWKLVFODVV

7KHJHW,QVWDQFH  PHWKRGJLYHVXVDZD\ WRLQVWDQWLDWHWKHFODVV DQGDOVRWRUHWXUQDQ LQVWDQFHRILW 2IFRXUVH6LQJOHWRQLV DQRUPDOFODVVLWKDV RWKHUXVHIXOLQVWDQFH YDULDEOHVDQGPHWKRGV

&RGH8S&ORVH WKHQZH ,IXQLTXH,QVWDQFHLVQXOO DQFH\HW QVW HL WK WHG UHD KDYHQ·WF DQGLILWGRHVQ·WH[LVWZH LQVWDQWLDWH6LQJOHWRQWKURXJKLWV SULYDWHFRQVWUXFWRUDQGDVVLJQ LWWRXQLTXH,QVWDQFH1RWHWKDW LIZHQHYHUQHHGWKHLQVWDQFHLW LI XQLTXH,QVWDQFH QXOO ^ QHYHUJHWVFUHDWHGWKLVLVOD]\ XQLTXH,QVWDQFH QHZ0\&ODVV  LQVWDQWLDWLRQ

XQLTXH,QVWDQFHKROGVRXU21( LQVWDQFHUHPHPEHULWLVD VWDWLFYDULDEOH

` UHWXUQXQLTXH,QVWDQFH

%\WKHWLPHZHKLWWKLVFRGHZH KDYHDQLQVWDQFHDQGZHUHWXUQLW

,IXQLTXH,QVWDQFHZDVQ·WQXOO WKHQLWZDVSUHYLRXVO\FUHDWHG :HMXVWIDOOWKURXJKWRWKH UHWXUQVWDWHPHQW

\RXDUHKHUH



Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 6

LQWHUYLHZZLWKVLQJOHWRQ

3DWWHUQV([SRVHG 7KLVZHHN·VLQWHUYLHZ

&RQIHVVLRQVRI D6LQJOHWRQ +HDG)LUVW-:OM\QV[\IV\QI\ML' 6LQJOHWRQAW][MM\WOM\IPWTLWN I;QVOTM\WVWJRMK\aW]LWV¼\QV[\IV\QI\MWVMaW]R][\I[S NWZIVQV[\IVKM;WUaKTI[[PI[I[\I\QKUM\PWLKITTMLOM\1V[\IVKM+ITT\PI\IVL1¼TT[PW_]X I\WVKMZMILa\W_WZS1VNIK\1UIaITZMILaJMPMTXQVOW\PMZWJRMK\[_PMVaW]ZMY]M[\UM +HDG)LUVW?MTT5Z;QVOTM\WV\PMZM[MMU[\WJMITW\]VLMZaW]ZKW^MZ[\WUISMITT\PQ[_WZS
&KDSWHU

Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Page 7

Return to Table of Contents

WKHVLQJOHWRQSDWWHUQ




Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Page 8

Return to Table of Contents

FKRFRODWHERLOHUVLQJOHWRQ

YiX`e gfn\i &KRF2+ROLFKDVGRQHDGHFHQWMRERIHQVXULQJEDGWKLQJVGRQ¶WKDSSHQGRQ¶W\DWKLQN"7KHQ DJDLQ\RXSUREDEO\VXVSHFWWKDWLIWZR&KRFRODWH%RLOHULQVWDQFHVJHWORRVHVRPHYHU\EDG WKLQJVFDQKDSSHQ +RZPLJKWWKLQJVJRZURQJLIPRUHWKDQRQHLQVWDQFHRI&KRFRODWH%RLOHULVFUHDWHGLQDQ DSSOLFDWLRQ"

6KDUSHQ \RXU SHQFLO

&DQ\RXKHOS&KRF2+ROLFLPSURYHWKHLU&KRFRODWH%RLOHUFODVV E\WXUQLQJLWLQWRDVLQJOHWRQ"

SXEOLFFODVV&KRFRODWH%RLOHU^ SULYDWHERROHDQHPSW\ SULYDWHERROHDQERLOHG

SULYDWH&KRFRODWH%RLOHU ^ HPSW\ WUXH ERLOHG IDOVH `

SXEOLFYRLG¿OO ^ LI LV(PSW\ ^ HPSW\ IDOVH ERLOHG IDOVH ¿OOWKHERLOHUZLWKDPLONFKRFRODWHPL[WXUH ` ` UHVWRI&KRFRODWH%RLOHUFRGH `



&KDSWHU

Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 9

WKHVLQJOHWRQSDWWHUQ

; QVOTM\WV8I\\MZVLMNQVML 1RZWKDW\RX·YHJRWWKHFODVVLFLPSOHPHQWDWLRQRI6LQJOHWRQ LQ\RXUKHDGLW·VWLPHWRVLWEDFNHQMR\DEDURIFKRFRODWH DQGFKHFNRXWWKHÀQHUSRLQWVRIWKH6LQJOHWRQ3DWWHUQ /HW·VVWDUWZLWKWKHFRQFLVHGHÀQLWLRQRIWKHSDWWHUQ


1RELJVXUSULVHVWKHUH%XWOHW·VEUHDNLWGRZQDELWPRUH

ƒ ?PI\¼[ZMITTaOWQVOWVPMZM'?M¼ZM\ISQVOIKTI[[IVLTM\\QVOQ\UIVIOMI [QVOTMQV[\IVKMWN Q\[MTN?M¼ZMIT[WXZM^MV\QVOIVaW\PMZKTI[[NZWUKZMI\QVOI VM_QV[\IVKMWVQ\[W_V
ƒ ?M¼ZMIT[WXZW^QLQVOIOTWJITIKKM[[XWQV\\W\PMQV[\IVKM"_PMVM^MZaW] VMMLIVQV[\IVKMR][\Y]MZa\PMKTI[[IVLQ\_QTTPIVLaW]JIKS\PM[QVOTM QV[\IVKM)[aW]¼^M[MMV_MKIVQUXTMUMV\\PQ[[W\PI\\PM;QVOTM\WVQ[KZMI\ML QVITIbaUIVVMZ_PQKPQ[M[XMKQITTaQUXWZ\IV\NWZZM[W]ZKMQV\MV[Q^MWJRMK\[

2ND\OHW·VFKHFNRXWWKHFODVVGLDJUDP

VVWDWLF PHWKRGLWKRGVR\RX H F Q D VW H 7KHJHW,QHDQVLW·VDFODVVP KLVPHWKRG ZKLFKP HQLHQWO\DFFHVVW GHXVLQJ FDQFRQY \ZKHUHLQ\RXUFR KDW·VMXVWDV IURPDQ QJHW,QVWDQFH 7YDULDEOHEXW 6LQJOHWR DFFHVVLQJDJOREDO QVWDQWLDWLRQ HDV\DV HQHILWVOLNHOD]\L ZHJHWEH6LQJOHWRQ IURPWK

7KHXQLTXH,QVWDQFH FODVVYDULDEOHKROGVRXU RQHDQGRQO\LQVWDQFH RI6LQJOHWRQ

6LQJOHWRQ VWDWLFXQLTXH,QVWDQFH 2WKHUXVHIXO6LQJOHWRQGDWD VWDWLFJHW,QVWDQFH 2WKHUXVHIXO6LQJOHWRQPHWKRGV

JOHWRQ $FODVVLPSOHPHQWLQJWKH6L6LQ HWRQ QJO QD WKD UH 3DWWHUQLVPR VVZLWKLWV LWLVDJHQHUDOSXUSRVHFOD RGV RZQVHWRIGDWDDQGPHWK \RXDUHKHUH



Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 10

WKUHDGVDUHDSUREOHP

a8) 0 M Z[P M 0W][\WV_MPI^MIXZWJTMU ,WORRNVOLNHWKH&KRFRODWH%RLOHUKDVOHWXVGRZQGHVSLWH WKHIDFWZHLPSURYHGWKHFRGHXVLQJ&ODVVLF6LQJOHWRQ VRPHKRZWKH&KRFRODWH%RLOHU·VÀOO PHWKRGZDVDEOH WRVWDUWÀOOLQJWKHERLOHUHYHQWKRXJKDEDWFKRIPLONDQG FKRFRODWHZDVDOUHDG\ERLOLQJ7KDW·VJDOORQVRIVSLOOHG PLON DQGFKRFRODWH :KDWKDSSHQHG"

:HGRQ·WNQRZZKDWKDSSHQHG7KH QHZ6LQJOHWRQFRGHZDVUXQQLQJÀQH7KHRQO\ WKLQJZHFDQWKLQNRILVWKDWZHMXVWDGGHGVRPH RSWLPL]DWLRQVWRWKH&KRFRODWH%RLOHU&RQWUROOHU WKDWPDNHVXVHRIPXOWLSOHWKUHDGV

&RXOGWKHDGGLWLRQRIWKUHDGVKDYHFDXVHG WKLV",VQ·WLWWKHFDVHWKDWRQFHZH·YHVHW WKHXQLTXH,QVWDQFHYDULDEOHWRWKHVROH LQVWDQFHRI&KRFRODWH%RLOHUDOOFDOOVWR JHW,QVWDQFH VKRXOGUHWXUQWKHVDPH LQVWDQFH"5LJKW"



&KDSWHU

Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 11

WKHVLQJOHWRQSDWWHUQ

%(WKH-90 :HKDYHWZRWKUHDGVHDFKH[HFXWLQJWKLVFRGH
0DNHVXUH\RXFKHFN\RXUDQVZHURQ SDJHEHIRUHWXUQLQJWKHSDJH

QXOO ^

XQLTXH,QVWDQFH  QHZ&KRFRODWH%RLOHU  `

7KUHDG

7KUHDG

2QH

7ZR

9DOXHRI XQLTXH,QVWDQFH

UHWXUQXQLTXH,QVWDQFH `

\RXDUHKHUH



Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 12

PXOWLWKUHDGLQJDQGVLQJOHWRQ

,MITQVO_Q\PU]T\Q\PZMILQVO 2XUPXOWLWKUHDGLQJZRHVDUHDOPRVWWULYLDOO\À[HGE\PDNLQJ JHW,QVWDQFH DV\QFKURQL]HGPHWKRG

SXEOLFFODVV6LQJOHWRQ^ RQL]HGNH\ZRUGWR %\DGGLQJWKHV\QFKU HHYHU\WKUHDGWR SULYDWHVWDWLF6LQJOHWRQXQLTXH,QVWDQFH RUF HI Z  JHW,QVWDQFH  FDQHQWHUWKH RWKHUXVHIXOLQVWDQFHYDULDEOHVKHUH ZDLWLWVWXUQEHIRUHLW RWKUHDGVPD\  PHWKRG7KDWLVQRWZ HVDPHWLPH WK DW KRG SULYDWH6LQJOHWRQ ^` HQWHUWKHPHW  SXEOLFVWDWLFV\QFKURQL]HG6LQJOHWRQJHW,QVWDQFH ^ LI XQLTXH,QVWDQFH QXOO ^ XQLTXH,QVWDQFH QHZ6LQJOHWRQ  ` UHWXUQXQLTXH,QVWDQFH `  RWKHUXVHIXOPHWKRGVKHUH `

,DJUHHWKLV À[HVWKHSUREOHP %XWV\QFKURQL]DWLRQ LVH[SHQVLYHLVWKLVDQ LVVXH"

/WWLXWQV\IVLQ\¼[IK\]ITTaITQ\\TM_WZ[M\PIVaW]UISMW]\"\PMWVTa \QUM[aVKPZWVQbI\QWVQ[ZMTM^IV\Q[\PMÅZ[\\QUM\PZW]OP\PQ[UM\PWL1V W\PMZ_WZL[WVKM_M¼^M[M\\PM]VQY]M1V[\IVKM^IZQIJTM\WIVQV[\IVKM WN ;QVOTM\WV_MPI^MVWN]Z\PMZVMML\W[aVKPZWVQbM\PQ[UM\PWL)N\MZ \PMÅZ[\\QUM\PZW]OP[aVKPZWVQbI\QWVQ[\W\ITTa]VVMMLMLW^MZPMIL



&KDSWHU

Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 13

WKHVLQJOHWRQSDWWHUQ

+IV_MQUXZW^MU]T\Q\PZMILQVO' .WZUW[\2I^IIXXTQKI\QWV[_MWJ^QW][TaVMML\WMV[]ZM\PI\\PM;QVOTM\WV_WZS[QV\PMXZM[MVKM WN U]T\QXTM\PZMIL[*]\Q\TWWS[NIQZTaM`XMV[Q^M\W[aVKPZWVQbM\PMOM\1V[\IVKMUM\PWL[W_PI\ LW_MLW' ?MTT_MPI^MINM_WX\QWV[

,WVW\PQVOQN\PMXMZNWZUIVKMWNOM\1V[\IVKMQ[V¼\KZQ\QKIT\W aW]ZIXXTQKI\QWV
5W^M\WIVMIOMZTaKZMI\MLQV[\IVKMZI\PMZ\PIVITIbQTa KZMI\MLWVM 1N aW]ZIXXTQKI\QWVIT_Ia[KZMI\M[IVL][M[IVQV[\IVKMWN \PM;QVOTM\WVWZ\PMW^MZPMILWN  KZMI\QWVIVLZ]V\QUMI[XMK\[WN \PM;QVOTM\WVIZMVW\WVMZW][aW]UIa_IV\\WKZMI\MaW]Z ;QVOTM\WVMIOMZTaTQSM\PQ["

SXEOLFFODVV6LQJOHWRQ^ *RDKHDGDQGFUHDWHDQ SULYDWHVWDWLF6LQJOHWRQXQLTXH,QVWDQFH QHZ6LQJOHWRQ  LQVW DQFHRI6LQJOHWRQLQ  DVW DWLFLQLWLDOL]HU7KLV SULYDWH6LQJOHWRQ ^` FRGHLVJXDUDQWHHGWREH  WKUHDGVDIH SXEOLFVWDWLF6LQJOHWRQJHW,QVWDQFH ^ UHWXUQXQLTXH,QVWDQFH DQ :H·YHDOUHDG\JRW HWXUQLW ` U VW MX VR H QF WD ` LQV

=[QVO\PQ[IXXZWIKP_MZMTaWV\PM2>5\WKZMI\M\PM]VQY]MQV[\IVKMWN \PM;QVOTM\WV_PMV \PMKTI[[Q[TWILML5O]IZIV\MM[\PI\\PMQV[\IVKM_QTTJMKZMI\MLJMNWZMIVa\PZMIL IKKM[[M[\PM[\I\QK]VQY]M1V[\IVKM^IZQIJTM

\RXDUHKHUH



Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 14

GRXEOHFKHFNHGORFNLQJ

=[M¹LW]JTMKPMKSMLTWKSQVOº\WZML]KM\PM][MWN [aVKPZWVQbI\QWVQVOM\1V[\IVKM ?Q\PLW]JTMKPMKSMLTWKSQVO_MÅZ[\KPMKS\W[MMQN IVQV[\IVKMQ[KZMI\MLIVLQN VW\<0-6_M [aVKPZWVQbM
7KHYRODWLOHNH\ZRUGHQVXUHVWKDWPXOWLSOHWKUHDGV KDQGOHWKHXQLTXH,QVWDQFHYDULDEOHFRUUHFWO\ZKHQLW LVEHLQJLQLWLDOL]HGWRWKH6LQJOHWRQLQVWDQFH 1N XMZNWZUIVKMQ[IVQ[[]MQVaW]Z][MWN \PMOM\1V[\IVKMUM\PWL\PMV\PQ[UM\PWLWN  QUXTMUMV\QVO\PM;QVOTM\WVKIVLZI[\QKITTaZML]KM\PMW^MZPMIL

:DWFKLW

JGRHVQ·W 'RXEOHFKHFNHGORFNLQ OLHU ZRUNLQ-DYDRUHDU

QDQGHDUOLHUPDQ\ 8QIRUWXQDWHO\LQ-DYDYHUVLR WLRQVRIWKHYRODWLOHNH\ZRUG -90VFRQWDLQLPSOHPHQWD  L]DWLRQIRUGRXEOHFKHFNHG WKDWDOORZLPSURSHUV\QFKURQ RWKHUWKDQ-DYD 90 DXVH XVW RXP ,I\ ORFNLQJ OHWRQ ILPSOHPHQWLQJ\RXU6LQJ FRQVLGHURWKHUPHWKRGVR



&KDSWHU

Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Page 15

Return to Table of Contents

WKHVLQJOHWRQSDWWHUQ

5MIV_PQTMJIKSI\\PM+PWKWTI\M.IK\WZa ?PQTM_M¼^MJMMVWNN LQIOVW[QVO\PMU]T\Q\PZMILQVOXZWJTMU[\PMKPWKWTI\MJWQTMZ PI[JMMVKTMIVML]XIVLQ[ZMILa\WOW*]\ÅZ[\_MPI^M\WÅ`\PMU]T\Q\PZMILQVO XZWJTMU[?MPI^MINM_[WT]\QWV[I\PIVLMIKP_Q\PLQNNMZMV\\ZILMWNN[[W_PQKP [WT]\QWVIZM_MOWQVO\WMUXTWa'

6KDUSHQ \RXU SHQFLO )RUHDFKVROXWLRQGHVFULEHLWVDSSOLFDELOLW\WRWKHSUREOHPRI¿[LQJWKH&KRFRODWH %RLOHUFRGH

6\QFKURQL]HWKHJHW,QVWDQFH PHWKRG

8VHHDJHULQVWDQWLDWLRQ

'RXEOHFKHFNHGORFNLQJ

+WVOZI\]TI\QWV[ )\\PQ[XWQV\\PM+PWKWTI\M.IK\WZaQ[IPIXXaK][\WUMZIVL+PWK70WTQK_I[OTIL\WPI^M[WUM M`XMZ\Q[MIXXTQML\W\PMQZJWQTMZKWLM6WUI\\MZ_PQKPU]T\Q\PZMILQVO[WT]\QWVaW]IXXTQML\PMJWQTMZ [PW]TLJMQVOWWL[PIXM_Q\PVWUWZMUQ[PIX[+WVOZI\]TI\QWV[AW]¼^MVW\WVTaUIVIOML\WM[KIXM TJ[WN PW\KPWKWTI\MQV\PQ[KPIX\MZJ]\aW]¼^MJMMV\PZW]OPITT\PMXW\MV\QITXZWJTMU[WN \PM;QVOTM\WV

\RXDUHKHUH



Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 16

T DDERXWVLQJOHWRQ

there are no Dumb Questions

Q:

Q:

Q:

A:

A:

A:

&ORSUCHASIMPLEPATTERN CONSISTINGOFONLYONECLASS 3INGLETONSSURESEEMTOHAVESOME PROBLEMS

#ANT)JUSTCREATEACLASSIN WHICHALLMETHODSANDVARIABLESARE DElNEDASSTATIC7OULDNTTHATBE THESAMEASA3INGLETON

7ELL WEWARNEDYOUUP FRONT"UTDONTLETTHEPROBLEMS DISCOURAGEYOUWHILEIMPLEMENTING 3INGLETONSCORRECTLYCANBETRICKY AFTER READINGTHISCHAPTERYOUARENOW WELLINFORMEDONTHETECHNIQUESFOR CREATING3INGLETONSANDSHOULDUSE THEMWHEREVERYOUNEEDTOCONTROL THENUMBEROFINSTANCESYOUARE CREATING

9ES IFYOURCLASSISSELF CONTAINEDANDDOESNTDEPENDON COMPLEXINITIALIZATION(OWEVER BECAUSEOFTHEWAYSTATIC INITIALIZATIONSAREHANDLEDIN*AVA THISCANGETVERYMESSY ESPECIALLYIF MULTIPLECLASSESAREINVOLVED/FTEN THISSCENARIOCANRESULTINSUBTLE HARDTOFINDBUGSINVOLVINGORDER OFINITIALIZATION5NLESSTHEREISA COMPELLINGNEEDTOIMPLEMENTYOUR hSINGLETONvTHISWAY ITISFARBETTERTO STAYINTHEOBJECTWORLD

7HATABOUTCLASSLOADERS )HEARDTHEREISACHANCETHATTWO CLASSLOADERSCOULDEACHENDUPWITH THEIROWNINSTANCEOF3INGLETON

9ES THATISTRUEASEACHCLASS LOADERDEFINESANAMESPACE)FYOU HAVETWOORMORECLASSLOADERS YOU CANLOADTHESAMECLASSMULTIPLETIMES ONCEINEACHCLASSLOADER .OW IFTHAT CLASSHAPPENSTOBEA3INGLETON THEN SINCEWEHAVEMORETHANONEVERSION OFTHECLASS WEALSOHAVEMORETHAN ONEINSTANCEOFTHE3INGLETON3O IF YOUAREUSINGMULTIPLECLASSLOADERS AND3INGLETONS BECAREFUL/NEWAY AROUNDTHISPROBLEMISTOSPECIFYTHE CLASSLOADERYOURSELF

5XPRUVRI6LQJOHWRQVEHLQJHDWHQE\WKHJDUEDJH FROOHFWRUVDUHJUHDWO\H[DJJHUDWHG 3ULRUWR-DYDDEXJLQWKHJDUEDJHFROOHFWRUDOORZHG6LQJOHWRQV WREHSUHPDWXUHO\FROOHFWHGLIWKHUHZDVQRJOREDOUHIHUHQFHWRWKHP,QRWKHU ZRUGV\RXFRXOGFUHDWHD6LQJOHWRQDQGLIWKHRQO\UHIHUHQFHWRWKH6LQJOHWRQ ZDVLQWKH6LQJOHWRQLWVHOILWZRXOGEHFROOHFWHGDQGGHVWUR\HGE\WKHJDUEDJH FROOHFWRU7KLVOHDGVWRFRQIXVLQJEXJVEHFDXVHDIWHUWKH6LQJOHWRQLV ³FROOHFWHG´WKHQH[WFDOOWRJHW,QVWDQFH SURGXFHGDVKLQ\QHZ6LQJOHWRQ,Q PDQ\DSSOLFDWLRQVWKLVFDQFDXVHFRQIXVLQJEHKDYLRUDVVWDWHLVP\VWHULRXVO\ UHVHWWRLQLWLDOYDOXHVRUWKLQJVOLNHQHWZRUNFRQQHFWLRQVDUHUHVHW 6LQFH-DYDWKLVEXJKDVEHHQ¿[HGDQGDJOREDOUHIHUHQFHLVQRORQJHU UHTXLUHG,I\RXDUHIRUVRPHUHDVRQVWLOOXVLQJDSUH-DYD-90WKHQEH DZDUHRIWKLVLVVXHRWKHUZLVH\RXFDQVOHHSZHOONQRZLQJ\RXU6LQJOHWRQV ZRQ¶WEHSUHPDWXUHO\FROOHFWHG



&KDSWHU

Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 17

WKHVLQJOHWRQSDWWHUQ

Q:

)VEALWAYSBEENTAUGHTTHAT ACLASSSHOULDDOONETHINGANDONE THINGONLY&ORACLASSTODOTWO THINGSISCONSIDEREDBAD//DESIGN )SNTA3INGLETONVIOLATINGTHIS

A:

9OUWOULDBEREFERRINGTO THEh/NE#LASS /NE2ESPONSIBILITYv PRINCIPLE ANDYES YOUARECORRECT THE3INGLETONISNOTONLYRESPONSIBLE FORMANAGINGITSONEINSTANCEAND PROVIDINGGLOBALACCESS ITISALSORE SPONSIBLEFORWHATEVERITSMAINROLEIS INYOURAPPLICATION3O CERTAINLYITCAN BEARGUEDITISTAKINGONTWORESPON SIBILITIES.EVERTHELESS ITISNTHARD TOSEETHATTHEREISUTILITYINACLASS MANAGINGITSOWNINSTANCEITCERTAINLY MAKESTHEOVERALLDESIGNSIMPLER)N ADDITION MANYDEVELOPERSAREFAMILIAR WITHTHE3INGLETONPATTERNASITISIN WIDEUSE4HATSAID SOMEDEVELOPERS DOFEELTHENEEDTOABSTRACTOUTTHE 3INGLETONFUNCTIONALITY

Q:

Q:

A:

A:

)WANTEDTOSUBCLASSMY 3INGLETONCODE BUT)RANINTO PROBLEMS)SITOKAYTOSUBCLASSA 3INGLETON

/NEPROBLEMWITHSUBCLASSING 3INGLETONISTHATTHECONSTRUCTORIS PRIVATE9OUCANTEXTENDACLASSWITH APRIVATECONSTRUCTOR3O THEFIRST THINGYOULLHAVETODOISCHANGE YOURCONSTRUCTORSOTHATITSPUBLIC ORPROTECTED"UTTHEN ITSNOTREALLY A3INGLETONANYMORE BECAUSEOTHER CLASSESCANINSTANTIATEIT )FYOUDOCHANGEYOURCONSTRUCTOR THERESANOTHERISSUE4HE IMPLEMENTATIONOF3INGLETONISBASED ONASTATICVARIABLE SOIFYOUDOA STRAIGHTFORWARDSUBCLASS ALLOFYOUR DERIVEDCLASSESWILLSHARETHESAME INSTANCEVARIABLE4HISISPROBABLY NOTWHATYOUHADINMIND3O FOR SUBCLASSINGTOWORK IMPLEMENTING REGISTRYOFSORTSISREQUIREDINTHEBASE CLASS

)STILLDONTTOTALLYUNDERSTAND WHYGLOBALVARIABLESAREWORSETHAN A3INGLETON

)N*AVA GLOBALVARIABLESARE BASICALLYSTATICREFERENCESTOOBJECTS 4HEREAREACOUPLEOFDISADVANTAGES TOUSINGGLOBALVARIABLESINTHIS MANNER7EVEALREADYMENTIONED ONETHEISSUEOFLAZYVERSUSEAGER INSTANTIATION"UTWENEEDTOKEEP INMINDTHEINTENTOFTHEPATTERNTO ENSUREONLYONEINSTANCEOFACLASS EXISTSANDTOPROVIDEGLOBALACCESS! GLOBALVARIABLECANPROVIDETHELATTER BUTNOTTHEFORMER'LOBALVARIABLES ALSOTENDTOENCOURAGEDEVELOPERS TOPOLLUTETHENAMESPACEWITHLOTS OFGLOBALREFERENCESTOSMALLOBJECTS 3INGLETONSDONTENCOURAGETHISIN THESAMEWAY BUTCANBEABUSED NONETHELESS

"EFOREIMPLEMENTINGSUCHASCHEME YOUSHOULDASKYOURSELFWHATYOU AREREALLYGAININGFROMSUBCLASSING A3INGLETON,IKEMOSTPATTERNS THE 3INGLETONISNOTNECESSARILYMEANT TOBEASOLUTIONTHATCANFITINTOA LIBRARY)NADDITION THE3INGLETONCODE ISTRIVIALTOADDTOANYEXISTINGCLASS ,AST IFYOUAREUSINGALARGENUMBER OF3INGLETONSINYOURAPPLICATION YOUSHOULDTAKEAHARDLOOKATYOUR DESIGN3INGLETONSAREMEANTTOBE USEDSPARINGLY

\RXDUHKHUH



Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 18

\RXUGHVLJQWRROER[


22%DVLFV

$EVWUDFWLRQ Q (QFDSVXODWLR HV UL YD W KD Z RUSKLVP (QFDSVXODWH HULWDQFH3RO\P LRQRYHULQK VLW SR RP F )DYRU ,QKHULWDQFH

V 223ULQFLSOH

RW LQWHUIDFHVQ 3URJUDPWR RQV WL LPSOHPHQWD HGGHVLJQV RRVHO\FRXSO 6WULYHIRUOMHFWVWKDWLQWHUDFW EHWZHHQRE UH[WHQVLRQ GEHRSHQIR &ODVVHVVKRXO RUPRGLILFDWLRQ EXWFORVHGI 'RQRW EVWUDFWLRQV 'HSHQGRQD QFUHWHFODVVHV FR GHSHQGRQ

:MDD=LHGAFLK

ƒ 7KH6LQJOHWRQ3DWWHUQHQVXUHV \RXKDYHDWPRVWRQHLQVWDQFH RIDFODVVLQ\RXUDSSOLFDWLRQ

ƒ 7KH6LQJOHWRQ3DWWHUQDOVR SURYLGHVDJOREDODFFHVVSRLQW WRWKDWLQVWDQFH

ƒ -DYD·VLPSOHPHQWDWLRQRIWKH 6LQJOHWRQ3DWWHUQPDNHVXVH RIDSULYDWHFRQVWUXFWRUDVWDWLF PHWKRGFRPELQHGZLWKDVWDWLF YDULDEOH

ƒ ([DPLQH\RXUSHUIRUPDQFH

RHQVXUH\RX :KHQ\RXQHHGLQWVWDQFHRIDFODVV H RQO\KDYHRQ \RXUDSSOLFDWLRQ UXQQLQJDURXQG6LQJOHWRQ WXUQWRWKH

223DWWHUQDVIDPHVDLO\RRIQHDOJRWRULWKPPDQV\

OGHDQ HV LQ QDYLW GVR PLWW KH WDG V DFWMHHVRKFW DN 3LRURKD VH\URYHHUGUDFDHIWKELQRRQHWGUHI PRE 6WU2DWEHJ $QWW QG \ D) U LFDOHO\DQ H HH HI KP DP F Z LW ' \Q D  RU LW G  OJ O  HV F D FW DO W G 'XOHQHDW$GEHQVWF\ELUFOLWDW6WFLHRUDUVF\WHWR0 WREVWKLQMHRWDKHJWHIDPHDQVHLOLLWRHVEMHGRVLQHFVI RQWEO\KXWDV  QHHVWVDW OH HQFDGSVHS J\DQJ KD UH H FW D F VL EO MH DV X EO ) J RU RQHD FOZLWKRLQXWJ I RY UHKDQVSQJLQRQ DFV\HDFSU UIRERU H OLH LQWHZUFKH WHHDW ILGFRRULI QFLHHQQWDVDHFUHGVLVIWD(LGDWQJOHWKQVREQGH[LLQDWIXUMHXZURRUHKLFWSGYLDHFKVGDW ODHQVVVJOGW DOSRLQW [W FVHD RER WOUI QHGRPWRW HG OH HQ UHJIU WH HS QG LQ D RU LQ H SH WV U HF 6 GH HQ R ' G LQ G DV U\ HQ FO WH S FO G YD GHS UHOD LYVXHEFWRODVVVXE H V QG HV HW HW D O FU FH G RQ DQ KR F VW OHDW DO\LO\HQJLQWKHHLU)DFWWRU\0HW LFW DWHF LIRQ DXDOWRWHPVSUQ OLWDWL\FFDWHVVWRL WDQWLDWLRQWRWKH QDRIDQ IXQFWLQLRVW LQV DFODVVGHIHU VXEFODVVHV

DQGUHVRXUFHFRQVWUDLQWVDQG FDUHIXOO\FKRRVHDQDSSURSULDWH 6LQJOHWRQLPSOHPHQWDWLRQIRU PXOWLWKUHDGHGDSSOLFDWLRQV DQGZHVKRXOGFRQVLGHUDOO DSSOLFDWLRQVPXOWLWKUHDGHG 

ƒ %HZDUHRIWKHGRXEOHFKHFNHG ORFNLQJLPSOHPHQWDWLRQLWLVQRW WKUHDGVDIHLQYHUVLRQVEHIRUH -DYDYHUVLRQ

ƒ %HFDUHIXOLI\RXDUHXVLQJ PXOWLSOHFODVVORDGHUVWKLV FRXOGGHIHDWWKH6LQJOHWRQ LPSOHPHQWDWLRQDQGUHVXOWLQ PXOWLSOHLQVWDQFHV

ƒ ,I\RXDUHXVLQJD-90HDUOLHU WKDQ\RX·OOQHHGWRFUHDWHD UHJLVWU\RI6LQJOHWRQVWRGHIHDW WKHJDUEDJHFROOHFWRU

ORWRIGHWDLOV $V\RX·YHVHHQGHVSLWHLWVDSSDUHQWVLPSOLFLW\WKHUHDUHDKLVFKD SWHU DGLQJW WHUUH RQ$I HQWDWL LQYROYHGLQWKH6LQJOHWRQ·VLPSOHP WKRXJK\RXDUHUHDG\WRJRRXWDQGXVH6LQJOHWRQLQWKHZLOG 

&KDSWHU

Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 19

WKHVLQJOHWRQSDWWHUQ

;Q\JIKSWXMV\PI\KI[MWNKPWKWTI\M\PI\aW]_MZM[MV\NWZ[WT^QVO \PMU]T\Q\PZMILQVOXZWJTMUIVLPI^M[WUMLW_V\QUM_WZSQVOWV \PQ[TQ\\TMKZW[[_WZLX]bbTM#ITTWN\PM[WT]\QWV_WZL[IZMNZWU\PQ[ KPIX\MZ























  









 0 3/ ,+# ,$  ('+"

 ""#" 0, !&,!,)0# '+ 0&# ,')#.  + '+!,..#!0 '*-)#*#+00',+ !1/#" 0&'/ 0, ,2#.$),3  '+%)#0,+ -.,2'"#/  /'+%)# '+/0+!# +" 0&.## 3,."/  )3#" *1)0'0&.#"'+% --.,!& '$ +,0 1/'+% 2   &,!,)0# !-'0) ,$ 0&#   +# "2+0%# ,2#. %), ) 2.' )#/  !.#0',+  ,*-+5 0&0 -.,"1!#/ ,')#./  , 0,0))5 "#$#0 0&# +#3 !,+/0.1!0,. 3# &2# 0, "#!).# 0&# !,+/0.1!0,. 

 1)0'-)#  !+ !1/# -., )#*/   '+%)#0,+ '/  !)// 0&0 *+%#/ + '+/0+!# ,$  $5,1",+0+##"0,3,..5 ,10)65 '+/0+0'0',+ 5,1 !+ !.#0# 5,1. '+/0+!# 

 .',. 0,   0&'/ !+ #0 5,1. '+%)#0,+/ 03, 3,."/  &# '+%)#0,+ 3/ #* .//#" '0 &" +, -1 )'!   &# !)//'! '*-)#*#+00',+ ",#/+0 &+")# 0&'/  '+%)#0,+ #+/1.#/ ,+)5 ,+# ,$ 0&#/# #4'/0  &# '+%)#0,+ 00#.+ &/ ,+# \RXDUHKHUH



Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 20

H[HUFLVHVROXWLRQV

([HUFLVH VROXWLRQV

%(WKH-90

7KHDG

7KHDG

2QH

7ZR

9DOXHRI XQLTXH,QVWDQFH

SXEOLFVWDWLF&KRFRODWH%RLOHU  JHW,QVWDQFH ^

LI XQLTXH,QVWDQFH

QXOO

SXEOLFVWDWLF&KRFRODWH%RLOHU  JHW,QVWDQFH ^

QXOO

LI XQLTXH,QVWDQFH

QXOO

QXOO ^

QXOO ^

XQLTXH,QVWDQFH  QHZ&KRFRODWH%RLOHU 

REMHFW!

REMHFW!

UHWXUQXQLTXH,QVWDQFH XQLTXH,QVWDQFH  QHZ&KRFRODWH%RLOHU 

REMHFW!

REMHFW! UHWXUQXQLTXH,QVWDQFH

6KDUSHQ \RXU SHQFLO

8KRKWKLVGRHVQ·W ORRNJRRG

7ZRGLIIHUHQW REMHFWVDUH UHWXUQHG:HKDYH WZR&KRFRODWH%RLOHU LQVWDQFHV

&DQ\RXKHOS&KRF2+ROLFLPSURYHWKHLU&KRFRODWH%RLOHUFODVV E\WXUQLQJLWLQWRDVLQJOHWRQ"

SXEOLFFODVV&KRFRODWH%RLOHU^ SULYDWHERROHDQHPSW\ SULYDWHERROHDQERLOHG SULYDWHVWDWLF&KRFRODWH%RLOHUXQLTXH,QVWDQFH SULYDWH SULYDWH&KRFRODWH%RLOHU ^ HPSW\ WUXH ERLOHG IDOVH ` SXEOLFVWDWLF&KRFRODWH%RLOHUJHW,QVWDQFH ^ LI XQLTXH,QVWDQFH QXOO ^ XQLTXH,QVWDQFH QHZ&KRFRODWH%RLOHU  ` UHWXUQXQLTXH,QVWDQFH `

SXEOLFYRLG¿OO ^ LI LV(PSW\ ^ HPSW\ IDOVH ERLOHG IDOVH ¿OOWKHERLOHUZLWKDPLONFKRFRODWHPL[WXUH ` ` UHVWRI&KRFRODWH%RLOHUFRGH `



&KDSWHU

Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 21

WKHVLQJOHWRQSDWWHUQ

([HUFLVHVROXWLRQV

6KDUSHQ \RXU SHQFLO )RUHDFKVROXWLRQGHVFULEHLWVDSSOLFDELOLW\WRWKHSUREOHPRI¿[LQJWKH&KRFRODWH %RLOHUFRGH

6\QFKURQL]HWKHJHW,QVWDQFH PHWKRG

$VWUDLJKWIRUZDUGWHFKQLTXHWKDWLVJXDUDQWHHGWRZRUN:HGRQ·WVHHPWRKDYHDQ\

SHUIRUPDQFHFRQFHUQVZLWKWKHFKRFRODWHERLOHUVRWKLVZRXOGEHDJRRGFKRLFH

8VHHDJHULQVWDQWLDWLRQ

:HDUHDOZD\VJRLQJWRLQVWDQWLDWHWKHFKRFRODWHERLOHULQRXUFRGHVRVWDWLFDOO\LQLDOL]LQJWKH LQVWDQFHZRXOGFDXVHQRFRQFHUQV7KLVVROXWLRQZRXOGZRUNDVZHOODVWKHV\QFKURQL]HGPHWKRG DOWKRXJKSHUKDSVEHOHVVREYLRXVWRDGHYHORSHUIDPLODUZLWKWKHVWDQGDUGSDWWHUQ 'RXEOHFKHFNHGORFNLQJ

*LYHQZHKDYHQRSHUIRUPDQFHFRQFHUQVGRXEOHFKHFNHGORFNLQJVHHPVOLNHRYHUNLOO,QDGGLWLRQZH·G KDYHWRHQVXUHWKDWZHDUHUXQQLQJDWOHDVW-DYD

\RXDUHKHUH



Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 5. One of a Kind Objects

Return to Table of Contents

Page 22

FURVVZRUGSX]]OHVROXWLRQ

([HUFLVHVROXWLRQV









$ 



%





%

$



%

"



%

!

&



%



&



"

&







 



$ "

'













"

&













$

$

&

)



%

"



$









































%



&





%

!

%





$

%

#

"



!



*

)

)



"





"



(



&



&

 

" 

"

!









$

%









"









 





&

!

"

 

#



&



&

'













$

&KDSWHU

Chapter 5. One of a Kind Objects Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Table of Contents Chapter 6. Encapsulating Invocation........................................................... 1 Section 6.1. Free hardware! Let's check out the Remote Control.................................................................................................. 3 Section 6.2. Taking a look at the vendor classes............................................................................................................................ 4 Section 6.3. Cubicle Conversation.................................................................................................................................................. 5 Section 6.4. Meanwhile, back at the Diner..., or, A brief introduction to the Command Pattern................................................. 7 Section 6.5. Let's study the interaction in a little more detail....................................................................................................... 8 Section 6.6. The Objectville Diner roles and responsibilities........................................................................................................ 9 Section 6.7. From the Diner to the Command Pattern................................................................................................................. 11 Section 6.8. Our first command object......................................................................................................................................... 13 Section 6.9. Using the command object........................................................................................................................................ 14 Section 6.10. Creating a simple test to use the Remote Control................................................................................................... 14 Section 6.11. Sharpen your pencil.................................................................................................................................................. 15 Section 6.12. The Command Pattern defined............................................................................................................................... 16 Section 6.13. The Command Pattern defined: the class diagram................................................................................................. 17 Section 6.14. BRAIN POWER........................................................................................................................................................ 17 Section 6.15. Assigning Commands to slots.................................................................................................................................. 19 Section 6.16. Implementing the Remote Control......................................................................................................................... 20 Section 6.17. Implementing the Commands................................................................................................................................. 21 Section 6.18. Putting the Remote Control Through Its Paces...................................................................................................... 22 Section 6.19. Now, Let's Check Out the Execution of Our Remote Control Test......................................................................... 23 Section 6.20. Time to Write That Documentation....................................................................................................................... 25 Section 6.21. What are we Doing?................................................................................................................................................ 26 Section 6.22. Time to QA That Undo Button!.............................................................................................................................. 29 Section 6.23. Using State to Implement Undo............................................................................................................................. 30 Section 6.24. Adding Undo to the Ceiling Fan Commands.......................................................................................................... 31 Section 6.25. Get Ready to Test the Ceiling Fan.......................................................................................................................... 32 Section 6.26. Testing the Ceiling Fan........................................................................................................................................... 33 Section 6.27. Every Remote Needs a Party Mode!....................................................................................................................... 34 Section 6.28. Using a Macro Command....................................................................................................................................... 35 Section 6.29. There are no Dumb Questions................................................................................................................................ 37 Section 6.30. More Uses of the Command Pattern: Queuing Requests...................................................................................... 38 Section 6.31. More Uses of the Command Pattern: Logging Requests........................................................................................ 39 Section 6.32. Tools for your Design Toolbox............................................................................................................................... 40 Section 6.33. Exercise Solutions................................................................................................................................................... 42 Section 6.34. Exercise Solutions................................................................................................................................................... 43

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 1

the Command Pattern

5

4 (QFDSVXODWLQJ,QYRFDWLRQ4 7KHVHWRSVHFUHWGURS ER[HVKDYHUHYROXWLRQL]HGWKHVS\ LQGXVWU\,MXVWGURSLQP\UHTXHVW DQGSHRSOHGLVDSSHDUJRYHUQPHQWV FKDQJHRYHUQLJKWDQGP\GU\FOHDQLQJ JHWVGRQH,GRQ·WKDYHWRZRUU\ DERXWZKHQZKHUHRUKRZLW MXVWKDSSHQV

,QWKLVFKDSWHUZHWDNHHQFDSVXODWLRQWRDZKROHQHZOHYHO ZH·UHJRLQJWRHQFDSVXODWHPHWKRGLQYRFDWLRQ7KDW¶VULJKWE\ HQFDSVXODWLQJPHWKRGLQYRFDWLRQZHFDQFU\VWDOOL]HSLHFHVRIFRPSXWDWLRQVRWKDWWKH REMHFWLQYRNLQJWKHFRPSXWDWLRQGRHVQ¶WQHHGWRZRUU\DERXWKRZWRGRWKLQJVLWMXVWXVHV RXUFU\VWDOOL]HGPHWKRGWRJHWLWGRQH:HFDQDOVRGRVRPHZLFNHGO\VPDUWWKLQJVZLWK WKHVHHQFDSVXODWHGPHWKRGLQYRFDWLRQVOLNHVDYHWKHPDZD\IRUORJJLQJRUUHXVHWKHPWR LPSOHPHQWXQGRLQRXUFRGH

WKLVLVDQHZFKDSWHU



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Page 2

Return to Table of Contents

KRPHDXWRPDWLRQRUEXVW

 RPH$XWRPDWLRQRU%XVW,QF + ,QGXVWULDO$YHQXH6XLWH )XWXUH&LW\,/

/ZMM\QVO[ U2WPVVa 1ZMKMV\TaZMKMQ^MLILMUWIVLJZQMÅVONZW QZVM_ 0]ZZQKIVM+-7WN?MI\PMZ7:IUIWV\PM 1_I[[W M`XIVLIJTM_MI\PMZ[\I\QWV1PI^M\W[Ia \PI\1¼LTQSM\W QUXZM[[ML_Q\P\PM[WN\_IZMIZKPQ\MK\]ZM UM)]\WUI\QWV I[SaW]\WLM[QOV\PM)81NWZW]ZVM_0W M[_M¼LJMPIXXa :MUW\M+WV\ZWT1VZM\]ZVNWZaW]Z[MZ^QK V[QV0WUM \WPIVL[WUMTaZM_IZLaW]_Q\P[\WKSWX\QW )]\WUI\QWVWZ*][\1VK ZMISQVOZMUW\M 1¼UMVKTW[QVOIXZW\W\aXMWNW]ZOZW]VLJ ZWTNMI\]ZM[[M^MV KWV\ZWTNWZaW]ZXMZ][IT
*QTT¹@º


&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 3

WKHFRPPDQGSDWWHUQ

.ZMMPIZL_IZM 4M\¼[KPMKSW]\\PM:MUW\M+WV\ZWT

7KHUHDUH´RQµDQG´RIIµ EXWWRQVIRUHDFKRIWKH VHYHQVORWV :H VORWVWRSURJUDP :H·YHJRWVHYHQ HQWGHYLFHLQHDFKVORW FDQSXWDGLIIHU WKHEXWWRQV DQGFRQWUROLWYLD

7KHVHWZREXWWRQV XVHGWRFRQWUROWK DUH KRXVHKROGGHYLFHVWH RUHG LQVORWRQH DQGWKHVHWZRF WKHKRXVHKROGGHYLFRQWURO VWRUHGLQVORWWZR H  DQGVRRQ

 *HW\RXU6KDUSLHRXWDQGKHUH ZULWH\RXUGHYLFHQDPHV

+HUH·VWKHJOREDO´XQGRµEXWWRQWKDW XQGRHVWKHODVWEXWWRQSUHVVHG

\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 4

YHQGRUFODVVHVIURPKRPHDXWRPDWLRQ


$SSOLDQFH&RQWURO RQ RII

6WHUHR RQ RII

&HLOLQJ/LJKW

VHW&G

RQ

VHW'YG

RII

79

GLP

2XWGRRU/LJKW

VHW5DGLR VHW9ROXPH

)DXFHW&RQWURO

RQ

RQ

RII

RSHQ9DOXH

RII

VHW,QSXW&KDQQHO

FORVH9DOXH

VHW9ROXPH

+RWWXE

&HLOLQJ)DQ FLUFXODWH

KLJK

*DUGHQ/LJKW

*DUDJH'RRU

PHGLXP ORZ

VHW'XVN7LPH

XS

RII

VHW'DZQ7LPH

GRZQ

JHW6SHHG

VWRS

PDQXDO2Q

MHWV2Q MHWV2II VHW7HPSHUDWXUHW

7KHUPRVWDW

OLJKW2Q

PDQXDO2II

VHW7HPSHUDWXUH

OLJKW2II

6SULQNOHU

6HFXULW\&RQWURO

ZDWHU2Q

/LJKW

ZDWHU2II RQ

DUP GLVDUP

RII

1\TWWS[TQSM_MPI^MY]Q\MI[M\WN KTI[[M[PMZMIVLVW\ITW\WN  QVL][\ZaMNNWZ\\WKWUM]X_Q\PI[M\WN KWUUWVQV\MZNIKM[6W\ WVTa\PI\Q\[W]VL[TQSM_MKIVM`XMK\UWZMWN \PM[MKTI[[M[QV\PM N]\]ZM,M[QOVQVOIZMUW\MKWV\ZWT)81Q[OWQVO\WJMQV\MZM[\QVO 4M\¼[OM\WV\W\PMLM[QOV



&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Page 5

Return to Table of Contents

WKHFRPPDQGSDWWHUQ

+]JQKTM+WV^MZ[I\QWV
6XH

:HOOZH·YHJRWDQRWKHUGHVLJQWRGR 0\ÀUVWREVHUYDWLRQLVWKDWZH·YHJRWD VLPSOHUHPRWHZLWKRQDQGRIIEXWWRQVEXW DVHWRIYHQGRUFODVVHVWKDWDUHTXLWH GLYHUVH

5IZa"AM[1\PW]OP\_M¼L[MMIJ]VKPWN KTI[[M[_Q\PWV IVLWNNUM\PWL[J]\PMZM_M¼^MOW\UM\PWL[TQSMLQU [M\WT]UM[M\,QZMK\QWV ;]M"6W\WVTa\PI\Q\[W]VL[TQSM_MKIVM`XMK\UWZM^MVLWZ KTI[[M[QV\PMN]\]ZM_Q\PR][\I[LQ^MZ[MUM\PWL[ 5IZa"1\PQVSQ\¼[QUXWZ\IV\_M^QM_\PQ[I[I[MXIZI\QWVWN  KWVKMZV["\PMZMUW\M[PW]TLSVW_PW_\WQV\MZXZM\J]\\WVXZM[[M[ IVLUISMZMY]M[\[J]\Q\[PW]TLV¼\SVW_ITW\IJW]\PWUM I]\WUI\QWVWZPW_\W\]ZVWVIPW\\]J ;]M";W]VL[TQSMOWWLLM[QOV*]\QN \PMZMUW\MQ[L]UJIVL R][\SVW_[PW_\WUISMOMVMZQKZMY]M[\[PW_LW_MLM[QOV\PM ZMUW\M[W\PI\Q\KIVQV^WSMIVIK\QWV\PI\[Ia\]ZV[WVITQOP\WZ WXMV[IOIZIOMLWWZ' 5IZa"1¼UVW\[]ZMJ]\_MLWV¼\_IV\\PMZMUW\M\WPI^M\W SVW_\PM[XMKQÅK[WN \PM^MVLWZKTI[[M[ ;]M"?PI\LWaW]UMIV' 5IZa"?MLWV¼\_IV\\PMZMUW\M\WKWV[Q[\WN I[M\WN QN  [\I\MUMV\[TQSM¹QN [TW\%%4QOP\\PMVTQOP\WVMT[MQN [TW\% 0W\\]J\PMVPW\\]JRM\[7Vº?MSVW_\PI\Q[IJILLM[QOV ;]M"1IOZMM?PMVM^MZIVM_^MVLWZKTI[[KWUM[W]\_M¼LPI^M \WOWQVIVLUWLQNa\PMKWLMXW\MV\QITTaKZMI\QVOJ]O[IVLUWZM _WZSNWZW]Z[MT^M[

\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Page 6

Return to Table of Contents

FRPPDQGSDWWHUQPLJKWZRUN

+H\,FRXOGQ·WKHOS RYHUKHDULQJ6LQFH&KDSWHU ,·YHEHHQERQLQJXSRQ'HVLJQ 3DWWHUQV7KHUH·VDSDWWHUQ FDOOHG´&RPPDQG3DWWHUQµ,WKLQN PLJKWKHOS

5IZa"AMIP'
&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 7

WKHFRPPDQGSDWWHUQ

5MIV_PQTMJIKSI\\PM,QVMZ WZ )JZQMNQV\ZWL]K\QWV\W\PM+WUUIVL8I\\MZV )[2WM[IQLQ\Q[ITQ\\TMPIZL\W]VLMZ[\IVL\PM+WUUIVL8I\\MZVJaR][\PMIZQVO Q\[LM[KZQX\QWV*]\LWV¼\NMIZ_MPI^M[WUMNZQMVL[ZMILa\WPMTX" ZMUMUJMZW]ZNZQMVLTaLQVMZNZWU+PIX\MZ'1\¼[JMMVI_PQTM[QVKM_M ^Q[Q\ML)TQKM.TWIVL\PM[PWZ\WZLMZKWWSJ]\_M¼^MOW\OWWLZMI[WV NWZZM\]ZVQVO_MTTJMaWVL\PMNWWLIVLOZMI\KWV^MZ[I\QWV"\PMLQVMZQ[ OWQVO\WPMTX][]VLMZ[\IVL\PM+WUUIVL8I\\MZV ;WTM\¼[\ISMI[PWZ\LM\W]ZJIKS\W\PMLQVMZIVL[\]La\PMQV\MZIK\QWV[ JM\_MMV\PMK][\WUMZ[\PM_IQ\ZM[[\PMWZLMZ[IVL\PM[PWZ\WZLMZ KWWS
1DLGEVXKNNG&KPGT

GTGJGTG 9KUJ[QWY

+PMKSQVOQVI\\PM7JRMK\^QTTM,QVMZ

2ND\ZHDOONQRZKRZWKH'LQHURSHUDWHV H &KHHV UZLWK %XUJH KDNH 0DOW6

 7KH:DLWUHVV 


WDNHVWKH2UGHU SODFHVLWRQWKH RUGHUFRXQWHU DQGVD\V´2UGHU XSµ

 7KH6KRUW2UGHU&RRNSUHSDUHV\RXUPHDO

IURPWKH2UGHU \RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 8

WKHGLQHU

4M\¼[[\]La\PMQV\MZIK\QWVQVITQ\\TMUWZMLM\IQT DQGJLYHQWKLV'LQHULVLQ2EMHFWYLOOHOHW·VWKLQN DERXWWKHREMHFWDQGPHWKRGFDOOVLQYROYHGWRR WVRIDQRUGHU 7KH2UGHUFRQVLV RPHU·VPHQX VOLSDQGWKHFXVWZULWWHQRQLW UH LWHPVWKDWD C RE A

&KHHVH UZLWK %XUJH KDNH 6 OW D 0

TE /

,·OOKDYHD%XUJHU ZLWK&KHHVHDQGD 0DOW6KDNH

RD E

R

HUH UW+ D W 6 TAK E/

RD E R 

7KHFXVWRPHUNQRZV ZKDWKHZDQWVDQG FUHDWHVDQRUGHU



7KH:DLWUHVVWDNHVWKH2UGHUDQ JHWVDURXQGWRLWVKHFDOOVLWV GZKHQVKH RUGHU8S  PHWKRGWREHJLQWKH2UGHU·VSUHSD UDWLRQ

DVDOO UGHUK LRQV 2  H K 7 WUXFW  WKHLQV WRSUHSDUH G H QHHG DO7KH WKHPHGLUHFWVWKH  2UGHU 2UGHU&RRN 6KRUW HWKRGVOLNH ZLWKP XUJHU  PDNH%

OR

R DE

5P



7KH6KRUW2UGHU &RRNIROORZVWKH LQVWUXFWLRQVRI WKH2UGHUDQG SURGXFHVWKHPHDO

KHHVH ZLWK& %XUJHU DNH 0DOW6K

OU

TP

UT

MAKE"URGER MAKE3HAKE



&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 9

WKHFRPPDQGSDWWHUQ


7KH:DLWUHVV·VMRELVWRWDNH2UGHU6OLSVDQG LQYRNHWKHRUGHU8S PHWKRGRQWKHP
HU8S ^ RLGRUG SXEOLFY DNH%XUJHU  P N FRR DNH6KDNH  P FRRN `

2ND\LQU FDUHZKDW HDOOLIHDZDLWUHVVZ LWEXWWKLVLVRQWKH2UGHU6OL RXOGSUREDEO\ S LV2EMHFW YLOOHZRUNDQGZKRFRRNV ZLWKXVK HUH

'RQ·WDVNPHWRFRRN ,MXVWWDNHRUGHUVDQG \HOO´2UGHUXSµ

6W_\PZW]OPW]\\PMLIa\PM?IQ\ZM[[¼[\ISM7ZLMZUM\PWLOM\[ XIZIUM\MZQbML_Q\PLQNNMZMV\WZLMZ[TQX[NZWULQNNMZMV\K][\WUMZ[J]\ \PI\LWM[V¼\XPI[MPMZ#[PMSVW_[ITT7ZLMZ[TQX[[]XXWZ\\PMWZLMZ=X UM\PWLIVL[PMKIVKITTWZLMZ=XIVa\QUM[PMVMML[IUMITXZMXIZML

7KH6KRUW2UGHU&RRNKDVWKHNQRZOHGJH UHTXLUHGWRSUHSDUHWKHPHDO

\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 10

WKHGLQHULVDPRGHOIRUFRPPDQGSDWWHUQ

2ND\ZHKDYHD 'LQHUZLWKD:DLWUHVVZKRLV GHFRXSOHGIURPWKH&RRNE\DQ 2UGHU6OLSVRZKDW"*HWWR WKHSRLQW

8I\QMVKM_M¼ZMOM\\QVO\PMZM
YiX`e gfn\i

%XUJHU 0DOW ,·OOKDYHD HVHDQGD ZLWK&KH

 RUGHU RIDQ RQVLVWV HU·VPHQX UGHUF 7KH2 GWKHFXVWRP WHQRQLW VOLSDQ KDWDUHZULW LWHPVW

6KDNH

CREATE/



KH ZVZKDW VDQ RPHUNQR 7KHFXVW SDUHGDQGFUHDWH ZDQWVSUH RUGHU TAKE/RDER

JHWV HRUGHUDQGZKHQVKH LQ 7KH:DLWUHVVWDNHVWKLWVRUGHU8S PHWKRGWREHJ DURXQGWRLWVKHFDOOV Q WKHRUGHU·VSUHSDUDWLR

ER5

P

7KH6KRUW2UGHU &RRNIROORZVWKH LQVWUXFWLRQVRIWKH 2UGHUDQGSURGXFHV WKHPHDO

ORD

VDOO UKD 2UGH WLRQV 7KH LQVWUXF HSDUH WKH WRSU KH  QHHG PHDO7FWVWKH  WKH UGLUH U&RRN 2UGH W2UGH GVOLNH 6KRU PHWKR ZLWK %XUJHU PDNH

&KHHVH ZLWK %XUJHU 6KDNH 0DOW

AKE3

T

RGER M

TPU

MAKE"U

HAKE

OU

%HIRUHZHPRYHRQVSHQGVRPHWLPHVWXG\LQJ WKHGLDJUDPWZRSDJHVEDFNDORQJZLWK'LQHU UROHVDQGUHVSRQVLELOLWLHVXQWLO\RXWKLQN\RX¶YH JRWDKDQGOHRQWKH2EMHFWYLOOH'LQHUREMHFWVDQG UHODWLRQVKLSV2QFH\RX¶YHGRQHWKDWJHWUHDG\ WRQDLOWKH&RPPDQG3DWWHUQ

RDER

H &KHHV UZLWK %XUJH 6KDNH 0DOW

&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 11

WKHFRPPDQGSDWWHUQ

.ZWU\PM,QVMZ\W\PM+WUUIVL8I\\MZV 7SIa_M¼^M[XMV\MVW]OP\QUMQV\PM7JRMK\^QTTM,QVMZ\PI\_MSVW_ITT\PM XMZ[WVITQ\QM[IVL\PMQZZM[XWV[QJQTQ\QM[Y]Q\M_MTT6W__M¼ZMOWQVOZM_WZS\PM ,QVMZLQIOZIU\WZMÆMK\\PM+WUUIVL8I\\MZVAW]¼TT[MM\PI\ITT\PMXTIaMZ[ IZM\PM[IUM#WVTa\PMVIUM[PI^MKPIVOML

W 7KHFRPPDQGREMHFG KR HW P QH R SURYLGHV QFDSVXODWHV H[HFXWH WKDWH DQEH WKHDFWLRQVDQGF KHDFWLRQV FDOOHGWRLQYRNHW RQWKH5HFHLYHU

7KHDFWLRQVDQGWKH 5HFHLYHUDUHERXQGWRJHWKHU LQWKHFRPPDQGREMHFW

H[HFXWH^ SXEOLFYRLG DFWLRQ  UHFHLYHU DFWLRQ  YHU UHFHL `

DFWLRQ DFWLRQ 

TE #

 

C RE A

OM

 

MAN

D/B



7KHFOLHQWLVUHVSRQVLEOHIRU KH FUHDWLQJWKHFRPPDQGREMHFW7 FRPPDQGREMHFWFRQVLVWVRIDVHW RIDFWLRQVRQDUHFHLYHU

5 HFHLYHU

JEC T



H[HFXWH

 

& RPPDQG

MMA



 

S E T #O

7KHFOLHQWFDOOVVHW&RPPDQG R DQ,QYRNHUREMHFWDQGSDVVHVLW Q WKHFRPPDQGREMHFWZKHUHLWJ HWV VWRUHGXQWLOLWLVQHHGHG

 

FUHDWH &RPPDQG 2EMHFW

&

OLHQW



ND

4 WILQVO\PM1V^WSMZ 

7KHFOLHQWFUHDWHVD FRPPDQGREMHFW



7KHFOLHQWGRHVD VHW&RPPDQG WRVWRUH WKHFRPPDQGREMHFWLQ WKHLQYRNHU



/DWHUWKHFOLHQWDVNV WKHLQYRNHUWRH[HFXWH WKHFRPPDQG1RWH DV\RX¶OOVHHODWHULQ WKHFKDSWHURQFHWKH FRPPDQGLVORDGHGLQWR WKHLQYRNHULWPD\EH XVHGDQGGLVFDUGHGRU LWPD\UHPDLQDQGEH XVHGPDQ\WLPHV

VHW&RPPDQG

XWXUH WKHI RPPDQG LQ  W LQ R F HS KH $WVRPYRNHUFDOOVW PHWKRG WKH,Q ·VH[HFXWH REMHFW

, QY R N H U EX

U EC

TE



H[HFXWH

 

 

& RPPDQG

HUH W+ U D 6W

 

DFWLRQ DFWLRQ 

ACTION ACTION 5

HFHLYHU

ZKLFKUHVXOWV LQWKHDFWLRQV EHLQJLQYRNHGRQ WKH5HFHLYHU

\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 12

ZKRGRHVZKDW"

5I\KP\PMLQVMZWJRMK\[IVLUM\PWL[_Q\P\PMKWZZM[XWVLQVOVIUM[NZWU\PM +WUUIVL8I\\MZV

'LQHU



&RPPDQG3DWWHUQ

Waitress

Command

Short Order Cook

execute()

orderUp()

Client

Order

Invoker

Customer

Receiver

takeOrder()

setCommand()

&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 13

WKHFRPPDQGSDWWHUQ

7]ZNQZ[\K WUUIVLWJRMK\ 1[V¼\Q\IJW]\\QUM_MJ]QTLW]ZÅZ[\KWUUIVLWJRMK\'4M\¼[OWIPMILIVL_ZQ\M [WUMKWLMNWZ\PMZMUW\MKWV\ZWT?PQTM_MPI^MV¼\ÅO]ZMLW]\PW_\WLM[QOV\PM ZMUW\MKWV\ZWT)81aM\J]QTLQVOINM_\PQVO[NZWU\PMJW\\WU]XUIaPMTX][

,PSOHPHQWLQJWKH&RPPDQGLQWHUIDFH .QZ[\\PQVO[ÅZ[\"ITTKWUUIVLWJRMK\[QUXTMUMV\\PM[IUMQV\MZNIKM_PQKP KWV[Q[\[WN WVMUM\PWL1V\PM,QVMZ_MKITTML\PQ[UM\PWLWZLMZ=X#PW_M^MZ _M\aXQKITTaR][\][M\PMVIUMM`MK]\M 0MZM¼[\PM+WUUIVLQV\MZNIKM" SXEOLFLQWHUIDFH&RPPDQG^ SXEOLFYRLGH[HFXWH  `

6LPSOH$OOZHQHHGLVRQHPHWKRGF

DOOHGH[HFXWH 

,PSOHPHQWLQJD&RPPDQGWRWXUQDOLJKWRQ 6W_TM\¼[[IaaW]_IV\\WQUXTMUMV\IKWUUIVLNWZ\]ZVQVOITQOP\WV :MNMZZQVO\WW]Z[M\WN ^MVLWZKTI[[M[\PM4QOP\KTI[[PI[\_WUM\PWL["WV IVLWNN0MZM¼[PW_aW]KIVQUXTMUMV\\PQ[I[IKWUUIVL"

/LJKW RQ RII

7KLVLVDFRPPDQGVRZHQHHGWR LPSOHPHQWWKH&RPPDQGLQWHUIDFH SXEOLFFODVV/LJKW2Q&RPPDQGLPSOHPHQWV&RPPDQG^ /LJKWOLJKW  SXEOLF/LJKW2Q&RPPDQG /LJKWOLJKW ^ WKLVOLJKW OLJKW `  SXEOLFYRLGH[HFXWH ^ OLJKWRQ  ` GFDOOVWKH ` 7KHH[HFXWHPHWKR 

7KHFRQVWUXFWRULVSDVVHGWKHVSHFLILF OLJKWWKDWWKLVFRPPDQGLVJRLQJWR FRQWUROVD\WKHOLYLQJURRPOLJKW DQGVWDVKHVLWLQWKHOLJKWLQVWDQFH YDULDEOH:KHQH[HFXWHJHWVFDOOHGWKLV LVWKHOLJKWREMHFWWKDWLVJRLQJWREH WKH5HFHLYHURIWKHUHTXHVW

HUHFHLYLQJ RQ PHWKRGRQWK OLJKWZH KH W LV LFK REMHFWZK DUHFRQWUROOLQJ

6W_\PI\aW]¼^MOW\I4QOP\7V+WUUIVLKTI[[TM\¼[[MMQN _MKIVX]\Q\\W][M \RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 14

XVLQJWKHFRPPDQGREMHFW

=[QVO\PMKWUUIVLWJRMK\ 7SIaTM\¼[UISM\PQVO[[QUXTM"[Ia_M¼^MOW\IZMUW\MKWV\ZWT_Q\PWVTaWVM J]\\WVIVLKWZZM[XWVLQVO[TW\\WPWTLILM^QKM\WKWV\ZWT"

G

PPDQ VORWWRKROGRXUFR

:HKDYHRQH SXEOLFFODVV6LPSOH5HPRWH&RQWURO^ QHGHYLFH &RPPDQGVORW ZKLFKZLOOFRQWUROR  :HKDYHDPHWKRGIRUVHWWLQJ SXEOLF6LPSOH5HPRWH&RQWURO ^` WKHFRPPDQGWKHVORWLVJRLQJ  WRFRQWURO7KLVFRXOGEHFDOOHG SXEOLFYRLGVHW&RPPDQG &RPPDQGFRPPDQG ^ VORW FRPPDQG PXOWLSOHWLPHVLIWKHFOLHQWRI ` WKLVFRGHZDQWHGWRFKDQJHWKH  EHKDYLRURIWKHUHPRWHEXWWRQ SXEOLFYRLGEXWWRQ:DV3UHVVHG ^ VORWH[HFXWH  7KLVPHWKRGLVFDOOHGZKHQWK H ` EXWWRQLVSUHVVHG$OOZHGRLVW DNH ` WKHFXU

UHQWFRPPDQGERXQGWR VORWDQGFDOOLWVH[HFXWH PHW WKH KRG

+ZMI\QVOI[QUXTM\M[\\W][M\PM:MUW\M+WV\ZWT 0MZM¼[R][\IJQ\WN KWLM\W\M[\W]\\PM[QUXTMZMUW\MKWV\ZWT4M\¼[\ISMITWWSIVL _M¼TTXWQV\W]\PW_\PMXQMKM[UI\KP\PM+WUUIVL8I\\MZVLQIOZIU"

QVSHDN LQ&RPPDQG3DWWHU ,QYRNHU 7KLVLVRXU&OLHQW 7KHUHPRWHLVRXU  D VHG LWZLOOEHSDV DWFDQ SXEOLFFODVV5HPRWH&RQWURO7HVW^ FRPPDQGREMHFWWKHTXHVWV U SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ NH PD R W EHXVHG 6LPSOH5HPRWH&RQWUROUHPRWH QHZ6LPSOH5HPRWH&RQWURO  /LJKWOLJKW QHZ/LJKW  1RZZHFUHDWHD/LJKW /LJKW2Q&RPPDQGOLJKW2Q QHZ/LJKW2Q&RPPDQG OLJKW  REMHFWWKLVZLOOEHWKH  5HFHLYHURIWKHUHTXHVW UHPRWHVHW&RPPDQG OLJKW2Q  +HUHFUHDWHDFRPPDQGDQG UHPRWHEXWWRQ:DV3UHVVHG  SDVVLWWRWKH5HFHLYHU ` `

+HUHSDVVWKHFRPPDQG WRWKH,QYRNHU

$QGWKHQZHVLPXODWHWKH EXWWRQEHLQJSUHVVHG



)LOH(GLW:LQGRZ+HOS'LQHU)RRG
MDYD5HPRWH&RQWURO7HVW /LJKWLV2Q

+HUH·VWKHRXWSXWRI UXQQLQJWKLVWHVWFRGH



&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 15

WKHFRPPDQGSDWWHUQ

6KDUSHQ \RXU SHQFLO 7SIaQ\¼[\QUMNWZaW]\WQUXTMUMV\\PM /IZIOM,WWZ7XMV+WUUIVLKTI[[.QZ[\[]XXTa\PMKWLMNWZ \PMKTI[[JMTW_AW]¼TTVMML\PM/IZIOM,WWZKTI[[LQIOZIU SXEOLFFODVV*DUDJH'RRU2SHQ&RPPDQG LPSOHPHQWV&RPPDQG^

*DUDJH'RRU XS GRZQ VWRS OLJKW2Q OLJKW2II


`

6W_\PI\aW]¼^MOW\aW]ZKTI[[_PI\Q[\PMW]\X]\WN \PMNWTTW_QVO KWLM'0QV\"\PM/IZIOM,WWZ]XUM\PWLXZQV\[W]\¹/IZIOM,WWZQ[ 7XMVº_PMVQ\Q[KWUXTM\M SXEOLFFODVV5HPRWH&RQWURO7HVW^ SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ 6LPSOH5HPRWH&RQWUROUHPRWH QHZ6LPSOH5HPRWH&RQWURO  /LJKWOLJKW QHZ/LJKW  *DUDJH'RRUJDUDJH'RRU QHZ*DUDJH'RRU  /LJKW2Q&RPPDQGOLJKW2Q QHZ/LJKW2Q&RPPDQG OLJKW  *DUDJH'RRU2SHQ&RPPDQGJDUDJH2SHQ  QHZ*DUDJH'RRU2SHQ&RPPDQG JDUDJH'RRU    UHPRWHVHW&RPPDQG OLJKW2Q  UHPRWHEXWWRQ:DV3UHVVHG  UHPRWHVHW&RPPDQG JDUDJH2SHQ  UHPRWHEXWWRQ:DV3UHVVHG  ` ` )LOH(GLW:LQGRZ+HOS*UHHQ(JJV +DP

MDYD5HPRWH&RQWURO7HVW


\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 16

FRPPDQGSDWWHUQGH¿QHG


$QHQFDSVXODWHGUHTXHVW

  




H[HFXWH ^ UHFHLYHUDFWLRQ  `

H[HFXWH

DQ G

LJ KW2

 

H[HFXWH

* D

UDJH'RR



Q&RPP

  



+L JK

H[HFXWH

H LO

H[HFXWH

6W



&  

HUHR 2

LQJ)DQ

 



/ 

?PI\_MPI^MV¼\MVKW]V\MZMLaM\Q[][QVOKWUUIVL[\W QUXTMUMV\Y]M]M[IVLTWO[IVL[]XXWZ\]VLWWXMZI\QWV[,WV¼\_WZZa \PW[MIZMXZM\\a[\ZIQOP\NWZ_IZLM`\MV[QWV[WN \PMJI[QK+WUUIVL 8I\\MZVIVL_M_QTTOM\\W\PMU[WWV?MKIVIT[WMI[QTa[]XXWZ\ _PI\¼[SVW_VI[\PM5M\I+WUUIVL8I\\MZVWVKM_MPI^M\PM JI[QK[QVXTIKM
&R P PD Q G

II

 

 

4M\¼[[\MX\PZW]OP\PQ[?MSVW_\PI\IKWUUIVLWJRMK\ MVKIX[]TI\M[IZMY]M[\JaJQVLQVO\WOM\PMZI[M\WN IK\QWV[WVI [XMKQÅKZMKMQ^MZ
DFWLRQ

 5 HFHLYHU

U2 SH Q

4M\¼[[\IZ\_Q\PQ\[WNÅKQITLMÅVQ\QWV"

  5H PRWH6O

RW

AW]¼^MLWVMaW]Z\QUMQV\PM7JRMK\^QTTM,QVMZaW]¼^MXIZ\Ta QUXTMUMV\ML\PMZMUW\MKWV\ZWT)81IVLQV\PMXZWKM[[aW]¼^M OW\INIQZTaOWWLXQK\]ZMWN PW_\PMKTI[[M[IVLWJRMK\[QV\MZIK\QV \PM+WUUIVL8I\\MZV6W__M¼ZMOWQVO\WLMÅVM\PM+WUUIVL 8I\\MZVIVLVIQTLW_VITT\PMLM\IQT[

$QLQYRNHUIRULQVWDQFH RQHVORWRIWKHUHPRWH FDQEHSDUDPHWHUL]HGZLWK GLIIHUHQWUHTXHVWV

&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 17

WKHFRPPDQGSDWWHUQ


HIRU 7KH&OLHQWLVUHVSRQVLEORPPDQGDQG H& HW QFU &R D FUHDWLQJ VHWWLQJLWV5HFHLYHU

7KH,QYRNHUKROGV DFRPPDQGDQGDW VRPHSRLQWDVNVWKH FRPPDQGWRFDUU\ RXWDUHTXHVWE\ FDOOLQJLWVH[HFXWH  PHWKRG

,QYRNHU

&OLHQW

VHW&RPPDQG

5HFHLYHU DFWLRQ

7KH5HFHLYHUNQRZVKRZWR SHUIRUPWKHZRUNQHHGHGWR FDUU\RXWWKHUHTXHVW$Q\FODVV FDQDFWDVD5HFHLYHU

RPPDQGV$V &RPPDQGGHFODUHVDQLQWHUIDFHIRUDOOFWKURXJKLWV NHG LQYR QGLV \RXDOUHDG\NQRZDFRPPD WRSUHIRUPDQ H[HFXWH PHWKRGZKLFKDVNVDUHFHLYHU DVDQXQGR  FHK HUID LVLQW HWK QRWLF DFWLRQ
LQWHUIDFH!! &RPPDQG H[HFXWH XQGR

&RQFUHWH&RPPDQG H[HFXWH XQGR

7KHH[HFXWH PHWKRGLQYRNHV WKHDFWLRQ V  RQWKHUHFHLYHU QHHGHGWRIXOILOO WKHUHTXHVW

SXEOLFYRLGH[HFXWH ^ UHFHLYHUDFWLRQ `

DFWLRQ ILQHVDELQGLQJEHWZHHQDQ 7KH&RQFUHWH&RPPDQGGH HUPDNHVDUHTXHVWE\FDOOLQJ DQGD5HFHLYHU7KH,QYRNHWH&RPPDQGFDUULHVLWRXWE\ H[HFXWH DQGWKH&RQFU QVRQWKH5HFHLYHU FDOOLQJRQHRUPRUHDFWLR

YiX`e gfn\i +RZGRHVWKHGHVLJQRIWKH&RPPDQG3DWWHUQVXSSRUWWKHGHFRXSOLQJRIWKHLQYRNHURID UHTXHVWDQGWKHUHFHLYHURIWKHUHTXHVW"

\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 18

ZKHUHGRZHEHJLQ"

2ND\,WKLQN,·YHJRWDJRRGIHHO IRUWKH&RPPDQG3DWWHUQQRZ*UHDW WLS-RH,WKLQNZHDUHJRLQJWRORRN OLNHVXSHUVWDUVDIWHUÀQLVKLQJRII WKH5HPRWH&RQWURO$3,

5IZa"5M\WW;W_PMZMLW_MJMOQV' ;]M"4QSM_MLQLQV\PM;QUXTM:MUW\M_MVMML\WXZW^QLMI_Ia \WI[[QOVKWUUIVL[\W[TW\[1VW]ZKI[M_MPI^M[M^MV[TW\[MIKP _Q\PIV¹WVºIVL¹WNN ºJ]\\WV;W_MUQOP\I[[QOVKWUUIVL[\W \PMZMUW\M[WUM\PQVOTQSM\PQ[" 5IZa"


&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 19

WKHFRPPDQGSDWWHUQ

)[[QOVQVO+WUUIVL[\W[TW\[ ;W_MPI^MIXTIV"?M¼ZMOWQVO\WI[[QOVMIKP[TW\\WIKWUUIVLQV \PMZMUW\MKWV\ZWT
 (DFKVORWJHWVDFRPPDQG

 :KHQWKHEXWWRQLVSUHVVHGWKHH[HFXWH  PHWKRGLVFDOOHGRQWKHFRUUHVSRQGLQJFRPPDQG

DQ G

L / 

H[HFXWH

JK W2Q&RPP

DQ G

L / 

H[HFXWH

Q) R

P DQG

H

WHUHR2

P DQG

J) D Q

R

&O

 

H[HFXWH

*D



6  

:H·OOZRUU\DERXWWKH UHPDLQLQJVORWVLQDELW

2 II

H[HFXWH

LOLQ

VH

+ LJ K

 & 

6  

H[HFXWH

H[HFXWH

JK W2II&RP



2

S



UDJH'RRU

L / 

 

HQ

H

J) D Q

H[HFXWH

H[HFXWH

JK W2II&RP

U& '

 & 

H[HFXWH

LOLQ

*D

L / 



JK W2Q&RPP

UDJH'RRU

H[HFXWH

II

WHUHR2

 ,QWKHH[HFXWH PHWKRGDFWLRQV DUHLQYRNHGRQWKHUHFLHYHU

7KH,QYRNHU RII RQ

 

 6W HR HU

\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 20

LPSOHPHQWLQJWKHUHPRWHFRQWURO

1UXTMUMV\QVO\PM:MUW\M+WV\ZWT WHLVJRLQJWR

SXEOLFFODVV5HPRWH&RQWURO^ 7KLVWLPHDURXQGWKHUHPR QGVZKLFK &RPPDQG>@RQ&RPPDQGV KDQGOHVHYHQ2QDQG2IIFRPPD \V DUUD LQJ RQG &RPPDQG>@RII&RPPDQGV ZH·OOKROGLQFRUUHVS  SXEOLF5HPRWH&RQWURO ^ ,QWKHFRQVWUXFWRUDOOZHQHHGWRGRLV RQ&RPPDQGV QHZ&RPPDQG>@ LQVWDQWLDWHDQGLQLWLDOL]HWKHRQDQGRII RII&RPPDQGV QHZ&RPPDQG>@ DUUD\V  &RPPDQGQR&RPPDQG QHZ1R&RPPDQG  IRU LQWL LL ^ RQ&RPPDQGV>[email protected] QR&RPPDQG RII&RPPDQGV>[email protected] QR&RPPDQG ` `  SXEOLFYRLGVHW&RPPDQG LQWVORW&RPPDQGRQ&RPPDQG&RPPDQGRII&RPPDQG ^ RQ&RPPDQGV>[email protected] RQ&RPPDQG 7KHVHW&RPPDQG PHWKRGWDNHVDVORWSRVLWLRQ RII&RPPDQGV>[email protected] RII&RPPDQG DQGDQ2QDQG2IIFRPPDQGWREHVWRUHGLQ ` WKDWVORW,WSXWVWKHVHFRPPDQGVLQWKHRQDQG  SXEOLFYRLGRQ%XWWRQ:DV3XVKHG LQWVORW ^ RIIDUUD\VIRUODWHUXVH RQ&RPPDQGV>[email protected]H[HFXWH  ` :KHQDQ2QRU2IIEXWWRQLV  SUHVVHGWKHKDUGZDUHWDNHV SXEOLFYRLGRII%XWWRQ:DV3XVKHG LQWVORW ^ FDUHRIFDOOLQJWKHFRUUHVSRQGLQJ RII&RPPDQGV>[email protected]H[HFXWH  PHWKRGVRQ%XWWRQ:DV3XVKHG RU ` RII%XWWRQ:DV3XVKHG      SXEOLF6WULQJWR6WULQJ ^ 6WULQJ%XIIHUVWULQJ%XII QHZ6WULQJ%XIIHU  VWULQJ%XIIDSSHQG ³?Q5HPRWH&RQWURO?Q´  IRU LQWL LRQ&RPPDQGVOHQJWKL ^ VWULQJ%XIIDSSHQG ³>VORW³L³@³RQ&RPPDQGV>[email protected]JHW&ODVV JHW1DPH ³³RII&RPPDQGV>[email protected]JHW&ODVV JHW1DPH ³?Q´  ` UHWXUQVWULQJ%XIIWR6WULQJ  ` :H·YHRYHUZULWWHQWR6WULQJ WRSULQWRXWHDFKVORWDQGLWV  ` FRUUHVSRQGLQJ

FRPPDQG


&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 21

WKHFRPPDQGSDWWHUQ

1UXTMUMV\QVO\PM+WUUIVL[ ?MTT_M¼^MITZMILaOW\\MVW]ZNMM\_M\QUXTMUMV\QVO\PM4QOP\7V+WUUIVLNWZ \PM;QUXTM:MUW\M+WV\ZWT?MKIVXT]O\PI\[IUMKWLMQVPMZMIVLM^MZa\PQVO _WZS[JMI]\QN]TTa7NN KWUUIVL[IZMVWLQNNMZMV\#QVNIK\\PM4QOP\7NN+WUUIVL TWWS[TQSM\PQ[" SXEOLFFODVV/LJKW2II&RPPDQGLPSOHPHQWV&RPPDQG^ /LJKWOLJKW  SXEOLF/LJKW2II&RPPDQG /LJKWOLJKW ^ WKLVOLJKW OLJKW ` 7KH/LJKW2II&RPPDQGZRUNVH[DFWO\  WKHVDPHZD\DVWKH/LJKW2Q&RPPDQG SXEOLFYRLGH[HFXWH ^ H[FHSWWKDWZHDUHELQGLQJWKHUHFHLYHU OLJKWRII  ` WRDGLIIHUHQWDFWLRQWKHRII PHWKRG `

4M\¼[\Za[WUM\PQVOITQ\\TMUWZMKPITTMVOQVO#PW_IJW]\_ZQ\QVOWVIVLWNN  KWUUIVL[NWZ\PM;\MZMW'7SIaWNN Q[MI[a_MR][\JQVL\PM;\MZMW\W\PMWNN UM\PWLQV\PM;\MZMW7NN+WUUIVL7VQ[ITQ\\TMUWZMKWUXTQKI\ML#TM\¼[[Ia_M _IV\\W_ZQ\MI;\MZMW7V?Q\P+,+WUUIVL

6WHUHR RQ RII VHW&G VHW'YG VHW5DGLR VHW9ROXPH

SXEOLFFODVV6WHUHR2Q:LWK&'&RPPDQGLPSOHPHQWV&RPPDQG^ 6WHUHRVWHUHR  SXEOLF6WHUHR2Q:LWK&'&RPPDQG 6WHUHRVWHUHR ^ ZHJHW WKLVVWHUHR VWHUHR -XVWOLNHWKH/LJKW2Q&RPPDQGHRZH WHU KHV RIW ` QFH QVWD KHL SDVVHGW VWRUH  DUHJRLQJWREHFRQWUROOLQJDQGZH SXEOLFYRLGH[HFXWH ^ DORFDOLQVWDQFHYDULDEOH LWLQ VWHUHRRQ  VWHUHRVHW&'  VWHUHRVHW9ROXPH   7RFDUU\RXWWKLVUHTXHVWZHQHHGWRFDOOWKUHH ` PHWKRGVRQWKHVWHUHRILUVWWXUQLWRQWKHQVHW `

LWWRSOD\WKH&'DQGILQDOO\VHWWKHYROXPHWR :K\":HOOLW·VEHWWHUWKDQULJKW"

6W\\WWJIL
\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Page 22

Return to Table of Contents

WHVWLQJWKHUHPRWHFRQWURO

8]\\QVO\PM:MUW\M+WV\ZWT\PZW]OPQ\[XIKM[ 7]ZRWJ_Q\P\PMZMUW\MQ[XZM\\aU]KPLWVM#ITT_MVMML\WLWQ[Z]V[WUM\M[\[IVL OM\[WUMLWK]UMV\I\QWV\WOM\PMZ\WLM[KZQJM\PM)810WUM)]\WUI\QWVWN *][\ 1VK[]ZMQ[OWQVO\WJMQUXZM[[MLLWV¼\aI\PQVS'?M¼^MUIVIOML\WKWUM]X_Q\P ILM[QOV\PI\Q[OWQVO\WITTW_\PMU\WXZWL]KMIZMUW\M\PI\Q[MI[a\WUIQV\IQV IVL\PMa¼ZMOWQVO\WPI^MVW\ZW]JTMKWV^QVKQVO\PM^MVLWZ[\W_ZQ\M[WUM[QUXTM KWUUIVLKTI[[M[QV\PMN]\]ZM[QVKM\PMaIZM[WMI[a\W_ZQ\M 4M\¼[OM\\W\M[\QVO\PQ[KWLM  SXEOLFFODVV5HPRWH/RDGHU^  SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ 5HPRWH&RQWUROUHPRWH&RQWURO QHZ5HPRWH&RQWURO   /LJKWOLYLQJ5RRP/LJKW QHZ/LJKW ³/LYLQJ5RRP´  LFHVLQ &UHDWHDOOWKHGHYLRQV /LJKWNLWFKHQ/LJKW QHZ/LJKW ³.LWFKHQ´  ORFDW U SH UR S HLU WK &HLOLQJ)DQFHLOLQJ)DQ QHZ&HLOLQJ)DQ ³/LYLQJ5RRP´  *DUDJH'RRUJDUDJH'RRU QHZ*DUDJH'RRU ³´  6WHUHRVWHUHR QHZ6WHUHR ³/LYLQJ5RRP´   /LJKW2Q&RPPDQGOLYLQJ5RRP/LJKW2Q  QHZ/LJKW2Q&RPPDQG OLYLQJ5RRP/LJKW  &UHDWHDOOWKH/LJKW /LJKW2II&RPPDQGOLYLQJ5RRP/LJKW2II  QHZ/LJKW2II&RPPDQG OLYLQJ5RRP/LJKW  &RPPDQGREMHFWV /LJKW2Q&RPPDQGNLWFKHQ/LJKW2Q  QHZ/LJKW2Q&RPPDQG NLWFKHQ/LJKW  /LJKW2II&RPPDQGNLWFKHQ/LJKW2II  QHZ/LJKW2II&RPPDQG NLWFKHQ/LJKW   &HLOLQJ)DQ2Q&RPPDQGFHLOLQJ)DQ2Q  2II &UHDWHWKH2QDQG QHZ&HLOLQJ)DQ2Q&RPPDQG FHLOLQJ)DQ  DQ I LQJ HLO HF IRUWK &HLOLQJ)DQ2II&RPPDQGFHLOLQJ)DQ2II  QHZ&HLOLQJ)DQ2II&RPPDQG FHLOLQJ)DQ   *DUDJH'RRU8S&RPPDQGJDUDJH'RRU8S QHZ*DUDJH'RRU8S&RPPDQG JDUDJH'RRU  &UHDWHWKH8SDQG'RZQ *DUDJH'RRU'RZQ&RPPDQGJDUDJH'RRU'RZQ FRPPDQGVIRUWKH*DUDJH QHZ*DUDJH'RRU'RZQ&RPPDQG JDUDJH'RRU   6WHUHR2Q:LWK&'&RPPDQGVWHUHR2Q:LWK&' QHZ6WHUHR2Q:LWK&'&RPPDQG VWHUHR  &UHDWHWKHVWHUHR2Q 6WHUHR2II&RPPDQGVWHUHR2II DQG2IIFRPPDQGV QHZ6WHUHR2II&RPPDQG VWHUHR  



&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 23

WKHFRPPDQGSDWWHUQ

UHPRWH&RQWUROVHW&RPPDQG OLYLQJ5RRP/LJKW2QOLYLQJ5RRP/LJKW2II  UHPRWH&RQWUROVHW&RPPDQG NLWFKHQ/LJKW2QNLWFKHQ/LJKW2II  UHPRWH&RQWUROVHW&RPPDQG FHLOLQJ)DQ2QFHLOLQJ)DQ2II  1RZWKDWZH·YHJRW UHPRWH&RQWUROVHW&RPPDQG VWHUHR2Q:LWK&'VWHUHR2II  DOORXUFRPPDQGVZH  FDQORDGWKHPLQWR 6\VWHPRXWSULQWOQ UHPRWH&RQWURO  WKHUHPRWHVORWV  UHPRWH&RQWURORQ%XWWRQ:DV3XVKHG   UHPRWH&RQWURORII%XWWRQ:DV3XVKHG   +HUH·VZKHUHZHXVHRXUWR6WULQJ  UHPRWH&RQWURORQ%XWWRQ:DV3XVKHG   PHWKRGWRSULQWHDFKUHPRWHVORWDQG UHPRWH&RQWURORII%XWWRQ:DV3XVKHG   WKHFRPPDQGWKDWLWLVDVVLJQHGWR UHPRWH&RQWURORQ%XWWRQ:DV3XVKHG   UHPRWH&RQWURORII%XWWRQ:DV3XVKHG   UHPRWH&RQWURORQ%XWWRQ:DV3XVKHG   UHPRWH&RQWURORII%XWWRQ:DV3XVKHG   ` $OOULJKWZHDUHUHDG\WRUROO ` 1RZZHVWHSWKURXJKHDFKVORW

DQGSXVKLWV2QDQG2IIEXWWRQ

6W_TM\¼[KPMKSW]\\PMM`MK]\QWVWNW]ZZMUW\MKWV\ZWT\M[\ )LOH(GLW:LQGRZ+HOS&RPPDQGV*HW7KLQJV'RQH

MDYD5HPRWH/RDGHU 5HPRWH&RQWURO >VORW@KHDG¿UVWFRPPDQGUHPRWH/LJKW2Q&RPPDQGKHDG¿UVWFRPPDQGUHPRWH/LJKW2II&RPPDQG >VORW@KHDG¿UVWFRPPDQGUHPRWH/LJKW2Q&RPPDQGKHDG¿UVWFRPPDQGUHPRWH/LJKW2II&RPPDQG >VORW@KHDG¿UVWFRPPDQGUHPRWH&HLOLQJ)DQ2Q&RPPDQGKHDG¿UVWFRPPDQGUHPRWH&HLOLQJ)DQ2II&RPPDQG >VORW@KHDG¿UVWFRPPDQGUHPRWH6WHUHR2Q:LWK&'&RPPDQGKHDG¿UVWFRPPDQGUHPRWH6WHUHR2II&RPPDQG >VORW@KHDG¿UVWFRPPDQGUHPRWH1R&RPPDQGKHDG¿UVWFRPPDQGUHPRWH1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGUHPRWH1R&RPPDQGKHDG¿UVWFRPPDQGUHPRWH1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGUHPRWH1R&RPPDQGKHDG¿UVWFRPPDQGUHPRWH1R&RPPDQG /LYLQJ5RRPOLJKWLVRQ /LYLQJ5RRPOLJKWLVRII .LWFKHQOLJKWLVRQ .LWFKHQOLJKWLVRII /LYLQJ5RRPFHLOLQJIDQLVRQKLJK /LYLQJ5RRPFHLOLQJIDQLVRII /LYLQJ5RRPVWHUHRLVRQ /LYLQJ5RRPVWHUHRLVVHWIRU&'LQSXW /LYLQJ5RRP6WHUHRYROXPHVHWWR /LYLQJ5RRPVWHUHRLVRII

2QVORWV2II6ORWV 2XUFRPPDQGVLQDFWLRQ5HPHPEHUWKHRXWSXW IURPHDFKGHYLFHFRPHVIURPWKHYHQGRUFODVVHV )RULQVWDQFHZKHQDOLJKWREMHFWLVWXUQHGRQLW SULQWV´/LYLQJ5RRPOLJKWLVRQµ



\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 24

QXOOREMHFW

 :DLW DVHFRQGZKDW LVZLWKWKDW1R&RPPDQGWKDW LVORDGHGLQVORWVIRXUWKURXJKVL[" 7U\LQJWRSXOODIDVWRQH"

/WWLKI\KP?MLQL[VMISITQ\\TM[WUM\PQVOQV\PMZM1V\PMZMUW\M KWV\ZWT_MLQLV¼\_IV\\WKPMKS\W[MMQN IKWUUIVL_I[TWILMLM^MZa \QUM_MZMNMZMVKMLI[TW\.WZQV[\IVKMQV\PMWV*]\\WV?I[8][PML UM\PWL_M_W]TLVMMLKWLMTQSM\PQ[" SXEOLFYRLGRQ%XWWRQ:DV3XVKHG LQWVORW ^ LI RQ&RPPDQGV>[email protected] QXOO ^ RQ&RPPDQGV>[email protected]H[HFXWH  ` `

;WPW_LW_MOM\IZW]VL\PI\'1UXTMUMV\IKWUUIVL\PI\LWM[VW\PQVO SXEOLFFODVV1R&RPPDQGLPSOHPHQWV&RPPDQG^ SXEOLFYRLGH[HFXWH ^` `

[email protected] QR&RPPDQG RII&RPPDQGV>[email protected] QR&RPPDQG `

;WQV\PMW]\X]\WN W]Z\M[\Z]VaW]IZM[MMQVO[TW\[\PI\PI^MV¼\JMMV I[[QOVML\WIKWUUIVLW\PMZ\PIV\PMLMNI]T\6W+WUUIVLWJRMK\ _PQKP_MI[[QOVML_PMV_MKZMI\ML\PM:MUW\M+WV\ZWT

)LUVW +HDG DEOH RU +RQ WLRQ 0HQ

3DWWHUQ +RQRUDEOH 0HQWLRQ

4HE.O#OMMANDOBJECTISANEXAMPLEOFANULLOBJECT!NULLOBJECTISUSEFUL WHENYOUDONTHAVEAMEANINGFULOBJECTTORETURN ANDYETYOUWANTTOREMOVE THERESPONSIBILITYFORHANDLINGQXOOFROMTHECLIENT&ORINSTANCE INOURREMOTE CONTROLWEDIDNTHAVEAMEANINGFULOBJECTTOASSIGNTOEACHSLOTOUTOFTHEBOX SOWEPROVIDEDA.O#OMMANDOBJECTTHATACTSASASURROGATEANDDOESNOTHING WHENITSEXECUTEMETHODISCALLED 9OULLFINDUSESFOR.ULL/BJECTSINCONJUNCTIONWITHMANY$ESIGN0ATTERNSAND SOMETIMESYOULLEVENSEE.ULL/BJECTLISTEDASA$ESIGN0ATTERN



&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 25

WKHFRPPDQGSDWWHUQ


OME!UTOMATIONOR"UST )NC

(OME NPROGRAMMINGINTERFACEFORYOUR HAT THEFOLLOWINGDESIGNANDAPPLICATIO TROLCODEASSIMPLEASPOSSIBLESOT 7EAREPLEASEDTOPRESENTYOUWITH TECON REMO PTHE TOKEE LWAS MARYDESIGNGOA ATTERNTO !UTOMATION2EMOTE#ONTROL/URPRI WEHAVEEMPLOYEDTHE#OMMAND0 ISEND 4OTH UCED PROD ESARE ENDORCLASS OFPRODUCING ITDOESNTREQUIRECHANGESASNEWV 7EBELIEVETHISWILLREDUCETHECOST TROLCLASSFROMTHE6ENDOR#LASSES LOGICALLYDECOUPLETHE2EMOTE#ON NCECOSTS NTENA GMAI NGOIN YOURO DUCE THEREMOTEASWELLASDRASTICALLYRE SANOVERVIEWOFOURDESIGN 4HEFOLLOWINGCLASSDIAGRAMPROVIDE

4HE2EMOTE,OADERCREATESA NUMBEROF#OMMAND/BJECTS THATARELOADEDINTOTHESLOTS OFTHE2EMOTE#ONTROL%ACH COMMANDOBJECTENCAPSULATES AREQUESTOFAHOME AUTOMATIONDEVICE

ETOF#OMMAND 4HE2EMOTE#ONTROLMANAGESAS BUTTONISPRESSED OBJECTS ONEPERBUTTON7HENA  METHODIS THECORRESPONDING"UTTON7AS0USHED ETHODONTHE CALLED WHICHINVOKESTHEEXECUTE M OTES HEREM NTOFT LLEXTE COMMAND4HATISTHEFU ASTHE KNOWLEDGEOFTHECLASSESITSINVOKING THE FROM EMOTE #OMMANDOBJECTDECOUPLESTHER UTOMATIONWORK CLASSESDOINGTHEACTUALHOME A

5HPRWH&RQWURO

5HPRWH/RDGHU

RQ&RPPDQGV RII&RPPDQGV

!LL2EMOTE#ONTROLCOMMANDS IMPLEMENTTHE#OMMAND INTERFACE WHICHCONSISTSOFONE METHODEXECUTE #OMMANDS ENCAPSULATEASETOFACTIONS ONASPECIFICVENDORCLASS4HE REMOTEINVOKESTHESEACTIONSBY CALLINGTHEEXECUTE METHOD

LQWHUIDFH!! &RPPDQG H[HFXWH

VHW&RPPDQG RQ%XWWRQ:DV3XVKHG RII%XWWRQ:DV3XVKHG

/LJKW RQ RII

/LJKW2Q&RPPDQG H[HFXWH

/LJKW2II&RPPDQG

H[HFXWH SXEOLFYRLGH[HFXWH ^ OLJKWRQ `

SXEOLFYRLGH[HFXWH ^ OLJKWRII `

RFORM 4HE6ENDOR#LASSESAREUSEDTOPE THEAC TUALHOME AUTOMATIONWORKOF THE CONTROLLINGDEVICES(ERE WEAREUSING ,IGHTCLASSASANEXAMPLE

TIONTHATCANBE 5SINGTHE#OMMAND)NTER FACE EACHAC THEREMOTEISIMPLEMENTED INVOKEDBYPRESSINGABUTTONON ND/BJECTHOLDS OMMA 4HE# OBJECT WITHASIMPLE#OMMAND NCEOFA6ENDOR#LASS AREFERENCETOANOBJECTTHATISANINSTA ONEORMORE ATCALLS ANDIMPLEMENTSANEXECUTEMETHODTH SHOWTWOSUCHCLASSES METHODSONTHATOBJECT(EREWE CTIVELY THATTURNALIGHTONANDOFF RESPE

\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 26

GRQ¶WIRUJHWXQGR

*UHDWMRELWORRNVOLNH \RX·YHFRPHXSZLWKDWHUULÀFGHVLJQ EXWDUHQ·W\RXIRUJHWWLQJRQHOLWWOHWKLQJ WKHFXVWRPHUDVNHGIRU" /,.(7+(81'2%87721

?PWWX[ ?MITUW[\NWZOW\T]KSQTaWVKM_M PI^MW]ZJI[QK+WUUIVLKTI[[M[]VLWQ[MI[a \WILL4M\¼[[\MX\PZW]OPILLQVO]VLW\WW]Z KWUUIVL[IVL\W\PMZMUW\MKWV\ZWT

?PI\IZM_MLWQVO' 7SIa_MVMML\WILLN]VK\QWVITQ\a\W[]XXWZ\\PM]VLWJ]\\WVWV\PMZMUW\M1\_WZS[TQSM \PQ["[Ia\PM4Q^QVO:WWU4QOP\Q[WNN IVLaW]XZM[[\PMWVJ]\\WVWV\PMZMUW\M7J^QW][Ta \PMTQOP\\]ZV[WV6W_QN aW]XZM[[\PM]VLWJ]\\WV\PMV\PMTI[\IK\QWV_QTTJMZM^MZ[ML·QV \PQ[KI[M\PMTQOP\_QTT\]ZVWNN*MNWZM_MOM\QV\WUWZMKWUXTM`M`IUXTM[TM\¼[OM\\PMTQOP\ _WZSQVO_Q\P\PM]VLWJ]\\WV" 

?PMVKWUUIVL[[]XXWZ\]VLW\PMaPI^MIV]VLWUM\PWL\PI\UQZZWZ[\PMM`MK]\M UM\PWL?PI\M^MZM`MK]\MTI[\LQL]VLWZM^MZ[M[;WJMNWZM_MKIVILL]VLW\WW]Z KWUUIVL[_MVMML\WILLIV]VLWUM\PWL\W\PM+WUUIVLQV\MZNIKM" SXEOLFLQWHUIDFH&RPPDQG^ SXEOLFYRLGH[HFXWH  SXEOLFYRLGXQGR  `

+HUH·VWKHQHZXQGR PHWKRG




&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 27

WKHFRPPDQGSDWWHUQ



4M\¼[[\IZ\_Q\P\PM4QOP\7V+WUUIVL"QN \PM4QOP\7V+WUUIVL¼[M`MK]\MUM\PWL _I[KITTML\PMV\PMWVUM\PWL_I[TI[\KITTML?MSVW_\PI\]VLWVMML[\WLW\PM WXXW[Q\MWN \PQ[JaKITTQVO\PMWNNUM\PWL SXEOLFFODVV/LJKW2Q&RPPDQGLPSOHPHQWV&RPPDQG^ /LJKWOLJKW  SXEOLF/LJKW2Q&RPPDQG /LJKWOLJKW ^ WKLVOLJKW OLJKW `  SXEOLFYRLGH[HFXWH ^ OLJKWRQ  `  SXEOLFYRLGXQGR ^ XUQVWKH H[HFXWH W XQGR  OLJKWRII  R ` OLJKWRQV WKHOLJKW ` VLPSO\WXUQV

EDFNRII

8QMKMWN KISM 6W_NWZ\PM4QOP\7NN+WUUIVL0MZM\PM]VLWUM\PWLR][\ VMML[\WKITT\PM4QOP\¼[WVUM\PWL

SXEOLFFODVV/LJKW2II&RPPDQGLPSOHPHQWV&RPPDQG^ /LJKWOLJKW  SXEOLF/LJKW2II&RPPDQG /LJKWOLJKW ^ WKLVOLJKW OLJKW `  SXEOLFYRLGH[HFXWH ^ OLJKWRII  `  SXEOLFYRLGXQGR ^  QGR WXUQV OLJKWRQ  $QGKHUHXDFNRQ E ` WKHOLJKW `

+W]TL\PQ[JMIVaMI[QMZ'7SIa_MIZMV¼\LWVMaM\#_MVMML\W_WZSITQ\\TM []XXWZ\QV\W\PM:MUW\M+WV\ZWT\WPIVLTM\ZIKSQVO\PMTI[\J]\\WVXZM[[ML IVL\PM]VLWJ]\\WVXZM[[ \RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Page 28

Return to Table of Contents

LPSOHPHQWLQJXQGR




SXEOLFFODVV5HPRWH&RQWURO:LWK8QGR^ &RPPDQG>@RQ&RPPDQGV ODVWFRPPDQG 7KLVLVZKHUHZH·OOVWDVKWKH Q &RPPDQG>@RII&RPPDQGV WWR &RPPDQGXQGR&RPPDQG H[HFXWHGIRUWKHXQGREX  SXEOLF5HPRWH&RQWURO:LWK8QGR ^ RQ&RPPDQGV QHZ&RPPDQG>@ RII&RPPDQGV QHZ&RPPDQG>@  &RPPDQGQR&RPPDQG QHZ1R&RPPDQG  IRU LQWL LL ^ -XVWOLNHWKHRWKHUVORWVXQGR RQ&RPPDQGV>[email protected] QR&RPPDQG RII&RPPDQGV>[email protected] QR&RPPDQG VWDUWVRIIZLWKD1R&RPPDQGVR ` SUHVVLQJXQGREHIRUHDQ\RWKHU XQGR&RPPDQG QR&RPPDQG EXWWRQZRQ·WGRDQ\WKLQJDWDOO `  SXEOLFYRLGVHW&RPPDQG LQWVORW&RPPDQGRQ&RPPDQG&RPPDQGRII&RPPDQG ^ RQ&RPPDQGV>[email protected] RQ&RPPDQG RII&RPPDQGV>[email protected] RII&RPPDQG `  :KHQDEXWWRQLVSUHVVHGZHWDNH SXEOLFYRLGRQ%XWWRQ:DV3XVKHG LQWVORW ^ RQ&RPPDQGV>[email protected]H[HFXWH  WKHFRPPDQGDQGILUVWH[HFXWH XQGR&RPPDQG RQ&RPPDQGV>[email protected] LWWKHQZHVDYHDUHIHUHQFHWR ` LWLQWKHXQGR&RPPDQGLQVWDQFH  YDULDEOH:HGRWKLVIRUERWK´RQµ SXEOLFYRLGRII%XWWRQ:DV3XVKHG LQWVORW ^ FRPPDQGVDQG´RIIµFRPPDQGV RII&RPPDQGV>[email protected]H[HFXWH  XQGR&RPPDQG RII&RPPDQGV>[email protected] ` :KHQWKHXQGREXWWRQLVSUHVVHGZH  SXEOLFYRLGXQGR%XWWRQ:DV3XVKHG ^ LQYRNHWKHXQGR PHWKRGRIWKH XQGR&RPPDQGXQGR  FRPPDQGVWRUHGLQXQGR&RPPDQG ` 7KLVUHYHUVHVWKHRSHUDWLRQRIWKH  ODVWFRPPDQGH[HFXWHG SXEOLF6WULQJWR6WULQJ ^ WR6WULQJFRGHKHUH ` `



&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 29

WKHFRPPDQGSDWWHUQ

@DUJV ^ 5HPRWH&RQWURO:LWK8QGRUHPRWH&RQWURO QHZ5HPRWH&RQWURO:LWK8QGR   XQGR  /LJKWOLYLQJ5RRP/LJKW QHZ/LJKW ³/LYLQJ5RRP´  &UHDWHD/LJKWDQGRXUQHZ&RPPDQGV II G2  DQ 2Q HQDEOHG/LJKW /LJKW2Q&RPPDQGOLYLQJ5RRP/LJKW2Q  QHZ/LJKW2Q&RPPDQG OLYLQJ5RRP/LJKW  /LJKW2II&RPPDQGOLYLQJ5RRP/LJKW2II  QHZ/LJKW2II&RPPDQG OLYLQJ5RRP/LJKW   UHPRWH&RQWUROVHW&RPPDQG OLYLQJ5RRP/LJKW2QOLYLQJ5RRP/LJKW2II   $GGWKHOLJKW&RPPDQGV UHPRWH&RQWURORQ%XWWRQ:DV3XVKHG   WRWKHUHPRWHLQVORW UHPRWH&RQWURORII%XWWRQ:DV3XVKHG   6\VWHPRXWSULQWOQ UHPRWH&RQWURO  7XUQWKH OLJKWRQWKHQ UHPRWH&RQWUROXQGR%XWWRQ:DV3XVKHG  RIIDQGW KHQXQGR UHPRWH&RQWURORII%XWWRQ:DV3XVKHG   UHPRWH&RQWURORQ%XWWRQ:DV3XVKHG   6\VWHPRXWSULQWOQ UHPRWH&RQWURO  7KHQWXUQWKHOLJKWRIIEDFNRQDQGXQGR UHPRWH&RQWUROXQGR%XWWRQ:DV3XVKHG  ` `

)VLPMZM¼[\PM\M[\ZM[]T\[ )LOH(GLW:LQGRZ+HOS8QGR&RPPDQGV'HI\(QWURS\

MDYD5HPRWH/RDGHU /LJKWLVRQ /LJKWLVRII

7XUQWKHOLJKWRQWKHQRII

+HUH·VWKH/LJKWFRPPDQGV

5HPRWH&RQWURO >VORW@KHDG¿UVWFRPPDQGXQGR/LJKW2Q&RPPDQGKHDG¿UVWFRPPDQGXQGR/LJKW2II&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >[email protected]KHDG¿UVWFRPPDQGXQGR/LJKW2II&RPPDQG 1RZXQG /LJKWLVRQ /LJKWLVRII /LJKWLVRQ

8QGRZDVSUHVVHGWKH/LJKW2II&RPPDQG XQGR WXUQVWKHOLJKWEDFNRQ

RKROGVWKH /LJKW2II&RPPDQGWKHODVW FRPPDQGLQYRNHG

7KHQZHWXUQWKHOLJKWRIIWKHQEDFNRQ

5HPRWH&RQWURO >VORW@KHDG¿UVWFRPPDQGXQGR/LJKW2Q&RPPDQGKHDG¿UVWFRPPDQGXQGR/LJKW2II&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >[email protected]KHDG¿UVWFRPPDQGXQGR/LJKW2Q&RPPDQG /LJKWLVRII

8QGRZDVSUHVVHGWKHOLJKWLVEDFNRII

1RZXQGRKROGVWKH/LJKW2Q&RPPDQGWKHODVW FRPPDQGLQYRNHG \RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 30

ZHQHHGWRNHHSVRPHVWDWHIRUXQGR

=[QVO[\I\M\WQUXTMUMV\=VLW 7SIaQUXTMUMV\QVO]VLWWV\PM4QOP\_I[QV[\Z]K\Q^MJ]\ITQ\\TM\WWMI[a
&HLOLQJ)DQ KLJK PHGLXP ORZ RII JHW6SHHG



ROGVORFDO SXEOLFFODVV&HLOLQJ)DQ^ OLQJ)DQFODVVKWKHFHLOLQJIDQ HL & KH W SXEOLFVWDWLF¿QDOLQW+,*+  DW 1RWLFHWK HQWLQJWKHVSHHGRI SXEOLFVWDWLF¿QDOLQW0(',80  VWDWHUHSUHV SXEOLFVWDWLF¿QDOLQW/2:  SXEOLFVWDWLF¿QDOLQW2))  6WULQJORFDWLRQ LQWVSHHG  SXEOLF&HLOLQJ)DQ 6WULQJORFDWLRQ ^ WKLVORFDWLRQ ORFDWLRQ +PPVRWRSURSHUO\ VSHHG 2)) ` LPSOHPHQWXQGR,·GKDYH  WRWDNHWKHSUHYLRXVVSHHGRI SXEOLFYRLGKLJK ^ WKHFHLOLQJIDQLQWRDFFRXQW VSHHG +,*+ FRGHWRVHWIDQWRKLJK `  SXEOLFYRLGPHGLXP ^ VSHHG 0(',80 FRGHWRVHWIDQWRPHGLXP ` 7KHVHPHWKRGVVHWWKH  VSHHGRIWKHFHLOLQJIDQ SXEOLFYRLGORZ ^ VSHHG /2: FRGHWRVHWIDQWRORZ `  SXEOLFYRLGRII ^ VSHHG 2)) FRGHWRWXUQIDQRII `  HFXUUHQW SXEOLFLQWJHW6SHHG ^ :HFDQJHWWK HLOLQJIDQ UHWXUQVSHHG VSHHGRIWKHF  ` XVLQJJHW6SHHG `



&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Page 31

Return to Table of Contents

WKHFRPPDQGSDWWHUQ

)LLQVO=VLW\W\PMKMQTQVONIVKWUUIVL[ 6W_TM\¼[\IKSTMILLQVO]VLW\W\PM^IZQW][+MQTQVO.IVKWUUIVL[
SXEOLFFODVV&HLOLQJ)DQ+LJK&RPPDQGLPSOHPHQWV&RPPDQG^ &HLOLQJ)DQFHLOLQJ)DQ LQWSUHY6SHHG  SXEOLF&HLOLQJ)DQ+LJK&RPPDQG &HLOLQJ)DQFHLOLQJ)DQ ^ WKLVFHLOLQJ)DQ FHLOLQJ)DQ `  SXEOLFYRLGH[HFXWH ^ SUHY6SHHG FHLOLQJ)DQJHW6SHHG  FHLOLQJ)DQKLJK  `  SXEOLFYRLGXQGR ^ LI SUHY6SHHG &HLOLQJ)DQ+,*+ ^ FHLOLQJ)DQKLJK  `HOVHLI SUHY6SHHG &HLOLQJ)DQ0(',80 ^ FHLOLQJ)DQPHGLXP  `HOVHLI SUHY6SHHG &HLOLQJ)DQ/2: ^ FHLOLQJ)DQORZ  `HOVHLI SUHY6SHHG &HLOLQJ)DQ2)) ^ FHLOLQJ)DQRII  ` ` `

FDOVWDWH :H·YHDGGHGOR RIWKH N DF WRNHHSWU RIWKHIDQ SUHYLRXVVSHHG

,QH[HFXWHEHIRUHZHFKDQJH WKHVSHHGRIWKHIDQZH QHHGWRILUVWUHFRUGLWV SUHYLRXVVWDWHMXVWLQFDVHZH QHHGWRXQGRRXUDFWLRQV

7RXQGRZHVHWWKHVSHHG RIWKHIDQEDFNWRLWV SUHYLRXVVSHHG

YiX`e gfn\i :H¶YHJRWWKUHHPRUHFHLOLQJIDQFRPPDQGVWRZULWHORZPHGLXPDQGRII&DQ\RXVHH KRZWKHVHDUHLPSOHPHQWHG"

\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Page 32

Return to Table of Contents

WHVWWKHFHLOLQJIDQ

/M\ZMILa\W\M[\\PMKMQTQVONIV
SXEOLFFODVV5HPRWH/RDGHU^  SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ 5HPRWH&RQWURO:LWK8QGRUHPRWH&RQWURO QHZ5HPRWH&RQWURO:LWK8QGR   &HLOLQJ)DQFHLOLQJ)DQ QHZ&HLOLQJ)DQ ³/LYLQJ5RRP´   &HLOLQJ)DQ0HGLXP&RPPDQGFHLOLQJ)DQ0HGLXP  +HUHZHLQVWDQWLDWHWKUHH QHZ&HLOLQJ)DQ0HGLXP&RPPDQG FHLOLQJ)DQ  FRPPDQGVKLJKPHGLXPDQGRII &HLOLQJ)DQ+LJK&RPPDQGFHLOLQJ)DQ+LJK  QHZ&HLOLQJ)DQ+LJK&RPPDQG FHLOLQJ)DQ  &HLOLQJ)DQ2II&RPPDQGFHLOLQJ)DQ2II  +HUHZHSXWPHGLXPLQ QHZ&HLOLQJ)DQ2II&RPPDQG FHLOLQJ)DQ  VORW]HURDQGKLJKLQ  VORWRQH:HDOVRORDG UHPRWH&RQWUROVHW&RPPDQG FHLOLQJ)DQ0HGLXPFHLOLQJ)DQ2II  XSWKHRIIFRPPDQGV UHPRWH&RQWUROVHW&RPPDQG FHLOLQJ)DQ+LJKFHLOLQJ)DQ2II   UHPRWH&RQWURORQ%XWWRQ:DV3XVKHG   )LUVWWXUQWKHIDQRQPHGLXP UHPRWH&RQWURORII%XWWRQ:DV3XVKHG   7KHQWXUQLWRII 6\VWHPRXWSULQWOQ UHPRWH&RQWURO  UHPRWH&RQWUROXQGR%XWWRQ:DV3XVKHG  8QGR,WVKRXOGJREDFNWRPHGLXP  UHPRWH&RQWURORQ%XWWRQ:DV3XVKHG   7XUQLWRQWRKLJKWKLVWLPH 6\VWHPRXWSULQWOQ UHPRWH&RQWURO  PHGLXP $QGRQHPRUHXQGRLWVKRXOGJREDFNWR UHPRWH&RQWUROXQGR%XWWRQ:DV3XVKHG  ` `



&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 33

WKHFRPPDQGSDWWHUQ


)LOH(GLW:LQGRZ+HOS8QGR7KLV

MDYD5HPRWH/RDGHU /LYLQJ5RRPFHLOLQJIDQLVRQPHGLXP /LYLQJ5RRPFHLOLQJIDQLVRII

7XUQWKHFHLOLQJIDQRQ PHGLXPWKHQWXUQLWRII

V +HUHDUHWKHFRPPDQG LQWKHUHPRWHFRQWURO

5HPRWH&RQWURO >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR&HLOLQJ)DQ0HGLXP&RPPDQGKHDG¿UVWFRPPDQGXQGR&HLOLQJ)DQ2II &RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR&HLOLQJ)DQ+LJK&RPPDQGKHDG¿UVWFRPPDQGXQGR&HLOLQJ)DQ2II&RP PDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG DQGXQGRKDVWKHODVW >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG FRPPDQGH[HFXWHGWKH >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG &HLOLQJ)DQ2I&RPPDQG >[email protected]KHDG¿UVWFRPPDQGXQGR&HLOLQJ)DQ2II&RPPDQG /LYLQJ5RRPFHLOLQJIDQLVRQPHGLXP /LYLQJ5RRPFHLOLQJIDQLVRQKLJK

8QGRWKHODVWFRPPDQGDQGLWJRHVEDFNWRPHGLXP 1RZWXUQLWRQKLJK

5HPRWH&RQWURO >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR&HLOLQJ)DQ0HGLXP&RPPDQGKHDG¿UVWFRPPDQGXQGR&HLOLQJ)DQ2II &RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR&HLOLQJ)DQ+LJK&RPPDQGKHDG¿UVWFRPPDQGXQGR&HLOLQJ)DQ2II&RP PDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGXQGR1R&RPPDQGKHDG¿UVWFRPPDQGXQGR1R&RPPDQG 1RZKLJKLVWKHODVW >[email protected]KHDG¿UVWFRPPDQGXQGR&HLOLQJ)DQ+LJK&RPPDQG /LYLQJ5RRPFHLOLQJIDQLVRQPHGLXP 

FRPPDQGH[HFXWHG

2QHPRUHXQGRDQGWKHFHLOLQJIDQ JRHVEDFNWRPHGLXPVSHHG

\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 34

PDFURFRPPDQGV

-^MZaZMUW\MVMML[I8IZ\a5WLM

6WHUHR RQ RII VHW&G

:KDW·VWKHSRLQWRIKDYLQJDUHPRWHLI\RXFDQ·W SXVKRQHEXWWRQDQGKDYHWKHOLJKWVGLPPHGWKH VWHUHRDQG79WXUQHGRQDQGVHWWRD'9'DQGWKH KRWWXEÀUHGXS"

79

VHW'YG

RQ

VHW5DGLR

RII

VHW9ROXPH

VHW,QSXW&KDQQHO

+RWWXE

VHW9ROXPH

RQ

/LJKW

RII FLUFXODWH MHWV2Q MHWV2II

RQ RII GLP

VHW7HPSHUDWXUH

+PP RXUUHPRWH FRQWUROZRXOGQHHGD EXWWRQIRUHDFKGHYLFH, GRQ·WWKLQNZHFDQGRWKLV

+ROGRQ6XHGRQ·WEH VRVXUH,WKLQNZHFDQ GRWKLVZLWKRXWFKDQJLQJWKH UHPRWHDWDOO

0DU\·VLGHDLVWRPDNHDQHZ NLQGRI&RPPDQGWKDWFDQ H[HFXWHRWKHU&RPPDQGV DQGPRUHWKDQRQHRIWKHP 3UHWW\JRRGLGHDKXK" SXEOLFFODVV0DFUR&RPPDQGLPSOHPHQWV&RPPDQG^ &RPPDQG>@FRPPDQGV  SXEOLF0DFUR&RPPDQG &RPPDQG>@FRPPDQGV ^ WKLVFRPPDQGV FRPPDQGV 7DNHDQDUUD\RI ` &RPPDQGVDQGVWRUHWKHPLQWKH0DFUR&RPPDQG  SXEOLFYRLGH[HFXWH ^ IRU LQWL LFRPPDQGVOHQJWKL ^ FRPPDQGV>[email protected]H[HFXWH  ` :KHQWKHPDFURJHWVH[HFXWHGE\WKHUHPRWH ` H[HFXWHWKRVHFRPPDQGVRQHDWDWLPH `



&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 35

WKHFRPPDQGSDWWHUQ

=[QVOIUIKZWKWUUIVL 4M\¼[[\MX\PZW]OPPW__M][MIUIKZWKWUUIVL" 

.QZ[\_MKZMI\M\PM[M\WN KWUUIVL[_M_IV\\WOWQV\W\PMUIKZW"

&UHDWHDOOWKHGHYLFHVDOLJKW

/LJKWOLJKW QHZ/LJKW ³/LYLQJ5RRP´  WYVWHUHRDQGKRWWXE 79WY QHZ79 ³/LYLQJ5RRP´  6WHUHRVWHUHR QHZ6WHUHR ³/LYLQJ5RRP´  +RWWXEKRWWXE QHZ+RWWXE  1RZFUHDWHDOOWKH2Q  FRPPDQGVWRFRQWUROWKHP /LJKW2Q&RPPDQGOLJKW2Q QHZ/LJKW2Q&RPPDQG OLJKW  6WHUHR2Q&RPPDQGVWHUHR2Q QHZ6WHUHR2Q&RPPDQG VWHUHR  792Q&RPPDQGWY2Q QHZ792Q&RPPDQG WY  +RWWXE2Q&RPPDQGKRWWXE2Q QHZ+RWWXE2Q&RPPDQG KRWWXE 

6KDUSHQ \RXU SHQFLO



?M_QTTIT[WVMMLKWUUIVL[NWZ\PMWNN J]\\WV[ _ZQ\M\PMKWLM\WKZMI\M\PW[MPMZM"

6M`\_MKZMI\M\_WIZZIa[WVMNWZ\PM7VKWUUIVL[IVLWVMNWZ\PM7NN KWU UIVL[IVLTWIL\PMU_Q\P\PMKWZZM[XWVLQVOKWUUIVL["

&UHDWHDQDUUD\IRU2Q DQGDQDUUD\IRU2II FRPPDQGV

&RPPDQG>@SDUW\2Q ^OLJKW2QVWHUHR2QWY2QKRWWXE2Q` &RPPDQG>@SDUW\2II ^OLJKW2IIVWHUHR2IIWY2IIKRWWXE2II`  DQGFUHDWHWZR 0DFUR&RPPDQGSDUW\2Q0DFUR QHZ0DFUR&RPPDQG SDUW\2Q  0DFUR&RPPDQGSDUW\2II0DFUR QHZ0DFUR&RPPDQG SDUW\2II  FRUUHVSRQGLQJPDFURV

WRKROGWKHP




$VVLJQWKHPDFUR FRPPDQGWRDEXWWRQDV ZHZRXOGDQ\FRPPDQG \RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 36

PDFURFRPPDQGH[HUFLVH



.QVITTa_MR][\VMML\WX][P[WUMJ]\\WV[IVL[MMQN \PQ[_WZS[ 6\VWHPRXWSULQWOQ UHPRWH&RQWURO  6\VWHPRXWSULQWOQ ³3XVKLQJ0DFUR2Q´  UHPRWH&RQWURORQ%XWWRQ:DV3XVKHG   6\VWHPRXWSULQWOQ ³3XVKLQJ0DFUR2II´  UHPRWH&RQWURORII%XWWRQ:DV3XVKHG  

+HUH·VWKHRXWSXW

)LOH(GLW:LQGRZ+HOS
MDYD5HPRWH/RDGHU

+HUHDUHWKHWZRP

DFURFRPPDQGV 5HPRWH&RQWURO >VORW@KHDG¿UVWFRPPDQGSDUW\0DFUR&RPPDQGKHDG¿UVWFRPPDQGSDUW\0DFUR&RPPDQG >VORW@KHDG¿UVWFRPPDQGSDUW\1R&RPPDQGKHDG¿UVWFRPPDQGSDUW\1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGSDUW\1R&RPPDQGKHDG¿UVWFRPPDQGSDUW\1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGSDUW\1R&RPPDQGKHDG¿UVWFRPPDQGSDUW\1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGSDUW\1R&RPPDQGKHDG¿UVWFRPPDQGSDUW\1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGSDUW\1R&RPPDQGKHDG¿UVWFRPPDQGSDUW\1R&RPPDQG >VORW@KHDG¿UVWFRPPDQGSDUW\1R&RPPDQGKHDG¿UVWFRPPDQGSDUW\1R&RPPDQG >[email protected]KHDG¿UVWFRPPDQGSDUW\1R&RPPDQG 3XVKLQJ0DFUR2Q /LJKWLVRQ /LYLQJ5RRPVWHUHRLVRQ /LYLQJ5RRP79LVRQ /LYLQJ5RRP79FKDQQHOLVVHWIRU'9' +RWWXELVKHDWLQJWRDVWHDPLQJGHJUHHV +RWWXELVEXEEOLQJ 3XVKLQJ0DFUR2II /LJKWLVRII /LYLQJ5RRPVWHUHRLVRII /LYLQJ5RRP79LVRII +RWWXELVFRROLQJWRGHJUHHV

$OOWKH&RPPDQGVLQWKHPDFUR DUHH[HFXWHGZKHQZHLQYRNH WKHRQPDFUR

DQGZKHQZHLQYRNHWKHRII PDFUR/RRNVOLNHLWZRUNV





&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 37

WKHFRPPDQGSDWWHUQ

([HUFLVH

@FRPPDQGV SXEOLF0DFUR&RPPDQG &RPPDQG>@FRPPDQGV ^ WKLVFRPPDQGV FRPPDQGV ` SXEOLFYRLGH[HFXWH ^ IRU LQWL LFRPPDQGVOHQJWKL ^ FRPPDQGV>[email protected]H[HFXWH  ` ` SXEOLFYRLGXQGR ^

` `

Q:

$O)ALWAYSNEEDARECEIVER 7HYCANTTHECOMMANDOBJECT IMPLEMENTTHEDETAILSOFTHE EXECUTE METHOD

A:

)NGENERAL WESTRIVEFORhDUMBv COMMANDOBJECTSTHATJUSTINVOKE ANACTIONONARECEIVERHOWEVER THEREAREMANYEXAMPLESOFhSMARTv COMMANDOBJECTSTHATIMPLEMENT MOST IFNOTALL OFTHELOGICNEEDED TOCARRYOUTAREQUEST#ERTAINLY YOUCANDOTHISJUSTKEEPINMIND YOULLNOLONGERHAVETHESAMELEVEL OFDECOUPLINGBETWEENTHEINVOKER ANDRECEIVER NORWILLYOUBEABLETO PARAMETERIZEYOURCOMMANDSWITH RECEIVERS

there are no Dumb Questions

Q:

(OWCAN)IMPLEMENTAHISTORY OFUNDOOPERATIONS)NOTHERWORDS )WANTTOBEABLETOPRESSTHEUNDO BUTTONMULTIPLETIMES

A:

'REATQUESTION)TSPRETTY EASYACTUALLYINSTEADOFKEEPINGJUST AREFERENCETOTHELAST#OMMAND EXECUTED YOUKEEPASTACKOFPREVIOUS COMMANDS4HEN WHENEVERUNDOIS PRESSED YOURINVOKERPOPSTHEFIRST ITEMOFFTHESTACKANDCALLSITSUNDO METHOD

Q:

#OULD)HAVEJUSTIMPLEMENTED 0ARTY-ODEASA#OMMANDBY CREATINGA0ARTY#OMMANDAND PUTTINGTHECALLSTOEXECUTETHEOTHER #OMMANDSINTHE0ARTY#OMMANDS EXECUTE METHOD

A:

9OUCOULDHOWEVER YOUD ESSENTIALLYBEhHARDCODINGvTHE PARTYMODEINTOTHE0ARTY#OMMAND 7HYGOTOTHETROUBLE7ITHTHE -ACRO#OMMAND YOUCANDECIDE DYNAMICALLYWHICH#OMMANDSYOU WANTTOGOINTOTHE0ARTY#OMMAND SOYOUHAVEMOREFLEXIBILITYUSING -ACRO#OMMANDS)NGENERAL THE -ACRO#OMMANDISAMOREELEGANT SOLUTIONANDREQUIRESLESSNEWCODE \RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 38

TXHXLQJUHTXHVWV

5WZM][M[WN\PM+WUUIVL8I\\MZV"Y]M]QVOZMY]M[\[ +WUUIVL[OQ^M][I_Ia\WXIKSIOMIXQMKMWN  KWUX]\I\QWVIZMKMQ^MZIVLI[M\WN IK\QWV[IVLXI[[Q\ IZW]VLI[IÅZ[\KTI[[WJRMK\6W_\PMKWUX]\I\QWVQ\[MTN  UIaJMQV^WSMLTWVOIN\MZ[WUMKTQMV\IXXTQKI\QWVKZMI\M[ \PMKWUUIVLWJRMK\1VNIK\Q\UIaM^MVJMQV^WSMLJaI LQNNMZMV\\PZMIL?MKIV\ISM\PQ[[KMVIZQWIVLIXXTaQ\\W UIVa][MN]TIXXTQKI\QWV[[]KPI[[KPML]TMZ[\PZMILXWWT[ IVLRWJY]M]M[\WVIUMINM_

RP S DW LRQ

VW

TXH

5H

X

DV N

7D VN

HW FK

DW LRQ

LOHU7

&R P S LO H U

Y ]M

X

1

VW

   

Q )L

TXH

U

5H

VW 'L

&R  

DV N

]M

LOHU7

5D \7UDFH

H[HFXWH

Q )L

DW LRQ

 

PS

PS

HW FK

 

'  

 H[HFXWH

H[HFXWH

H[HFXWH

H[HFXWH

HW ZRUN)

DQ FLDO&RPS W

 5D \7UDFH H[HFXWH

2W J

RZ QORDG

H[HFXWH



&R  

Q )L

 H[HFXWH

HW ZRUN)

DQ FLDO&RPS W

H[HFXWH

LEX HG& W

H[HFXWH



H[HFXWH

RZ QORDG

  

1  

H[HFXWH

H[HFXWH

 





1UIOQVMIRWJY]M]M"aW]ILLKWUUIVL[\W\PMY]M]M WVWVMMVLIVLWV\PMW\PMZMVL[Q\IOZW]XWN \PZMIL[
'   

QJWKH 2EMHFWVLPSOHPHQWLFHDUH FRPPDQGLQWHUIDXHXH DGGHGWRWKHT

WDWLRQ

+WUUIVL[

X

DQ FLDO&RPS W



 

1   

&R P S LO H U

 


RZ

TXH

H[HFXWH H[HFXWH

K UH D G

 

  7

7 K UH D G

VW

H[HFXWH

5D \7UDFH

HW ZRUN)

5H



'   

 

7 K UH D G

 

 

 

H[HFXWH

HW FK

   

7KUHDGVUHPRYHFRPPDQGV IURPWKHTXHXHRQHE\RQH DQGFDOOWKHLUH[HFXWH  PHWKRG2QFHFRPSOHWH WKH\JREDFNIRUDQHZ FRPPDQGREMHFW

7D VN

7KLVJLYHVXVDQHIIHFWLYHZD\ WROLPLWFRPSXWDWLRQWRD IL[HGQXPEHURIWKUHDGV

G

 QORD 7 K UH D G

YiX`e gfn\i +RZPLJKWDZHEVHUYHUPDNH XVHRIVXFKDTXHXH":KDWRWKHU DSSOLFDWLRQVFDQ\RXWKLQNRI"

&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 39

WKHFRPPDQGSDWWHUQ

5WZM][M[WN\PM+WUUIVL8I\\MZV"TWOOQVOZMY]M[\[
LQWHUIDFH! !

0W_LWM[\PQ[_WZS')[_MM`MK]\MKWUUIVL[_M[\WZMIPQ[\WZaWN \PMUWVLQ[S ?PMVIKZI[PWKK]Z[_MZMTWIL\PMKWUUIVLWJRMK\[IVLQV^WSM\PMQZM`MK]\M UM\PWL[QVJI\KPIVLQVWZLMZ

H[HFXWH XQGR VWRUH ORDG

6W_\PQ[SQVLWN TWOOQVO_W]TLV¼\UISM[MV[MNWZIZMUW\MKWV\ZWT#PW_M^MZ\PMZM IZMUIVaIXXTQKI\QWV[\PI\QV^WSMIK\QWV[WVTIZOMLI\I[\Z]K\]ZM[\PI\KIV¼\JMY]QKSTa [I^MLMIKP\QUMIKPIVOMQ[UILM*a][QVOTWOOQVO_MKIV[I^MITT\PMWXMZI\QWV[ [QVKM\PMTI[\KPMKSXWQV\IVLQN \PMZMQ[I[a[\MUNIQT]ZMIXXTa\PW[MWXMZI\QWV[\WW]Z KPMKSXWQV\
H[HFXWH VWRUH ORDG

5HVWRUH

G ORD

 &

$VHDFKFRPPDQG LVH[HFXWHGLWLV VWRUHGRQGLVN

&UDVK

ORDG

DQG2

H[HFXWH VWRUH ORDG

&R PPDQG 7

 

ORD G

H[HFXWH VWRUH ORDG

RP P

QH

DQG7K

H $IWHUDV\VWHPIDLOXU WKHREMHFWVDUH  UHORDGHGDQGH[HFXWHG  GHU RU HFW LQWKHFRUU

 H[ HF XWH

H[HFXWH

ZR

UH

&

RP P

H

H[HFXWH VWRUH ORDG

UH VWR

H[HFXWH VWRUH ORDG

&

RP P

XWH [HF H

, QY R NH U

H

ZR

&R PPDQG7

 

, QY R N H U

H[HFXWH VWRUH ORDG

UH

DQG2

H[HFXWH  H[ HF XWH

VWRUH

VWR UH

RP P

:HDGGWZRPHWKRGV IRUORJJLQJ

QH

 &

XWH [HF H

&RPPDQG

DQG7K

\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 40

\RXUGHVLJQWRROER[


22%DVLFV

EVWUDFWL 'HSHQGRQD QFUHWHFODVVHV GHSHQGRQFR

223DWWHLQUHVQDVILQDPHVDLO\RRIQHDOJRWRULWKPPQDDQVO\

GHFRXSOHVDQREMHFWPDNLQJ DUHTXHVWIURPWKHRQHWKDW NQRZVKRZWRSHUIRUPLW FHQWHURIWKLVGHFRXSOLQJDQG HQFDSVXODWHVDUHFHLYHUZLWK DQDFWLRQ RUVHWRIDFWLRQV 

ƒ $QLQYRNHUPDNHVDUHTXHVWRI D&RPPDQGREMHFWE\FDOOLQJ LWVH[HFXWH PHWKRGZKLFK LQYRNHVWKRVHDFWLRQVRQWKH UHFHLYHU

RW LQWHUIDFHVQ 3URJUDPWR RQV WL LPSOHPHQWD HGGHVLJQV RRVHO\FRXSO 6WULYHIRUOMHFWVWKDWLQWHUDFW EHWZHHQRE 

RU GEHRSHQI &ODVVHVVKREXOXWFORVHGIRU RQ H[WHQVL LRQ PRGLILFDW W RQV'RQR

ƒ 7KH&RPPDQG3DWWHUQ

ƒ $&RPPDQGREMHFWLVDWWKH

$EVWUDFWLRQ Q (QFDSVXODWLR  HV UL YD W KD Z RUSKLVP (QFDSVXODWH DQFH 3RO\P RYHULQKHULW Q LR VLW SR RP )DYRUF ,QKHULWDQFH

V 223ULQFLSOH

:MDD=LHGAFLK

ƒ ,QYRNHUVFDQEHSDUDPHWHUL]HG ZLWK&RPPDQGVHYHQ G\QDPLFDOO\DWUXQWLPH

GHFRXSOHDQ :KHQ\RXQHHGWHTRXHVWVIURP REMHFWPDNLQJU WNQRZKRZ WKHREMHFWVWKD UHTXHVWVXVH WRSHUIRUPWKH WWHUQ WKH&RPPDQG3D

DGKHGPLWW YLWGLFLQHDOHDQO\DQ 3LRURKD VH\UYHGUHIGHIHHQG$QWW PRE 6WU2DWEHJ HI KP FQDNHDFWWVMHWREHVRKVWKFWWUMHRWDKH\VFWGDWHVRGOJDP LWODP D LOL' RU\Q LWRVI XW  DO  'XOHQHDW$FGHVREHQHUVWF\DFDELWUFKEOLWDWRRQ6WHWFULHRHWUDUVZDF) HV OH D HQFDGSVHS WR0 WXWE JIEOJXHDQ WH KDJ\ O\KDV DWVILQDW  RQ RFOZEM UHHV LW HDFMHH VKR FWI\RU VHDV D HG FQJ VL EOUI DVLWHF ) RQ DW HD [L DW RE LQ QJ VS SG OH H WK KD UH X H RQ UF UH D QW XU QG Q WH F H FW OLH QV D LQ ZKH LQWH RUWOUIV LG RU R G MH F (  LQ I LH RY W G RE RP  H LI VV SU Q HQ IU W DF DOVSDRLUHQWTXHVW OD R RW \ HQ [W W F RE Q HQ FKGDVHVHDVXVJOODWHJ WH RUKLYLFOH LQDWWH IZURH SHRU WV6GDLQRUHJUOHGVVVHSXEHVHGFOGDVDHFVLQG QJH 'GHHF LG GHQ HQUH S YDU\GLQ DS ODQG HS QF HW HW ( O  OD FU WR FH G  EF RQ H DQ HWKR VLQJ\RX OHDW DOLY\LVXO\HQJLQW&VWKH LFW RHPVLUFPWR)DDFLQWWRGFWU\0 DWHF LIRQ DXDOWRWHPVSUQ WKWLHURQHEW\ROHWKWWKHGLIIHUHQW \FFDWHV WL OLW DQ QD MH VW LR LQ RE D LD FW Q RI XQ QW I DVD LQVWD VZLW DFODVVGSDHIUDHUPHWHUL]HFOLHQWORJUHTXHVWVDQG VXEFODVVHVUH TXHVWVTXHXHRU RSHUDWLRQV RDEOH VXSSRUWXQG

ƒ &RPPDQGVPD\VXSSRUWXQGR E\LPSOHPHQWLQJDQXQGR PHWKRGWKDWUHVWRUHVWKHREMHFW WRLWVSUHYLRXVVWDWHEHIRUH WKHH[FXWH PHWKRGZDVODVW FDOOHG

ƒ 0DFUR&RPPDQGVDUHDVLPSOH H[WHQVLRQRI&RPPDQGWKDW DOORZPXOWLSOHFRPPDQGVWR EHLQYRNHG/LNHZLVH0DFUR &RPPDQGVFDQHDVLO\VXSSRUW XQGR 

ƒ ,QSUDFWLFHLWLVQRWXQFRPPRQ IRU´VPDUWµ&RPPDQGREMHFWV WRLPSOHPHQWWKHUHTXHVW WKHPVHOYHVUDWKHUWKDQ GHOHJDWLQJWRDUHFHLYHU

ƒ &RPPDQGVPD\DOVREHXVHG WRLPSOHPHQWORJJLQJDQG WUDQVDFWLRQDOV\VWHPV



&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 41

WKHFRPPDQGSDWWHUQ




 

 

 









  

 





   +)** .* &%   &$$%   *+ & + &%* %  ) -)  ) ,**  %) && ,)-&) + +/

 + * + ) -)* % + )$&+ &%+)&#  !+ ++ "%&.* + + &%* % + ) -)  %&+) + % &$$% % &   !+ ++ "%&.* &. +& + + %* &%    &$$% %'*,#+* + *

 &# & ,*+&$) % + &$$% '++)%  ,)  )*+ &$$% &!+ &%+)&## + *  %-&") % ) -) ) 

 &$'%/ ++ &+ ,* .&) & $&,+ ,* %**  ## &$$%* ')&-  + *   &&" % + * ')*&% .)  % +#/ &,'#  )) * &,+  )(,*+    +)**  %+ & + *

\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 42

H[HUFLVHVROXWLRQV

([HUFLVH VROXWLRQV

5I\KP\PMLQVMZWJRMK\[IVLUM\PWL[_Q\P\PMKWZZM[XWVLQVOVIUM[NZWU\PM +WUUIVL8I\\MZV

'LQHU

&RPPDQG3DWWHUQ

Waitress

Command

Short Order Cook

execute()

orderUp()

Client

Order

Invoker

Customer

Receiver

takeOrder()

setCommand()

6KDUSHQ \RXU SHQFLO SXEOLFFODVV*DUDJH'RRU2SHQ&RPPDQGLPSOHPHQWV&RPPDQG^ *DUDJH'RRUJDUDJH'RRU SXEOLF*DUDJH'RRU2SHQ&RPPDQG *DUDJH'RRUJDUDJH'RRU ^ WKLVJDUDJH'RRU JDUDJH'RRU ` SXEOLFYRLGH[HFXWH ^ JDUDJH'RRUXS  ` ` )LOH(GLW:LQGRZ+HOS*UHHQ(JJV +DP

MDYD5HPRWH&RQWURO7HVW /LJKWLVRQ *DUDJH'RRULV2SHQ 



&KDSWHU

Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 6. Encapsulating Invocation

Return to Table of Contents

Page 43

WKHFRPPDQGSDWWHUQ

?ZQ\M\PM]VLWUM\PWLNWZ5IKZW+WUUIVL

([HUFLVH SXEOLFFODVV0DFUR&RPPDQGLPSOHPHQWV&RPPDQG^ &RPPDQG>@FRPPDQGV SXEOLF0DFUR&RPPDQG &RPPDQG>@FRPPDQGV ^ WKLVFRPPDQGV FRPPDQGV `

([HUFLVH VROXWLRQV

SXEOLFYRLGH[HFXWH ^ IRU LQWL LFRPPDQGVOHQJWKL ^ FRPPDQGV>[email protected]H[HFXWH  ` ` SXEOLFYRLGXQGR ^ IRU LQWL LFRPPDQGVOHQJWKL ^ FRPPDQGV>[email protected]XQGR  ` ` `

6KDUSHQ \RXU SHQFLO

?M_QTTIT[WVMMLKWUUIVL[NWZ\PMWNN J]\\WV ?ZQ\M\PMKWLM\WKZMI\M\PW[MPMZM"

/LJKW2II&RPPDQGOLJKW2II QHZ/LJKW2II&RPPDQG OLJKW  6WHUHR2II&RPPDQGVWHUHR2II QHZ6WHUHR2II&RPPDQG VWHUHR  792II&RPPDQGWY2II QHZ792II&RPPDQG WY  +RWWXE2II&RPPDQGKRWWXE2II QHZ+RWWXE2II&RPPDQG KRWWXE 



 





























!











"

$



#



 



























#







 









! #











































"





#



















!









  



#



%

 



! 











!





 













$









"



!



\RXDUHKHUH



Chapter 6. Encapsulating Invocation Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Table of Contents Chapter 7. Being Adaptive............................................................................ 1 Section 7.1. Adapters all around us................................................................................................................................................. 2 Section 7.2. Object oriented adapters............................................................................................................................................. 3 Section 7.3. If it walks like a duck and quacks like a duck, then it must might be a duck turkey wrapped with a duck adapter.... . . 4 Section 7.4. Test drive the adapter................................................................................................................................................. 6 Section 7.5. The Adapter Pattern explained................................................................................................................................... 7 Section 7.6. There are no Dumb Questions.................................................................................................................................... 8 Section 7.7. Adapter Pattern defined.............................................................................................................................................. 9 Section 7.8. Object and class adapters.......................................................................................................................................... 10 Section 7.9. Duck Magnets............................................................................................................................................................. 11 Section 7.10. Duck Magnets Answer............................................................................................................................................. 12 Section 7.11. Fireside Chats........................................................................................................................................................... 13 Section 7.12. Real world adapters................................................................................................................................................. 14 Section 7.13. Adapting an Enumeration to an Iterator................................................................................................................. 15 Section 7.14. Fireside Chats........................................................................................................................................................... 18 Section 7.15. And now for something different............................................................................................................................ 20 Section 7.16. Home Sweet Home Theater..................................................................................................................................... 21 Section 7.17. Watching a movie (the hard way)............................................................................................................................ 22 Section 7.18. Lights, Camera, Facade!.......................................................................................................................................... 24 Section 7.19. There are no Dumb Questions................................................................................................................................ 26 Section 7.20. Constructing your home theater facade................................................................................................................. 27 Section 7.21. Implementing the simplified interface................................................................................................................... 28 Section 7.22. Time to watch a movie (the easy way).................................................................................................................... 29 Section 7.23. Facade Pattern defined........................................................................................................................................... 30 Section 7.24. The Principle of Least Knowledge........................................................................................................................... 31 Section 7.25. How NOT to Win Friends and Influence Objects................................................................................................... 32 Section 7.26. there are no Dumb Questions................................................................................................................................. 33 Section 7.27. The Facade and the Principle of Least Knowledge................................................................................................. 35 Section 7.28. Tools for your Design Toolbox................................................................................................................................ 36 Section 7.29. Exercise solutions................................................................................................................................................... 38 Section 7.30. Exercise solutions................................................................................................................................................... 39 Section 7.31. Exercise solutions.................................................................................................................................................... 40

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 1

the Adapter and Facade Patterns

4

%HLQJ$GDSWLYH 4

'R\RXWKLQNWKHUHDGHUVDUH UHDOO\JHWWLQJWKHLPSUHVVLRQZH·UH ZDWFKLQJDKRUVHUDFHUDWKHUWKDQ VLWWLQJLQDSKRWRVWXGLR"


5

7KDW·VWKH EHDXW\RIRXUSURIHVVLRQ ZHFDQPDNHWKLQJVORRN OLNHVRPHWKLQJWKH\·UHQRW

:UDSSHGLQ WKLVFRDW,·PD GLIIHUHQWPDQ

,QWKLVFKDSWHUZH·UHJRLQJWRDWWHPSWVXFKLPSRVVLEOHIHDWVDV SXWWLQJDVTXDUHSHJLQDURXQGKROH6RXQGLPSRVVLEOH"1RWZKHQZHKDYH 'HVLJQ3DWWHUQV5HPHPEHUWKH'HFRUDWRU3DWWHUQ":HZUDSSHGREMHFWVWRJLYHWKHPQHZ UHVSRQVLELOLWLHV1RZZH¶UHJRLQJWRZUDSVRPHREMHFWVZLWKDGLIIHUHQWSXUSRVHWRPDNHWKHLU LQWHUIDFHVORRNOLNHVRPHWKLQJWKH\¶UHQRW:K\ZRXOGZHGRWKDW"6RZHFDQDGDSWDGHVLJQ H[SHFWLQJRQHLQWHUIDFHWRDFODVVWKDWLPSOHPHQWVDGLIIHUHQWLQWHUIDFH7KDW¶VQRWDOOZKLOH ZH¶UHDWLWZH¶UHJRLQJWRORRNDWDQRWKHUSDWWHUQWKDWZUDSVREMHFWVWRVLPSOLI\WKHLULQWHUIDFH

WKLVLVDQHZFKDSWHU



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 2

DGDSWHUVHYHU\ZKHUH

)LIX\MZ[ITTIZW]VL][
-]ZWXMIV?ITT7]\TM\ )+8W_MZ)LIX\MZ ;\IVLIZL)+8T]O

[SRVHV RXWOHWHSRZHU OO D Z  Q D H J RS 7KH(XU UIDFHIRUJHWWLQ RQHLQWH

7KH86ODSWRSH[SHFWV DQRWKHULQWHUIDFH

7KHDGDSWHUFRQYHUWVRQH LQWHUIDFHLQWRDQRWKHU

AW]SVW__PI\\PMILIX\MZLWM["Q\[Q\[QVJM\_MMV\PMXT]OWN aW]ZTIX\WXIVL\PM -]ZWXMIV)+W]\TM\#Q\[RWJQ[\WILIX\\PM-]ZWXMIVW]\TM\[W\PI\aW]KIVXT]OaW]Z TIX\WXQV\WQ\IVLZMKMQ^MXW_MZ7ZTWWSI\Q\\PQ[_Ia"\PMILIX\MZKPIVOM[\PMQV\MZNIKM WN \PMW]\TM\QV\WWVM\PI\aW]ZTIX\WXM`XMK\[

 OZRUOG HUUHD LQNRI" K W R  \ K DQ W +RZP HUVFDQ\RX DGDSW

;WUM)+ILIX\MZ[IZM[QUXTM·\PMaWVTaKPIVOM\PM[PIXMWN \PMW]\TM\[W\PI\Q\UI\KPM[ aW]ZXT]OIVL\PMaXI[[\PM)+K]ZZMV\[\ZIQOP\\PZW]OP·J]\W\PMZILIX\MZ[IZMUWZM KWUXTM`QV\MZVITTaIVLUIaVMML\W[\MX\PMXW_MZ]XWZLW_V\WUI\KPaW]ZLM^QKM[¼ VMML[ 7SIa\PI\¼[\PMZMIT_WZTL_PI\IJW]\WJRMK\WZQMV\MLILIX\MZ['?MTTW]Z77ILIX\MZ[ XTIa\PM[IUMZWTMI[\PMQZZMIT_WZTLKW]V\MZXIZ\["\PMa\ISMIVQV\MZNIKMIVLILIX\Q\\W WVM\PI\IKTQMV\Q[M`XMK\QVO 

&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 3

WKHDGDSWHUSDWWHUQ

7JRMK\WZQMV\MLILIX\MZ[ ;IaaW]¼^MOW\IVM`Q[\QVO[WN\_IZM[a[\MU\PI\aW]VMML\W_WZSIVM_^MVLWZKTI[[TQJZIZa QV\WJ]\\PMVM_^MVLWZLM[QOVML\PMQZQV\MZNIKM[LQNNMZMV\Ta\PIV\PMTI[\^MVLWZ" >MVLWZ +TI[[

AW]Z -`Q[\QVO ;a[\MU

\RX·YH DWFKWKHRQHJRLQJWRZRUN P ·W VQ RH G H DF W 7KHLULQWHUIUFRGHDJDLQVW7KLVLVQ· ZULWWHQ\RX 7SIaaW]LWV¼\_IV\\W[WT^M\PMXZWJTMUJaKPIVOQVOaW]ZM`Q[\QVOKWLMIVLaW]KIV¼\ KPIVOM\PM^MVLWZ¼[KWLM;W_PI\LWaW]LW'?MTTaW]KIV_ZQ\MIKTI[[\PI\ILIX\[\PM VM_^MVLWZQV\MZNIKMQV\W\PMWVMaW]¼ZMM`XMK\QVO

AW]Z -`Q[\QVO ;a[\MU

>MVLWZ +TI[[

)LIX\MZ

7KHDGDSWHULPSOHPHQWVWKH LQWHUIDFH\RXUFODVVHVH[SHFW

DFH HYHQGRULQWHUI $QGWDONVWRWKUHTXHVWV XU WRVHUYLFH\R


)LIX\MZ

1RFRGHFKDQJHV

1HZFRGH

>MVLWZ +TI[[

Q RIDVROXWLR &DQ\RXWKLQNUHTXLUH<28WR WKDWGRHVQ·W GGLWLRQDOFRGH ZULWH$1<DWKHQHZYHQGRU WRLQWHJUDWH DERXWPDNLQJWKH FODVVHV"+RZ\WKHDGDSWHUFODVV YHQGRUVXSSO

1RFRGHFKDQJHV \RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 4

WXUNH\DGDSWHU

1NQ\_ITS[TQSMIL]KSIVLY]IKS[TQSMIL]KS \PMVQ\U][\UQOP\JMIL]KS\]ZSMa_ZIXXML _Q\PIL]KSILIX\MZ ,W·VWLPHWRVHHDQDGDSWHULQDFWLRQ5HPHPEHURXU GXFNVIURP&KDSWHU"/HW·VUHYLHZDVOLJKWO\VLPSOLÀHG YHUVLRQRIWKH'XFNLQWHUIDFHVDQGFODVVHV

SXEOLFLQWHUIDFH'XFN^ SXEOLFYRLGTXDFN  SXEOLFYRLGÀ\  `

RXQGRXU 7KLVWLPHDU HQWD'XFN HP SO GXFNVLP DWDOORZV LQWHUIDFHWKDFNDQGIO\ 'XFNVWRTX

+HUH·VDVXEFODVVRI'XFNWKH0DOODUG'XFN SXEOLFFODVV0DOODUG'XFNLPSOHPHQWV'XFN^ SXEOLFYRLGTXDFN ^ 6\VWHPRXWSULQWOQ ³4XDFN´  `  SXEOLFYRLGÀ\ ^ 6\VWHPRXWSULQWOQ ³,¶PÀ\LQJ´  ` `

KHGXFN HQWDWLRQVW 6LPSOHLPSOHP WZKDWLWLVGRLQJ MXVWSULQWVRX

1RZLW·VWLPHWRPHHWWKHQHZHVWIRZORQWKHEORFN

H\JREEOH

·WTXDFNWK

SXEOLFLQWHUIDFH7XUNH\^ SXEOLFYRLGJREEOH  SXEOLFYRLGÀ\  `



7XUNH\VGRQ

7XUNH\VFDQIO\DOWKRXJKWKH\ FDQRQO\IO\VKRUWGLVWDQFHV

&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 5

WKHDGDSWHUSDWWHUQ

DWLRQ

SOHPHQW SXEOLFFODVV:LOG7XUNH\LPSOHPHQWV7XUNH\^ +HUH·VDFRQFUHWHLPNLWMXVW XF SXEOLFYRLGJREEOH ^ H' OLN RI7XUNH\ V 6\VWHPRXWSULQWOQ ³*REEOHJREEOH´  SULQWVRXWLWVDFWLRQ `  SXEOLFYRLGÀ\ ^ 6\VWHPRXWSULQWOQ ³,¶PÀ\LQJDVKRUWGLVWDQFH´  ` ` 1RZOHW·VVD\\RX·UHVKRUWRQ'XFNREMHFWVDQG\RX·GOLNHWR XVHVRPH7XUNH\REMHFWVLQWKHLUSODFH2EYLRXVO\ZHFDQ·W XVHWKHWXUNH\VRXWULJKWEHFDXVHWKH\KDYHDGLIIHUHQWLQWHUIDFH 6ROHW·VZULWHDQ$GDSWHU

&RGH8S&ORVH

)LUVW\RXQHHGWRLPSOHPHQWWKHLQWHUIDFH RIWKHW\SH\RX·UHDGDSWLQJWR7KLVLVWKH LQWHUIDFH\RXUFOLHQWH[SHFWVWRVHH

SXEOLFFODVV7XUNH\$GDSWHULPSOHPHQWV'XFN^ 7XUNH\WXUNH\  1H[WZHQHHGWRJHWDUHIHUHQFHWR SXEOLF7XUNH\$GDSWHU 7XUNH\WXUNH\ ^ WKHREMHFWWKDWZHDUHDGDSWLQJKHUH WKLVWXUNH\ WXUNH\ ZHGRWKDWWKURXJKWKHFRQVWUXFWRU `  SXEOLFYRLGTXDFN ^ 1RZZHQHHGWRLPSOHPHQWDOOWKHPHWKRGVLQ WXUNH\JREEOH  WKHLQWHUIDFHWKHTXDFN WUDQVODWLRQEHWZHHQ ` FODVVHVLVHDV\MXVWFDOOWKHJREEOH PHWKRG  SXEOLFYRLGÀ\ ^ IRU LQWL LL ^ WXUNH\À\  (YHQWKRXJKERWKLQWHUIDFHVKDYHDIO\  ` PHWKRG7XUNH\VIO\LQVKRUWVSXUWVWKH\ ` FDQ·WGRORQJGLVWDQFHIO\LQJOLNHGXFNV7R `

PDSEHWZHHQD'XFN·VIO\ PHWKRGDQGD 7XUNH\·VZHQHHGWRFDOOWKH7XUNH\·VIO\  PHWKRGILYHWLPHVWRPDNHXSIRULW \RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 6

WHVWWKHDGDSWHU


N

F SXEOLFFODVV'XFN7HVW'ULYH^ DWHD'X /HW·VFUH SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ UNH\ 0DOODUG'XFNGXFN QHZ0DOODUG'XFN  DQGD7X  :LOG7XUNH\WXUNH\ QHZ:LOG7XUNH\  $QGWKHQZUDSWKHWXUNH\ 'XFNWXUNH\$GDSWHU QHZ7XUNH\$GDSWHU WXUNH\  LQD7XUNH\$GDSWHUZKLFK  PDNHVLWORRNOLNHD'XFN 6\VWHPRXWSULQWOQ ³7KH7XUNH\VD\V´  WXUNH\JREEOH  WXUNH\À\  7KHQOHW·VWHVWWKH7XUNH\  PDNHLWJREEOHPDNHLWIO\ 6\VWHPRXWSULQWOQ ³?Q7KH'XFNVD\V´  WHVW'XFN GXFN  1RZOHW·VWHVWWKHGXFN  E\FDOOLQJWKHWHVW'XFN  6\VWHPRXWSULQWOQ ³?Q7KH7XUNH\$GDSWHUVD\V´  PHWKRGZKLFKH[SHFWVD WHVW'XFN WXUNH\$GDSWHU  ` 'XFNREMHFW 1RZW  VWDWLFYRLGWHVW'XFN 'XFNGXFN ^ RIIWKKHELJWHVW HWXUNH ZHWU\ GXFNTXDFN  \DVD WRSDV +HUH·VRXUWHVW'XF GXFNÀ\  V GXFN N  JHWVDGXFNDQGFD PHWKRGLW ` OOVLWVTXDFN  DQGIO\ PH `

WKRGV

7HVWUXQ

)LOH(GLW:LQGRZ+HOS'RQ¶W)RUJHW7R'XFN

MDYD5HPRWH&RQWURO7HVW 7KH7XUNH\VD\V *REEOHJREEOH

7KH7XUNH\JREEOHVDQG IOLHVDVKRUWGLVWDQFH

,¶PÀ\LQJDVKRUWGLVWDQFH 7KH'XFNVD\V 4XDFN ,¶PÀ\LQJ 7KH7XUNH\$GDSWHUVD\V *REEOHJREEOH ,¶PÀ\LQJDVKRUWGLVWDQFH ,¶PÀ\LQJDVKRUWGLVWDQFH ,¶PÀ\LQJDVKRUWGLVWDQFH ,¶PÀ\LQJDVKRUWGLVWDQFH ,¶PÀ\LQJDVKRUWGLVWDQFH 

7KH'XFNTXDFNVDQGIOLHV MXVWOLNH\RX·GH[SHFW

Q $QGWKHDGDSWHUJREEOHVZKHIHZ  TXDFN LVFDOOHGDQGIOLHVD KH WLPHVZKHQIO\ LVFDOOHG7QRZVLW WHVW'XFN PHWKRGQHYHUN XFN KDVDWXUNH\GLVJXLVHGDVDG

&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 7

WKHDGDSWHUSDWWHUQ




+TQMV\

UHTX

HVW

VW TXH G5H H W D VO WUDQ

)LIX\MM

7KH&OLHQWLVLPSOHPHQWHG DJDLQVWWKHWDUJHWLQWHUIDFH

)LIX\MZ DGDSWH H LQWHUI DFH

FH WHUID

HWLQ

WDUJ

7KH$GDSWHULPSOHPHQWVWKH WDUJHWLQWHUIDFHDQGKROGVDQ LQVWDQFHRIWKH$GDSWHH

0MZM¼[PW_\PM+TQMV\][M[\PM)LIX\MZ 

QWHG ULPSOHPH'XFN H W S D G $ \ 7XUNH JHWLQWHUIDFH WKHWDU

7KHFOLHQWPDNHVDUHTXHVWWRWKH DGDSWHUE\FDOOLQJDPHWKRGRQLWXVLQJ WKHWDUJHWLQWHUIDFH



7KHDGDSWHUWUDQVODWHVWKDWUHTXHVWLQWR RQHRUPRUHFDOOVRQWKHDGDSWHHXVLQJ WKHDGDSWHHLQWHUIDFH



7KHFOLHQWUHFHLYHVWKHUHVXOWVRIWKH FDOODQGQHYHUNQRZVWKHUHLVDQDGDSWHU GRLQJWKHWUDQVODWLRQ

7XUNH\ZDVWKH DGDSWHHLQWHUIDFH

HH OLHQWDQG$GDSW V 1RWHWKDWWKH& RZ NQ U KH LW QH DUHGHFRXSOHG²U DERXWWKHRWKH

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 8

DGDSWHUSDWWHUQGH¿QHG

6KDUSHQ \RXU SHQFLO /HW¶VVD\ZHDOVRQHHGDQ$GDSWHUWKDWFRQYHUWVD'XFNWRD7XUNH\ /HW¶VFDOOLW'XFN$GDSWHU:ULWHWKDWFODVV

+RZGLG\RXKDQGOHWKHÀ\PHWKRG DIWHUDOOZHNQRZGXFNVÀ\ORQJHUWKDQWXUNH\V "&KHFNWKHDQVZHUVDW WKHHQGRIWKHFKDSWHUIRURXUVROXWLRQ'LG\RXWKLQNRIDEHWWHUZD\"

there are no

Dumb Questions

Q:

+RZPXFK´DGDSWLQJµGRHV DQDGDSWHUQHHGWRGR",WVHHPVOLNH LI,QHHGWRLPSOHPHQWDODUJHWDUJHW LQWHUIDFH,FRXOGKDYHD/27RIZRUNRQ P\KDQGV

A:




Q:

'RHVDQDGDSWHUDOZD\VZUDSRQH DQGRQO\RQHFODVV"

A:

7KH$GDSWHU3DWWHUQ·VUROHLVWR FRQYHUWRQHLQWHUIDFHLQWRDQRWKHU:KLOH PRVWH[DPSOHVRIWKHDGDSWHUSDWWHUQVKRZ DQDGDSWHUZUDSSLQJRQHDGDSWHHZHERWK NQRZWKHZRUOGLVRIWHQDELWPRUHPHVV\ 6R\RXPD\ZHOOKDYHVLWXDWLRQVZKHUHDQ DGDSWHUKROGVWZRRUPRUHDGDSWHHVWKDWDUH QHHGHGWRLPSOHPHQWWKHWDUJHWLQWHUIDFH 7KLVUHODWHVWRDQRWKHUSDWWHUQFDOOHGWKH )DFDGH3DWWHUQSHRSOHRIWHQFRQIXVHWKH WZR5HPLQGXVWRUHYLVLWWKLVSRLQWZKHQZH WDONDERXWIDFDGHVODWHULQWKLVFKDSWHU

Q:

:KDWLI,KDYHROGDQGQHZSDUWV RIP\V\VWHPWKHROGSDUWVH[SHFWWKH ROGYHQGRULQWHUIDFHEXWZH·YHDOUHDG\ ZULWWHQWKHQHZSDUWVWRXVHWKHQHZ YHQGRULQWHUIDFH",WLVJRLQJWRJHW FRQIXVLQJXVLQJDQDGDSWHUKHUHDQGWKH XQZUDSSHGLQWHUIDFHWKHUH:RXOGQ·W,EH EHWWHURIIMXVWZULWLQJP\ROGHUFRGHDQG IRUJHWWLQJWKHDGDSWHU"

A:

1RWQHFHVVDULO\2QHWKLQJ\RX FDQGRLVFUHDWHD7ZR:D\$GDSWHUWKDW VXSSRUWVERWKLQWHUIDFHV7RFUHDWHD7ZR :D\$GDSWHUMXVWLPSOHPHQWERWKLQWHUIDFHV LQYROYHGVRWKHDGDSWHUFDQDFWDVDQROG LQWHUIDFHRUDQHZLQWHUIDFH

&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 9

WKHDGDSWHUSDWWHUQ

)LIX\MZ8I\\MZVLMNQVML -VW]OPL]KS[\]ZSMa[IVL)+XW_MZILIX\MZ[#TM\¼[OM\ZMITIVLTWWSI\\PMWNÅKQIT LMÅVQ\QWVWN \PM)LIX\MZ8I\\MZV"


6W__MSVW_\PQ[XI\\MZVITTW_[][\W][MIKTQMV\_Q\PIVQVKWUXI\QJTMQV\MZNIKMJa KZMI\QVOIV)LIX\MZ\PI\LWM[\PMKWV^MZ[QWV
LQWHUIDFH!! 7DUJHW

&OLHQW

UHTXHVW

7KH$GDSWHULPSOHPHQWV WKH7DUJHWLQWHUIDFH

7KHFOLHQWVHHVRQO\WKH 7DUJHWLQWHUIDFH $GDSWHU UHTXHVW

$GDSWHH VSHFLILF5HTXHVW

$GDSWHULVFRPSRVHG ZLWKWKH$GDSWHH

$OOUHTXHVWVJHW GHOHJDWHGWRWKH $GDSWHH


\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 10

REMHFWDQGFODVVDGDSWHUV

7JRMK\IVLKTI[[ILIX\MZ[ 6W_LM[XQ\MPI^QVOLMÅVML\PMXI\\MZV_MPI^MV¼\\WTLaW]\PM_PWTM[\WZaaM\
7DUJHW

&OLHQW

$GDSWHH

UHTXHVW

VSHFLILF5HTXHVW

$GDSWHU UHTXHVW

,QVWHDGRIXVLQJFRP SRVLWLRQWRDGDSWWKH $GDSWHHWKH$GDSWHUQRZ VXEFODVVHVWKH$GDSWHH DQGWKH7DUJHWFODVVHV 4WWSNIUQTQIZ'
YiX`e gfn\i 2EMHFWDGDSWHUVDQGFODVVDGDSWHUVXVHWZRGLIIHUHQWPHDQVRI DGDSWLQJWKHDGDSWHH FRPSRVLWLRQYHUVXVLQKHULWDQFH +RZGRWKHVH LPSOHPHQWDWLRQGLIIHUHQFHVDIIHFWWKHÀH[LELOLW\RIWKHDGDSWHU"



&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 11

WKHDGDSWHUSDWWHUQ

'XFN0DJQHWV AW]ZRWJQ[\W\ISM\PML]KSIVL\]ZSMaUIOVM\[IVL LZIO\PMUW^MZ\PMXIZ\WN \PMLQIOZIU\PI\LM[KZQJM[ \PMZWTMXTIaMLJa\PI\JQZLQVW]ZMIZTQMZM`IUXTM
&ODVV$GDSWHU 7DUJHW

&OLHQW

$GDSWHH

UHTXHVW

VSHFLILF5HTXHVW

$GDSWHU UHTXHVW

2EMHFW$GDSWHU &OLHQW

LQWHUIDFH!! 7DUJHW UHTXHVW

$GDSWHU UHTXHVW

$GDSWHH VSHFLILF5HTXHVW

'UDJWKHVHRQWR KHFODVVGLDJUDP WRVKRZZKLFKSDW RIWKHGLDJUD UHSUHVHQWVWKH'UW XFNDQGZKLFK P UHSUHVHQWVWKH7XU NH\

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 12

H[HUFLVHDQVZHUV

'XFN0DJQHWV $QVZHU

1RWHWKHFODVVD SWHUXVHV PXOWLSOHLQKHULWDQGD FDQ·WGRLWLQ-DYDFHVR\RX 'XFNFODVV

7XUNH\FODVV

&ODVV$GDSWHU 7DUJHW

&OLHQW UHTXHVW

&OLHQWWKLQNVKH·V WDONLQJWRD'XFN

$GDSWHH VSHFLILF5HTXHVW

GRHVQRW XUNH\FODVVHWKRGVDV KH W 7 H V K L W 7 H J U DPHP 7KH7D VV7KLV KDYHWKHVWWKH$GDSWHUFDQ X 'XFNFOD KHFOLHQW E  N F X ' GFDOOV $GDSWHU LVZKDWW HWKRGVRQ XFNPHWKRDQGLQYRNH ' H N D W UHTXHVW G P Q URX LQYRNHV DQGWXUQDRQWKH7XUNH\ V G R K 7KH$GDSWHUOHWVWKH W H P WRUHTXHVWVRQD'XFN 7XUNH\UHVSRQG E \H [W HQG LQJ  %27+FODVVHV 'XFNDQ G7XUNH\ 

'XFNLQWHUIDFH 2EMHFW$GDSWHU &OLHQW

&OLHQWWKLQNVKH·V WDONLQJWRD'XFN

LQWHUIDFH!! 7DUJHW UHTXHVW

SWHU &ODVV$GXDFN K LW Z V D -XVW JHWLVWKH' W WKH7DU LVLVZKDWWKHFOLHQ FODVV7K HWKRGVRQ $GDSWHU LQYRNHVP UHTXHVW

7KH$GDSWHULPSOHPHQW LQWHUIDFHEXWZKHQ VWKH'XFN JHWVD PHWKRGFDOOLWWXUQVDLWURX GHOHJDWHVWKHFDOOVWRD QGDQG 7XUNH\ 

7KH7XUNH\FODVVGRHVQ LQWHUIDFHDVWKH'XFN ·WKDYHWKHVDPH ,QRWKHUZRUGV 7XUNH\VGRQ·WKDYHTXD FN PHWKRGVHWF

$GDSWHH

7XUNH\ REMHFW

VSHFLILF5HTXHVW

KH7XUNH\ $GDSWHUWKDWWKH KH W R W V QN 7KD H LOOJHWFDOOVW $GDSWHH HVZ RQWKH'XFNLQWHUIDF FOLHQWPDN

&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 13

WKHDGDSWHUSDWWHUQ


7JRMK\)LIX\MZ *MKI][M1][MKWUXW[Q\QWV1¼^MOW\ITMO]X1KIV VW\WVTaILIX\IVILIX\MMKTI[[J]\IVaWN Q\[ []JKTI[[M[

+TI[[)LIX\MZ


1VUaXIZ\WN \PM_WZTL_MTQSM\W][M KWUXW[Q\QWVW^MZQVPMZQ\IVKM#aW]UIaJM [I^QVOINM_TQVM[WN KWLMJ]\ITT1¼ULWQVOQ[ _ZQ\QVOITQ\\TMKWLM\WLMTMOI\M\W\PMILIX\MM ?MTQSM\WSMMX\PQVO[ÆM`QJTM .TM`QJTMUIaJMMNÅKQMV\'6W=[QVOIKTI[[ ILIX\MZ\PMZMQ[R][\WVMWN UMVW\IVILIX\MZ IVLIVILIX\MM AW]¼ZM_WZZQMLIJW]\WVMTQ\\TMWJRMK\'AW] UQOP\JMIJTM\WY]QKSTaW^MZZQLMIUM\PWL J]\IVaJMPI^QWZ1ILL\WUaILIX\MZKWLM _WZS[_Q\PUaILIX\MMKTI[[IVLITTQ\[ []JKTI[[M[ 0MaKWUMWVK]\UMIJZMIS1R][\VMML\W KWUXW[M_Q\P\PM[]JKTI[[\WUISM\PI\_WZS

AMIPJ]\_PI\QN I[]JKTI[[WN ILIX\MMILL[ [WUMVM_JMPI^QWZ
;W]VL[UM[[a AW]_IVVI[MMUM[[a'4WWSQV\PMUQZZWZ

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 14

UHDOZRUOGDGDSWHUV

:MIT_WZTLILIX\MZ[ 4M\¼[\ISMITWWSI\\PM][MWN I[QUXTM)LIX\MZQV\PMZMIT_WZTL [WUM\PQVOUWZM[MZQW][\PIV,]KS[I\TMI[\

7TL_WZTL-V]UMZI\WZ[ 1N aW]¼^MJMMVIZW]VL2I^INWZI_PQTM aW]XZWJIJTaZMUMUJMZ\PI\\PMMIZTa KWTTMK\QWV[\aXM[>MK\WZ;\IKS0I[P\IJTM IVLINM_W\PMZ[QUXTMUMV\IUM\PWL MTMUMV\[_PQKPZM\]ZV[IV-V]UMZI\QWV
LRQKDVD (QXPHUDW LQWHUIDFH!! (QXPHUDWLRQ KDV0RUH(OHPHQWV

7HOOV\RXLIWKHUHDUHDQ\ Q PRUHHOHPHQWVLQWKHFROOHFWLR

QH[W(OHPHQW

*LYHV\RXWKHQH[WHOHPHQW LQWKHFROOHFWLRQ

6M__WZTL1\MZI\WZ[ ?PMV;]VZMTMI[ML\PMQZUWZMZMKMV\ +WTTMK\QWV[KTI[[M[\PMaJMOIV][QVOIV 1\MZI\WZQV\MZNIKM\PI\TQSM-V]UMZI\QWV ITTW_[aW]\WQ\MZI\M\PZW]OPI[M\WN Q\MU[ QVIKWTTMK\QWVJ]\IT[WILL[\PMIJQTQ\a\W ZMUW^MQ\MU[

HUIDFH VLPSOHLQW

LQWHUIDFH! ,WHUDWRU KDV1H[W QH[W UHPRYH

 $QDORJRXVWRKDV0RUH(OHPHQWV  DFH HUI LQW WLRQ HUD LQWKH(QXP 7KLVPHWKRGMXVWWHOOV\RXLIVLQ \RX·YHORRNHGDWDOOWKHLWHP WKHFROOHFWLRQ *LYHV\RXWKHQH[W HOHPHQWLQWKHFROOHFWLRQ 5HPRYHVDQLWHP IURPWKHFROOHFWLRQ

)VL\WLIa ?MIZMWN\MVNIKML_Q\PTMOIKaKWLM\PI\M`XW[M[\PM -V]UMZI\WZQV\MZNIKMaM\_M¼LTQSMNWZW]ZVM_KWLM\WWVTa ][M1\MZI\WZ[1\TWWS[TQSM_MVMML\WJ]QTLIVILIX\MZ



&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 15

WKHDGDSWHUSDWWHUQ

)LIX\QVOIV-V]UMZI\QWV\WIV1\MZI\WZ .QZ[\_M¼TTTWWSI\\PM\_WQV\MZNIKM[\WÅO]ZMW]\PW_\PMUM\PWL[UIXNZWU WVM\W\PMW\PMZ1VW\PMZ_WZL[_M¼TTÅO]ZMW]\_PI\\WKITTWV\PMILIX\MM _PMV\PMKTQMV\QV^WSM[IUM\PWLWV\PM\IZOM\

7KHVHWZRPHWKRGVORRNHDV\ WKH\PDSVWUDLJKWWRKDV1H[W  DQGQH[W LQ,WHUDWRU

7DUJHWLQWHUIDFH LQWHUIDFH!! ,WHUDWRU

LQWHUIDFH!! (QXPHUDWLRQ KDV0RUH(OHPHQWV QH[W(OHPHQW

KDV1H[W QH[W UHPRYH

$GDSWHHLQWHUIDFH

%XWZKDWDERXWWKLVPHWKRG UHPRYH LQ,WHUDWRU"7KHUH·V QRWKLQJOLNHWKDWLQ(QXPHUDWLRQ

,M[QOVQVO\PM)LIX\MZ 0MZM¼[_PI\\PMKTI[[M[[PW]TLTWWSTQSM"_MVMMLIVILIX\MZ\PI\QUXTMUMV\[ \PM

(QXPHUDWLRQ,WHUDWRU LVWKHDGDSWHU

LQWHUIDFH!! ,WHUDWRU KDV1H[W QH[W UHPRYH

(QXPHUDWLRQ,WHUDWRU KDV1H[W QH[W UHPRYH

:H·UHPDNLQJWKH(QXPHUDWLRQV LQ\RXUROGFRGHORRNOLNH ,WHUDWRUVIRU\RXUQHZFRGH

LQWHUIDFH!!

$FODVV LPSOHPHQWLQJ WKH(QXPHUDWLRQ LQWHUIDFHLVWKH DGDSWHH

(QXPHUDWLRQ

KDV0RUH(OHPHQWV QH[W(OHPHQW

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 16

HQXPHUDWLRQLWHUDWRUDGDSWHU

,MITQVO_Q\P\PMZMUW^MUM\PWL ?MTT_MSVW_-V]UMZI\QWVR][\LWM[V¼\[]XXWZ\ZMUW^M1\¼[I¹ZMILWVTaºQV\MZNIKM
?ZQ\QVO\PM-V]UMZI\QWV1\MZI\WZILIX\MZ 0MZM¼[[QUXTMJ]\MNNMK\Q^MKWLMNWZITT\PW[MTMOIKaKTI[[M[[\QTTXZWL]KQVO-V]UMZI\QWV["

SXEOLFFODVV(QXPHUDWLRQ,WHUDWRULPSOHPHQWV,WHUDWRU ^ (QXPHUDWLRQHQXP  SXEOLF(QXPHUDWLRQ,WHUDWRU (QXPHUDWLRQHQXP ^ WKLVHQXP HQXP `  SXEOLFERROHDQKDV1H[W ^ UHWXUQHQXPKDV0RUH(OHPHQWV  `  SXEOLF2EMHFWQH[W ^ UHWXUQHQXPQH[W(OHPHQW  `  SXEOLFYRLGUHPRYH ^ WKURZQHZ8QVXSSRUWHG2SHUDWLRQ([FHSWLRQ  ` `



6LQFHZH·UHDGDSWLQJ(QXPHUDWLRQ WR,WHUDWRURXU$GDSWHU LPSOHPHQWVWKH,WHUDWRULQWHUIDFH LWKDVWRORRNOLNHDQ,WHUDWRU

7KH(QXPHUDWLRQZH·UHDGDSWLQJ :H·UHXVLQJFRPSRVLWLRQVRZHVWDVK LWLQDQLQVWDQFHYDULDEOH 7KH,WHUDWRU·VKDV1H[W PHWKRG LVGHOHJDWHGWRWKH(QXPHUDWLRQ·V KDV0RUH(OHPHQWV PHWKRG DQGWKH,WHUDWRU·VQH[W PHWKRG LVGHOHJDWHGWRWKH(QXPHUDWLRQV·V QH[W(OHPHQW PHWKRG 8QIRUWXQDWHO\ZHFDQ·WVXSSRUW ,WHUDWRU·VUHPRYH PHWKRGVR ZHKDYHWRSXQW LQRWKHUZRUGV ZHJLYHXS +HUHZHMXVWWKURZ DQH[FHSWLRQ

&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 17

WKHDGDSWHUSDWWHUQ

([HUFLVH

?PQTM2I^IPI[OWVM\PMLQZMK\QWVWN \PM1\MZI\WZ\PMZMQ[VM^MZ\PMTM[[ITW\WN  TMOIKaKTQMV\KWLM\PI\LMXMVL[WV\PM-V]UMZI\QWVQV\MZNIKM[WIV)LIX\MZ \PI\KWV^MZ\[IV1\MZI\WZ\WIV-V]UMZI\QWVQ[IT[WY]Q\M][MN]T ?ZQ\MIV)LIX\MZ\PI\ILIX\[IV1\MZI\WZ\WIV-V]UMZI\QWVAW]KIV\M[\aW]Z KWLMJaILIX\QVOIV)ZZIa4Q[\
YiX`e gfn\i 6RPH$&DGDSWHUVGRPRUHWKDQMXVWFKDQJHWKHLQWHUIDFH±WKH\DGGRWKHUIHDWXUHVOLNHVXUJH SURWHFWLRQLQGLFDWRUOLJKWVDQGRWKHUEHOOVDQGZKLVWOHV ,I\RXZHUHJRLQJWRLPSOHPHQWWKHVHNLQGVRIIHDWXUHVZKDWSDWWHUQZRXOG\RXXVH"

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 18

¿UHVLGHFKDWVGHFRUDWRUDQGDGDSWHU


,MKWZI\WZ

)LIX\MZ

1¼UQUXWZ\IV\5aRWJQ[ITTIJW]\ZM[XWV[QJQTQ\a· aW]SVW_\PI\_PMVI,MKWZI\WZQ[QV^WT^ML\PMZM¼[ OWQVO\WJM[WUMVM_ZM[XWV[QJQTQ\QM[WZJMPI^QWZ[ ILLML\WaW]ZLM[QOV AW]O]a[_IV\ITT\PMOTWZa_PQTM][ILIX\MZ[ IZMLW_VQV\PM\ZMVKPM[LWQVO\PMLQZ\a_WZS" KWV^MZ\QVOQV\MZNIKM[7]ZRWJ[UIaVW\JM OTIUWZW][J]\W]ZKTQMV\[[]ZMLWIXXZMKQI\M ][UISQVO\PMQZTQ^M[[QUXTMZ
&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 19

WKHDGDSWHUSDWWHUQ

,MKWZI\WZ

)LIX\MZ *]\\PMOZMI\\PQVOIJW]\][ILIX\MZ[Q[\PI\_M ITTW_KTQMV\[\WUISM][MWN VM_TQJZIZQM[IVL []J[M\[_Q\PW]\KPIVOQVOIVaKWLM\PMaR][\ZMTa WV][\WLW\PMKWV^MZ[QWVNWZ\PMU0MaQ\¼[I VQKPMJ]\_M¼ZMOWWLI\Q\

?MTT][LMKWZI\WZ[LW\PI\I[_MTTWVTa_MITTW_ VM_JMPI^QWZ\WJMILLML\WKTI[[M[_Q\PW]\IT\MZQVO M`Q[\QVOKWLM1[\QTT[Ia\PI\ILIX\MZ[IZMR][\NIVKa LMKWZI\WZ[·1UMIVR][\TQSM][aW]_ZIXIVWJRMK\ 6WVWVWVW\I\ITT?MIT_Ia[KWV^MZ\\PM QV\MZNIKMWN _PI\_M_ZIXaW]VM^MZLW1¼L [IaILMKWZI\WZQ[TQSMIVILIX\MZ#Q\Q[R][\\PI\ aW]LWV¼\KPIVOM\PMQV\MZNIKM =PVW7]ZRWJQVTQNMQ[\WM`\MVL\PM JMPI^QWZ[WZZM[XWV[QJQTQ\QM[WN \PMWJRMK\[_M _ZIX_MIZMV¼\I[QUXTMXI[[\PZW]OP 0Ma_PWIZMaW]KITTQVOI[QUXTMXI[[ \PZW]OP'+WUMWVLW_VIVL_M¼TT[MMPW_ TWVOaW]TI[\KWV^MZ\QVOINM_QV\MZNIKM[ 5IaJM_M[PW]TLIOZMM\WLQ[IOZMM?M[MMU \WTWWS[WUM_PI\[QUQTIZWVXIXMZJ]\KTMIZTa _MIZMUQTM[I_IaQVW]ZQV\MV\ 7PaMIP1¼U_Q\PaW]\PMZM

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 20

ZKRGRHVZKDW"

)VLVW_NWZ[WUM\PQVOLQNNMZMV\ 7KHUH·VDQRWKHUSDWWHUQLQWKLVFKDSWHU AW]¼^M[MMVPW_\PM)LIX\MZ8I\\MZVKWV^MZ\[\PMQV\MZNIKMWN IKTI[[QV\W WVM\PI\IKTQMV\Q[M`XMK\QVOAW]IT[WSVW__MIKPQM^M\PQ[QV2I^IJa _ZIXXQVO\PMWJRMK\\PI\PI[IVQVKWUXI\QJTMQV\MZNIKM_Q\PIVWJRMK\\PI\ QUXTMUMV\[\PMKWZZMK\WVM ?M¼ZMOWQVO\WTWWSI\IXI\\MZVVW_\PI\IT\MZ[IVQV\MZNIKMJ]\NWZI LQNNMZMV\ZMI[WV"\W[QUXTQNa\PMQV\MZNIKM1\¼[IX\TaVIUML\PM.IKILM 8I\\MZVJMKI][M\PQ[XI\\MZVPQLM[ITT\PMKWUXTM`Q\aWN WVMWZUWZM KTI[[M[JMPQVLIKTMIV_MTTTQ\NIKILM

5I\KPMIKPXI\\MZV_Q\PQ\[QV\MV\"

3DWWHUQ

,QWHQW

Decorator

Converts one interface to another

Adapter

DoesnÕt alter the interface, but adds responsibility

Facade Makes an interface simpler



&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 21

WKHDGDSWHUSDWWHUQ

0 WUM;_MM\0WUM,XTIaMZIXZWRMK\QWV^QLMW[a[\MUIV I]\WUI\ML[KZMMV[]ZZW]VL[W]VLIVLM^MVIXWXKWZVXWXXMZ +PMKSW]\ITT\PMKWUXWVMV\[aW]¼^MX]\\WOM\PMZ"

$PSOLILHU

7XQHU DPSOLILHU

RQ RII VHW$P VHW)P VHW)UHTXHQF\

WXQHU GYG3OD\HU FG3OD\HU RQ RII VHW&G VHW'YG VHW6WHUHR6RXQG VHW6XUURXQG6RXG VHW7XQHU VHW9ROXPH

&G3OD\HU DPSOLILHU

'YG3OD\HU DPSOLILHU RQ RII HMHFW SDXVH SOD\ SOD\ VHW6XUURXQG$XGLR VHW7ZR&KDQQHO$XGLR VWRS

RQ RII HMHFW SDXVH SOD\ SOD\ VWRS

6FUHHQ XS GRZQ

7KDW·VDORWRI FODVVHVDORW RILQWHUDFWLRQV DQGDELJVHWRI LQWHUIDFHVWR OHDUQDQGXVH

3URMHFWRU GYG3OD\HU

3RSFRUQ3RSSHU RQ RII SRS

7KHDWHU/LJKWV

RQ RII WY0RGH ZLGH6FUHHQ0RGH

RQ RII GLP

AW]¼^M[XMV\_MMS[Z]VVQVO_QZMUW]V\QVO\PMXZWRMK\WZUISQVOITT \PMKWVVMK\QWV[IVLÅVM\]VQVO6W_Q\¼[\QUM\WX]\Q\ITTQVUW\QWV IVLMVRWaIUW^QM

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Page 22

Return to Table of Contents

WDVNVWRZDWFKDPRYLH

?I\KPQVOIUW^QM\PMPIZL_Ia 3LFNRXWD'9'UHOD[DQGJHWUHDG\IRUPRYLHPDJLF2K WKHUH·VMXVWRQHWKLQJ²WRZDWFKWKHPRYLH\RXQHHGWR SHUIRUPDIHZWDVNV 

<]ZVWV\PMXWXKWZVXWXXMZ



;\IZ\\PMXWXXMZXWXXQVO



,QU\PMTQOP\[



8]\\PM[KZMMVLW_V



<]ZV\PMXZWRMK\WZWV



;M\\PMXZWRMK\WZQVX]\\W,>,



8]\\PMXZWRMK\WZWV_QLM[KZMMVUWLM <]ZV\PM[W]VLIUXTQÅMZWV ;M\\PMIUXTQÅMZ\W,>,QVX]\

!





;M\\PMIUXTQÅMZ\W[]ZZW]VL[W]VL



;M\\PMIUXTQÅMZ^WT]UM\WUMLQ]U



<]ZV\PM,>,8TIaMZWV



;\IZ\\PM,>,8TIaMZXTIaQVO

,·PDOUHDG\H[KDXVWHG DQGDOO,·YHGRQHLVWXUQ HYHU\WKLQJRQ

&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 23

WKHDGDSWHUSDWWHUQ

/HW·VFKHFNRXWWKRVHVDPHWDVNVLQWHUPVRIWKHFODVVHVDQGWKH PHWKRGFDOOVQHHGHGWRSHUIRUPWKHP

UW

7XUQRQWKHSRSFRUQSRSSHUDQGVWD SRSSLQJ SRSSHURQ  SRSSHUSRS  OLJKWVGLP  

DVVHV 6L[GLIIHUHQWFO  HG OY YR LQ

VFUHHQGRZQ  SURMHFWRURQ  SURMHFWRUVHW,QSXW GYG  SURMHFWRUZLGH6FUHHQ0RGH  DPSRQ  DPSVHW'YG GYG  DPSVHW6XUURXQG6RXQG  DPSVHW9ROXPH   GYGRQ  GYGSOD\ PRYLH 

'LPWKHOLJKWVWR 3XWWKHVFUHHQGRZQ LWLQ 7XUQRQWKHSURMHFWRUDQGSXW  RYLH KHP RUW GHI PR UHHQ HVF ZLG 9'SXW 7XUQRQWKHDPSVHWLWWR' VHWWKH QG GHD PR XQG LWLQVXUURXQGVR YROXPHWR  7XUQRQWKH'9'SOD\HU RYLH HP \WK SOD DQG),1$//<

%XWWKHUH·VPRUH

ƒ?PMV\PMUW^QMQ[W^MZPW_LWaW]\]ZVM^MZa\PQVOWNN ' ?W]TLV¼\aW]PI^M\WLWITTWN \PQ[W^MZIOIQVQVZM^MZ[M'

ƒ?W]TLV¼\Q\JMI[KWUXTM`\WTQ[\MV\WI+,WZ\PMZILQW' ƒ1N aW]LMKQLM\W]XOZILMaW]Z[a[\MUaW]¼ZMXZWJIJTaOWQVO \WPI^M\WTMIZVI[TQOP\TaLQNNMZMV\XZWKML]ZM ;W_PI\\WLW'
\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 24

OLJKWVFDPHUDIDFDGH

4QOP\[+IUMZI.IKILM  ).IKILMQ[R][\_PI\aW]VMML"_Q\P\PM.IKILM8I\\MZVaW]KIV\ISMIKWUXTM` []J[a[\MUIVLUISMQ\MI[QMZ\W][MJaQUXTMUMV\QVOI.IKILMKTI[[\PI\ XZW^QLM[WVMUWZMZMI[WVIJTMQV\MZNIKM,WV¼\_WZZa#QN aW]VMML\PMXW_MZ WN \PMKWUXTM`[]J[a[\MUQ\¼[[\QTT\PMZMNWZaW]\W][MJ]\QN ITTaW]VMMLQ[I [\ZIQOP\NWZ_IZLQV\MZNIKM\PM.IKILMQ[\PMZMNWZaW] 4M\¼[\ISMITWWSI\PW_\PM.IKILMWXMZI\M["



/KAY TIMETOCREATEA &ACADEFORTHEHOME THEATERSYSTEM4ODO SS THISWECREATEANEWCLA E CAD R&A ATE 4HE ME (O WHICHEXPOSESAFEW S SIMPLEMETHODSSUCHA   VIE -O WATCH

 4HE&ACADECLASSTREATS

THEHOMETHEATER COMPONENTSASA SUBSYSTEM ANDCALLS ONTHESUBSYSTEM TOIMPLEMENTITS WATCH-OVIE METHOD

7KH)DFDGH +RPH7KHDWHU)DFDGH ZDWFK0RYLH HQG0RYLH OLVWHQ7R&G HQG&G OLVWHQ7R5DGLR HQG5DGLR

$PSOLILHU WXQHU GYG3OD\HU FG3OD\HU RQ RII

7XQHU

VHW&G

'YG3OD\HU

VHW'YG

DPSOLILHU

VHW6WHUHR6RXQG

RQ

DPSOLILHU

VHW6XUURXQG6RXG

RII

VHW7XQHU

VHW$P

RII

VHW9ROXPH

VHW)P

RQ HMHFW

SOD\

SDXVH

VHW)UHTXHQF\

SOD\ SOD\

&G3OD\HU DPSOLILHU

6FUHHQ

VHW6XUURXQG$XGLR VHW7ZR&KDQQHO$XGLR VWRS

RQ

XS

RII

GRZQ

HMHFW SDXVH

KH 7KHVXEV\VWHPSOWLI\LQJ VLP LV H DG DF )

SOD\ SOD\

3URMHFWRU

VWRS GYG3OD\HU RQ

3RSFRUQ3RSSHU

RII WY0RGH

RQ RII

7KHDWHU/LJKWV

SRS RQ RII

ZLGH6FUHHQ0RGH

RQ

GLP



&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 25

WKHDGDSWHUSDWWHUQ

ZDWFK0

$FOLHQWRIWKH VXEV\VWHPIDFDGH

RYLH



LS 9OURCLIENTCODENOWCAL THEATER METHODSONTHEHOME SYSTEM &ACADE NOTONTHESUB EWEJUST OVI AM TCH 3ONOWTOWA -OVIE CALLONEMETHOD WATCH ITHTHE ANDITCOMMUNICATESW CTOR OJE PR YER LIGHTS $6$PLA OPCORN AMPLIFIER SCREEN ANDP MAKERFORUS

,·YHJRWWRKDYHP\ ORZOHYHODFFHVV



 )RUPHUO\SUHVLGHQWRI RO FKR K6 +LJ UH PR XVK WKH5 E &OX $96FLHQFH

4HE&ACADESTILLLEAVESTHESUBSYSTEM ACCESSIBLETOBEUSEDDIREC TLY)FYOU NEEDTHEADVANCEDFUNCTIONALITY OFTHESUBSYSTEMCLASSES THEYARE AVAILABLEFORYOURUSE

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 26

IDFDGHYHUVXVDGDSWHU

there are no

Dumb Questions

Q:

Q:

A:

A:

Q:

Q:

,IWKH)DFDGHHQFDSVXODWHVWKH VXEV\VWHPFODVVHVKRZGRHVDFOLHQW WKDWQHHGVORZHUOHYHOIXQFWLRQDOLW\JDLQ DFFHVVWRWKHP"

)DFDGHVGRQ·W´HQFDSVXODWHµWKH VXEV\VWHPFODVVHVWKH\PHUHO\SURYLGHD VLPSOLILHGLQWHUIDFHWRWKHLUIXQFWLRQDOLW\7KH VXEV\VWHPFODVVHVVWLOOUHPDLQDYDLODEOH IRUGLUHFWXVHE\FOLHQWVWKDWQHHGWRXVH PRUHVSHFLILFLQWHUIDFHV7KLVLVDQLFH SURSHUW\RIWKH)DFDGH3DWWHUQLWSURYLGHV DVLPSOLILHGLQWHUIDFHZKLOHVWLOOH[SRVLQJWKH IXOOIXQFWLRQDOLW\RIWKHV\VWHPWRWKRVHZKR PD\QHHGLW 'RHVWKHIDFDGHDGGDQ\ IXQFWLRQDOLW\RUGRHVLWMXVWSDVVWKURXJK HDFKUHTXHVWWRWKHVXEV\VWHP"

A:

$IDFDGHLVIUHHWRDGGLWVRZQ ´VPDUWVµLQDGGLWLRQWRPDNLQJXVHRIWKH VXEV\VWHP)RULQVWDQFHZKLOHRXUKRPH WKHDWHUIDFDGHGRHVQ·WLPSOHPHQWDQ\QHZ EHKDYLRULWLVVPDUWHQRXJKWRNQRZWKDWWKH SRSFRUQSRSSHUKDVWREHWXUQHGRQEHIRUHLW FDQSRS DVZHOODVWKHGHWDLOVRIKRZWRWXUQ RQDQGVWDJHDPRYLHVKRZLQJ 

Q:

'RHVHDFKVXEV\VWHPKDYHRQO\ RQHIDFDGH"

A:

1RWQHFHVVDULO\7KHSDWWHUQ FHUWDLQO\DOORZVIRUDQ\QXPEHURIIDFDGHVWR EHFUHDWHGIRUDJLYHQVXEV\VWHP



:KDWLVWKHEHQHILWRIWKHIDFDGH RWKHUWKDQWKHIDFWWKDW,QRZKDYHD VLPSOHULQWHUIDFH"

7KH)DFDGH3DWWHUQDOVRDOORZV \RXWRGHFRXSOH\RXUFOLHQWLPSOHPHQWDWLRQ IURPDQ\RQHVXEV\VWHP/HW·VVD\IRU LQVWDQFHWKDW\RXJHWDELJUDLVHDQGGHFLGH WRXSJUDGH\RXUKRPHWKHDWHUWRDOOQHZ FRPSRQHQWVWKDWKDYHGLIIHUHQWLQWHUIDFHV :HOOLI\RXFRGHG\RXUFOLHQWWRWKHIDFDGH UDWKHUWKDQWKHVXEV\VWHP\RXUFOLHQWFRGH GRHVQ·WQHHGWRFKDQJHMXVWWKHIDFDGH DQGKRSHIXOO\WKHPDQXIDFWXUHULVVXSSO\LQJ WKDW  6RWKHZD\WRWHOOWKHGLIIHUHQFH EHWZHHQWKH$GDSWHU3DWWHUQDQGWKH )DFDGH3DWWHUQLVWKDWWKHDGDSWHUZUDSV RQHFODVVDQGWKHIDFDGHPD\UHSUHVHQW PDQ\FODVVHV"

A:

1R5HPHPEHUWKH$GDSWHU3DWWHUQ FKDQJHVWKHLQWHUIDFHRIRQHRUPRUH FODVVHVLQWRRQHLQWHUIDFHWKDWDFOLHQWLV H[SHFWLQJ:KLOHPRVWWH[WERRNH[DPSOHV VKRZWKHDGDSWHUDGDSWLQJRQHFODVV\RX PD\QHHGWRDGDSWPDQ\FODVVHVWRSURYLGH WKHLQWHUIDFHDFOLHQWLVFRGHGWR/LNHZLVH D)DFDGHPD\SURYLGHDVLPSOLILHGLQWHUIDFH WRDVLQJOHFODVVZLWKDYHU\FRPSOH[ LQWHUIDFH 7KHGLIIHUHQFHEHWZHHQWKHWZRLVQRWLQ WHUPVRIKRZPDQ\FODVVHVWKH\´ZUDSµLW LVLQWKHLULQWHQW7KHLQWHQWRIWKH$GDSWHU 3DWWHUQLVWRDOWHUDQLQWHUIDFHVRWKDWLW PDWFKHVRQHDFOLHQWLVH[SHFWLQJ7KH LQWHQWRIWKH)DFDGH3DWWHUQLVWRSURYLGHD VLPSOLILHGLQWHUIDFHWRDVXEV\VWHP

$IDFDGHQRW RQO\VLPSOLILHV DQLQWHUIDFHLW GHFRXSOHVDFOLHQW IURPDVXEV\VWHP RIFRPSRQHQWV )DFDGHVDQG DGDSWHUVPD\ ZUDSPXOWLSOH FODVVHVEXWD IDFDGH·VLQWHQWLV WRVLPSOLI\ZKLOH DQDGDSWHU·V LVWRFRQYHUW WKHLQWHUIDFH WRVRPHWKLQJ GLIIHUHQW

&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Page 27

Return to Table of Contents

WKHDGDSWHUSDWWHUQ

+WV[\Z]K\QVOaW]ZPWUM\PMI\MZNIKILM 4M\¼[[\MX\PZW]OP\PMKWV[\Z]K\QWVWN \PM0WUM
SXEOLFFODVV+RPH7KHDWHU)DFDGH^ $PSOL¿HUDPS +HUH·VWKHFRPSRVLWLRQWKHVH 7XQHUWXQHU DUHDOOWKHFRPSRQHQWVRIWKH 'YG3OD\HUGYG VXEV\VWHPZHDUHJRLQJWRXVH &G3OD\HUFG 3URMHFWRUSURMHFWRU 7KHDWHU/LJKWVOLJKWV 6FUHHQVFUHHQ 3RSFRUQ3RSSHUSRSSHU  SXEOLF+RPH7KHDWHU)DFDGH $PSOL¿HUDPS 7XQHUWXQHU 'YG3OD\HUGYG &G3OD\HUFG 3URMHFWRUSURMHFWRU 7KHIDFDGHLVSDVVHGD 6FUHHQVFUHHQ UHIHUHQFHWRHDFKFRPSRQHQW 7KHDWHU/LJKWVOLJKWV RIWKHVXEV\VWHPLQLWV 3RSFRUQ3RSSHUSRSSHU ^ FRQVWUXFWRU7KHIDFDGH  WKLVDPS DPS WKHQDVVLJQVHDFKWRWKH WKLVWXQHU WXQHU FRUUHVSRQGLQJLQVWDQFHYDULDEOH WKLVGYG GYG WKLVFG FG WKLVSURMHFWRU SURMHFWRU WKLVVFUHHQ VFUHHQ WKLVOLJKWV OLJKWV WKLVSRSSHU SRSSHU `  RWKHUPHWKRGVKHUH :H·UHMXVWDERXWWRIL  OOWKHVHLQ `

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 28

LPSOHPHQWLQJIDFDGH

1UXTMUMV\QVO\PM[QUXTQNQMLQV\MZNIKM 6W_Q\¼[\QUM\WJZQVO\PMKWUXWVMV\[WN \PM[]J[a[\MU\WOM\PMZQV\WI]VQÅMLQV\MZNIKM 4M\¼[QUXTMUMV\\PM_I\KP5W^QMIVLMVL5W^QMUM\PWL["

SXEOLFYRLGZDWFK0RYLH 6WULQJPRYLH ^ 6\VWHPRXWSULQWOQ ³*HWUHDG\WRZDWFKDPRYLH´  SRSSHURQ  SRSSHUSRS  HVHTXHQFH OLJKWVGLP   ZDWFK0RYLH IROORZVWKHVDPEX WZUDSV RUH EHI VFUHHQGRZQ  QG KD ZHKDGWRGRE\ DOO RHV WG SURMHFWRURQ  WKD LWXSLQDKDQG\PHWKRG HDFKWDVNZH SURMHFWRUZLGH6FUHHQ0RGH  IRU DW WK WLFH 1R UN WKHZR DPSRQ  LW\WRWKH DUHGHOHJDWLQJWKHUHVSRQVLELOWKH DPSVHW'YG GYG  LQ VXEV\VWHP HQW SRQ FRP LQJ RQG UHVS FRU DPSVHW6XUURXQG6RXQG  DPSVHW9ROXPH   GYGRQ  GYGSOD\ PRYLH  `  SXEOLFYRLGHQG0RYLH ^ 6\VWHPRXWSULQWOQ ³6KXWWLQJPRYLHWKHDWHUGRZQ´  SRSSHURII  OLJKWVRQ  VFUHHQXS  SURMHFWRURII  $QGHQG0RYLH WDNHVFDUH DPSRII  RIVKXWWLQJHYHU\WKLQJGRZQ GYGVWRS  IRUXV$JDLQHDFKWDVNLV GYGHMHFW  GHOHJDWHGWRWKHDSSURSULDWH GYGRII  FRPS RQHQWLQWKHVXEV\VWHP `

YiX`e gfn\i 7KLQNDERXWWKHIDFDGHV\RX¶YHHQFRXQWHUHGLQWKH-DYD$3, :KHUHZRXOG\RXOLNHWRKDYHDIHZQHZRQHV"



&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 29

WKHDGDSWHUSDWWHUQ


+HUHZH·UHFUHDWLQJWKHFRPSRQHQWV

SXEOLFFODVV+RPH7KHDWHU7HVW'ULYH^ ULJKWLQWKHWHVWGULYH1RUPDOO\WKH SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ FOLHQWLVJLYHQDIDFDGHLWGRHVQ·WKDYH LQVWDQWLDWHFRPSRQHQWVKHUH XFWRQHLWVHOI WRFRQVWU  +RPH7KHDWHU)DFDGHKRPH7KHDWHU  QHZ+RPH7KHDWHU)DFDGH DPSWXQHUGYGFG )LUVW\RXLQVWDQWLDWH SURMHFWRUVFUHHQOLJKWVSRSSHU  WKH)DFDGHZLWKDOOWKH  FRPSRQHQWVLQWKHVXEV\VWHP KRPH7KHDWHUZDWFK0RYLH ³5DLGHUVRIWKH/RVW$UN´  KRPH7KHDWHUHQG0RYLH  8VHWKHVLPSOLILHGLQWHUIDFHWR ` ILUVWVWDUWWKHPRYLHXSDQG `

WKHQVKXWLWGRZQ

)LOH(GLW:LQGRZ+HOS6QDNHV:K\¶G,W+DYH7R%H6QDNHV"

+HUH·VWKHRXWSXW &DOOLQJWKH)DFDGH·V ZDWFK0RYLH GRHVDOO WKLVZRUNIRUXV

DQGKHUHZH·UHGRQH ZDWFKLQJWKHPRYLHVR FDOOLQJHQG0RYLH WXUQV HYHU\WKLQJRII

MDYD+RPH7KHDWHU7HVW'ULYH *HWUHDG\WRZDWFKDPRYLH 3RSFRUQ3RSSHURQ 3RSFRUQ3RSSHUSRSSLQJSRSFRUQ 7KHDWHU&HLOLQJ/LJKWVGLPPLQJWR 7KHDWHU6FUHHQJRLQJGRZQ 7RS2/LQH3URMHFWRURQ 7RS2/LQH3URMHFWRULQZLGHVFUHHQPRGH [DVSHFWUDWLR 7RS2/LQH$PSOL¿HURQ 7RS2/LQH$PSOL¿HUVHWWLQJ'9'SOD\HUWR7RS2/LQH'9'3OD\HU 7RS2/LQH$PSOL¿HUVXUURXQGVRXQGRQ VSHDNHUVVXEZRRIHU 7RS2/LQH$PSOL¿HUVHWWLQJYROXPHWR 7RS2/LQH'9'3OD\HURQ 7RS2/LQH'9'3OD\HUSOD\LQJ³5DLGHUVRIWKH/RVW$UN´ 6KXWWLQJPRYLHWKHDWHUGRZQ 3RSFRUQ3RSSHURII 7KHDWHU&HLOLQJ/LJKWVRQ 7KHDWHU6FUHHQJRLQJXS 7RS2/LQH3URMHFWRURII 7RS2/LQH$PSOL¿HURII 7RS2/LQH'9'3OD\HUVWRSSHG³5DLGHUVRIWKH/RVW$UN´ 7RS2/LQH'9'3OD\HUHMHFW 7RS2/LQH'9'3OD\HURII

 \RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 30

IDFDGHSDWWHUQGH¿QHG

.IKILM8I\\MZVLMNQVML


VH +DSS\FOLHQWZKR H DP HF E VW MX E MR  HDVLHUEHFDXVHRI  WKHIDFDGH

&OLHQW

)DFDGH

8QLILHGLQWHUIDFH WKDWLVHDVLHUWRXVH

SUBSYSTEMCLASSES

XEV\VWHP

0RUHFRPSOH[V




&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 31

WKHDGDSWHUSDWWHUQ


'HVLJQ3ULQFLSOH 3ULQFLSOHRI/HDVW.QRZOHGJH WDONRQO\WR\RXULPPHGLDWHIULHQGV

*]\_PI\LWM[\PQ[UMIVQVZMIT\MZU['1\UMIV[_PMVaW] IZMLM[QOVQVOI[a[\MUNWZIVaWJRMK\JMKIZMN]TWN \PM V]UJMZWN KTI[[M[Q\QV\MZIK\[_Q\PIVLIT[WPW_Q\KWUM[\W QV\MZIK\_Q\P\PW[MKTI[[M[
YiX`e gfn\i

+RZPDQ\FODVVHVLVWKLVFRGHFRXSOHGWR"

SXEOLFÀRDWJHW7HPS ^ UHWXUQVWDWLRQJHW7KHUPRPHWHU JHW7HPSHUDWXUH  `

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 32

SULQFLSOHRIOHDVWNQRZOHGJH

0W_67<\W?QV.ZQMVL[IVL1VÆ]MVKM7JRMK\[ 7SIaJ]\PW_LWaW]SMMXNZWULWQVO\PQ['
ƒ ƒ ƒ ƒ


XVQRW HOLQHVWHOO HUH G XL J H HV K DWZ DWW 1RWLFHWK KRGVRQREMHFWVWK HWKRGV HW P HU P K OO W D R F J R W URPFDOOLQ UHWXUQHGI

WWKDWLV QHQWµDVDQ\REMHF 7KLQNRID´FRPSRLQVWDQFHYDULDEOH,QRWKHU UHIHUHQFHGE\DQ DVD+$6$UHODWLRQVKLS ZRUGVWKLQNRIWKLV


:LWKRXWWKH 3ULQFLSOH

SXEOLFÀRDWJHW7HPS ^ 7KHUPRPHWHUWKHUPRPHWHU VWDWLRQJHW7KHUPRPHWHU  UHWXUQWKHUPRPHWHUJHW7HPSHUDWXUH  `

+HUHZHJHWWKHWKHUPRPHWHUREMHFW IURPWKHVWDWLRQDQGWKHQFDOOWKH JHW7HPSHUDWXUH PHWKRGRXUVHOYHV

:LWKWKH 3ULQFLSOH

SXEOLFÀRDWJHW7HPS ^ UHWXUQVWDWLRQJHW7HPSHUDWXUH  `

:KHQZHDSSO\WKHSULQFLSOHZHDGGD PHWKRGWRWKH6WDWLRQFODVVWKDWPDNHV WKHUHTXHVWWRWKHWKHUPRPHWHUIRUXV 7KLVUHGXFHVWKHQXPEHURIFODVVHVZH·UH GHSHQGHQWRQ 

&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 33

WKHDGDSWHUSDWWHUQ

3MMXQVOaW]ZUM\PWLKITT[QVJW]VL[ 0MZM¼[I+IZKTI[[\PI\LMUWV[\ZI\M[ITT\PM_Ia[aW]KIVKITTUM\PWL[IVL[\QTT ILPMZM\W\PM8ZQVKQXTMWN 4MI[\3VW_TMLOM"

SXEOLFFODVV&DU^  (QJLQHHQJLQH  RWKHULQVWDQFHYDULDEOHV

SRQHQWRI +HUH·VDFRP HFDQFDOO :  WKLVFODVV LWVPHWKRGV

+HUHZH·UHFUHDWLQJDQHZ REMHFWLWVPHWKRGVDUHOHJDO

  

SXEOLF&DU ^  LQLWLDOL]HHQJLQHHWF `

   

SXEOLFYRLGVWDUW .H\NH\ ^  'RRUVGRRUV QHZ'RRUV  

ERROHDQDXWKRUL]HG NH\WXUQV 

     

     `

LI DXWKRUL]HG ^  HQJLQHVWDUW   XSGDWH'DVKERDUG'LVSOD\   GRRUVORFN  `

   `

SXEOLFYRLGXSGDWH'DVKERDUG'LVSOD\ ^  XSGDWHGLVSOD\ `



there are no

Dumb Questions

Q:

7KHUHLVDQRWKHUSULQFLSOHFDOOHG WKH/DZRI'HPHWHUKRZDUHWKH\ UHODWHG"

A:

7KHWZRDUHRQHDQGWKHVDPH DQG\RX·OOHQFRXQWHUWKHVHWHUPVEHLQJ LQWHUPL[HG:HSUHIHUWRXVHWKH3ULQFLSOHRI /HDVW.QRZOHGJHIRUDFRXSOHRIUHDVRQV   WKHQDPHLVPRUHLQWXLWLYHDQG  WKHXVHRI WKHZRUG´/DZµLPSOLHVZHDOZD\VKDYHWR

DSSO\WKLVSULQFLSOH,QIDFWQRSULQFLSOHLVD ODZDOOSULQFLSOHVVKRXOGEHXVHGZKHQDQG ZKHUHWKH\DUHKHOSIXO$OOGHVLJQLQYROYHV WUDGHRIIV DEVWUDFWLRQVYHUVXVVSHHGVSDFH YHUVXVWLPHDQGVRRQ DQGZKLOHSULQFLSOHV SURYLGHJXLGDQFHDOOIDFWRUVVKRXOGEHWDNHQ LQWRDFFRXQWEHIRUHDSSO\LQJWKHP

Q:

$UHWKHUHDQ\GLVDGYDQWDJHV WRDSSO\LQJWKH3ULQFLSOHRI/HDVW .QRZOHGJH"

A:


\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 34

YLRODWLQJWKHSULQFLSOHRIOHDVWNQRZOHGJH

6KDUSHQ \RXU SHQFLO 'RHLWKHURIWKHVHFODVVHVYLRODWHWKH3ULQFLSOHRI/HDVW.QRZOHGJH" :K\RUZK\QRW"

SXEOLF+RXVH^ :HDWKHU6WDWLRQVWDWLRQ RWKHUPHWKRGVDQG FRQVWUXFWRU SXEOLFÀRDWJHW7HPS ^ UHWXUQVWDWLRQJHW7KHUPRPHWHU JHW7HPSHUDWXUH  ` `

SXEOLF+RXVH^ :HDWKHU6WDWLRQVWDWLRQ RWKHUPHWKRGVDQGFRQVWUXFWRU SXEOLFÀRDWJHW7HPS ^ 7KHUPRPHWHUWKHUPRPHWHU VWDWLRQJHW7KHUPRPHWHU  UHWXUQJHW7HPS+HOSHU WKHUPRPHWHU  `  SXEOLFÀRDWJHW7HPS+HOSHU 7KHUPRPHWHUWKHUPRPHWHU ^ UHWXUQWKHUPRPHWHUJHW7HPSHUDWXUH  ` `

+DUGKDWDUHDZDWFKRXW IRUIDOOLQJDVVXPSWLRQV

YiX`e gfn\i &DQ\RXWKLQNRIDFRPPRQXVHRI-DYDWKDWYLRODWHVWKH3ULQFLSOHRI/HDVW.QRZOHGJH" 6KRXOG\RXFDUH" $QVZHU+RZDERXW6\VWHPRXWSULQWOQ "



&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 35

WKHDGDSWHUSDWWHUQ


&OLHQW

7KH+RPH7KHDWHU)DFDGH PDQDJHVDOOWKRVHVXEV\VWHP FRPSRQHQWVIRUWKHFOLHQW ,WNHHSVWKHFOLHQWVLPSOH DQGIOH[LEOH

+RPH7KHDWHU)DFDGH ZDWFK0RYLH HQG0RYLH OLVWHQ7R&G HQG&G OLVWHQ7R5DGLR HQG5DGLR

$PSOLILHU

H DGHWKHKRP :HFDQXSJU SRQHQWVZLWKRXW WKHDWHUFRPKHFOLHQW DIIHFWLQJW

WXQHU GYG3OD\HU FG3OD\HU RQ RII

7XQHU

VHW&G

'YG3OD\HU

VHW'YG

DPSOLILHU

VHW6WHUHR6RXQG

RQ

DPSOLILHU

VHW6XUURXQG6RXG

RII

VHW7XQHU

VHW$P

RII

VHW9ROXPH

VHW)P

RQ HMHFW SDXVH

VHW)UHTXHQF\

SOD\ SOD\ VHW6XUURXQG$XGLR

&G3OD\HU DPSOLILHU

V\VWHPVDGKHULQJ :HWU\WRNHHSVXE/HDVW.QRZOHGJH WRWKH3ULQFLSOHRIHWVWRRFRPSOH[DQG DVZHOO,IWKLVJ UHLQWHUPLQJOLQJZH WRRPDQ\IULHQGVDLWLRQDOIDFDGHVWR FDQLQWURGXFHDGG V\VWHPV IRUPOD\HUVRIVXE

VHW7ZR&KDQQHO$XGLR VWRS

RQ

6FUHHQ XS

RII

GRZQ

HMHFW SDXVH SOD\ SOD\

3URMHFWRU

VWRS GYG3OD\HU RQ

3RSFRUQ3RSSHU

RII WY0RGH

RQ RII

7KHDWHU/LJKWV

SRS

ZLGH6FUHHQ0RGH

RQ RII GLP

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 36

\RXUGHVLJQWRROER[


22%DVLFV

LQWHUIDF 3URJUDPWR V RQ WL WD HQ HP LPSO HGGHVLJQV RRVHO\FRXSO DFW O RU I YH 6WUL HU L DW FWVWK QW EHWZHHQREMH UH[WHQVLRQ IR Q SH GEHR &ODVVHVVKRXO PRGLILFDWLRQ RU EXWFORVHGI 'RQRW EVWUDFWLRQV D 'HSHQGRQ V RQ WL UH QF GHSHQGRQFR GV R\RXUIULHQ 2QO\WDONW

223DWWHLQUHVQDVILQDPHVDLO\RRIQHDOJRWRULWKPPQDDQVO\

H[LVWLQJFODVVDQGLWVLQWHUIDFH LVQRWWKHRQH\RXQHHGXVHDQ DGDSWHU DQGXQLI\DODUJHLQWHUIDFHRU FRPSOH[VHWRILQWHUIDFHVXVHD IDFDGH

ƒ $QDGDSWHUFKDQJHVDQ LQWHUIDFHLQWRRQHDFOLHQW H[SHFWV

ƒ $IDFDGHGHFRXSOHVDFOLHQW LTXH QHZWHFKQ :HKDYHD DLQLQJDORZOHYHO IRUPDLQW JLQRXUGHVLJQV RIFRXSOLQ WDONRQO\WR\RXU UHPHPEHU IULHQGV 

IURPDFRPSOH[VXEV\VWHP

ƒ ,PSOHPHQWLQJDQDGDSWHUPD\ UHTXLUHOLWWOHZRUNRUDJUHDWGHDO RIZRUNGHSHQGLQJRQWKHVL]H DQGFRPSOH[LW\RIWKHWDUJHW LQWHUIDFH

ƒ ,PSOHPHQWLQJDIDFDGHUHTXLUHV

DQG7:2QHZSDWWHUQV (DFKFKDQJHVDQLQWHUIDFH WKHDGDSWHUWRFRQYHUW DQGWKHIDFDGHWRXQLI\ ƒ DQGVLPSOLI\

GVR PLWW WDG YLWGLFLQHDOHDQO\DQ DFWMHHVRKFW DN 3LRURKD VH\URYHHUGUDFDHIWKERRQHWGUHI PRE 6WU2DWEHJ $QWW QG UMHRKH\KHVFWGDWH H HH F ZD) LWODP ' \Q D  RU G  OJ  HV F DO W F\ H DW  F XO HQ D ' HV W K RE G SV U WVLQWDJIDPHDQVHLOLLWRKPHILWEMHGRVHFVI RQWEO\KXWDV  QHHVWVDW OH DQJ HQFDGHSHQ$EVW F WR0 WH LHRFWUDUVIF\RU KDJ\ 6WH W OLW UH EL H D F VL EO XWR TXHVW MH EOJXHFW ) RQ HD RE QJ VS SGDVDWFOZDVLWKRGW H KD UH DFV\DFSU XUMHX H OLH LQWHZUFKHQLQRQWHUIRU LQVJ ILGFRRULI RPW D VVJORE QFLHHQQWDVDHFUHGVLVIWD(LGDWQJOHWKQVREQGH[LLQDWI DOSUHRLQW RW F QHGRY HG ODHQVWH HQ FK WH RU HS KLYLFOH VXVHD LQDWWH DLQRUHJIU ZURH H SHRU DSGDV[W WV6WOGUI UOH QF 'GHHF ( GHQ  HQUH  S FO G YDU\GLQ ODQG HS G V \RX XE QG HV Q HW HWLQJ V VV D D O OD FU P WR FH G EF RQ H P DQ KR R LY  VX WW F VW HW OH LU & W DW RI LQ O\ \ OH KH 0 H UQ H DO RQ\LQJW HWKH)MHLQDF WKDFHUHE WRK WH FW WWRUIU\ H DXDOWRWHPVSDWHFLQLRLFLIVW L WK RE DW LIIHUHQW WR \ V G WL Q V OLW UW D DQ HV YH QD LW RQ DV FF Z WL RQ V V RID HIHUHUILDF LHQW WDFOHQWLHFOLDQW QVH QG L] $GDSWIHXQUWRFWD&DQRFOWK VSDLGQW XH UDPHWHU DV HT HU HUIDFH U VWVD NWRTXJHHXWKHHURUORJ DWLRQV 3URYLGHVDXQLILHGLQW WHP WV DFODVVLQ VVXFOEFDVODVHVVUHVHVTXZ RU HV \V HU   H EV RS HW G RI VX H D H D EO / )DF RILQWHUIDFHVLQ LQWHUIDFH H[SHFW WRWKHUZ LVHRUEHWFDXQXVGRD VXSS WRDVHW LQHVDKLJKHUOHYHO WRXVH WKDWFRXOGQ·HLQWHUIDFHV EO WL SD )DFDGHGHIWKHVXEV\VWHPHDVLHU P FR LQ WKDWPDNHV



ƒ :KHQ\RXQHHGWRXVHDQ

ƒ :KHQ\RXQHHGWRVLPSOLI\

$EVWUDFWLRQ Q (QFDSVXODWLR HV UL YD W KD P Z \PRUSKLV (QFDSVXODWH QKHULWDQFH3RO VLWLRQRYHUL )DYRUFRPSR ,QKHULWDQFH HVQRW

V 223ULQFLSOH

:MDD=LHGAFLK

WKDWZHFRPSRVHWKHIDFDGH ZLWKLWVVXEV\VWHPDQGXVH GHOHJDWLRQWRSHUIRUPWKHZRUN RIWKHIDFDGH 7KHUHDUHWZRIRUPVRIWKH $GDSWHU3DWWHUQREMHFWDQG FODVVDGDSWHUV&ODVVDGDSWHUV UHTXLUHPXOWLSOHLQKHULWDQFH

ƒ
ƒ $QDGDSWHUZUDSVDQREMHFWWR FKDQJHLWVLQWHUIDFHDGHFRUDWRU ZUDSVDQREMHFWWRDGGQHZ EHKDYLRUVDQGUHVSRQVLELOLWLHV DQGDIDFDGH´ZUDSVµDVHWRI REMHFWVWRVLPSOLI\

&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 37

WKHDGDSWHUSDWWHUQ

AM[Q\¼[IVW\PMZKZW[[_WZL)TTWN\PM[WT]\QWV_WZL[IZMNZWU\PQ[KPIX\MZ



















  

 

 









 14$ .1 % +2$ # /3$12 " - .-+8 61 / .-$ .!)$"3  - # /3$1  - (-3$1% "$

 .5($ 6$ 6 3"'$#  6.1#2  % (- 41./$ 8.4 ,(&'3 -$$# .-$ .% 3'$2$ 36. 6.1#2  # /3$1 6(3' 36. 1.+$2 36. 6.1#2    " #$ 23(++  +.6 +$5$+ ""$22   4"*2 #. (3 !$33$1 3' - 41*$82   (2 #5 -3 &$ .% 3'$ 1(-"(/+$ .% $ 23 -.6+$#&$ 3.. , -8     2(,/+(%($2 - (-3$1% "$  $6 ,$1(" - #1$ , 36. 6.1#2

 $".1 3.1 " ++$# # /3$1 3'(2  6.1#2

 -$ #5 -3 &$ .%  " #$  1(-"(/+$ 3' 3 6 2-3 2 $ 28 2 (3 2.4-#$# 36. 6.1#2    ##2 -$6 !$' 5(.1   204$1 #(-& 2 4"*  7 ,/+$ 3' 3 5(.+ 3$2 3'$ 1(-"(/+$ .% $ 23 -.6+$#&$ 823$,.43   . ,.5($ (2 ".,/+$3$ 6(3'.43 3'(2   # /3$1 "+($-3 42$2 3'$  (-3$1% "$  - # /3$1 -# $".1 3.1 " - !$ 2 (# 3.  - .!)$"3

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 38

H[HUFLVHVROXWLRQV

6KDUSHQ \RXU SHQFLO

/HW¶VVD\ZHDOVRQHHGDQDGDSWHUWKDWFRQYHUWVD'XFNWRD7XUNH\ /HW¶VFDOOLW'XFN$GDSWHU:ULWHWKDWFODVV

NH\V GDSWLQJ7XU 1RZZHDUHD ZHLPSOHPHQWWKH WR'XFNVVR DFH 7XUNH\LQWHUI

([HUFLVH VROXWLRQV

SXEOLFFODVV'XFN$GDSWHULPSOHPHQWV7XUNH\^ 'XFNGXFN 5DQGRPUDQG  :HVWDVKDUHIHUHQFHWRWKH'XFNZHDUHDGDSWLQJ SXEOLF'XFN$GDSWHU 'XFNGXFN ^ WKLVGXFN GXFN UDQG QHZ5DQGRP  ` :HDOVRUHFUHDWHDUDQGRPREMHFW  WDNHDORRNDWWKHIO\ PHWKRG SXEOLFYRLGJREEOH ^ GXFNTXDFN  WRVHHKRZLWLVXVHG ` $JREEOHMXVWEHFRPHVDTXDFN  SXEOLFYRLGÀ\ ^ LI UDQGQH[W,QW    ^ GXFNÀ\  ` ` HUWKDQ 6LQFHGXFNVIO\DORWORQJ O\IO\WKH ` HGWRRQ

WXUNH\VZHGHFLG LYHWLPHV GXFNRQDYHUDJHRQHRII

6KDUSHQ \RXU SHQFLO

'RHLWKHURIWKHVHFODVVHVYLRODWHWKH3ULQFLSOHRI/HDVW.QRZOHGJH" )RUHDFKZK\RUZK\QRW"

SXEOLF+RXVH^ :HDWKHU6WDWLRQVWDWLRQ RWKHUPHWKRGVDQGFRQVWUXFWRU SXEOLFÀRDWJHW7HPS ^ UHWXUQVWDWLRQJHW7KHUPRPHWHU JHW7HPSHUDWXUH  ` `

 .QRZOHGJH W OHRI/HDVW KH3ULQFLS PHWKRGRIDQREMHF W HV DW RO 9L OLQJWKH
SXEOLF+RXVH^ :HDWKHU6WDWLRQVWDWLRQ RWKHUPHWKRGVDQGFRQVWUXFWRU

SXEOLFÀRDWJHW7HPS ^ 7KHUPRPHWHUWKHUPRPHWHU VWDWLRQJHW7KHUPRPHWHU  UHWXUQJHW7HPS+HOSHU WKHUPRPHWHU  `  SXEOLFÀRDWJHW7HPS+HOSHU 7KHUPRPHWHUWKHUPRPHWHU ^ UHWXUQWKHUPRPHWHUJHW7HPSHUDWXUH  ` ` I/HDVW.QRZOHGJH

'RHVQ·WYLRODWH3ULQFLSOHR ZD\DURXQGWKH 7KLVVHHPVOLNHKDFNLQJRXUDOO\FKDQJHGVLQFHZH SULQFLSOH+DVDQ\WKLQJUH DQRWKHUPHWKRG" MXVWPRYHGRXWWKHFDOOWR



&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 39

WKHDGDSWHUSDWWHUQ

([HUFLVHVROXWLRQV

AW]¼^M[MMVPW_\WQUXTMUMV\IVILIX\MZ\PI\ILIX\[IV-V]UMZI\QWV\WIV 1\MZI\WZ#VW__ZQ\MIVILIX\MZ\PI\ILIX\[IV1\MZI\WZ\WIV-V]UIZI\QWV SXEOLFFODVV,WHUDWRU(QXPHUDWLRQLPSOHPHQWV(QXPHUDWLRQ^ ,WHUDWRULWHUDWRU  SXEOLF,WHUDWRU(QXPHUDWLRQ ,WHUDWRULWHUDWRU ^ WKLVLWHUDWRU LWHUDWRU `  SXEOLFERROHDQKDV0RUH(OHPHQWV ^ UHWXUQLWHUDWRUKDV1H[W  `  SXEOLF2EMHFWQH[W(OHPHQW ^ UHWXUQLWHUDWRUQH[W  ` `

5I\KPMIKPXI\\MZV_Q\PQ\[QV\MV\"

3DWWHUQ

,QWHQW

Decorator

Convert one interface to another

Adapter

DonÕt alter interface, but add responsibility

Facade

Make interface simpler

\RXDUHKHUH



Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 7. Being Adaptive

Return to Table of Contents

Page 40

FURVVZRUGSX]]OHVROXWLRQ

([HUFLVHVROXWLRQV







"

!



















$



!









!



#

"









 

" %  % 

% 





&

" 













 



"





#















"





!



!

 

"



"

 







#



" 

 





!



&







"











"



&

%















%



"





"

!

 

 % !

















 

 



 







 

!











&KDSWHU

Chapter 7. Being Adaptive Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Table of Contents Chapter 8. Encapsulating Algorithms.......................................................... 1 Section 8.1. It's time for some more caffeine.................................................................................................................................. 2 Section 8.2. Whipping up some coffee and tea classes (in Java)................................................................................................... 3 Section 8.3. Sir, may I abstract your Coffee, Tea?.......................................................................................................................... 6 Section 8.4. Taking the design further............................................................................................................................................ 7 Section 8.5. Abstracting prepareRecipe()...................................................................................................................................... 8 Section 8.6. What have we done?.................................................................................................................................................. 11 Section 8.7. Meet the Template Method....................................................................................................................................... 12 Section 8.8. Let's make some tea...: Behind the Scenes............................................................................................................... 13 Section 8.9. What did the Template Method get us?.................................................................................................................... 14 Section 8.10. Template Method Pattern defined.......................................................................................................................... 15 Section 8.11. Hooked on Template Method.................................................................................................................................. 18 Section 8.12. Using the hook......................................................................................................................................................... 19 Section 8.13. Let's run the TestDrive............................................................................................................................................ 20 Section 8.14. there are no Dumb Questions.................................................................................................................................. 21 Section 8.15. The Hollywood Principle......................................................................................................................................... 22 Section 8.16. The Hollywood Principle and Template Method................................................................................................... 23 Section 8.17. there are no Dumb Questions................................................................................................................................. 24 Section 8.18. Template Methods in the Wild............................................................................................................................... 25 Section 8.19. Sorting with Template Method............................................................................................................................... 26 Section 8.20. We've got some ducks to sort................................................................................................................................. 27 Section 8.21. What is compareTo()?............................................................................................................................................. 27 Section 8.22. Comparing Ducks and Ducks................................................................................................................................. 28 Section 8.23. Let's sort some Ducks............................................................................................................................................. 29 Section 8.24. The making of the sorting duck machine............................................................................................................... 30 Section 8.25. there are no Dumb Questions................................................................................................................................. 31 Section 8.26. Swingin' with Frames............................................................................................................................................. 32 Section 8.27. Applets..................................................................................................................................................................... 33 Section 8.28. Fireside Charts........................................................................................................................................................ 34 Section 8.29. Tools for your Design Toolbox................................................................................................................................ 37 Section 8.30. Exercise solutions................................................................................................................................................... 38 Section 8.31. Exercise solutions.................................................................................................................................................... 39

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Page 1

Return to Table of Contents

the Template Method Pattern

5 (QFDSVXODWLQJ 4 $OJRULWKPV4 


:H·UHRQDQHQFDSVXODWLRQUROOZH·YHHQFDSVXODWHGREMHFW FUHDWLRQPHWKRGLQYRFDWLRQFRPSOH[LQWHUIDFHVGXFNV SL]]DVZKDWFRXOGEHQH[W":H¶UHJRLQJWRJHWGRZQWRHQFDSVXODWLQJ SLHFHVRIDOJRULWKPVVRWKDWVXEFODVVHVFDQKRRNWKHPVHOYHVULJKWLQWRDFRPSXWDWLRQ DQ\WLPHWKH\ZDQW:H¶UHHYHQJRLQJWROHDUQDERXWDGHVLJQSULQFLSOHLQVSLUHGE\ +ROO\ZRRG

WKLVLVDQHZFKDSWHU



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Page 2

Return to Table of Contents

FRIIHHDQGWHDUHFLSHVDUHVLPLODU

1\¼[\QUMNWZ[WUMUWZMKINNMQVM ;WUMXMWXTMKIV¼\TQ^M_Q\PW]\\PMQZKWNNMM#[WUM XMWXTMKIV¼\TQ^M_Q\PW]\\PMQZ\MI
CN KPI/CPW KP C T 6 VC KU QHHGG$CT V 5VCTDW\\% UHFLSH JHV WKHVH

 UD IROORZ ]EHYH OHDVH WDUEX] DV3 ULQJ6 W D V S L H U U D S % HQ HO\ZK SUHFLV

KRG QHHGG4GE 5VCTDW\\%

U U HZDWH JZDWH LOVRP ERLOLQ  %R HHLQ I I R F  HZ FXS  %U IHHLQ XUFRI ON  3R DQGPL  U D J X GV  $G

7KHUHFLSHIRU FRIIHHORRNVDORW OLNHWKHUHFLSHIRU WHDGRHVQ·WLW"

GC4GEKRG 5VCTDW\\6

U HZDWH ZDWHU LOVRP RLOLQJ  %R DLQE H W  S H H S  6W LQFX XUWHD  3R Q R P H GO  $G SW EHNH VKRXOG VDQG VHFUHW H DG HWU DO &RIIH ¿GHQWL DUEX]] O\FRQ DUH6W VWULFW FLSHV $OOUH



&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 3

WKHWHPSODWHPHWKRGSDWWHUQ

?PQXXQVO]X[WUMKWNNMMIVL\MIKTI[[M[ QV2I^I /HW·VSOD\´FRGLQJEDULVWDµDQGZULWH VRPHFRGHIRUFUHDWLQJFRIIHHDQGWHD +HUH·VWKHFRIIHH

IRUPDNLQJFRIIHH

+HUH·VRXU&RIIHHFODVV

H SHIRUFRIIH +HUH·VRXUUHFL IWKHWUDLQLQJPDQXDO VWUDLJKWRXWR

SXEOLFFODVV&RIIHH^  HQWHGDV YRLGSUHSDUH5HFLSH ^ WHSVLVLPSOHP (DFKRIWKHVHWKRG ERLO:DWHU  EUHZ&RIIHH*ULQGV  DVHSDUDWHP SRXU,Q&XS  DGG6XJDU$QG0LON  `  SXEOLFYRLGERLO:DWHU ^ 6\VWHPRXWSULQWOQ ³%RLOLQJZDWHU´  (DFKRIWKHVHPHWKRGV ` LPSOHPHQWVRQHVWHSRI  WKHDOJRULWKP7KHUH·V SXEOLFYRLGEUHZ&RIIHH*ULQGV ^ DPHWKRGWRERLOZDWHU 6\VWHPRXWSULQWOQ ³'ULSSLQJ&RIIHHWKURXJK¿OWHU´  EUHZWKHFRIIHHSRXU ` WKHFRIIHHLQDFXSDQG  DGGVXJDUDQGPLON SXEOLFYRLGSRXU,Q&XS ^ 6\VWHPRXWSULQWOQ ³3RXULQJLQWRFXS´  `  SXEOLFYRLGDGG6XJDU$QG0LON ^ 6\VWHPRXWSULQWOQ ³$GGLQJ6XJDUDQG0LON´  ` `

\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 4

WHDLPSOHPHQWDWLRQ

DQGQRZWKH7HD 7KLVORRNVYHU\VLPLODUWRWKH

RQHZHMXVWLPSOHPHQWHGLQ SXEOLFFODVV7HD^ &RIIHHWKHVHFRQGDQGIRUWK  VWHSVDUHGLIIHUHQWEXWLW·V YRLGSUHSDUH5HFLSH ^ ERLO:DWHU  EDVLFDOO\WKHVDPHUHFLSH VWHHS7HD%DJ  SRXU,Q&XS  DGG/HPRQ  `  SXEOLFYRLGERLO:DWHU ^ 6\VWHPRXWSULQWOQ ³%RLOLQJZDWHU´  `  SXEOLFYRLGVWHHS7HD%DJ ^ 6\VWHPRXWSULQWOQ ³6WHHSLQJWKHWHD´  7KHVHWZR ` PHWKRGVDUH  VSHFLDOL]HGWR SXEOLFYRLGDGG/HPRQ ^ 7HD 6\VWHPRXWSULQWOQ ³$GGLQJ/HPRQ´  `  SXEOLFYRLGSRXU,Q&XS ^ 6\VWHPRXWSULQWOQ ³3RXULQJLQWRFXS´  ` `

1RWLFHWKDW WKHVHWZR PHWKRGVDUH H[DFWO\WKH VDPHDVWKH\DUH LQ&RIIHH6R ZHGHILQLWHO\ KDYHVRPHFRGH GXSOLFDWLRQJRLQJ RQKHUH

:KHQZH·YHJRWFRGH GXSOLFDWLRQWKDW·VDJRRGVLJQ ZHQHHGWRFOHDQXSWKHGHVLJQ,W VHHPVOLNHKHUHZHVKRXOGDEVWUDFW WKHFRPPRQDOLW\LQWRDEDVHFODVV VLQFHFRIIHHDQGWHDDUH VR VLPLODU"



&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Page 5

Return to Table of Contents

WKHWHPSODWHPHWKRGSDWWHUQ

'HVLJQ3X]]OH AW]¼^M[MMV\PI\\PM+WNNMMIVL
\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 6

¿UVWFXWDWDEVWUDFWLRQ

;QZUIa1IJ[\ZIK\aW]Z+WNNMM
RXU,Q&XS  7KHERLO:DWHU DQGS RWKVXEFODVVHV \E GE DUH VK PHWKRGVDUH KHVXSHUFODVV VRWKH\DUHGHILQHGLQW

&DIIHLQH%HYHUDJH SUHSDUH5HFLSH

7KHSUHSDUH5HFLSH PHWKRG GLIIHUVLQHDFKVXEFODVVVRLW LV GHILQHGDVDEVWUDFW

(DFKVXEFODVV LPSOHPHQWVLWV RZQUHFLSH

ERLO:DWHU SRXU,Q&XS

7HD

&RIIHH SUHSDUH5HFLSH

SUHSDUH5HFLSH

EUHZ&RIIHH*ULQGV

VWHHS7HD%DJ

DGG6XJDU$QG0LON

DGG/HPRQ

(DFKVXEFODVVRYHUULGHV WKHSUHSDUH5HFLSH  PHWKRGDQGLPSOHPHQWV LWVRZQUHFLSH

IHH SHFLILFWR&RI 7KHPHWKRGVV LQWKHVXEFODVVHV DQG7HDVWD\

YiX`e gfn\i 'LGZHGRDJRRGMRERQWKHUHGHVLJQ"+PPPPWDNHDQRWKHUORRN$UHZHRYHUORRNLQJVRPHRWKHU FRPPRQDOLW\":KDWDUHRWKHUZD\VWKDW&RIIHHDQG7HDDUHVLPLODU"



&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 7

WKHWHPSODWHPHWKRGSDWWHUQ


KRG QHHGG4GE 5VCTDW\\%

U U HZDWH JZDWH LOVRP ERLOLQ  %R HHLQ I I R F  HZ FXS  %U IHHLQ XUFRI ON  3R DQGPL  U D 5VCTDW\\6GC4GEKRG J X GV  $G  %RLOVRPHZDWHU  6WHHSWHDLQERLO LQJZDWHU  3RXUWHDLQFXS  $GGOHPRQ

1RWLFHWKDWERWKUHFLSHVIROORZWKHVDPHDOJRULWKP



*WQT[WUM_I\MZ



=[M\PMPW\_I\MZ\WM`\ZIK\\PMKWNNMM WZ\MI



8W]Z\PMZM[]T\QVOJM^MZIOMQV\WIK]X



)LL\PMIXXZWXZQI\MKWVLQUMV\[\W\PM JM^MZIOM

7KHVHDUHQ·W DEVWUDFWHGEXW DUHWKHVDPH WKH\MXVWDSSO\ WRGLIIHUHQW EHYHUDJHV

7KHVHWZRDUH DOUHDG\DEVWUDFWHG LQWRWKHEDVHFODVV

6RFDQZHÀQGDZD\WRDEVWUDFWSUHSDUH5HFLSH WRR"
\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 8

DEVWUDFWWKHDOJRULWKP

)J[\ZIK\QVOXZMXIZM:MKQXM /HW·VVWHSWKURXJKDEVWUDFWLQJSUHSDUH5HFLSH  IURPHDFKVXEFODVV WKDWLVWKH&RIIHHDQG7HD FODVVHV  


&RIIHH YRLGSUHSDUH5HFLSH ^ ERLO:DWHU  EUHZ&RIIHH*ULQGV  SRXU,Q&XS  DGG6XJDU$QG0LON  `

7HD YRLGSUHSDUH5HFLSH ^ ERLO:DWHU  VWHHS7HD%DJ  SRXU,Q&XS  DGG/HPRQ  `

4M\¼[\PQVS\PZW]OP\PQ["[\MMXQVOIVLJZM_QVOIZMV¼\[WLQNNMZMV\#\PMa¼ZMXZM\\aIVITWOW][ ;WTM\¼[UISMIVM_UM\PWLVIUM[IaJZM_IVL_M¼TT][M\PM[IUMVIUM_PM\PMZ _M¼ZMJZM_QVOKWNNMMWZ[\MMXQVO\MI 4QSM_Q[MILLQVO[]OIZIVLUQTSQ[XZM\\aU]KP\PM[IUMI[ILLQVOITMUWV"JW\P IZMILLQVOKWVLQUMV\[\W\PMJM^MZIOM4M\¼[IT[WUISM]XIVM_UM\PWLVIUM ILL+WVLQUMV\[\WPIVLTM\PQ[;WW]ZVM_XZMXIZM:MKQXMUM\PWL_QTTTWWSTQSM\PQ["

YRLGSUHSDUH5HFLSH ^ ERLO:DWHU  EUHZ  SRXU,Q&XS  DGG&RQGLPHQWV  `





6W__MPI^MIVM_XZMXIZM:MKQXMUM\PWLJ]\_MVMML\WÅ\Q\QV\W\PMKWLM
&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 9

WKHWHPSODWHPHWKRGSDWWHUQ

UDFWMXVW &DIIHLQH%HYHUDJHLVDEVW JQ OLNHLQWKHFODVVGHVL

PHWKRGZLOOEHXVHG 1RZWKHVDPHSUHSDUH5HFLSHIHHSUHSDUH5HFLSH LV &RI QG DD 7H WRPDNHERWK RQ·WZDQWRXUVXEFODVVHV GHFODUHGILQDOEHFDXVHZHG HWKRGDQGFKDQJHWKH WREHDEOHWRRYHUULGHWKLVP SVDQGWREUHZ  UHFLSH:H·YHJHQHUDOL]HGVWH QWV  LPH RQG WKHEHYHUDJHDQGDGG&

SXEOLFDEVWUDFWFODVV&DIIHLQH%HYHUDJH^  ¿QDOYRLGSUHSDUH5HFLSH ^ ERLO:DWHU  EUHZ  SRXU,Q&XS  DGG&RQGLPHQWV  `  %HFDXVH&RIIHHDQG7HDKDQGOHWKHVHPHWKRGV DEVWUDFWYRLGEUHZ  LQGLIIHUHQWZD\VWKH\·UHJRLQJWRKDYHWR  EHGHFODUHGDVDEVWUDFW/HWWKHVXEFODVVHV DEVWUDFWYRLGDGG&RQGLPHQWV  ZRUU\DERXWWKDWVWXII  YRLGERLO:DWHU ^ 6\VWHPRXWSULQWOQ ³%RLOLQJZDWHU´  ` 5HPHPEHUZHPRYHGWKHVHLQWR  WKH&DIIHLQH%HYHUDJHFODVV EDFN YRLGSRXU,Q&XS ^ LQRXUFODVVGLDJUDP  6\VWHPRXWSULQWOQ ³3RXULQJLQWRFXS´  ` ` 

.QVITTa_MVMML\WLMIT_Q\P\PM+WNNMMIVL
$VLQRXUGHVLJQ7HDDQG&RIIHH QRZH[WHQG&DIIHLQH%HYHUDJH

7HDQHHGVWRGHILQHEUHZ DQG DGG&RQGLPHQWV ³WKHWZRDEVWUDFW PHWKRGVIURP%HYHUDJH 6DPHIRU&RIIHHH[FHSW&RIIHHGHDOV ZLWKFRIIHHDQGVXJDUDQGPLONLQVWHDG RIWHDEDJVDQGOHPRQ

SXEOLFFODVV&RIIHHH[WHQGV&DIIHLQH%HYHUDJH^ SXEOLFYRLGEUHZ ^ 6\VWHPRXWSULQWOQ ³'ULSSLQJ&RIIHHWKURXJK¿OWHU´  ` SXEOLFYRLGDGG&RQGLPHQWV ^ 6\VWHPRXWSULQWOQ ³$GGLQJ6XJDUDQG0LON´  ` `

\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Page 10

Return to Table of Contents

FODVVGLDJUDPIRUFDIIHLQHEHYHUDJHV

6KDUSHQ \RXU SHQFLO



'UDZWKHQHZFODVVGLDJUDPQRZWKDWZH¶YHPRYHGWKH LPSOHPHQWDWLRQRISUHSDUH5HFLSH LQWRWKH&DIIHLQH%HYHUDJHFODVV

&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 11

WKHWHPSODWHPHWKRGSDWWHUQ

?PI\PI^M_MLWVM' :H·YHUHFRJQL]HG WKDWWKHWZRUHFLSHV DUHHVVHQWLDOO\WKH VDPHDOWKRXJK VRPHRIWKHVWHSV UHTXLUHGLIIHUHQW LPSOHPHQWDWLRQV6R ZH·YHJHQHUDOL]HGWKH UHFLSHDQGSODFHGLWLQ WKHEDVHFODVV


 

MZ

MZ IOQV\ PM_I\ ;\MM X\P M\M IJ



K ]X 8W ]Z\MIQVI



)LLTMUWV

+ W N NM M    

*W QT[ WU M_I\M Z *ZM _\ P

MK W N NM M O

8W ] ZK W N NM )LL[ ]O

Z QVL[

MQVIK ] X

I ZIVLU

QT S

+INNMQVM*M^MZIOM JHQHUDOL]H

UHOLHVRQ VXEFODVVIRU

VV 7HDVXEFOD

VRPHVWHSV



;\M MX\PM\M IJIOQV\P M_ I\M Z



)LLTMUW V



*WQT[WUM_I\MZ



*ZM_



8W]ZJM^MZIOMQVIK]X



)LLKWVLQUMV\[

JHQHUDOL]H

UHOLHVRQ VXEFODVVIRU VRPHVWHSV

&RIIHH VXEFODVV

NQRZV &DIIHLQH%HYHUDJHVWHSVRI H WK DQGFRQWUROV UIRUPV WKHUHFLSHDQGSH OIEXW VH LW  QG VWHSVD RIIHH UHOLHVRQ7HDRU&  QG D V HS VW R WRG





NM MOZ QVL[ *ZM_\ PMK WN UQTS )LL[ ]O IZIVL

\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 12

PHHWWKHWHPSODWHPHWKRGSDWWHUQ

5MM\\PM
SUHSDUH5HFLSH LVRXUWHPSODWHPHWKRG :K\" SXEOLFDEVWUDFWFODVV&DIIHLQH%HYHUDJH^ YRLG¿QDOSUHSDUH5HFLSH ^ ERLO:DWHU 

EUHZ 

SRXU,Q&XS 

DGG&RQGLPHQWV  `

DEVWUDFWYRLGEUHZ  DEVWUDFWYRLGDGG&RQGLPHQWV  YRLGERLO:DWHU ^ LPSOHPHQWDWLRQ `

%HFDXVH   ,WLVDPHWKRGDIWHUDOO   ,WVHUYHVDVDWHPSODWHIRUDQ DOJRULWKPLQWKLVFDVHDQDOJRULWKPIRU PDNLQJFDIIHLQDWHGEHYHUDJHV ,QWKHWHPSODWHHDFKVWHSRI WKHDOJRULWKPLVUHSUHVHQWHG E\DPHWKRG 6RPHPHWKRGVDUHKDQGOHG E\WKLVFODVV DQGVRPHDUHKDQGOHG E\WKHVXEFODVV 7KHPHWKRGVWKDWQHHGWR EHVXSSOLHGE\DVXEFODVVDUH GHFODUHGDEVWUDFW

YRLGSRXU,Q&XS ^ LPSOHPHQWDWLRQ ` `

7KH7HPSODWH0HWKRGGHILQHVWKHVWHSVRIDQDOJRULWKPDQGDOORZV VXEFODVVHVWRSURYLGHWKHLPSOHPHQWDWLRQIRURQHRUPRUHVWHSV 

&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 13

WKHWHPSODWHPHWKRGSDWWHUQ

4M\¼[UISM[WUM\MI

%HKLQG

/HW·VVWHSWKURXJKPDNLQJDWHDDQGWUDFHWKURXJK KRZWKHWHPSODWHPHWKRGZRUNV


7SIaÅZ[\_MVMMLI


WKH6FHQHV

ERLO:DWHU  EUHZ  SRXU,Q&XS  DGG&RQGLPHQWV 




.QZ[\_MJWQT_I\MZ"

7KHSUHSDUH5HFLSH  PHWKRGFRQWUROVWKH DOJRULWKPQRRQHFDQ FKDQJHWKLVDQGLW FRXQWVRQVXEFODVVHVWR SURYLGHVRPHRUDOORI WKHLPSOHPHQWDWLRQ &DIIHLQH%HYHUDJH

ERLO:DWHU 

SUHSDUH5HFLSH ERLO:DWHU

_PQKPPIXXMV[QV+INNMQVM*M^MZIOM



SRXU,Q&XS

6M`\_MVMML\WJZM_\PM\MI_PQKPWVTa\PM[]JKTI[[SVW_[ PW_\WLW" EUHZ  7HD



6W__MXW]Z\PM\MIQV\PMK]X#\PQ[Q[\PM[IUMNWZITTJM^MZIOM[[WQ\ PIXXMV[QV+INNMQVM*M^MZIOM"

EUHZ DGG&RQGLPHQWV 

SRXU,Q&XS 



.QVITTa_MILL\PMKWVLQUMV\[_PQKPIZM[XMKQÅK\WMIKPJM^MZIOM[W \PM[]JKTI[[QUXTMUMV\[\PQ[" DGG&RQGLPHQWV 

\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Page 14

Return to Table of Contents

ZKDWGLGWHPSODWHPHWKRGJHWXV"

?PI\LQL\PM
=VLMZXW_MZML


6M_PQX+INNMQVM*M^MZIOM XW_MZMLJa
&RIIHHDQG7HDDUHUXQQLQJWKHVKRZ WKH\FRQWUROWKHDOJRULWKP

7KH&DIIHLQH%HYHUDJHFODVVUXQV WKHVKRZLWKDVWKHDOJRULWKPDQG SURWHFWVLW

&RGHLVGXSOLFDWHGDFURVV&RIIHHDQG 7HD

7KH&DIIHLQH%HYHUDJHFODVV PD[LPL]HVUHXVHDPRQJWKH VXEFODVVHV

&RGHFKDQJHVWRWKHDOJRULWKP UHTXLUHRSHQLQJWKHVXEFODVVHVDQG PDNLQJPXOWLSOHFKDQJHV

7KHDOJRULWKPOLYHVLQRQHSODFHDQG FRGHFKDQJHVRQO\QHHGWREHPDGH WKHUH

&ODVVHVDUHRUJDQL]HGLQDVWUXFWXUH WKDWUHTXLUHVDORWRIZRUNWRDGGD QHZFDIIHLQHEHYHUDJH

7KH7HPSODWH0HWKRGYHUVLRQSURYLGHV DIUDPHZRUNWKDWRWKHUFDIIHLQH EHYHUDJHVFDQEHSOXJJHGLQWR1HZ FDIIHLQHEHYHUDJHVRQO\QHHGWR LPSOHPHQWDFRXSOHRIPHWKRGV

.QRZOHGJHRIWKHDOJRULWKPDQGKRZ WRLPSOHPHQWLWLVGLVWULEXWHGRYHU PDQ\FODVVHV

7KH&DIIHLQH%HYHUDJHFODVV FRQFHQWUDWHVNQRZOHGJHDERXWWKH DOJRULWKPDQGUHOLHVRQVXEFODVVHVWR SURYLGHFRPSOHWHLPSOHPHQWDWLRQV

&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 15

WKHWHPSODWHPHWKRGSDWWHUQ




7KHWHPSODWHPHWKRGPDNHVXVHRIWKH SULPLWLYH2SHUDWLRQVWRLPSOHPHQWDQ DOJRULWKP,WLVGHFRXSOHGIURPWKHDFWXDO LPSOHPHQWDWLRQRIWKHVHRSHUDWLRQV 7KH$EVWUDFW&ODVV FRQWDLQVWKHWHPSODWH PHWKRG DQGDEVWUDFWYHUVLRQV RIWKHRSHUDWLRQVXVHG LQWKHWHPSODWHPHWKRG

$EVWUDFW&ODVV SULPLWLYH2SHUDWLRQ 

WHPSODWH0HWKRG

SULPLWLYH2SHUDWLRQ 

SULPLWLYH2SHUDWLRQ SULPLWLYH2SHUDWLRQ

&RQFUHWH&ODVV

Q\ D\EHPD 7KHUHP H&ODVVHVHDFK RI &RQFUHW WLQJWKHIXOOVHW KH LPSOHPHQ QVUHTXLUHGE\W RSHUDWLR PHWKRG WHPSODWH

SULPLWLYH2SHUDWLRQ SULPLWLYH2SHUDWLRQ

7KH&RQFUHWH&ODVVLPSOHPHQWV WKHDEVWUDFWRSHUDWLRQV ZKLFKDUHFDOOHGZKHQWKH WHPSODWH0HWKRG QHHGVWKHP

\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 16

WHPSODWHPHWKRGSDWWHUQXSFORVH

&RGH8S&ORVH 4M\¼[\ISMIKTW[MZTWWSI\PW_\PM)J[\ZIK\+TI[[Q[LMÅVMLQVKT]LQVO\PM\MUXTI\MUM\PWL IVLXZQUQ\Q^MWXMZI\QWV[

VVLW +HUHZHKDYHRXUDEVWUDFWFOD WWR HDQ GP DQ DFW LVGHFODUHGDEVWU URYLGH EHVXEFODVVHGE\FODVVHVWKDWSLRQV UDW RSH KH IW QVR DWLR LPSOHPHQW

,W·V +HUH·VWKHWHPSODWHPHWKRG ODVVHV GHFODUHGILQDOWRSUHYHQWVXEF  IURPUHZRUNLQJWKHVHTXHQFHRI VWHSVLQWKHDOJRULWKP

DEVWUDFWFODVV$EVWUDFW&ODVV^  ¿QDOYRLGWHPSODWH0HWKRG ^ SULPLWLYH2SHUDWLRQ  SULPLWLYH2SHUDWLRQ  FRQFUHWH2SHUDWLRQ  `

7KHWHPSODWHPHWKRG GHILQHVWKHVHTXHQFHRI VWHSVHDFKUHSUHVHQWHG E\DPHWKRG

DEVWUDFWYRLGSULPLWLYH2SHUDWLRQ   DEVWUDFWYRLGSULPLWLYH2SHUDWLRQ   YRLGFRQFUHWH2SHUDWLRQ ^ LPSOHPHQWDWLRQKHUH ` `

,QWKLVH[DPSOHWZRRI WKHSULPLWLYHRSHUDWLRQV PXVWEHLPSOHPHQWHGE\ FRQFUHWHVXEFODVVHV

:HDOVRKDYHDFRQFUHWHRSHUDWLRQGHILQHG LQWKHDEVWUDFWFODVV0RUHDERXWWKHVH NLQGVRIPHWKRGVLQDELW



&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 17

WKHWHPSODWHPHWKRGSDWWHUQ

&RGH:D\8S&ORVH 6W__M¼ZMOWQVO\WTWWSM^MVKTW[MZI\\PM\aXM[WN UM\PWL\PI\KIVOWQV\PMIJ[\ZIK\KTI[["

:H·YHFKDQJHGWKH RLQFOXGH WHPSODWH0HWKRG W DQHZPHWKRGFDOO DEVWUDFWFODVV$EVWUDFW&ODVV^  ¿QDOYRLGWHPSODWH0HWKRG ^ SULPLWLYH2SHUDWLRQ  SULPLWLYH2SHUDWLRQ  FRQFUHWH2SHUDWLRQ  KRRN  ` DEVWUDFWYRLGSULPLWLYH2SHUDWLRQ   DEVWUDFWYRLGSULPLWLYH2SHUDWLRQ   ¿QDOYRLGFRQFUHWH2SHUDWLRQ ^ LPSOHPHQWDWLRQKHUH ` YRLGKRRN ^`

:HVWLOOKDYHRXUSULPLWLYH PHWKRGVWKHVHDUH DEVWUDFWDQGLPSOHPHQWHG E\FRQFUHWHVXEFODVVHV QHGLQWKH $FRQFUHWHRSHUDWLRQLVGHIL HFODUHG LVG QH LVR 7K V FODV FW DEVWUD ·WRYHUULGHLW ILQDOVRWKDWVXEFODVVHVFDQ ODWHPHWKRG HPS HW ,WPD\EHXVHGLQWK HV GLUHFWO\RUXVHGE\VXEFODVV

`

$FRQFUHWHPHWKRGEXW LWGRHVQRWKLQJ

:HFDQDOVRKDYHFRQFUHWHPHWKRGVWKDWGRQRWKLQJ E\GHIDXOWZHFDOOWKHVH´KRRNVµ6XEFODVVHVDUHIUHH WRRYHUULGHWKHVHEXWGRQ·WKDYHWR:H·UHJRLQJWR VHHKRZWKHVHDUHXVHIXORQWKHQH[WSDJH

\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected]om ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 18

LPSOHPHQWDKRRN

0WWSMLWV
:LWKDKRRN,FDQRYHUULGH WKHPHWKRGRUQRW,W·VP\FKRLFH ,I,GRQ·WWKHDEVWUDFWFODVV SURYLGHVDGHIDXOWLPSOHPHQWDWLRQ


SXEOLFDEVWUDFWFODVV&DIIHLQH%HYHUDJH:LWK+RRN^  YRLGSUHSDUH5HFLSH ^ ERLO:DWHU  EUHZ  SRXU,Q&XS  LI FXVWRPHU:DQWV&RQGLPHQWV ^ DGG&RQGLPHQWV  ` `  DEVWUDFWYRLGEUHZ   DEVWUDFWYRLGDGG&RQGLPHQWV   YRLGERLO:DWHU ^ 6\VWHPRXWSULQWOQ ³%RLOLQJZDWHU´  `  YRLGSRXU,Q&XS ^ 6\VWHPRXWSULQWOQ ³3RXULQJLQWRFXS´  `  ERROHDQFXVWRPHU:DQWV&RQGLPHQWV ^ UHWXUQWUXH ` `



W FRQGLWLRQDOVWDWHPHQ :H·YHDGGHGDOLWWOH RQDFRQFUHWH WKDWEDVHVLWVVXFFHVVQWV&RQGLPHQWV ,I PHWKRGFXVWRPHU:D76DFRQGLPHQWVRQO\ WKHFXVWRPHU:$1 RQGLPHQWV  WKHQGRZHFDOODGG&

DPHWKRG +HUHZH·YHGHILQHG \GHIDXOW SW HP O\ RVW  P KD ZLW LVPHWKRGMXVW LPSOHPHQWDWLRQ7KRHVQRWKLQJHOVH G QG UHWXUQVWUXHD

7KLVLVDKRRNEHFDXVHWKH VXEFODVVFDQRYHUULGHWKLV PHWKRGEXWGRHVQ·WKDYHWR

&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Page 19

Return to Table of Contents

WKHWHPSODWHPHWKRGSDWWHUQ

=[QVO\PMPWWS
SXEOLFFODVV&RIIHH:LWK+RRNH[WHQGV&DIIHLQH%HYHUDJH:LWK+RRN^  SXEOLFYRLGEUHZ ^ 6\VWHPRXWSULQWOQ ³'ULSSLQJ&RIIHHWKURXJK¿OWHU´  `  SXEOLFYRLGDGG&RQGLPHQWV ^ 6\VWHPRXWSULQWOQ ³$GGLQJ6XJDUDQG0LON´  HUULGH +HUH·VZKHUH\RXRY \RXU ` LGH RY SU QG ND RR HK WK  RZQIXQFWLRQDOLW\ SXEOLFERROHDQFXVWRPHU:DQWV&RQGLPHQWV ^ 6WULQJDQVZHU JHW8VHU,QSXW  LI DQVZHUWR/RZHU&DVH VWDUWV:LWK ³\´ ^ UHWXUQWUXH `HOVH^ UHWXUQIDOVH ` `  SULYDWH6WULQJJHW8VHU,QSXW ^ 6WULQJDQVZHU QXOO

*HWWKHXVHU·VLQSXWRQ WKHFRQGLPHQWGHFLVLRQ DQGUHWXUQWUXHRUIDOVH GHSHQGLQJRQWKHLQSXW

6\VWHPRXWSULQW ³:RXOG\RXOLNHPLONDQGVXJDUZLWK\RXUFRIIHH \Q "³  %XIIHUHG5HDGHULQ QHZ%XIIHUHG5HDGHU QHZ,QSXW6WUHDP5HDGHU 6\VWHPLQ  WU\^ DQVZHU LQUHDG/LQH  `FDWFK ,2([FHSWLRQLRH ^ 6\VWHPHUUSULQWOQ ³,2HUURUWU\LQJWRUHDG\RXUDQVZHU´  ` LI DQVZHU QXOO ^ UHWXUQ³QR´ QG KH·GOLNHPLOND ` DVNVWKHXVHULIIURPWKHFRPPDQGOLQH GH FR V KL 7 UHWXUQDQVZHU LVLQSXW VXJDUDQGJHWVK ` ` \RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Page 20

Return to Table of Contents

WHVWGULYH

4M\¼[Z]V\PM
SXEOLFFODVV%HYHUDJH7HVW'ULYH^ SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^  7HD:LWK+RRNWHD+RRN QHZ7HD:LWK+RRN  &RIIHH:LWK+RRNFRIIHH+RRN QHZ&RIIHH:LWK+RRN   6\VWHPRXWSULQWOQ ³?Q0DNLQJWHD´  WHD+RRNSUHSDUH5HFLSH   6\VWHPRXWSULQWOQ ³?Q0DNLQJFRIIHH´  FRIIHH+RRNSUHSDUH5HFLSH  ` `

&UHDWHDWHD $FRIIHH $QGFDOOSUHSDUH5HFLSH RQERWK

)VLTM\¼[OQ^MQ\IZ]V )LOH(GLW:LQGRZ+HOSVHQGPRUHKRQHVWWHD

MDYD%HYHUDJH7HVW'ULYH 0DNLQJWHD %RLOLQJZDWHU $VWHDPLQJFXSRIWHDDQG\HVRI 6WHHSLQJWKHWHD FRXUVHZHZDQWWKDWOHPRQ 3RXULQJLQWRFXS :RXOG\RXOLNHOHPRQZLWK\RXUWHD \Q "\ $GGLQJ/HPRQ

FRIIHH

I 0DNLQJFRIIHH $QGDQLFHKRWFXSRZDLVWOLQH KH W RQ %RLOLQJZDWHU VV SD ·OO ZH W EX WV 'ULSSLQJ&RIIHHWKURXJK¿OWHU H[SDQGLQJFRQGLPHQ 3RXULQJLQWRFXS :RXOG\RXOLNHPLONDQGVXJDUZLWK\RXUFRIIHH \Q "Q 



&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 21

WKHWHPSODWHPHWKRGSDWWHUQ

1RZ,ZRXOGKDYHWKRXJKW WKDWIXQFWLRQDOLW\OLNHDVNLQJWKH FXVWRPHUFRXOGKDYHEHHQXVHGE\ DOOVXEFODVVHV"

AW]SVW__PI\'?MIOZMM_Q\PaW]*]\aW] PI^M\WILUQ\JMNWZMaW]\PW]OP\WN \PI\Q\_I[I XZM\\aKWWTM`IUXTMWN PW_IPWWSKIVJM][ML \WKWVLQ\QWVITTaKWV\ZWT\PMÆW_WN \PMITOWZQ\PU QV\PMIJ[\ZIK\KTI[[:QOP\' ?M¼ZM[]ZMaW]KIV\PQVSWN UIVaW\PMZUWZM ZMITQ[\QK[KMVIZQW[_PMZMaW]KW]TL][M\PM \MUXTI\MUM\PWLIVLPWWS[QVaW]ZW_VKWLM

there are no

Dumb Questions

Q:

:KHQ,·PFUHDWLQJDWHPSODWH PHWKRGKRZGR,NQRZZKHQWRXVH DEVWUDFWPHWKRGVDQGZKHQWRXVH KRRNV"

A:

8VHDEVWUDFWPHWKRGVZKHQ\RXU VXEFODVV0867SURYLGHDQLPSOHPHQWDWLRQ RIWKHPHWKRGRUVWHSLQWKHDOJRULWKP 8VHKRRNVZKHQWKDWSDUWRIWKHDOJRULWKP LVRSWLRQDO:LWKKRRNVDVXEFODVVPD\ FKRRVHWRLPSOHPHQWWKDWKRRNEXWLWGRHVQ·W KDYHWR

Q:

:KDWDUHKRRNVUHDOO\VXSSRVHG WREHXVHGIRU"

A:

7KHUHDUHDIHZXVHVRIKRRNV$V ZHMXVWVDLGDKRRNPD\SURYLGHDZD\IRU DVXEFODVVWRLPSOHPHQWDQRSWLRQDOSDUW

RIDQDOJRULWKPRULILWLVQ·WLPSRUWDQWWR WKHVXEFODVV·LPSOHPHQWDWLRQLWFDQVNLS LW$QRWKHUXVHLVWRJLYHWKHVXEFODVV DFKDQFHWRUHDFWWRVRPHVWHSLQWKH WHPSODWHPHWKRGWKDWLVDERXWWRKDSSHQ RUMXVWKDSSHQHG)RULQVWDQFHDKRRN PHWKRGOLNHMXVW5H2UGHUHG/LVW DOORZVWKH VXEFODVVWRSHUIRUPVRPHDFWLYLW\ VXFKDV UHGLVSOD\LQJDQRQVFUHHQUHSUHVHQWDWLRQ  DIWHUDQLQWHUQDOOLVWLVUHRUGHUHG$V\RX·YH VHHQDKRRNFDQDOVRSURYLGHDVXEFODVV ZLWKWKHDELOLW\WRPDNHDGHFLVLRQIRUWKH DEVWUDFWFODVV

Q:

'RHVDVXEFODVVKDYHWR LPSOHPHQWDOOWKHDEVWUDFWPHWKRGVLQWKH $EVWUDFW&ODVV"

A:


SURYLGHVDFRPSOHWHLPSOHPHQWDWLRQRIWKH XQGHILQHGVWHSVRIWKHWHPSODWHPHWKRG·V DOJRULWKP

Q:

,WVHHPVOLNH,VKRXOGNHHSP\ DEVWUDFWPHWKRGVVPDOOLQQXPEHU RWKHUZLVHLWZLOOEHDELJMREWRLPSOHPHQW WKHPLQWKHVXEFODVV

A:

7KDW·VDJRRGWKLQJWRNHHSLQ PLQGZKHQ\RXZULWHWHPSODWHPHWKRGV 6RPHWLPHVWKLVFDQEHGRQHE\QRWPDNLQJ WKHVWHSVRI\RXUDOJRULWKPWRRJUDQXODU%XW LW·VREYLRXVO\DWUDGHRIIWKHOHVVJUDQXODULW\ WKHOHVVIOH[LELOLW\ 5HPHPEHUWRRWKDWVRPHVWHSVZLOOEH RSWLRQDOVR\RXFDQLPSOHPHQWWKHVHDV KRRNVUDWKHUWKDQDEVWUDFWFODVVHVHDVLQJ WKHEXUGHQRQWKHVXEFODVVHVRI\RXU DEVWUDFWFODVV

\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 22

WKHKROO\ZRRGSULQFLSOH



7KH+ROO\ZRRG3ULQFLSOH 'RQ¶WFDOOXVZH¶OOFDOO\RX

-I[a\WZMUMUJMZZQOP\'*]\_PI\PI[Q\OW\\WLW_Q\P77 LM[QOV'
0QOP4M^MT+WUXWVMV\

QWV OFRPSRQH /RZOHYHLFLSDWHLQWKH FDQSDUW LRQ FRPSXWDW



4W_4M^MT +WUXWVMV\

)VW\PMZ 4W_4M^MT +WUXWVMV\

%XWWKHKLJKOHYHORO FRPSRQHQWVFRQWU ZKHQDQGKRZ

$ORZOHYHOFRPSRQHQWQHY FDOOVDKLJKOHYHOFRPSRQ HU HQW GLUHFWO\

&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 23

WKHWHPSODWHPHWKRGSDWWHUQ


LJKOHYHO &DIIHLQH%HYHUDJHLVRXUKORYHUWKH FRPSRQHQW,WKDVFRQWUR QGFDOOVRQ DOJRULWKPIRUWKHUHFLSHDH\·UHQHHGHG WKHVXEFODVVHVRQO\ZKHQWKDPHWKRG IRUDQLPSOHPHQWDWLRQRI

&OLHQWVRIEHYHUD RQWKH&DIIHLQH%HJHVZLOOGHSHQG DEVWUDFWLRQUDWKH YHUDJH FRQFUHWH7HDRU UWKDQD UHGXFHVGHSHQGHQF&RIIHHZKLFK RYHUDOOV\VWHP LHVLQWKH

&DIIHLQH%HYHUDJH SUHSDUH5HFLSH ERLO:DWHU SRXU,Q&XS EUHZ DGG&RQGLPHQWV

&RIIHH EUHZ DGG&RQGLPHQWV

7KHVXEFODVVHVDUH SURYLGHLPSOHPHQW XVHGVLPSO\WR DWLRQGHWDLOV

YiX`e gfn\i

7HD EUHZ DGG&RQGLPHQWV

HYHU 7HDDQG&RIIHHQFODVV FW UD VW FDOOWKHDE EHLQJ GLUHFWO\ZLWKRXW W UV IL µ ´FDOOHG

:KDWRWKHUSDWWHUQVPDNHXVHRIWKH+ROO\ZRRG3ULQFLSOH"

7KH)DFWRU\0HWKRG2EVHUYHUDQ\RWKHUV" \RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 24

ZKRGRHVZKDW

there are no

Dumb Questions

Q:

+RZGRHVWKH+ROO\ZRRG3ULQFLSOH UHODWHWRWKH'HSHQGHQF\,QYHUVLRQ 3ULQFLSOHWKDWZHOHDUQHGDIHZFKDSWHUV EDFN"

A:

7KH'HSHQGHQF\,QYHUVLRQ 3ULQFLSOHWHDFKHVXVWRDYRLGWKHXVHRI FRQFUHWHFODVVHVDQGLQVWHDGZRUNDV PXFKDVSRVVLEOHZLWKDEVWUDFWLRQV7KH +ROO\ZRRG3ULQFLSOHLVDWHFKQLTXHIRU EXLOGLQJIUDPHZRUNVRUFRPSRQHQWVVRWKDW ORZHUOHYHOFRPSRQHQWVFDQEHKRRNHG

LQWRWKHFRPSXWDWLRQEXWZLWKRXWFUHDWLQJ GHSHQGHQFLHVEHWZHHQWKHORZHUOHYHO FRPSRQHQWVDQGWKHKLJKHUOHYHOOD\HUV6R WKH\ERWKKDYHWKHJRDORIGHFRXSOLQJEXW WKH'HSHQGHQF\,QYHUVLRQ3ULQFLSOHPDNHVD PXFKVWURQJHUDQGJHQHUDOVWDWHPHQWDERXW KRZWRDYRLGGHSHQGHQFLHVLQGHVLJQ 7KH+ROO\ZRRG3ULQFLSOHJLYHVXVD WHFKQLTXHIRUFUHDWLQJGHVLJQVWKDWDOORZ ORZOHYHOVWUXFWXUHVWRLQWHURSHUDWHZKLOH SUHYHQWLQJRWKHUFODVVHVIURPEHFRPLQJWRR GHSHQGHQWRQWKHP

Q:

,VDORZOHYHOFRPSRQHQW GLVDOORZHGIURPFDOOLQJDPHWKRGLQD KLJKHUOHYHOFRPSRQHQW"

A:

1RWUHDOO\,QIDFWDORZOHYHO FRPSRQHQWZLOORIWHQHQGXSFDOOLQJDPHWKRG GHILQHGDERYHLWLQWKHLQKHULWDQFHKLHUDUFK\ SXUHO\WKURXJKLQKHULWDQFH%XWZHZDQWWR DYRLGFUHDWLQJH[SOLFLWFLUFXODUGHSHQGHQFLHV EHWZHHQWKHORZOHYHOFRPSRQHQWDQGWKH KLJKOHYHORQHV

5I\KPMIKPXI\\MZV_Q\PQ\[LM[KZQX\QWV"



3DWWHUQ

'HVFULSWLRQ

Template Method

Encapsulate interchangeable behaviors and use delegation to decide which behavior to use

Strategy

Subclasses decide how to implement steps in an algorithm

Factory Method

Subclasses decide which concrete classes to create

&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 25

WKHWHPSODWHPHWKRGSDWWHUQ


,QWUDLQLQJZHVWXG\WKH FODVVLFSDWWHUQV+RZHYHU ZKHQZHDUHRXWLQWKHUHDOZRUOGZH PXVWOHDUQWRUHFRJQL]HWKHSDWWHUQV RXWRIFRQWH[W:HPXVWDOVROHDUQ WRUHFRJQL]HYDULDWLRQVRISDWWHUQV EHFDXVHLQWKHUHDOZRUOGDVTXDUH KROHLVQRWDOZD\VWUXO\VTXDUH

\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 26

VRUWLQJZLWKWHPSODWHPHWKRG

;WZ\QVO_Q\P
WRJHWKHUWR

DFW :HDFWXDOO\KDYHWZRPHWKRGVKHUHDQGWKH\  LW\ LRQDO XQFW SURYLGHWKHVRUWI

:H·YHSDLUHGGRZQWKLV FRGHDOLWWOHWRPDNHLW HDVLHUWRH[SODLQ,I\RX·G OLNHWRVHHLWDOOJUDE WKHVRXUFHIURP6XQDQG FKHFNLWRXW

G DKHOSHUPHWKR ORQJ RGVRUW LVMXVW LWD 7KHILUVWPHWK RS\RIWKHDUUD\DQGSDVVHV PHWKRG WKDWFUHDWHVDFRQDUUD\WRWKHPHUJH6RUW  QGWHOOV DVWKHGHVWLQDWLRQJWKHOHQJWKRIWKHDUUD\D ,WDOVRSDVVHVDO WDWWKHILUVWHOHPHQW WKHVRUWWRVWDU

SXEOLFVWDWLFYRLGVRUW 2EMHFW>@D ^ 2EMHFWDX[>@  2EMHFW>@ DFORQH  PHUJH6RUW DX[DDOHQJWK  `

7KHPHUJH6RUW PHWKRGFRQWDLQV WKHVRUWDOJRULWKPDQGUHOLHVRQDQ LPSOHPHQWDWLRQRIWKHFRPSDUH7R  PHWKRGWRFRPSOHWHWKHDOJRULWKP SULYDWHVWDWLFYRLGPHUJH6RUW 2EMHFWVUF>@2EMHFWGHVW>@   LQWORZLQWKLJKLQWRII  ^

7KLQNRIWKLVDVWKH WHPSODWHPHWKRG

IRU LQWL ORZLKLJKL ^ IRU LQWM LM!ORZ  &RPSDUDEOH GHVW>M@ FRPSDUH7R &RPSDUDEOH GHVW>[email protected] !M ^ VZDS GHVWMM  ` FRPSDUH7R LVWKHPHWKRGZH 7KLVLVDFRQFUHWHPHWKRGDOUHDG\ ` QHHGWRLPSOHPHQWWR´ILOORXWµ UHWXUQ GHILQHGLQWKH$UUD\VFODVV WKHWHPSODWHPHWKRG `



&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 27

WKHWHPSODWHPHWKRGSDWWHUQ

?M¼^MOW\[WUML]KS[\W[WZ\ 4M\¼[[IaaW]PI^MIVIZZIaWN L]KS[\PI\aW]¼LTQSM\W[WZ\0W_ LWaW]LWQ\'?MTT\PM[WZ\\MUXTI\MUM\PWLQV)ZZIa[OQ^M[][\PM ITOWZQ\PUJ]\aW]VMML\W\MTTQ\PW_\WKWUXIZML]KS[_PQKPaW]LWJa QUXTMUMV\QVO\PMKWUXIZM
1RLWGRHVQ·W$UHQ·W ZHVXSSRVHGWREH VXEFODVVLQJVRPHWKLQJ",WKRXJKW WKDWZDVWKHSRLQWRI7HPSODWH 0HWKRG$QDUUD\GRHVQ·WVXEFODVV DQ\WKLQJVR,GRQ·WJHWKRZZH·G XVHVRUW 

:H·YHJRWDQDUUD\RI 'XFNVZHQHHGWRVRUW

/WWLXWQV\0MZM¼[\PMLMIT"\PMLM[QOVMZ[WN [WZ\_IV\MLQ\ \WJM][MN]TIKZW[[ITTIZZIa[[W\PMaPIL\WUISM[WZ\I[\I\QK UM\PWL\PI\KW]TLJM][MLNZWUIVa_PMZM*]\\PI\¼[WSIa Q\_WZS[ITUW[\\PM[IUMI[QN Q\_MZMQVI[]XMZKTI[[6W_ PMZMQ[WVMUWZMLM\IQT"JMKI][M[WZ\ZMITTaQ[V¼\LMÅVMLQV W]Z[]XMZKTI[[\PM[WZ\UM\PWLVMML[\WSVW_\PI\aW]¼^M QUXTMUMV\ML\PMKWUXIZM
?PI\Q[KWUXIZM
$P,JUHDWHU WKDQ\RX"

,GRQ·WNQRZ WKDW·VZKDW FRPSDUH7R WHOOVXV

\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 28

LPSOHPHQWLQJFRPSDUDEOH

+WUXIZQVO,]KS[IVL,]KS[ 7SIa[WaW]SVW_\PI\QN aW]_IV\\W[WZ\,]KS[ aW]¼ZMOWQVO\WPI^M\WQUXTMUMV\\PQ[KWUXIZM
UDEOH

5HPHPEHUZHQHHGWRLPSOHPHQWWKH&RPSD  LQWHUIDFHVLQFHZHDUHQ·WUHDOO\VXEFODVVLQJ

SXEOLFFODVV'XFNLPSOHPHQWV&RPSDUDEOH^ 6WULQJQDPH LQWZHLJKW 2XU'XFNVKDYHDQDPHDQGDZHLJKW  SXEOLF'XFN 6WULQJQDPHLQWZHLJKW ^ WKLVQDPH QDPH WKLVZHLJKW ZHLJKW `  :H·UHNHHSLQ·LWVLPSOHDOO'XFNVGR SXEOLF6WULQJWR6WULQJ ^ LVSULQWWKHLUQDPHDQGZHLJKW UHWXUQQDPH³ZHLJKV³ZHLJKW `   2ND\KHUH·VZKDWVRUWQHHGV  SXEOLFLQWFRPSDUH7R 2EMHFWREMHFW ^  FRPSDUH7R WDNHVDQRWKHU'XFNWRFRPSDUH7+,6'XFNWR 'XFNRWKHU'XFN  'XFN REMHFW  LI WKLVZHLJKWRWKHU'XFNZHLJKW ^ UHWXUQ +HUH·VZKHUHZHVSHFLI\KRZ'XFNV `HOVHLI WKLVZHLJKW RWKHU'XFNZHLJKW ^ FRPSDUH,I7+,6'XFNZHLJKVOHVV UHWXUQ WKDQRWKHU'XFNWKHQZHUHWXUQ `HOVH^WKLVZHLJKW!RWKHU'XFNZHLJKW UHWXUQ LIWKH\DUHHTXDOZHUHWXUQDQGLI ` 7+,6'XFNZHLJKVPRUHZHUHWXUQ ` `



&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 29

WKHWHPSODWHPHWKRGSDWWHUQ

4M\¼[[WZ\[WUM,]KS[ +HUH·VWKHWHVWGULYHIRUVRUWLQJ'XFNV

SXEOLFFODVV'XFN6RUW7HVW'ULYH^ SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ 'XFN>@GXFNV ^ QHZ'XFN ³'DII\´  QHZ'XFN ³'HZH\´  QHZ'XFN ³+RZDUG´  QHZ'XFN ³/RXLH´  QHZ'XFN ³'RQDOG´  QHZ'XFN ³+XH\´ `

1RWLFHWKDWZH 6\VWHPRXWSULQWOQ ³%HIRUHVRUWLQJ´  FDOO$UUD\V·VWDWL F GLVSOD\ GXFNV  PHWKRGVRUWDQG SDVVLWRXU'XFNV $UUD\VVRUW GXFNV   6\VWHPRXWSULQWOQ ³?Q$IWHUVRUWLQJ´  GLVSOD\ GXFNV  `

UD\RI :HQHHGDQDUORRNJRRG 'XFNVWKHVH

/HW·VSULQWWKHPWRVHH WKHLUQDPHVDQGZHLJKWV ,W·VVRUWWLPH

 /HW·VSULQWWKHP DJDLQ WRVHH WV HLJK GZ WKHLUQDPHVDQ

SXEOLFVWDWLFYRLGGLVSOD\ 'XFN>@GXFNV ^ IRU LQWL LGXFNVOHQJWKL ^ 6\VWHPRXWSULQWOQ GXFNV>[email protected]  ` ` `

/HWWKHVRUWLQJFRPPHQFH )LOH(GLW:LQGRZ+HOS'RQDOG1HHGV7R*R2Q$'LHW

MDYD'XFN6RUW7HVW'ULYH %HIRUHVRUWLQJ 'DII\ZHLJKV 'HZH\ZHLJKV +RZDUGZHLJKV /RXLHZHLJKV 'RQDOGZHLJKV +XH\ZHLJKV $IWHUVRUWLQJ 'HZH\ZHLJKV /RXLHZHLJKV +XH\ZHLJKV +RZDUGZHLJKV 'DII\ZHLJKV 'RQDOGZHLJKV

7KHXQVRUWHG'XFNV

7KHVRUWHG'XFNV



\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 30

EHKLQGWKHVFHQHVVRUWLQJGXFNV


%HKLQG

/HW·VWUDFHWKURXJKKRZWKH$UUD\VVRUW WHPSODWH WKH6FHQHV PHWKRGZRUNV:H·OOFKHFNRXWKRZWKHWHPSODWH PHWKRGFRQWUROVWKHDOJRULWKPDQGDWFHUWDLQ SRLQWVLQWKHDOJRULWKPKRZLWDVNVRXU'XFNVWR VXSSO\WKHLPSOHPHQWDWLRQRIDVWHS IRU LQWL ORZLKLJKL ^



.QZ[\_MVMMLIVIZZIaWN ,]KS["

FRPSDUH7R  VZDS  `

'XFN>@GXFNV ^QHZ'XFN ³'DII\´ `






7KHVRUW PHWKRGFRQWUROV WKHDOJRULWKPQRFODVVFDQ FKDQJHWKLVVRUW FRXQWV RQD&RPSDUDEOHFODVVWR SURYLGHWKHLPSOHPHQWDWLRQRI FRPSDUH7R


GXFNV>@FRPSDUH7R GXFNV>@ 

)LUVW'XFN 

FRPSDUH7R WR6WULQJ

'XFNWRFRPSDUHLWWR

1RLQKHULWDQFHXQOLNH DW\SLFDOWHPSODWH PHWKRG

1N \PM,]KS[IZMVW\QV[WZ\MLWZLMZ\PMa¼ZM[_IXXML_Q\P \PMKWVKZM\M[_IXUM\PWLQV)ZZIa[" VZDS

$UUD\V VRUW VZDS






Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 31

WKHWHPSODWHPHWKRGSDWWHUQ

there are no

Dumb Questions

Q:

WKHDOJRULWKPWRWKHLWHPVEHLQJVRUWHG

,VWKLVUHDOO\WKH7HPSODWH 0HWKRG3DWWHUQRUDUH\RXWU\LQJWRR KDUG"

A:

7KHSDWWHUQFDOOVIRULPSOHPHQWLQJ DQDOJRULWKPDQGOHWWLQJVXEFODVVHVVXSSO\ WKHLPSOHPHQWDWLRQRIWKHVWHSV²DQGWKH $UUD\VVRUWLVFOHDUO\QRWGRLQJWKDW%XW DVZHNQRZSDWWHUQVLQWKHZLOGDUHQ·W DOZD\VMXVWOLNHWKHWH[WERRNSDWWHUQV7KH\ KDYHWREHPRGLILHGWRILWWKHFRQWH[WDQG LPSOHPHQWDWLRQFRQVWUDLQWV 7KHGHVLJQHUVRIWKH$UUD\VVRUW PHWKRG KDGDIHZFRQVWUDLQWV,QJHQHUDO\RXFDQ·W VXEFODVVD-DYDDUUD\DQGWKH\ZDQWHGWKH VRUWWREHXVHGRQDOODUUD\V DQGHDFKDUUD\ LVDGLIIHUHQWFODVV 6RWKH\GHILQHGDVWDWLF PHWKRGDQGGHIHUUHGWKHFRPSDULVRQSDUWRI

6RZKLOHLW·VQRWDWH[WERRNWHPSODWH PHWKRGWKLVLPSOHPHQWDWLRQLVVWLOOLQWKH VSLULWRIWKH7HPSODWH0HWKRG3DWWHUQ$OVR E\HOLPLQDWLQJWKHUHTXLUHPHQWWKDW\RXKDYH WRVXEFODVV$UUD\VWRXVHWKLVDOJRULWKP WKH\·YHPDGHVRUWLQJLQVRPHZD\VPRUH IOH[LEOHDQGXVHIXO

Q:

7KLVLPSOHPHQWDWLRQRIVRUWLQJ DFWXDOO\VHHPVPRUHOLNHWKH6WUDWHJ\ 3DWWHUQWKDQWKH7HPSODWH0HWKRG 3DWWHUQ:K\GRZHFRQVLGHULW 7HPSODWH0HWKRG"

A:


XVLQJWKH$UUD\VREMHFWWRVRUWRXUDUUD\VR WKDW·VVLPLODUWR6WUDWHJ\%XWUHPHPEHU LQ6WUDWHJ\WKHFODVVWKDW\RXFRPSRVH ZLWKLPSOHPHQWVWKHHQWLUHDOJRULWKP7KH DOJRULWKPWKDW$UUD\VLPSOHPHQWVIRUVRUW LVLQFRPSOHWHLWQHHGVDFODVVWRILOOLQWKH PLVVLQJFRPSDUH7R PHWKRG6RLQWKDW ZD\LW·VPRUHOLNH7HPSODWH0HWKRG

Q:

$UHWKHUHRWKHUH[DPSOHVRI WHPSODWHPHWKRGVLQWKH-DYD$3,"

A:

@LQWRIILQWOHQ 

YiX`e gfn\i :HNQRZWKDWZHVKRXOGIDYRUFRPSRVLWLRQRYHULQKHULWDQFHULJKW":HOOWKHLPSOHPHQWHUVRIWKH VRUW WHPSODWHPHWKRGGHFLGHGQRWWRXVHLQKHULWDQFHDQGLQVWHDGWRLPSOHPHQWVRUW DVDVWDWLF PHWKRGWKDWLVFRPSRVHGZLWKD&RPSDUDEOHDWUXQWLPH+RZLVWKLVEHWWHU"+RZLVLWZRUVH"+RZ ZRXOG\RXDSSURDFKWKLVSUREOHP"'R-DYDDUUD\VPDNHWKLVSDUWLFXODUO\WULFN\"

)

YiX`e gfn\i 7KLQNRIDQRWKHUSDWWHUQWKDWLVDVSHFLDOL]DWLRQRIWKHWHPSODWHPHWKRG,QWKLVVSHFLDOL]DWLRQSULPLWLYH RSHUDWLRQVDUHXVHGWRFUHDWHDQGUHWXUQREMHFWV:KDWSDWWHUQLVWKLV"

\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 32

WKHSDLQWKRRN

;_QVOQV¼_Q\P.ZIUM[ =XVM`\WVW]Z
SXEOLFFODVV0\)UDPHH[WHQGV-)UDPH^

:H·UHH[WHQGLQ DPHWKRGXSGDWH WKDWFRQWUROVWKH DOJRULWKPIRUXSGDWLQJWKHVFUHHQ :HFDQKRRNLQWRWKDWDOJRULWKPE\ RYHUULGLQJWKHSDLQW KRRNPHWKRG

SXEOLF0\)UDPH 6WULQJWLWOH ^ VXSHU WLWOH  WKLVVHW'HIDXOW&ORVH2SHUDWLRQ -)UDPH(;,7B21B&/26(  WKLVVHW6L]H   WKLVVHW9LVLEOH WUXH  ` SXEOLFYRLGSDLQW *UDSKLFVJUDSKLFV ^ VXSHUSDLQW JUDSKLFV  6WULQJPVJ ³,UXOH´ JUDSKLFVGUDZ6WULQJ PVJ  `

'RQ·WORRNEHKLQGWKH FXUWDLQ-XVWVRPH LQLWLDOL]DWLRQKHUH

\ -)UDPH·VXSGDWHDOJRULWKPFDOOVSDLQW % RN DKR LW·V KLQJ VQRW GRH DLQW GHIDXOWS :H·UHRYHUULGLQJSDLQW DQGWHOOLQJWKH -)UDPHWRGUDZDPHVVDJHLQWKHZLQGRZ

SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ 0\)UDPHP\)UDPH QHZ0\)UDPH ³+HDG)LUVW'HVLJQ3DWWHUQV´  ` `

+HUH·VWKHPHVVDJHWKDWJHWV SDLQWHGLQWKHIUDPHEHFDXVHZH·YH KRRNHGLQWRWKHSDLQW PHWKRG



&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 33

WKHWHPSODWHPHWKRGSDWWHUQ

)XXTM\[ 7]ZÅVIT[\WXWV\PM[INIZQ"\PMIXXTM\ AW]XZWJIJTaSVW_IVIXXTM\Q[I[UITTXZWOZIU\PI\Z]V[QVI_MJXIOM)Va IXXTM\U][\[]JKTI[[)XXTM\IVL\PQ[KTI[[XZW^QLM[[M^MZITPWWS[4M\¼[\ISMITWWS I\INM_WN \PMU"

YHU 7KHLQLWKRRNDOORZVWKHDSSOHWWRGRZKDWH PH VWWL HILU WWK DSSOH WKH LWZDQWVWRLQLWLDOL]H

SXEOLFFODVV0\$SSOHWH[WHQGV$SSOHW^ 6WULQJPHVVDJH   SXEOLFYRLGLQLW ^ UHSDLQW LVDFRQFUHWHPHWKRGLQWKH$SSOHW Z VNQR QHQW RPSR YHOF HUOH PHVVDJH ³+HOOR:RUOG,¶PDOLYH´ VXSS WOHW WKD FODVV UHSDLQW  WKHDSSOHWQHHGVWREHUHGUDZQ `  7KHVWDUWKRRNDOORZVWKHDSSOHWWRGR SXEOLFYRLGVWDUW ^ VRPHWKLQJZKHQWKHDSSOHWLVMXVWDERXW PHVVDJH ³1RZ,¶PVWDUWLQJXS´ UHSDLQW  WREHGLVSOD\HGRQWKHZHESDJH `  SXEOLFYRLGVWRS ^ PHVVDJH ³2KQRZ,¶PEHLQJVWRSSHG´ JHWKH ,IWKHXVHUJRHVWRDQRWKHUSDSOHWFDQGR UHSDLQW  HDS WK DQG HG VXV VWRSKRRNL ` SLWVDFWLRQV ZKDWHYHULWQHHGVWRGRWRVWR  SXEOLFYRLGGHVWUR\ ^ DSSOHWLVJRLQJDZD\ ` $QGWKHGHVWUR\KRRNLVXVHGZKHQWKHDSSOHW  SXEOLFYRLGSDLQW *UDSKLFVJ ^ LVJRLQJWREHGHVWUR\HGVD\ZKHQWKHEURZVHU JGUDZ6WULQJ PHVVDJH  SDQHLVFORVHG:HFRXOGWU\WRGLVSOD\ ` VRPHWKLQJKHUHEXWZKDWZRXOGEHWKHSRLQW" `

:HOOORRN\KHUH2XUROGIULHQGWKH SDLQW PHWKRG$SSOHWDOVRPDNHV XVHRIWKLVPHWKRGDVDKRRN

&RQFUHWHDSSOHWVPDNHH[WHQVLYHXVHRIKRRNVWRVXSSO\WKHLU RZQEHKDYLRUV%HFDXVHWKHVHPHWKRGVDUHLPSOHPHQWHGDV KRRNVWKHDSSOHWLVQ·WUHTXLUHGWRLPSOHPHQWWKHP \RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 34

¿UHVLGHFKDWVWHPSODWHPHWKRGDQGVWUDWHJ\



;\ZI\MOa

)DFWRU\0HWKRG +H\,KHDUG WKDW

6WXMQ\¼[UMIT\PW]OPJMKIZMN]T·aW]IVL .IK\WZa5M\PWLIZMZMTI\MLIZMV¼\aW]' 1_I[R][\SQLLQVO *]\[MZQW][Ta_PI\IZMaW] LWQVOPMZM'?MPI^MV¼\PMIZLNZWUaW]QVMQOP\ KPIX\MZ[

AW]UQOP\_IV\\WZMUQVL\PMZMILMZ_PI\ aW]¼ZMITTIJW]\[QVKMQ\¼[JMMV[WTWVO

0Ma\PI\LWM[[W]VLITW\TQSM_PI\1LW*]\ UaQV\MV\¼[ITQ\\TMLQNNMZMV\NZWUaW]Z[#UaRWJ Q[\WLMÅVM\PMW]\TQVMWN IVITOWZQ\PUJ]\ TM\Ua[]JKTI[[M[LW[WUMWN \PM_WZS


1¼LPMIZLaW]_MZMWV\PMÅVITLZIN\WN aW]Z KPIX\MZIVL1\PW]OP\1¼L[_QVOJa\W[MMPW_ Q\_I[OWQVO?MPI^MITW\QVKWUUWV[W1 \PW]OP\1UQOP\JMIJTM\WPMTX

1LWV¼\SVW_[QVKM+PIX\MZXMWXTMPI^M JMMV[\WXXQVOUMQV\PM[\ZMM\[IaQVO¹)ZMV¼\ aW]\PI\XI\\MZVº;W1\PQVS\PMaSVW__PW 1IU*]\NWZaW]Z[ISM"1LMÅVMINIUQTaWN  ITOWZQ\PU[IVLUISM\PMUQV\MZKPIVOMIJTM ;QVKMMIKPITOWZQ\PUQ[MVKIX[]TI\ML\PMKTQMV\ KIV][MLQNNMZMV\ITOWZQ\PU[MI[QTa

1¼UVW\[]ZM1¼LX]\Q\Y]Q\MTQSM\PI\IVL IVa_Ia1¼UVW\[\]KS][QVOQVPMZQ\IVKMNWZ ITOWZQ\PUQUXTMUMV\I\QWV[1WNNMZKTQMV\[I KPWQKMWN ITOWZQ\PUQUXTMUMV\I\QWV\PZW]OP WJRMK\KWUXW[Q\QWV

&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 35

WKHWHPSODWHPHWKRGSDWWHUQ


;\ZI\MOa

AW]UQOP\JMITQ\\TMUWZMMNÅKQMV\R][\ITQ\\TM IVLZMY]QZMNM_MZWJRMK\[)VLaW]UQOP\IT[W JMITQ\\TMTM[[KWUXTQKI\MLQVKWUXIZQ[WV\W UaLMTMOI\QWVUWLMTJ]\1¼UUWZMÆM`QJTM JMKI][M1][MWJRMK\KWUXW[Q\QWV?Q\PUM KTQMV\[KIVKPIVOM\PMQZITOWZQ\PU[I\Z]V\QUM [QUXTaJa][QVOILQNNMZMV\[\ZI\MOaWJRMK\ +WUMWV\PMaLQLV¼\KPWW[MUMNWZ+PIX\MZ NWZVW\PQVO

AMIP_MTT1¼UZMITPIXXaNWZaIJ]\LWV¼\ NWZOM\1¼U\PMUW[\][MLXI\\MZVIZW]VL ?Pa'*MKI][M1XZW^QLMIN]VLIUMV\IT UM\PWLNWZKWLMZM][M\PI\ITTW_[[]JKTI[[M[\W [XMKQNaJMPI^QWZ1¼U[]ZMaW]KIV[MM\PI\\PQ[ Q[XMZNMK\NWZKZMI\QVONZIUM_WZS[ AMIP1O]M[[J]\_PI\IJW]\LMXMVLMVKa' AW]¼ZM_IaUWZMLMXMVLMV\\PIVUM 0W_¼[\PI\'5a[]XMZKTI[[Q[IJ[\ZIK\ *]\aW]PI^M\WLMXMVLWVUM\PWL[ QUXTMUMV\MLQVaW]Z[]XMZKTI[[_PQKPIZMXIZ\ WN aW]ZITOWZQ\PU1LWV¼\LMXMVLWVIVaWVM# 1KIVLW\PMMV\QZMITOWZQ\PUUa[MTN 4QSM1[IQL;\ZI\MOa1¼UZMITPIXXaNWZaW]
7SIaWSIaLWV¼\OM\\W]KPa1¼TTTM\aW] _WZSJ]\TM\UMSVW_QN aW]VMMLUa[XMKQIT \MKPVQY]M[IVa_Ia1¼UIT_Ia[OTIL\WPMTX

/W\Q\,WV¼\KITT][_M¼TTKITTaW] \RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 36

FURVVZRUGSX]]OH

1\¼[\PI\\QUMIOIQV







  



























 20 2$&6 31$1  0 2'$0 2' (-'$0(2 -"$

 6/$ .% 1.02 31$# (- 00 61  '$ 0 ,$ '..* ,$2'.# 2' 2 5$ .4$00.#$ 2. /0(-2  3+$  '$ $,/+ 2$ $2'.#  22$0- 31$1  2. #$%$0 (,/+$,$-2 2(.- 2. .2'$0 "+ 11$1  .%%$$ -#   .-2 " ++ 31 5$++ " ++ 6.3 (1 *-.5- 1 2'$  0(-"(/+$   2$,/+ 2$ ,$2'.# #$%(-$1 2'$ 12$/1 .%    - 2'(1 "' /2$0 5$ & 4$ 6.3 ,.0$    '$ 2$,/+ 2$ ,$2'.# (1 313 ++6 #$%(-$# (-  "+ 11   + 11 2' 2 +(*$1 5$! / &$1

  +&.0(2', 12$/1 0$ (,/+$,$-2$# !6 '..* ,$2'.#1   "2.06 $2'.# (1  .% $,/+ 2$ $2'.#

 '$ 12$/1 (- 2'$ +&.0(2', 2' 2 ,312 !$ 13//+($# !6 2'$ 13!"+ 11$1 0$ 313 ++6 #$"+ 0$#   3$6 .3($ -# $5$6 ++ 5$(&'  /.3-#1   ,$2'.# (- 2'$ !120 "2 13/$0"+ 11 2' 2 #.$1 -.2'(-& .0 /0.4(#$1 #$% 3+2 !$' 4(.0 (1 " ++$#  ,$2'.#  (& '$ #$# / 22$0 30 % 4.0(2$ ".%%$$ 1'./ (- !)$"24(++$   '$ 00 61 "+ 11 (,/+$,$-21 (21 2$,/+ 2$ ,$2'.# 1  ,$2'.#

&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 37

WKHWHPSODWHPHWKRGSDWWHUQ


RW LQWHUIDFHVQ 3URJUDPWR RQV WL LPSOHPHQWD HGGHVLJQV RRVHO\FRXSO 6WULYHIRUOMHFWVWKDWLQWHUDFW EHWZHHQRE UH[WHQVLRQ GEHRSHQIR &ODVVHVVKRXO RUPRGLILFDWLRQ EXWFORVHGI 'RQRW EVWUDFWLRQV 'HSHQGRQD QFUHWHFODVVHV FR GHSHQGRQ GV R\RXUIULHQ 2QO\WDONW X OO ZH·OOFD \R 'RQ·WFDOOXV

223DWWHUQV

WKHVWHSVRIDQDOJRULWKP GHIHUULQJWRVXEFODVVHVIRUWKH LPSOHPHQWDWLRQRIWKRVHVWHSV

ƒ 7KH7HPSODWH0HWKRG

22%DVLFV

3DWWHUQJLYHVXVDQLPSRUWDQW WHFKQLTXHIRUFRGHUHXVH

$EVWUDFWLRQ Q (QFDSVXODWLR  HV UL YD W KD P Z (QFDSVXODWH RO\PRUSKLV HULWDQFH 3 LRQRYHULQK VLW SR RP F ,QKHULWDQFH )DYRU

V 223ULQFLSOH

:MDD=LHGAFLK

ƒ $´WHPSODWHPHWKRGµGHILQHV

ƒ 7KHWHPSODWHPHWKRG·V DEVWUDFWFODVVPD\GHILQH FRQFUHWHPHWKRGVDEVWUDFW PHWKRGVDQGKRRNV

ƒ $EVWUDFWPHWKRGVDUH LPSOHPHQWHGE\VXEFODVVHV

ƒ +RRNVDUHPHWKRGVWKDWGR UHPLQGV WSULQFLSOH 2XUQHZHV RXUVXSHUFODVVHV \RXWKDW\JWKHVKRZVROHW  DUHUXQQLQ RXUVXEFODVVHVZKHQ  WKHPFDOO\ GHGMXVWOLNHWKH\ WKH\·UHQHHZRRG GRLQ+ROO\

$QGRXUQHZHVWSDWWHUQ OHWVFODVVHVLPSOHPHQWLQJ DQDOJRULWKPGHIHUVRPH VWHSVWRVXEFODVVHV

PDQV\ RI LO\R QHDOJRWRULWKLRPQD DILQDP HVD O GHI GUHILQHV LW W G P  \ KH DG HJ W YH K YLWGLFLQHDOHDQO\DQ VR HV U DW V DF UR DN 3 VH WU FW  WW 6 2E MHRU\GRU\QKD $QDPRE QG UHWZD) R RQ W HH HI KP W F K LWODP ' D DF HW U LW H OJ R E  HV F D FW DO G F\ H DW RVIWEXWDV  KH  F MH R XO HQ D ' W HV K WH RE G SV U LOL W WV QHHVDWVLQWDJIDP LW OH DQJ HQFDGHSHQ$EVW F WR0 WH LHRFWUDUVIF\RU KDJ\ EM 6WH W OLW  RQXWO\K W R UH EL H VKR VH H D HG F DQ VL EO MH DV X EO ) J RQ DW HD FO [L DW RE LQ QJ VS SG OH D LWHF H WK DW KD I UH Z X H V DFV\DFSU DUH QW QGXUMHFWV[W FLHHQ H OLH D LQWHZUFKHQLQRQWHUIRU R GW (QVRE LQVJ ILGFRRULI RPW HGRY D VVGW Q HQVWH IUOH DOSUHRLTXQWHVHVW OD RW F WO Q UI H HG HQ FK UH WH DW J RU HS KL VX D QG LQ D RU Z LQ I H SH DS WV U HF 6 G H QJ GH QF YLFODVVH GODJOORE RWRODVVVXEHV 'HQUHGHQ DVQDHF WHLYGVXH FOFH G YDU\GLQ ODDW HS GVLQGLGFUSHW(URH0 VD\RUHXTX HWLQVJ D WH P EF RQ P DQ VX KR R WW F VW DS HW OH LU & W LQ QF O\ \ OH KH UQ H ( DO HE W RQ\LQJ HGDS)MHWDF XWHVW HWWRUU\WKHU HURQHEWDS\RLWVXKOHWK UHHQTX FW LQIJ DHU\R HLIV DXDOWRWHPVSDWHFLQLRLFLIVW L WW RE DW $ WR \ WH G WL Q V OD OLW D DQ HV QD DV FF Z WL D V QF QW HQ IXQFW RI GDVHID) FW WDGH HU HFOLDLHWK(QW QWHUVHEHTZLW\WXHKKOHVWRGWWVGLIDLQIJQG MH HIXWLQHWKH  QVDL] LRE Q F HU '\R HU D HW P V UD DV FO SD U H LH D FO WK RJ  O H 0 H FW L] RU HUDQHQRWSHUDWLRQ W D MH H HU I V OD RE HX LI HW S VW KGPLQQG QTXHP HRUFOHUOLHRJ HT DVWVPD7 SD HVUD QVZULLWXHWK LRVJR QWU DW DO VXEFODVVUHHVTX Q H RS D HX L] I H TX HU R EO  V DVVH V H RQ RSHUODW WV HVUDXQ XHVW VXEDFOQG TXSDW VNPGHOHWRDHWRD UHRU VWR HSQVHT VXSS VWLRU RUHRJ EOHXHVHRP GULTXQJ HVUHGHILQ XQWVHU VV  OD QV EF LR VX GHVHI DW VXSSUHRUTXW V KRHUGOHW HWRS KRXW RDEOH VXSS7RUHPWSOXQDWGH0 VRIDQDOJRULWKPZLW  UH HS VW WX Q XF DL WU UW V FH DOJRULWKP·V FKDQJLQJWKH

QRWKLQJRUGHIDXOWEHKDYLRULQ WKHDEVWUDFWFODVVEXWPD\EH RYHUULGGHQLQWKHVXEFODVV

ƒ 7RSUHYHQWVXEFODVVHVIURP FKDQJLQJWKHDOJRULWKPLQWKH WHPSODWHPHWKRGGHFODUHWKH WHPSODWHPHWKRGDVILQDO

ƒ 7KH+ROO\ZRRG3ULQFLSOHJXLGHV XVWRSXWGHFLVLRQPDNLQJLQ KLJKOHYHOPRGXOHVWKDWFDQ GHFLGHKRZDQGZKHQWRFDOO ORZOHYHOPRGXOHV

ƒ
ƒ 7KH6WUDWHJ\DQG7HPSODWH 0HWKRG3DWWHUQVERWK HQFDSVXODWHDOJRULWKPVRQH E\LQKHULWDQFHDQGRQHE\ FRPSRVLWLRQ

ƒ 7KH)DFWRU\0HWKRGLVD VSHFLDOL]DWLRQRI7HPSODWH 0HWKRG \RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 38

H[HUFLVHVROXWLRQV

6KDUSHQ \RXU SHQFLO

([HUFLVH VROXWLRQV

'UDZWKHQHZFODVVGLDJUDPQRZWKDWZH¶YHPRYHG SUHSDUH5HFLSH LQWRWKH&DIIHLQH%HYHUDJHFODVV

&DIIHLQH%HYHUDJH SUHSDUH5HFLSH ERLO:DWHU SRXU,Q&XS EUHZ DGG&RQGLPHQWV

&RIIHH EUHZ DGG&RQGLPHQWV

7HD EUHZ DGG&RQGLPHQWV

5I\KPMIKPXI\\MZV_Q\PQ\[LM[KZQX\QWV"



3DWWHUQ

'HVFULSWLRQ

Template Method

Encapsulate interchangable behaviors and use delegation to decide which behavior to use

Strategy

Subclasses decide how to implement steps in an algorithm

Factory Method

Subclasses decide which concrete classes to create

&KDSWHU

Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 8. Encapsulating Algorithms

Return to Table of Contents

Page 39

WKHWHPSODWHPHWKRGSDWWHUQ

([HUFLVHVROXWLRQV







 







!

 

" 











"













!











!











"

"











 



% $ 





!

" 

& 





"







 











"

 



$



"







!









"

 





"





















"









!





#



"

&

%



&

"





"





"

"







\RXDUHKHUH



Chapter 8. Encapsulating Algorithms Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Table of Contents Chapter 9. Well-Managed Collections.......................................................... 1 Section 9.1. Breaking News: Objectville Diner Merge and Objectville Pancake House................................................................ 2 Section 9.2. Check out the Menu Items.......................................................................................................................................... 3 Section 9.3. Lou and Mel's Menu implementations....................................................................................................................... 4 Section 9.4. What's the problem with having two different menu representations?.................................................................... 6 Section 9.5. Sharpen your pencil.................................................................................................................................................... 8 Section 9.6. What now?.................................................................................................................................................................. 8 Section 9.7. Can we encapsulate the iteration?.............................................................................................................................. 9 Section 9.8. Meet the Iterator Pattern........................................................................................................................................... 11 Section 9.9. Adding an Iterator to DinerMenu............................................................................................................................. 12 Section 9.10. Reworking the Diner Menu with Iterator............................................................................................................... 13 Section 9.11. Exercise..................................................................................................................................................................... 13 Section 9.12. Fixing up the Waitress code.................................................................................................................................... 14 Section 9.13. Testing our code....................................................................................................................................................... 15 Section 9.14. What have we done so far?...................................................................................................................................... 16 Section 9.15. What we have so far................................................................................................................................................. 17 Section 9.16. Making some improvements................................................................................................................................... 18 Section 9.17. There are no Dumb Questions................................................................................................................................. 18 Section 9.18. Cleaning things up with java.util.Iterator............................................................................................................... 19 Section 9.19. We are almost there................................................................................................................................................ 20 Section 9.20. What does this get us?............................................................................................................................................. 21 Section 9.21. Iterator Pattern defined.......................................................................................................................................... 22 Section 9.22. There are no Dumb Questions................................................................................................................................ 24 Section 9.23. Single Responsibility............................................................................................................................................... 25 Section 9.24. BRAIN POWER....................................................................................................................................................... 26 Section 9.25. BRAIN2 POWER..................................................................................................................................................... 26 Section 9.26. Taking a look at the Café Menu.............................................................................................................................. 28 Section 9.27. Sharpen your pencil................................................................................................................................................ 28 Section 9.28. Reworking the Café Menu code.............................................................................................................................. 29 Section 9.29. Code Up Close......................................................................................................................................................... 29 Section 9.30. Adding the Café Menu to the Waitress.................................................................................................................. 30 Section 9.31. Breakfast, lunch AND dinner.................................................................................................................................. 31 Section 9.32. What did we do?...................................................................................................................................................... 32 Section 9.33. We decoupled the Waitress.................................................................................................................................... 32 Section 9.34. ... and we made the Waitress more extensible....................................................................................................... 33 Section 9.35. But there's more!..................................................................................................................................................... 33 Section 9.36. Iterators and Collections......................................................................................................................................... 34 Section 9.37. Iterators and Collections in Java 5......................................................................................................................... 35 Section 9.38. Code Magnets.......................................................................................................................................................... 36 Section 9.39. Is the Waitress ready for prime time?.................................................................................................................... 37 Section 9.40. Just when we thought it was safe........................................................................................................................... 39 Section 9.41. What do we need?................................................................................................................................................... 40 Section 9.42. The Composite Pattern defined.............................................................................................................................. 42 Section 9.43. Designing Menus with Composite.......................................................................................................................... 45 Section 9.44. Implementing the Menu Component..................................................................................................................... 46 Section 9.45. Implementing the Menu Item................................................................................................................................. 47 Section 9.46. Implementing the Composite Menu...................................................................................................................... 48 Section 9.47. Getting ready for a test drive.................................................................................................................................. 50 Section 9.48. Now for the test drive.............................................................................................................................................. 51 Section 9.49. Getting ready for a test drive.................................................................................................................................. 52 Section 9.50. Flashback to Iterator............................................................................................................................................... 54 Section 9.51. The Composite Iterator........................................................................................................................................... 55 Section 9.52. The Null Iterator..................................................................................................................................................... 58 Section 9.53. Give me the vegetarian menu................................................................................................................................. 59 Section 9.54. The magic of Iterator & Composite together.......................................................................................................... 60 Section 9.55. Patterns Exposed..................................................................................................................................................... 62 Section 9.56. WHO DOES WHAT?............................................................................................................................................... 65

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Section 9.57. Tools for your Design Toolbox................................................................................................................................ 66 Section 9.58. Exercise solutions................................................................................................................................................... 67 Section 9.59. Code Magnets Solution........................................................................................................................................... 68 Section 9.60. WHO DOES WHAT?.............................................................................................................................................. 69 Section 9.61. Exercise Solution..................................................................................................................................................... 70

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 1

the Iterator and Composite Patterns

4

:HOO0DQDJHG 5 &ROOHFWLRQV

4


7KHUHDUHORWVRI ZD\VWRVWXII REMHFWVLQWRDFROOHFWLRQ3XWWKHP LQDQ$UUD\D6WDFND/LVWD+DVKWDEOHWDNH\RXUSLFN(DFKKDVLWVRZQDGYDQWDJHVDQG WUDGHRIIV%XWDWVRPHSRLQW\RXUFOLHQWLVJRLQJWRZDQWWRLWHUDWHRYHUWKRVHREMHFWVDQG ZKHQKHGRHVDUH\RXJRLQJWRVKRZKLP\RXULPSOHPHQWDWLRQ":HFHUWDLQO\KRSHQRW 7KDWMXVWZRXOGQ¶WEHSURIHVVLRQDO:HOO\RXGRQ¶WKDYHWRULVN\RXUFDUHHU\RX¶UHJRLQJ WRVHHKRZ\RXFDQDOORZ\RXUFOLHQWVWRLWHUDWHWKURXJK\RXUREMHFWVZLWKRXWHYHUJHWWLQJ DSHHNDWKRZ\RXVWRUH\RXUREMHFWV


Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 2

ELJQHZV

*ZMISQVO6M_["7JRMK\ ^QTTM,QVMZIVL 7JRMK\ ^QTTM8IVKISM0W][M5MZOM
7KH\ ZDQWWRXVH P\3DQFDNH+RXVH PHQXDVWKHEUHDNIDVWPHQX DQGWKH'LQHU·VPHQXDVWKH OXQFKPHQX:H·YHDJUHHGRQ DQLPSOHPHQWDWLRQIRUWKH PHQXLWHPV

EXWZHFDQ·WDJUHHRQ KRZWRLPSOHPHQWRXUPHQXV 7KDWMRNHURYHUWKHUHXVHGDQ $UUD\/LVWWRKROGKLVPHQXLWHPVDQG ,XVHGDQ$UUD\1HLWKHURQHRIXVLV ZLOOLQJWRFKDQJHRXULPSOHPHQWDWLRQV ZHMXVWKDYHWRRPXFKFRGHZULWWHQ WKDWGHSHQGVRQWKHP

/RX 0HO



&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 3

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

+PMKSW]\\PM5MV]1\MU[ )\TMI[\4W]IVL5MTIOZMMWV\PM QUXTMUMV\I\QWVWN \PM5MV]1\MU[ 4M\¼[KPMKSW]\\PMQ\MU[WVMIKP UMV]IVLIT[W\ISMITWWSI\\PM QUXTMUMV\I\QWV

IOXQFKLWHPV 7KH'LQHUPHQXKDVORWVR HFRQVLVWVRI RXV H+ FDN ZKLOHWKH3DQ HQXLWHPKDVD EUHDNIDVWLWHPV(YHU\P DQGDSULFH LRQ LSW VFU GH HD QDP

SXEOLFFODVV0HQX,WHP^ 6WULQJQDPH 6WULQJGHVFULSWLRQ ERROHDQYHJHWDULDQ GRXEOHSULFH  SXEOLF0HQX,WHP 6WULQJQDPH 6WULQJGHVFULSWLRQ ERROHDQYHJHWDULDQ GRXEOHSULFH  ^ WKLVQDPH QDPH WKLVGHVFULSWLRQ GHVFULSWLRQ WKLVYHJHWDULDQ YHJHWDULDQ WKLVSULFH SULFH `  SXEOLF6WULQJJHW1DPH ^ UHWXUQQDPH `  SXEOLF6WULQJJHW'HVFULSWLRQ ^ UHWXUQGHVFULSWLRQ `  SXEOLFGRXEOHJHW3ULFH ^ UHWXUQSULFH `  SXEOLFERROHDQLV9HJHWDULDQ ^ UHWXUQYHJHWDULDQ ` `

1DLGEVXKNNG&KPGT

6EGETARIAN" ,4 &AKIN "AC  ONWITHLETTUC ETOMATOON WHOLEWHE AT ",4 "ACONWITH  LETTUCETOMA TOONWHOLEW 3OUPOFTHED HEAT AY !BOWLOFTH ESOUPOFTHED +  "S AY W ASIDEOFP 0A ITH NCAKE"REAKFAST OTATOSALAD 0ANCAKES (OT$OG  WITHSCRAMBLE DEGGS ANDTO !HOTDOG AST WITHSAURKRAU 2E T RELISH GU ON TOPPEDW LAR 0A NCAKE"REAKFA IONS ITHCH ST 0ANCAKES 3TEAMED6EGG EESE  WI THFRIEDEGGS S IES AUSAGE !MEDLEYOF AND"ROWN2ICE  STEAMEDVEGE "LU EBOV TABLES ERRER Y0 ANCAKEES BRO 0ANCAKES WNRIC  MADEWITHFRE SHBLUEBERRIES ANDBLUEBE RRYSYRUP

1DLGEVXKNNG2CPECMG* QWUG

7AFmES 7AFmES W  ITHYOURCHOIC EOFBLUEBERRIE ORSTRAWBE S RRIES

$0HQX,WHPFRQVLVWV DIODJWRLQGLFDWHLIRIDQDPHDGHVFULSWLRQ DQGDSULFH
7KHVHJHWWHUPHWKRGV OHW\RXDFFHVVWKHILHOGV RIWKHPHQXLWHP

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 4

WZRPHQXV

4W]IVL5MT¼[5MV]QUXTMUMV\I\QWV[ 6W_TM\¼[\ISMITWWSI\_PI\4W]IVL5MTIZM IZO]QVOIJW]\
,XVHGDQ$UUD\/LVW VR,FDQHDVLO\ H[SDQGP\PHQX

WKH

+HUH·V/RX·VLPSOHPHQWDWLRQRI 3DQFDNH+RXVHPHQX

^ SXEOLFFODVV3DQFDNH+RXVH0HQXLPSOHPHQWV0HQX^ $UUD\/LVWPHQX,WHPV  /RX·VXVLQJDQ$UUD\/LVWWRVWRUH SXEOLF3DQFDNH+RXVH0HQX ^ KLVPHQXLWHPV PHQX,WHPV QHZ$UUD\/LVW   DGG,WHP ³. %¶V3DQFDNH%UHDNIDVW´ ³3DQFDNHVZLWKVFUDPEOHGHJJVDQGWRDVW´ WUXH (DFKPHQXLWHPLVDGGHGWRWKH   $UUD\/LVWKHUHLQWKHFRQVWUXFWRU  D DGG,WHP ³5HJXODU3DQFDNH%UHDNIDVW´ (DFK0HQX,WHPKDVDQDPH LW·VD ³3DQFDNHVZLWKIULHGHJJVVDXVDJH´ GHVFULSWLRQZKHWKHURUQRW IDOVH YHJHWDULDQLWHPDQGWKHSULFH    DGG,WHP ³%OXHEHUU\3DQFDNHV´ ³3DQFDNHVPDGHZLWKIUHVKEOXHEHUULHV´ WUXH    DGG,WHP ³:DIÀHV´ ³:DIÀHVZLWK\RXUFKRLFHRIEOXHEHUULHVRUVWUDZEHUULHV´ WUXH   DQHZ /RXFUHDWHVDUJXPHQW HP LW ` X HQ P D QHDFK 7RDGG SXEOLFYRLGDGG,WHP 6WULQJQDPH6WULQJGHVFULSWLRQ HFWSDVVLQJL 0HQX,WHPREM VLWWRWKH$UUD\/LVW ERROHDQYHJHWDULDQGRXEOHSULFH DQGWKHQDGG ^ 0HQX,WHPPHQX,WHP QHZ0HQX,WHP QDPHGHVFULSWLRQYHJHWDULDQSULFH  PHQX,WHPVDGG PHQX,WHP  `  PHQXLWHPV 7KHJHW0HQX,WHPV PHWKRGUHWXUQVWKHOLVWRI SXEOLF$UUD\/LVWJHW0HQX,WHPV ^ UHWXUQPHQX,WHPV ` UPHQXFRGHWKDWGHSHQGV RWKHUPHQXPHWKRGVKHUH `



/RXKDVDEXQFKRIRWKH +HGRHVQ·WZDQW RQWKH$UUD\/LVWLPSOHPHQWDWLRQ RGH DWF WRKDYHWRUHZULWHDOOWK

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 5

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV +DDK$Q$UUD\OLVW, XVHGD5($/$UUD\VR,FDQ FRQWUROWKHPD[LPXPVL]HRIP\PHQX DQGJHWP\0HQX,WHPVZLWKRXW KDYLQJWRXVHDFDVW

UPHQX

WDWLRQRIWKH'LQH

PSOHPHQ $QGKHUH·V0HO·VL

SXEOLFFODVV'LQHU0HQXLPSOHPHQWV0HQX^ ^ QJDQ$UUD\VRKH 0HOWDNHVDGLIIHUHQWDSSURDFKKH·VXVL GUHWULHYHPHQX VWDWLF¿QDOLQW0$;B,7(06  QXDQ HPH FDQFRQWUROWKHPD[VL]HRIWK LQWQXPEHU2I,WHPV  HFWV LWHPVRXWZLWKRXWKDYLQJWRFDVWKLVREM 0HQX,WHP>@PHQX,WHPV  SXEOLF'LQHU0HQX ^ /LNH/RX0HOFUHDWHVKLVPHQXLWHPVLQWKH PHQX,WHPV QHZ0HQX,WHP>0$;B,7([email protected]  FRQVWUXFWRUXVLQJWKHDGG,WHP KHOSHUPHWKRG DGG,WHP ³9HJHWDULDQ%/7´ ³ )DNLQ¶ %DFRQZLWKOHWWXFH WRPDWRRQZKROHZKHDW´WUXH  DGG,WHP ³%/7´ ³%DFRQZLWKOHWWXFH WRPDWRRQZKROHZKHDW´IDOVH  DGG,WHP ³6RXSRIWKHGD\´ ³6RXSRIWKHGD\ZLWKDVLGHRISRWDWRVDODG´IDOVH  DGG,WHP ³+RWGRJ´ ³$KRWGRJZLWKVDXUNUDXWUHOLVKRQLRQVWRSSHGZLWKFKHHVH´ IDOVH  DFRXSOHRIRWKHU'LQHU0HQXLWHPVDGGHGKHUH ` DGG,WHP WDNHVDOOWKHSDUDPHWHUV  QHFHVVDU\WRFUHDWHD0HQX,WHPDQG H SXEOLFYRLGDGG,WHP 6WULQJQDPH6WULQJGHVFULSWLRQ LQVWDQWLDWHVRQH,WDOVRFKHFNVWRPDN ERROHDQYHJHWDULDQGRXEOHSULFH  VXUHZHKDYHQ·WKLWWKHPHQXVL]HOLPLW ^ 0HQX,WHPPHQX,WHP QHZ0HQX,WHP QDPHGHVFULSWLRQYHJHWDULDQSULFH  LI QXPEHU2I,WHPV! 0$;B,7(06 ^ 6\VWHPHUUSULQWOQ ³6RUU\PHQXLVIXOO&DQ¶WDGGLWHPWRPHQX´  `HOVH^ 0HOVSHFLILFDOO\ZDQWVWRNHHSKLVPHQXXQGHUD PHQX,WHPV>QXPEHU2I,[email protected] PHQX,WHP QXPEHU2I,WHPV QXPEHU2I,WHPV FHUWDLQVL]H SUHVXPDEO\VRKHGRHVQ·WKDYHWR ` UHPHPEHUWRRPDQ\UHFLSHV  `  SXEOLF0HQX,WHP>@JHW0HQX,WHPV ^ JHW0HQX,WHPV UHWXUQVWKHDUUD\RIPHQXLWHPV UHWXUQPHQX,WHPV `  /LNH/RX0HOKDVDEXQFKRIFRGHWKDWGHSHQGVRQWKHLPSOHPHQWDWLRQRI RWKHUPHQXPHWKRGVKHUH KLVPHQXEHLQJDQ$UUD\+H·VWRREXV\FRRNLQJWRUHZULWHDOORIWKLV ` \RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 6

MDYDHQDEOHGZDLWUHVV

?PI\¼[\PMXZWJTMU_Q\PPI^QVO\ _WLQNNMZMV\ UMV]ZMXZM[MV\I\QWV['
UHVVLV 7KH:DLW DYDHQDEOHG J JHWWLQ

4M\¼[KPMKSW]\\PM[XMKIVL\PMV[\MX\PZW]OP_PI\Q\UQOP\\ISM\W QUXTMUMV\PMZ




7KHVSHFIRU WKH:DLWUHVV

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 7

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

4M\¼[[\IZ\Ja[\MXXQVO\PZW]OPPW__M¼LQUXTMUMV\\PMXZQV\5MV]UM\PWL"




7KHPHWKRGORRNV WKHVDPHEXWWKH FDOOVDUHUHWXUQLQJ GLIIHUHQWW\SHV

3DQFDNH+RXVH0HQXSDQFDNH+RXVH0HQX QHZ3DQFDNH+RXVH0HQX  $UUD\/LVWEUHDNIDVW,WHPV SDQFDNH+RXVH0HQXJHW0HQX,WHPV  'LQHU0HQXGLQHU0HQX QHZ'LQHU0HQX  0HQX,WHP>@OXQFK,WHPV GLQHU0HQXJHW0HQX,WHPV 



7KHLPSOHPHQWDWLRQ LVVKRZLQJWKURXJK EUHDNIDVWLWHPVDUH LQDQ$UUD\/LVWOXQFK LWHPVDUHLQDQ$UUD\

6W_\WXZQV\W]\\PMQ\MU[NZWU\PM8IVKISM0W][M5MV]_M¼TTTWWX\PZW]OP\PM Q\MU[WV\PMJZMISNI[\1\MU[)ZZIa4Q[\)VL\WXZQV\W]\\PM,QVMZQ\MU[_M¼TT TWWX\PZW]OP\PM)ZZIa IRU LQWL LEUHDNIDVW,WHPVVL]H L ^ 0HQX,WHPPHQX,WHP  0HQX,WHP EUHDNIDVW,WHPVJHW L  6\VWHPRXWSULQW PHQX,WHPJHW1DPH ³³  6\VWHPRXWSULQWOQ PHQX,WHPJHW3ULFH ³³  6\VWHPRXWSULQWOQ PHQX,WHPJHW'HVFULSWLRQ  ` IRU LQWL LOXQFK,WHPVOHQJWKL ^ 0HQX,WHPPHQX,WHP OXQFK,WHPV>[email protected] 6\VWHPRXWSULQW PHQX,WHPJHW1DPH ³³  6\VWHPRXWSULQWOQ PHQX,WHPJHW3ULFH ³³  6\VWHPRXWSULQWOQ PHQX,WHPJHW'HVFULSWLRQ  `



1RZZHKDYHWR LPSOHPHQWWZRGLIIHUHQW ORRSVWRVWHSWKURXJK WKHWZRLPSOHPHQWDWLRQV RIWKHPHQXLWHPV RQHORRSIRUWKH $UUD\/LVW DQGDQRWKHUIRU WKH$UUD\

1UXTMUMV\QVOM^MZaW\PMZUM\PWLQV\PM?IQ\ZM[[Q[OWQVO\WJMI^IZQI\QWVWN  \PQ[\PMUM?M¼ZMIT_Ia[OWQVO\WVMML\WOM\JW\PUMV][IVL][M\_WTWWX[\W Q\MZI\M\PZW]OP\PMQZQ\MU[1N IVW\PMZZM[\I]ZIV\_Q\PILQNNMZMV\QUXTMUMV\I\QWV Q[IKY]QZML\PMV_M¼TTPI^M\PZMMTWWX[

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Page 8

Return to Table of Contents

ZKDW¶VWKHJRDO

6KDUSHQ \RXU SHQFLO *I[MLWVW]ZQUXTMUMV\I\QWVWN XZQV\5MV]_PQKPWN \PMNWTTW_QVOIXXTa'

R) ?MIZMKWLQVO\W\PM 8IVKISM0W][M5MV]IVL,QVMZ5MV] KWVKZM\MQUXTMUMV\I\QWV[VW\\WIV QV\MZNIKM

R*
R+ 1N _MLMKQLML\W[_Q\KPNZWU][QVO ,QVMZ5MV]\WIVW\PMZ\aXMWN UMV] \PI\QUXTMUMV\MLQ\[TQ[\WN UMV]Q\MU[ _Q\PI0I[P\IJTM_M¼LPI^M\WUWLQNa ITW\WN KWLMQV\PM?IQ\ZM[[

R+
R, ?MPI^ML]XTQKI\MKWLM"\PMXZQV\5MV] UM\PWLVMML[\_W[MXIZI\MTWWX[\W Q\MZI\MW^MZ\PM\_WLQNNMZMV\SQVL[WN  UMV][)VLQN _MILLMLI\PQZLUMV] _M¼LPI^MaM\IVW\PMZTWWX

R-
?PI\VW_' 5MTIVL4W]IZMX]\\QVO][QVILQNÅK]T\XW[Q\QWV


&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 9

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

+IV_MMVKIX[]TI\M\PMQ\MZI\QWV' 1N _M¼^MTMIZVMLWVM\PQVOQV\PQ[JWWSQ\¼[MVKIX[]TI\M_PI\^IZQM[1\¼[WJ^QW][ _PI\Q[KPIVOQVOPMZM"\PMQ\MZI\QWVKI][MLJaLQNNMZMV\KWTTMK\QWV[WN WJRMK\[ JMQVOZM\]ZVMLNZWU\PMUMV][*]\KIV_MMVKIX[]TI\M\PQ['4M\¼[_WZS \PZW]OP\PMQLMI 

7RLWHUDWHWKURXJKWKHEUHDNIDVWLWHPVZHXVHWKHVL]H DQGJHW  PHWKRGVRQWKH$UUD\/LVW

IRU LQWL LEUHDNIDVW,WHPVVL]H L ^ 0HQX,WHPPHQX,WHP  0HQX,WHP EUHDNIDVW,WHPVJHW L  ` GET

GET GET

GET

JHW KHOSVXVVWHS WKURXJKHDFKLWHP

)ZZIa4Q[\

 0H QX,WHP





 0 HQX,WHP



 0H QX,WHP



 0 HQ

X,WHP

$Q$UUD\/LVW RI0HQX,WHPV



$QGWRLWHUDWHWKURXJKWKHOXQFKLWHPVZHXVHWKH$UUD\OHQJWKÀHOGDQG WKHDUUD\VXEVFULSWQRWDWLRQRQWKH0HQX,WHP$UUD\

)ZZIa

LUNCH)TEMS;=

IRU LQWL LOXQFK,WHPVOHQJWKL ^ 0HQX,WHPPHQX,WHP OXQFK,WHPV>[email protected] `



LUNCH)TE LUNCH LUNC

:HXVHWKHDUUD\ VXEVFULSWVWRVWHS WKURXJKLWHPV

MS;=

 0 HQX,WHP



) TEMS

H) TE

 0H QX,WHP

;= 

MS;

 0H QX,WHP

= 

 0 HQ

X,WHP

$Q$UUD\RI 0HQX,WHPV \RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 10

HQFDSVXODWLQJLWHUDWLRQ



1RZZKDWLIZHFUHDWHDQREMHFWOHW·VFDOOLWDQ,WHUDWRU WKDWHQFDSVXODWHVWKHZD\ZHLWHUDWHWKURXJKD FROOHFWLRQRIREMHFWV"/HW·VWU\WKLVRQWKH$UUD\/LVW

:HDVNWKHEUHDNIDVW0HQX IRUDQLWHUDWRURILWV ,WHUDWRULWHUDWRU EUHDNIDVW0HQXFUHDWH,WHUDWRU 0HQX,WHPV $QGZKLOHWKHUHDUHPRUHLWHPVOHIW ZKLOH LWHUDWRUKDV1H[W ^ 0HQX,WHPPHQX,WHP  0HQX,WHP LWHUDWRUQH[W  ` NEXT :HJHWWKHQH[WLWHP

GET

GET

U



 , WHUDWR

GET

)ZZIa4Q[\

GET

7KHFOLHQWMXVWFDOOVKDV1H[W DQG QH[W EHKLQGWKHVFHQHVWKHLWHUDWRU FDOOVJHW RQWKH$UUD\/LVW

 0H QX,WHP





 0 HQX,WHP

 0H QX,WHP



 0 HQ



X,WHP



/HW·VWU\WKDWRQWKH$UUD\WRR

,WHUDWRULWHUDWRU OXQFK0HQXFUHDWH,WHUDWRU  ZKLOH LWHUDWRUKDV1H[W ^ 0HQX,WHPPHQX,WHP  0HQX,WHP LWHUDWRUQH[W  ` LUNCH)TEMS;= 

LUNCH)TE  , WHUDWR

U



6DPHVLWXDWLRQKHUHWKHFOLHQWMXVWFDOOV KDV1H[W DQGQH[W EHKLQGWKHVFHQHV WKHLWHUDWRULQGH[HVLQWRWKH$UUD\

)ZZIa

NEXT

:RZWKLVFRGH LVH[DFWO\WKH VDPHDVWKH EUHDNIDVW0HQX FRGH

LUNCH LUNC

MS;=

 0 HQX,WHP



) TEMS

H) TE

 0H QX,WHP

;= 

MS;

 0H QX,WHP

= 

 0 HQ



X,WHP

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Page 11

Return to Table of Contents

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

5MM\\PM1 \MZI\WZ8I\\MZV ?MTTQ\TWWS[TQSMW]ZXTIVWN MVKIX[]TI\QVOQ\MZI\QWVR][\UQOP\ IK\]ITTa_WZS#IVLI[aW]¼^MXZWJIJTaITZMILaO]M[[MLQ\Q[I ,M[QOV8I\\MZVKITTML\PM1\MZI\WZ8I\\MZV
7KHKDV1H[W PHWKRG WHOOVXVLIWKHUHDUH PRUHHOHPHQWVLQWKH DJJUHJDWHWRLWHUDWH WKURXJK

LQWHUIDFH!! ,WHUDWRU KDV1H[W QH[W

7KHQH[W PHWKRG UHWXUQVWKHQH[W REMHFWLQWKHDJJUHJDWH 6W_WVKM_MPI^M\PQ[QV\MZNIKM_MKIVQUXTMUMV\1\MZI\WZ[NWZ IVaSQVLWN KWTTMK\QWVWN WJRMK\["IZZIa[TQ[\[PI[P\IJTM[XQKS aW]ZNI^WZQ\MKWTTMK\QWVWN WJRMK\[4M\¼[[Ia_M_IV\ML\W QUXTMUMV\\PM1\MZI\WZNWZ\PM)ZZIa][MLQV\PM,QVMZ5MV]1\ _W]TLTWWSTQSM\PQ["

LQWHUIDFH!! ,WHUDWRU KDV1H[W QH[W

'LQHU0HQX,WHUDWRU KDV1H[W QH[W

:KHQ ZHVD\ &2//(&7,21ZHMXVW PHDQDJURXSRIREMHFWV7KH\ PLJKWEHVWRUHGLQYHU\GLIIHUHQW GDWDVWUXFWXUHVOLNHOLVWVDUUD\V KDVKWDEOHVEXWWKH\·UH VWLOOFROOHFWLRQV:HDOVR VRPHWLPHVFDOOWKHVH $**5(*$7(6

'LQHU0HQX,WHUDWRULVDQ LPSOHPHQWDWLRQRI,WHUDWRU WKDWNQRZVKRZWRLWHUDWH RYHUDQDUUD\RI0HQX,WHPV

4M\¼[OWIPMILIVLQUXTMUMV\\PQ[1\MZI\WZIVLPWWSQ\QV\W\PM ,QVMZ5MV]\W[MMPW_\PQ[_WZS[

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 12

PDNHDQLWHUDWRU

)LLQVOIV1\MZI\WZ\W,QVMZ5MV] 7RDGGDQ,WHUDWRUWRWKH'LQHU0HQXZHÀUVWQHHGWRGHÀQHWKH ,WHUDWRU,QWHUIDFH

SXEOLFLQWHUIDFH,WHUDWRU^ ERROHDQKDV1H[W  2EMHFWQH[W  `

+HUH·VRXUWZRPHWKRGV ROHDQ 7KHKDV1H[W PHWKRGUHWXUQVDERUHPRUH UHD WKH QRW URU HWKH LQGLFDWLQJZK HOHPHQWVWRLWHUDWHRYHU DQGWKHQH[W PHW UHWXUQVWKHQH[WHOHPKRG HQW

$QGQRZZHQHHGWRLPSOHPHQWDFRQFUHWH,WHUDWRUWKDWZRUNV IRUWKH'LQHUPHQX

:HLPSOHPHQWWKH ,WHUDWRULQWHUIDFH

SXEOLFFODVV'LQHU0HQX,WHUDWRULPSOHPHQWV,WHUDWRU^ QVWKH 0HQX,WHP>@LWHPV SRVLWLRQPDLQWDL RIWKH Q LR VLW SR LQWSRVLWLRQ  W HQ FXUU KHDUUD\  LWHUDWLRQRYHUW SXEOLF'LQHU0HQX,WHUDWRU 0HQX,WHP>@LWHPV ^ WKLVLWHPV LWHPV 7KHFRQVWUXFWRUWDNHVWKH ` DUUD\RIPHQXLWHPVZHDUH  JRLQJWRLWHUDWHRYHU SXEOLF2EMHFWQH[W ^ 0HQX,WHPPHQX,WHP LWHPV>[email protected] 7KHQH[W PHWKRGUHWXUQVWKH SRVLWLRQ SRVLWLRQ UHWXUQPHQX,WHP QH[WLWHPLQWKHDUUD\DQG ` LQFUHPHQWVWKHSRVLWLRQ  SXEOLFERROHDQKDV1H[W ^ LI SRVLWLRQ! LWHPVOHQJWK__LWHPV>[email protected] QXOO ^ UHWXUQIDOVH `HOVH^ UHWXUQWUXH ` ` %HFDXVHWKHGLQHUFKHIZHQWDKHDGDQG ` 7KHKDV1

H[W PHWKRGFKHFNVWR VHHLIZH·YHVHHQDOOWKHHOHPHQWV RIWKHDUUD\DQGUHWXUQVWUXHLI WKHUHDUHPRUHWRLWHUDWHWKURXJK



DOORFDWHGDPD[VL]HGDUUD\ZHQHHGWR FKHFNQRWRQO\LIZHDUHDWWKHHQGRI WKHDUUD\EXWDOVRLIWKHQH[WLWHPLV QXOOZKLFKLQGLFDWHVWKHUHDUHQRPRUH LWHPV

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 13

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

:M_WZSQVO\PM,QVMZ5MV]_Q\P1\MZI\WZ 2ND\ZH·YHJRWWKHLWHUDWRU7LPHWRZRUNLWLQWRWKH 'LQHU0HQXDOOZHQHHGWRGRLVDGGRQHPHWKRGWRFUHDWHD 'LQHU0HQX,WHUDWRUDQGUHWXUQLWWRWKHFOLHQW

^ SXEOLFFODVV'LQHU0HQXLPSOHPHQWV0HQX^ VWDWLF¿QDOLQW0$;B,7(06  LQWQXPEHU2I,WHPV  0HQX,WHP>@PHQX,WHPV  FRQVWUXFWRUKHUH  :H·UHQRWJRLQJWRQHHGWKHJHW0HQX,WHPV  DGG,WHPKHUH PHWKRGDQ\PRUHDQGLQIDFWZHGRQ·WZDQWLW  SXEOLF0HQX,WHP>@JHW0HQX,WHPV ^ EHFDXVHLWH[SRVHVRXULQWHUQDOLPSOHPHQWDWLRQ UHWXUQPHQX,WHPV `  SXEOLF,WHUDWRUFUHDWH,WHUDWRU ^ UHWXUQQHZ'LQHU0HQX,WHUDWRU PHQX,WHPV  +HUH·VWKHFUHDWH,WHUDWRU PHWKRG ` ,WFUHDWHVD'LQHU0HQX,WHUDWRU  IURPWKHPHQX,WHPVDUUD\DQG RWKHUPHQXPHWKRGVKHUH UHWXUQVLWWRWKHFOLHQW `

:H·UHUHWXUQLQJWKH,WHUDWRULQWHUIDFH7KHFOLHQW GRHVQ·WQHHGWRNQRZKRZWKHPHQX,WHPVDUHPDLQWDLQHG LQWKH'LQHU0HQXQRUGRHVLWQHHGWRNQRZKRZWKH 'LQHU0HQX,WHUDWRULVLPSOHPHQWHG,WMXVWQHHGVWRXVHWKH LWHUDWRUVWRVWHSWKURXJKWKHLWHPVLQWKHPHQX

([HUFLVH *RDKHDGDQGLPSOHPHQWWKH3DQFDNH+RXVH,WHUDWRU\RXUVHOIDQGPDNHWKHFKDQJHV QHHGHGWRLQFRUSRUDWHLWLQWRWKH3DQFDNH+RXVH0HQX

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 14

WKHZDLWUHVVLWHUDWHV

.Q`QVO]X\PM?IQ\ZM[[KWLM 6W__MVMML\WQV\MOZI\M\PMQ\MZI\WZKWLM QV\W\PM?IQ\ZM[[?M[PW]TLJMIJTM\WOM\ ZQLWN [WUMWN \PMZML]VLIVKaQV\PMXZWKM[[ 1V\MOZI\QWVQ[XZM\\a[\ZIQOP\NWZ_IZL"ÅZ[\_M KZMI\MIXZQV\5MV]UM\PWL\PI\\ISM[IV 1\MZI\WZ\PMV_M][M\PMOM\1\MZI\WZUM\PWLWV MIKPUMV]\WZM\ZQM^M\PM1\MZI\WZIVLXI[[Q\\W \PMVM_UM\PWL

1HZDQGLPSURYHG ZLWK,WHUDWRU

SXEOLFFODVV:DLWUHVV^ ,QWKHFRQVWUXFWRUWKH:DLWUHVV 3DQFDNH+RXVH0HQXSDQFDNH+RXVH0HQX 'LQHU0HQXGLQHU0HQX WDNHVWKHWZRPHQXV  SXEOLF:DLWUHVV 3DQFDNH+RXVH0HQXSDQFDNH+RXVH0HQX'LQHU0HQXGLQHU0HQX ^ WKLVSDQFDNH+RXVH0HQX SDQFDNH+RXVH0HQX 7KHSULQW0HQX  WKLVGLQHU0HQX GLQHU0HQX PHWKRGQRZ ` FUHDWHVWZR  SXEOLFYRLGSULQW0HQX ^ LWHUDWRUVRQHIRU ,WHUDWRUSDQFDNH,WHUDWRU SDQFDNH+RXVH0HQXFUHDWH,WHUDWRU  HDFKPHQX ,WHUDWRUGLQHU,WHUDWRU GLQHU0HQXFUHDWH,WHUDWRU  6\VWHPRXWSULQWOQ ³0(18?Q?Q%5($.)$67´  $QGWKHQFDOOVWKH SULQW0HQX SDQFDNH,WHUDWRU  RYHUORDGHGSULQW0HQX  6\VWHPRXWSULQWOQ ³?Q/81&+´  SULQW0HQX GLQHU,WHUDWRU  ZLWKHDFKLWHUDWRU ` 7HVWLIWKHUHDUH  7KHRYHUORDGHG SULYDWHYRLGSULQW0HQX ,WHUDWRULWHUDWRU ^ DQ\PRUHLWHPV SULQW0HQX  ZKLOH LWHUDWRUKDV1H[W ^ WKH *HW PHWKRGXVHV 0HQX,WHPPHQX,WHP  0HQX,WHP LWHUDWRUQH[W  QH[WLWHP WKH,WHUDWRUWR 6\VWHPRXWSULQW PHQX,WHPJHW1DPH ³³  VWHSWKURXJKWKH 6\VWHPRXWSULQW PHQX,WHPJHW3ULFH ³³  6\VWHPRXWSULQWOQ PHQX,WHPJHW'HVFULSWLRQ  PHQXLWHPVDQG ` SULQWWKHP ` 8VHWKHLWHPWR  RWKHUPHWKRGVKHUH JHWQDPHSULFH 1RWHWKDWZH·UHGRZQ ` DQGGHVFULSWLRQ WRRQHORRS

DQGSULQWWKHP



&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 15

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV


)LUVWZHFUHDWHWKHQHZPHQXV SXEOLFFODVV0HQX7HVW'ULYH^ SXEOLFVWDWLFYRLGPDLQ 6WULQJDUJV>@ ^ 3DQFDNH+RXVH0HQXSDQFDNH+RXVH0HQX QHZ3DQFDNH+RXVH0HQX  'LQHU0HQXGLQHU0HQX QHZ'LQHU0HQX   :DLWUHVVZDLWUHVV QHZ:DLWUHVV SDQFDNH+RXVH0HQXGLQHU0HQX   ZDLWUHVVSULQW0HQX  ` `

7KHQZHFUHDWHD :DLWUHVVDQGSDVV KHUWKHPHQXV

7KHQZHSULQWWKHP

0MZM¼[\PM\M[\Z]V )LOH(GLW:LQGRZ+HOS*UHHQ(JJV +DP

MDYD'LQHU0HQX7HVW'ULYH

)LUVWZHLWHUDWH

0(18 WKURXJKWKHSDQFDNH  PHQX %5($.)$67 $QGWKHQ . %¶V3DQFDNH%UHDNIDVW3DQFDNHVZLWKVFUDPEOHGHJJVDQGWRDVW WKHOXQFK 5HJXODU3DQFDNH%UHDNIDVW3DQFDNHVZLWKIULHGHJJVVDXVDJH PHQXDOO %OXHEHUU\3DQFDNHV3DQFDNHVPDGHZLWKIUHVKEOXHEHUULHV ZLWKWKH :DIÀHV:DIÀHVZLWK\RXUFKRLFHRIEOXHEHUULHVRUVWUDZEHUULHV /81&+ 9HJHWDULDQ%/7 )DNLQ¶ %DFRQZLWKOHWWXFH WRPDWRRQZKROHZKHDW %/7%DFRQZLWKOHWWXFH WRPDWRRQZKROHZKHDW 6RXSRIWKHGD\6RXSRIWKHGD\ZLWKDVLGHRISRWDWRVDODG +RWGRJ$KRWGRJZLWKVDXUNUDXWUHOLVKRQLRQVWRSSHGZLWKFKHHVH 6WHDPHG9HJJLHVDQG%URZQ5LFH6WHDPHGYHJHWDEOHVRYHUEURZQULFH 3DVWD6SDJKHWWLZLWK0DULQDUD6DXFHDQGDVOLFHRIVRXUGRXJKEUHDG

VDPH LWHUDWLRQ FRGH



\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 16

LWHUDWRUDGYDQWDJHV

?PI\PI^M_MLWVM[WNIZ' .WZ[\IZ\MZ[_M¼^MUILMW]Z7JRMK\^QTTMKWWS[ ^MZaPIXXa
:RRKRR1R FRGHFKDQJHVRWKHU WKDQDGGLQJWKH FUHDWH,WHUDWRU PHWKRG

?M¼^MIT[WPMTXMLW]Z[MT^M[QV\PMXZWKM[[
9HJJLHEXUJHU

0IZL\W5IQV\IQV ?IQ\ZM[[1UXTMUMV\I\QWV



6M_0QX ?IQ\ZM[[8W_MZMLJa1\MZI\WZ

7KH0HQXVDUHQRWZHOO HQFDSVXODWHGZHFDQVHHWKH 'LQHULVXVLQJDQ$UUD\/LVWDQGWKH 3DQFDNH+RXVHDQ$UUD\

7KH0HQXLPSOHPHQWDWLRQVDUHQRZ HQFDSVXODWHG7KH:DLWUHVVKDV QRLGHDKRZWKH0HQXVKROGWKHLU FROOHFWLRQRIPHQXLWHPV

:HQHHGWZRORRSVWRLWHUDWHWKURXJK WKH0HQX,WHPV

$OOZHQHHGLVDORRSWKDW SRO\PRUSKLFDOO\KDQGOHVDQ\ FROOHFWLRQRILWHPVDVORQJDVLW LPSOHPHQWV,WHUDWRU

7KH:DLWUHVVLVERXQGWRFRQFUHWH FODVVHV 0HQX,WHP>@DQG$UUD\/LVW 

7KH:DLWUHVVQRZXVHVDQLQWHUIDFH ,WHUDWRU 

7KH:DLWUHVVLVERXQGWRWZRGLIIHUHQW FRQFUHWH0HQXFODVVHVGHVSLWHWKHLU LQWHUIDFHVEHLQJDOPRVWLGHQWLFDO

7KH0HQXLQWHUIDFHVDUHQRZH[DFWO\ WKHVDPHDQGXKRKZHVWLOOGRQ¶W KDYHDFRPPRQLQWHUIDFHZKLFK PHDQVWKH:DLWUHVVLVVWLOOERXQGWR WZRFRQFUHWH0HQXFODVVHV:H¶G EHWWHU¿[WKDW

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 17

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

?PI\_MPI^M[WNIZ *MNWZM_MKTMIV\PQVO[]XTM\¼[OM\IJQZL¼[MaM^QM_WN W]ZK]ZZMV\LM[QOV

PHQWWKH 7KHVHWZRPHQXVLPSOH RGVEXW HWK P RI VHW VDPHH[DFW QJWKHVDPH WKH\DUHQ·WLPSOHPHQWLJWRIL[WKLV RLQ HJ H·U : DFH ,QWHUI URPDQ\ DQGIUHHWKH:DLWUHVVI 0HQXV HWH QFU GHSHQGHQFLHVRQFR

3DQFDNH+RXVH0HQX PHQX,WHPV

:H·UHQRZ XVLQJDFRPPRQ ,WHUDWRU LQWHUIDFH DQGZH·YH LPSOHPHQWHG WZRFRQFUHWH FODVVHV

7KH,WHUDWRUDOORZVWKH:DLWUHVVWREHGHFRXSOHG IURPWKHDFWXDOLPSOHPHQWDWLRQRIWKHFRQFUHWH FODVVHV6KHGRHVQ·WQHHGWRNQRZLID0HQXLV LPSOHPHQWHGZLWKDQ$UUD\DQ$UUD\/LVWRUZLWK Œ RWHV$OOVKHFDUHVLVWKDWVKHFDQJHWDQ 3RVW,WQ ,WHUDWRUWRGRKHULWHUDWLQJ

LQWHUIDFH!! ,WHUDWRU

:DLWUHVV SULQW0HQX

KDV1H[W

FUHDWH,WHUDWRU

QH[W

'LQHU0HQX PHQX,WHPV

FUHDWH,WHUDWRU

1RWHWKDWWKHLWHUDWRUJLYHXVDZD\WRVWHS WKURXJKWKHHOHPHQWVRIDQDJJUHJDWHZLWK RXW IRUFLQJWKHDJJUHJDWHWRFOXWWHULWVRZQLQWH ZLWKDEXQFKRIPHWKRGVWRVXSSRUWWUDYHUVDOUIDFH HOHPHQWV,WDOVRDOORZVWKHLPSOHPHQWDWLRQRIRILWV LWHUDWRUWROLYHRXWVLGHRIWKHDJJUHJDWHLQR WKH WKHU ZRUGVZH·YHHQFDSVXODWHGWKHLQWHUDWLRQ

3DQFDNH+RXVH0HQX,WHUDWRU

'LQHU0HQX,WHUDWRU

KDV1H[W

KDV1H[W

QH[W

QH[W

HQW QG'LQHU0HQXLPSOHP 3DQFDNH+RXVH0HQXD WRU PHWKRGWKH\DUH WKHQHZFUHDWH,WHUDWLQJWKHLWHUDWRUIRUWKHLU UHVSRQVLEOHIRUFUHDPVLPSOHPHQWDWLRQV UHVSHFWLYHPHQXLWH

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 18

LPSURYHWKHLWHUDWRU

5ISQVO[WUMQUXZW^MUMV\[ 7SIa_MSVW_\PMQV\MZNIKM[WN 8IVKISM0W][M5MV]IVL,QVMZ5MV]IZMM`IK\Ta\PM[IUM IVLaM\_MPI^MV¼\LMÅVMLIKWUUWVQV\MZNIKMNWZ\PMU;W_M¼ZMOWQVO\WLW\PI\IVL KTMIV]X\PM?IQ\ZM[[ITQ\\TMUWZM AW]UIaJM_WVLMZQVO_Pa_M¼ZMVW\][QVO\PM2I^I1\MZI\WZQV\MZNIKM·_MLQL\PI\[WaW] KW]TL[MMPW_\WJ]QTLIVQ\MZI\WZNZWU[KZI\KP6W_\PI\_M¼^MLWVM\PI\_M¼ZMOWQVO\W [_Q\KP\W][QVO\PM2I^I1\MZI\WZQV\MZNIKMJMKI][M_M¼TTOM\ITW\WN TM^MZIOMJaQUXTMUMV\QVO \PI\QV[\MILWN W]ZPWUMOZW_V1\MZI\WZQV\MZNIKM?PI\SQVLWN TM^MZIOM'AW]¼TT[WWV[MM .QZ[\TM\¼[KPMKSW]\\PMRI^I]\QT1\MZI\WZQV\MZNIKM"

7KLVORRNVMXVWOLNHRXUSUHYLRXVGHILQLWLRQ

LQWHUIDFH!! ,WHUDWRU KDV1H[W QH[W

([FHSWZHKDYHDQDGGLWLRQDOPHWKRGWKDW DOORZVXVWRUHPRYHWKHODVWLWHPUHWXUQHGE\ WKHQH[W PHWKRGIURPWKHDJJUHJDWH

UHPRYH


there are no

Dumb Questions

Q:

:KDWLI,GRQ·WZDQWWRSURYLGH WKHDELOLW\WRUHPRYHVRPHWKLQJIURPWKH XQGHUO\LQJFROOHFWLRQRIREMHFWV"

A:

7KHUHPRYH PHWKRGLVFRQVLGHUHG RSWLRQDO


Q:

+RZGRHVUHPRYH EHKDYH XQGHUPXOWLSOHWKUHDGVWKDWPD\EH XVLQJGLIIHUHQWLWHUDWRUVRYHUWKHVDPH FROOHFWLRQRIREMHFWV"

WKHUXQWLPHH[FHSWLRQ MDYDODQJ8QVXSSRUWHG2SHUDWLRQ([FHSWLRQ 7KH,WHUDWRU$3,GRFXPHQWDWLRQVSHFLILHVWKDW WKLVH[FHSWLRQPD\EHWKURZQIURPUHPRYH  DQGDQ\FOLHQWWKDWLVDJRRGFLWL]HQZLOO FKHFNIRUWKLVH[FHSWLRQZKHQFDOOLQJWKH UHPRYH PHWKRG

A:

7KHEHKDYLRURIWKHUHPRYH LV XQVSHFLILHGLIWKHFROOHFWLRQFKDQJHVZKLOH \RXDUHLWHUDWLQJRYHULW6R\RXVKRXOGEH FDUHIXOLQGHVLJQLQJ\RXURZQPXOWLWKUHDGHG FRGHZKHQDFFHVVLQJDFROOHFWLRQ FRQFXUUHQWO\

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 19

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

+TMIVQVO\PQVO[]X_Q\PRI^I]\QT1\MZI\WZ 4M\¼[[\IZ\_Q\P\PM8IVKISM0W][M5MV]KPIVOQVOQ\W^MZ\WRI^I]\QT1\MZI\WZQ[ OWQVO\WJMMI[a?MR][\LMTM\M\PM8IVKISM0W][M5MV]1\MZI\WZKTI[[ILLIV QUXWZ\RI^I]\QT1\MZI\WZ\W\PM\WXWN 8IVKISM0W][M5MV]IVLKPIVOMWVMTQVM WN \PM8IVKISM0W][M5MV]" SXEOLF,WHUDWRUFUHDWH,WHUDWRU ^ UHWXUQPHQX,WHPVLWHUDWRU  `

,QVWHDGRIFUHDWLQJRXURZQLWHUDWRU QRZZHMXVWFDOOWKHLWHUDWRU PHWKRG RQWKHPHQX,WHPV$UUD\/LVW

)VL\PI\¼[Q\8IVKISM0W][M5MV]Q[LWVM 6W__MVMML\WUISM\PMKPIVOM[\WITTW_\PM,QVMZ5MV]\W_WZS_Q\PRI^I]\QT1\MZI\WZ

)LUVWZHLPSRUWMDYDXWLO,WHUDWRUWKH LPSRUWMDYDXWLO,WHUDWRU  LQWHUIDFHZH·UHJRLQJWRLPSOHPHQW SXEOLFFODVV'LQHU0HQX,WHUDWRULPSOHPHQWV,WHUDWRU^ 0HQX,WHP>@OLVW LQWSRVLWLRQ   SXEOLF'LQHU0HQX,WHUDWRU 0HQX,WHP>@OLVW ^ WKLVOLVW OLVW ` 1RQHRIRXUFXUUHQW  LPSOHPHQWDWLRQFKDQJHV SXEOLF2EMHFWQH[W ^ LPSOHPHQWDWLRQKHUH ` EXWZHGRQHHGWRLPSOHPHQWUHPRYH   +HUHEHFDXVHWKHFKHILVXVLQJDIL[HG SXEOLFERROHDQKDV1H[W ^ VL]HG$UUD\ZHMXVWVKLIWDOOWKH LPSOHPHQWDWLRQKHUH ` HOHPHQWVXSRQHZKHQUHPRYH LVFDOOHG  SXEOLFYRLGUHPRYH ^ LI SRVLWLRQ  ^ WKURZQHZ,OOHJDO6WDWH([FHSWLRQ  ³SRVLWLRQ@ QXOO ^ IRU LQWL SRVLWLRQL OLVWOHQJWK L ^ OLVW>[email protected] OLVW>L@ ` OLVW>OLVWOHQJWK@ QXOO ` ` ` \RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 20

GHFRXSOHWKHZDLWUHVVIURPWKHPHQXV

?MIZMITUW[\\PMZM ?MR][\VMML\WOQ^M\PM5MV][IKWUUWVQV\MZNIKMIVLZM_WZS\PM?IQ\ZM[[ ITQ\\TM
7KLVLVDVLPSOHLQWHUIDFHWKDWMXVW OHWVFOLHQWVJHWDQLWHUDWRUIRU WKHLWHPVLQWKHPHQX

SXEOLFLQWHUIDFH0HQX^ SXEOLF,WHUDWRUFUHDWH,WHUDWRU  `

6W__MVMML\WILLIVLPSOHPHQWV0HQX\WJW\P\PM 8IVKISM0W][M5MV]IVL\PM,QVMZ5MV]KTI[[LMÅVQ\QWV[IVL ]XLI\M\PM?IQ\ZM[["

1RZW

KH:DLWUHVVXVHVWKHMDYDXWLO,WHUDWRUDVZHOO LPSRUWMDYDXWLO,WHUDWRU  SXEOLFFODVV:DLWUHVV^ 0HQXSDQFDNH+RXVH0HQX :HQHHGWRUHSODFHWKH 0HQXGLQHU0HQX FRQFUHWH0HQXFODVVHV  ZLWKWKH0HQX,QWHUIDFH SXEOLF:DLWUHVV 0HQXSDQFDNH+RXVH0HQX0HQXGLQHU0HQX ^ WKLVSDQFDNH+RXVH0HQX SDQFDNH+RXVH0HQX WKLVGLQHU0HQX GLQHU0HQX `  SXEOLFYRLGSULQW0HQX ^ ,WHUDWRUSDQFDNH,WHUDWRU SDQFDNH+RXVH0HQXFUHDWH,WHUDWRU  ,WHUDWRUGLQHU,WHUDWRU GLQHU0HQXFUHDWH,WHUDWRU  6\VWHPRXWSULQWOQ ³0(18?Q?Q%5($.)$67´  SULQW0HQX SDQFDNH,WHUDWRU  6\VWHPRXWSULQWOQ ³?Q/81&+´  1RWKLQJFKDQJHV SULQW0HQX GLQHU,WHUDWRU  KHUH `  SULYDWHYRLGSULQW0HQX ,WHUDWRULWHUDWRU ^ ZKLOH LWHUDWRUKDV1H[W ^ 0HQX,WHPPHQX,WHP  0HQX,WHP LWHUDWRUQH[W  6\VWHPRXWSULQW PHQX,WHPJHW1DPH ³³  6\VWHPRXWSULQW PHQX,WHPJHW3ULFH ³³  6\VWHPRXWSULQWOQ PHQX,WHPJHW'HVFULSWLRQ  ` `  RWKHUPHWKRGVKHUH ` 

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 21

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

?PI\LWM[\PQ[OM\][' RI 7KLVVROYHVWKHSUREOHP RQ LQJ QG HSH WKH:DLWUHVVG WKHFRQFUHWH0HQXV


 DLWUHVVIURPWKH :H·YHGHFRXSOHG:IWKHPHQXVVRQRZ LPSOHPHQWDWLRQR DWRUWRLWHUDWH ZHFDQXVHDQ,WHUPHQXLWHPVZLWKRXW RYHUDQ\OLVWRI RXWKRZWKHOLVW KDYLQJWRNQRZDE HQWHG RILWHPVLVLPSOHP

1RZ:DLWUHVV RQO\QHHGVWR EHFRQFHUQHG ZLWK0HQXVDQG ,WHUDWRUV

+HUH·VRXUQHZ0HQXLQWHUIDFH ,WVSHFLILHVWKHQHZPHWKRG FUHDWH,WHUDWRU  LQWHUIDFH!! 0HQX

7KLVVROYHVWKHSUREOHPRI WKH:DLWUHVVGHSHQGLQJRQ WKHLPSOHPHQWDWLRQRIWKH 0HQX,WHPV

LQWHUIDFH!! ,WHUDWRU

:DLWUHVV SULQW0HQX

KDV1H[W

FUHDWH,WHUDWRU FUHDWH,WHUDWRU

QH[W UHPRYH

3DQFDNH+RXVH0HQX

'LQHU0HQX

PHQX,WHPV

PHQX,WHPV

FUHDWH,WHUDWRU

FUHDWH,WHUDWRU

3DQFDNH+RXVH0HQXDQG'LQHU0HQXQRZ WKH0HQXLQWHUIDFHZKLFKPHDQVWKH\QLPSOHPHQW LPSOHPHQWWKHQHZFUHDWH,WHUDWRU PH HHGWR WKRG

(DFKFRQFUHWH0HQXLVUHVSRQVLEOH IRUFUHDWLQJWKHDSSURSULDWH FRQFUHWH,WHUDWRUFODVV

3DQFDNH+RXVH0HQX,WHUDWRU

'LQHU0HQX,WHUDWRU

KDV1H[W

KDV1H[W

QH[W

QH[W

UHPRYH

UHPRYH

'LQHU0HQXUHWXUQVDQ 'LQHU0HQX,WHUDWRU IURPLWV :H·UHQRZXVLQJWKH$UUD\/LVW FUHDWH,WHUDWRU  LWHUDWRUVXSSOLHGE\MDYDXWLO PHWKRGEHFDXVHWKDW·V :HGRQ·WQHHGWKLVDQ\PRUH WKHNLQGRILWHUDWRU UHTXLUHGWRLWHUDWH RYHULWV$UUD\RI PHQXLWHPV

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Page 22

Return to Table of Contents

LWHUDWRUSDWWHUQGH¿QHG

1\MZI\WZ8I\\MZVLMNQVML AW]¼^MITZMILa[MMVPW_\WQUXTMUMV\\PM1\MZI\WZ 8I\\MZV_Q\PaW]Z^MZaW_VQ\MZI\WZAW]¼^MIT[W[MMV PW_2I^I[]XXWZ\[Q\MZI\WZ[QV[WUMWN Q\[KWTTMK\QWV WZQMV\MLKTI[[M[\PM)ZZIa4Q[\6W_Q\¼[\QUM\WKPMKS W]\\PMWNÅKQITLMÅVQ\QWVWN \PMXI\\MZV"



7KH,WHUDWRU3DWWHUQDOORZV WUDYHUVDORIWKHHOHPHQWV RIDQDJJUHJDWHZLWKRXW H[SRVLQJWKHXQGHUO\LQJ LPSOHPHQWDWLRQ ,WDOVRSODFHVWKHWDVNRI WUDYHUVDORQWKHLWHUDWRU REMHFWQRWRQWKHDJJUHJDWH ZKLFKVLPSOLILHVWKH DJJUHJDWHLQWHUIDFHDQG LPSOHPHQWDWLRQDQGSODFHV WKHUHVSRQVLELOLW\ZKHUHLW VKRXOGEH

4M\¼[KPMKSW]\\PMKTI[[LQIOZIU\WX]\ITT\PMXQMKM[QV KWV\M`\



&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 23

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

+DYLQJDFRPPRQLQWHUIDFHIRU\RXU DJJUHJDWHVLVKDQG\IRU\RXUFOLHQW LWGHFRXSOHV\RXUFOLHQWIURPWKH LPSOHPHQWDWLRQRI\RXUFROOHFWLRQRIREMHFWV LQWHUIDFH!! $JJUHJDWH

LQWHUIDFH!! ,WHUDWRU

&OLHQW

FUHDWH,WHUDWRU

KDV1H[W QH[W UHPRYH

7KH,WHUDWRULQWHUIDFH SURYLGHVWKHLQWHUIDFH WKDWDOOLWHUDWRUV PXVWLPSOHPHQWDQG DVHWRIPHWKRGV IRUWUDYHUVLQJRYHU HOHPHQWVRIDFROOHFWLRQ +HUHZH·UHXVLQJWKH MDYDXWLO,WHUDWRU,I\RX GRQ·WZDQWWRXVH-DYD·V ,WHUDWRULQWHUIDFH\RX FDQDOZD\VFUHDWH\RXU RZQ

&RQFUHWH,WHUDWRU

&RQFUHWH$JJUHJDWH

KDV1H[W

FUHDWH,WHUDWRU

QH[W

7KH&RQFUHWH$JJUHJDWH KDVDFROOHFWLRQRI REMHFWVDQGLPSOHPHQWV WKHPHWKRGWKDW UHWXUQVDQ,WHUDWRU IRULWVFROOHFWLRQ

(DFK&RQFUHWH$JJUHJDWH LVUHVSRQVLEOHIRU LQVWDQWLDWLQJD &RQFUHWH,WHUDWRUWKDW FDQLWHUDWHRYHULWV FROOHFWLRQRIREMHFWV

UHPRYH

7KH&RQFUHWH,WHUDWRULV UHVSRQVLEOHIRUPDQDJLQJ WKHFXUUHQWSRVLWLRQRI WKHLWHUDWLRQ

YiX`e gfn\i 7KHFODVVGLDJUDPIRUWKH,WHUDWRU3DWWHUQORRNVYHU\VLPLODUWRDQRWKHU3DWWHUQ\RX¶YHVWXGLHGFDQ\RX WKLQNRIZKDWLWLV"+LQW$VXEFODVVGHFLGHVZKLFKREMHFWWRFUHDWH

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 24

T DDERXWLWHUDWRU

there are no

Dumb Questions

Q:

,·YHVHHQRWKHUERRNVVKRZWKH ,WHUDWRUFODVVGLDJUDPZLWKWKHPHWKRGV ILUVW QH[W LV'RQH DQGFXUUHQW,WHP  :K\DUHWKHVHPHWKRGVGLIIHUHQW"

A:

7KRVHDUHWKH´FODVVLFµPHWKRG QDPHVWKDWKDYHEHHQXVHG7KHVHQDPHV KDYHFKDQJHGRYHUWLPHDQGZHQRZKDYH QH[W KDV1H[W DQGHYHQUHPRYH LQ MDYDXWLO,WHUDWRU /HW·VORRNDWWKHFODVVLFPHWKRGV7KH QH[W DQGFXUUHQW,WHP KDYHEHHQPHUJHG LQWRRQHPHWKRGLQMDYDXWLO7KHLV'RQH  PHWKRGKDVREYLRXVO\EHFRPHKDV1H[W  EXWZHKDYHQRPHWKRGFRUUHVSRQGLQJWR ILUVW 7KDW·VEHFDXVHLQ-DYDZHWHQGWR MXVWJHWDQHZLWHUDWRUZKHQHYHUZHQHHGWR VWDUWWKHWUDYHUVDORYHU1HYHUWKHOHVV\RX FDQVHHWKHUHLVYHU\OLWWOHGLIIHUHQFHLQWKHVH LQWHUIDFHV,QIDFWWKHUHLVDZKROHUDQJH RIEHKDYLRUV\RXFDQJLYH\RXULWHUDWRUV 7KHUHPRYH PHWKRGLVDQH[DPSOHRIDQ H[WHQVLRQLQMDYDXWLO,WHUDWRU

Q:

,·YHKHDUGDERXW´LQWHUQDOµ LWHUDWRUVDQG´H[WHUQDOµLWHUDWRUV:KDW DUHWKH\":KLFKNLQGGLGZHLPSOHPHQW LQWKHH[DPSOH"

A:

:HLPSOHPHQWHGDQH[WHUQDOLWHUDWRU ZKLFKPHDQVWKDWWKHFOLHQWFRQWUROVWKH LWHUDWLRQE\FDOOLQJQH[W WRJHWWKHQH[W HOHPHQW$QLQWHUQDOLWHUDWRULVFRQWUROOHG E\WKHLWHUDWRULWVHOI,QWKDWFDVHEHFDXVH LW·VWKHLWHUDWRUWKDW·VVWHSSLQJWKURXJKWKH HOHPHQWV\RXKDYHWRWHOOWKHLWHUDWRUZKDW WRGRZLWKWKRVHHOHPHQWVDVLWJRHVWKURXJK WKHP7KDWPHDQV\RXQHHGDZD\WRSDVV DQRSHUDWLRQWRDQLWHUDWRU,QWHUQDOLWHUDWRUV DUHOHVVIOH[LEOHWKDWH[WHUQDOLWHUDWRUV EHFDXVHWKHFOLHQWGRHVQ·WKDYHFRQWURORI WKHLWHUDWLRQ+RZHYHUVRPHPLJKWDUJXH



WKDWWKH\DUHHDVLHUWRXVHEHFDXVH\RXMXVW KDQGWKHPDQRSHUDWLRQDQGWHOOWKHPWR LWHUDWHDQGWKH\GRDOOWKHZRUNIRU\RX

Q:

&RXOG,LPSOHPHQWDQ,WHUDWRUWKDW FDQJREDFNZDUGVDVZHOODVEDFNZDUGV"

A:

'HILQLWHO\,QWKDWFDVH\RX·G SUREDEO\ZDQWWRDGGWZRPHWKRGVRQHWR JHWWRWKHSUHYLRXVHOHPHQWDQGRQHWRWHOO \RXZKHQ\RX·UHDWWKHEHJLQQLQJRIWKH FROOHFWLRQRIHOHPHQWV-DYD·V&ROOHFWLRQ )UDPHZRUNSURYLGHVDQRWKHUW\SHRILWHUDWRU LQWHUIDFHFDOOHG/LVW,WHUDWRU7KLVLWHUDWRU DGGVSUHYLRXV DQGDIHZRWKHUPHWKRGV WRWKHVWDQGDUG,WHUDWRULQWHUIDFH,WLV VXSSRUWHGE\DQ\&ROOHFWLRQWKDWLPSOHPHQWV WKH/LVWLQWHUIDFH

Q:

:KRGHILQHVWKHRUGHULQJRIWKH LWHUDWLRQLQDFROOHFWLRQOLNH+DVKWDEOH ZKLFKDUHLQKHUHQWO\XQRUGHUHG"

A:

,WHUDWRUVLPSO\QRRUGHULQJ7KH XQGHUO\LQJFROOHFWLRQVPD\EHXQRUGHUHGDV LQDKDVKWDEOHRULQDEDJWKH\PD\HYHQ FRQWDLQGXSOLFDWHV6RRUGHULQJLVUHODWHG WRERWKWKHSURSHUWLHVRIWKHXQGHUO\LQJ FROOHFWLRQDQGWRWKHLPSOHPHQWDWLRQ,Q JHQHUDO\RXVKRXOGPDNHQRDVVXPSWLRQV DERXWRUGHULQJXQOHVVWKH&ROOHFWLRQ GRFXPHQWDWLRQLQGLFDWHVRWKHUZLVH

Q:


A:

FROOHFWLRQDVORQJDVLWVXSSRUWV,WHUDWRU :HGRQ·WFDUHDERXWKRZWKHFROOHFWLRQ LVLPSOHPHQWHGZHFDQVWLOOZULWHFRGHWR LWHUDWHRYHULW

Q:

,I,·PXVLQJ-DYDZRQ·W,DOZD\V ZDQWWRXVHWKHMDYDXWLO,WHUDWRU LQWHUIDFHVR,FDQXVHP\RZQLWHUDWRU LPSOHPHQWDWLRQVZLWKFODVVHVWKDWDUH DOUHDG\XVLQJWKH-DYDLWHUDWRUV"

A:

3UREDEO\,I\RXKDYHDFRPPRQ ,WHUDWRULQWHUIDFHLWZLOOFHUWDLQO\PDNHLW HDVLHUIRU\RXWRPL[DQGPDWFK\RXURZQ DJJUHJDWHVZLWK-DYDDJJUHJDWHVOLNH $UUD\/LVWDQG9HFWRU%XWUHPHPEHULI\RX QHHGWRDGGIXQFWLRQDOLW\WR\RXU,WHUDWRU LQWHUIDFHIRU\RXUDJJUHJDWHV\RXFDQ DOZD\VH[WHQGWKH,WHUDWRULQWHUIDFH

Q:

,·YHVHHQDQ(QXPHUDWLRQ LQWHUIDFHLQ-DYDGRHVWKDWLPSOHPHQW WKH,WHUDWRU3DWWHUQ"

A:

:HWDONHGDERXWWKLVLQWKH$GDSWHU &KDSWHU5HPHPEHU"7KHMDYDXWLO (QXPHUDWLRQLVDQROGHULPSOHPHQWDWLRQ RI,WHUDWRUWKDWKDVVLQFHEHHQUHSODFHG E\MDYDXWLO,WHUDWRU(QXPHUDWLRQKDV WZRPHWKRGVKDV0RUH(OHPHQWV  FRUUHVSRQGLQJWRKDV1H[W DQG QH[W(OHPHQW FRUUHVSRQGLQJWRQH[W  +RZHYHU\RX·OOSUREDEO\ZDQWWRXVH,WHUDWRU RYHU(QXPHUDWLRQDVPRUH-DYDFODVVHV VXSSRUWLW,I\RXQHHGWRFRQYHUWIURPRQH WRDQRWKHUUHYLHZWKH$GDSWHU&KDSWHU DJDLQZKHUH\RXLPSOHPHQWHGWKHDGDSWHU IRU(QXPHUDWLRQDQG,WHUDWRU

:KHQZHZULWHPHWKRGVWKDWWDNH ,WHUDWRUVDVSDUDPHWHUVZHDUHXVLQJ SRO\PRUSKLFLWHUDWLRQ7KDWPHDQVZHDUH FUHDWLQJFRGHWKDWFDQLWHUDWHRYHUDQ\

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 25

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

; QVOTM:M[XWV[QJQTQ\a :KDWLIZHDOORZHGRXUDJJUHJDWHVWR LPSOHPHQWWKHLULQWHUQDOFROOHFWLRQVDQG UHODWHGRSHUDWLRQV$1'WKHLWHUDWLRQ PHWKRGV":HOOZHDOUHDG\NQRZWKDW ZRXOGH[SDQGWKHQXPEHURIPHWKRGVLQ WKHDJJUHJDWHEXWVRZKDW":K\LVWKDW VREDG" ?MTT\W[MM_PaaW]ÅZ[\VMML\WZMKWOVQbM\PI\_PMV_MITTW_ IKTI[[\WVW\WVTa\ISMKIZMWN Q\[W_VJ][QVM[[UIVIOQVO [WUMSQVLWN IOOZMOI\MJ]\IT[W\ISMWVUWZMZM[XWV[QJQTQ\QM[ TQSMQ\MZI\QWV\PMV_M¼^MOQ^MV\PMKTI[[\_WZMI[WV[\WKPIVOM <_W'A]X\_W"Q\KIVKPIVOMQN \PMKWTTMK\QWVKPIVOM[QV [WUM_IaIVLQ\KIVKPIVOMQN \PM_Ia_MQ\MZI\MKPIVOM[;W WVKMIOIQVW]ZNZQMVL+0)6/-Q[I\\PMKMV\MZWN IVW\PMZ LM[QOVXZQVKQXTM"

(YHU\UHVSRQVLELOLW\RID FODVVLVDQDUHDRISRWHQWLDO FKDQJH0RUHWKDQRQH UHVSRQVLELOLW\PHDQVPRUH WKDQRQHDUHDRIFKDQJH 7KLVSULQFLSOHJXLGHVXVWR NHHSHDFKFODVVWRDVLQJOH UHVSRQVLELOLW\

'HVLJQ3ULQFLSOH $FODVVVKRXOGKDYHRQO\RQH UHDVRQWRFKDQJH

&RKHVLRQLVDWHUP\RX¶OO

+HDG)LUVW

?MSVW__M_IV\\WI^WQLKPIVOMQVIKTI[[TQSM\PMXTIO]M ·UWLQNaQVOKWLMXZW^QLM[ITT[WZ\[WN WXXWZ\]VQ\QM[NWZ XZWJTMU[\WKZMMXQV0I^QVO\_W_Ia[\WKPIVOMQVKZMI[M[ \PMXZWJIJQTQ\a\PMKTI[[_QTTKPIVOMQV\PMN]\]ZMIVL_PMV Q\LWM[Q\¼[OWQVO\WINNMK\\_WI[XMK\[WN aW]ZLM[QOV
22*OXH

KHDUXVHGDVDPHDVXUHRI KRZFORVHO\DFODVVRUD PRGXOHVXSSRUWVDVLQJOH SXUSRVHRUUHVSRQVLELOLW\

:HVD\WKDWDPRGXOHRU FODVVKDVKLJKFRKHVLRQ ZKHQLWLVGHVLJQHGDURXQGDVHWRI UHODWHGIXQFWLRQVDQGZHVD\LWKDVORZ FRKHVLRQZKHQLWLVGHVLJQHGDURXQGD VHWRIXQUHODWHGIXQFWLRQV &RKHVLRQLVDPRUHJHQHUDOFRQFHSW WKDQWKH6LQJOH5HVSRQVLELOLW\3ULQFLSOH EXWWKHWZRDUHFORVHO\UHODWHG &ODVVHVWKDWDGKHUHWRWKHSULQFLSOH WHQGWRKDYHKLJKFRKHVLRQDQGDUH PRUHPDLQWDLQDEOHWKDQFODVVHVWKDW WDNHRQPXOWLSOHUHVSRQVLELOLWLHVDQG KDYHORZFRKHVLRQ

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 26

PXOWLSOHUHVSRQVLELOLWLHV

YiX`e gfn\i ([DPLQHWKHVHFODVVHVDQGGHWHUPLQHZKLFKRQHVKDYHPXOWLSOHUHVSRQVLELOLWLHV

*XPEDOO0DFKLQH

3HUVRQ

JHW&RXQW

VHW1DPH

*DPH

3KRQH

VHW$GGUHVV

ORJLQ VLJQXS

WDON

ORDG

ILUH

JHW/RFDWLRQ

KDQJ8S

VDYH

PRYH

JHW6WDWH

GLDO

VHW3KRQH1XPEHU

VHQG'DWD

UHVW

IODVK

,WHUDWRU 'HFN2I&DUGV

KDV1H[W

6KRSSLQJ&DUW

KDV1H[W QH[W

DGG

UHPRYH

UHPRYH

DGG&DUG

FKHFN2XW

UHPRYH&DUG

VDYH)RU/DWHU

QH[W UHPRYH

VKXIIOH

+DUGKDWDUHDZDWFKRXW IRUIDOOLQJDVVXPSWLRQV

)

YiX`e gfn\i

'HWHUPLQHLIWKHVHFODVVHVKDYHORZRUKLJKFRKHVLRQ 3OD\HU$FWLRQV

*DPH ORJLQ VLJQXS PRYH ILUH

*DPH6HVVLRQ ORJLQ VLJQXS

PRYH

3OD\HU

ILUH

JHW+LJK6FRUH

UHVW

JHW1DPH

UHVW JHW+LJK6FRUH JHW1DPH



&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 27

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

*RRGWKLQJ\RX·UHOHDUQLQJ DERXWWKH,WHUDWRUSDWWHUQ EHFDXVH,MXVWKHDUGWKDW2EMHFWYLOOH 0HUJHUVDQG$FTXLVLWLRQVKDVGRQH DQRWKHUGHDOZH·UHPHUJLQJZLWK 2EMHFWYLOOH&DIpDQGDGRSWLQJWKHLU GLQQHUPHQX

:RZDQGZHWKRXJKWWKLQJV ZHUHDOUHDG\FRPSOLFDWHG 1RZZKDWDUHZHJRLQJWRGR" &RPHRQ WKLQNSRVLWLYHO\,·P VXUHZHFDQÀQGDZD\WR ZRUNWKHPLQWRWKH ,WHUDWRU3DWWHUQ

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Page 28

Return to Table of Contents

DQHZPHQX


0HQX LPSOHPHQWRXUQHZ &DIH0HQXGRHVQ·W LVHDVLO\IL[HG VLQD+DVKWDEOH LQWHUIDFHEXWWKLV 7KH&DIpLVVWRULQJWKHLUPHQXLWHP HVKRUWO\ SXEOLFFODVV&DIH0HQXLPSOHPHQWV0HQX^ ^ ·OOVH :H 'RHVWKDWVXSSRUW,WHUDWRU"

+DVKWDEOHPHQX,WHPV QHZ+DVKWDEOH   PVDUH /LNHWKHRWKHU0HQXVWKHPHQXLWH SXEOLF&DIH0HQX ^ LQLWLDOL]HGLQWKHFRQVWUXFWRU DGG,WHP ³9HJJLH%XUJHUDQG$LU)ULHV´ ³9HJJLHEXUJHURQDZKROHZKHDWEXQOHWWXFHWRPDWRDQGIULHV´ WUXH  DGG,WHP ³6RXSRIWKHGD\´ ³$FXSRIWKHVRXSRIWKHGD\ZLWKDVLGHVDODG´ IDOVH  DGG,WHP ³%XUULWR´ ³$ODUJHEXUULWRZLWKZKROHSLQWREHDQVVDOVDJXDFDPROH´ WUXH  ` P  +HUH·VZKHUHZHFUHDWHDQHZ0HQX,WHH SXEOLFYRLGDGG,WHP 6WULQJQDPH6WULQJGHVFULSWLRQ DGGLWWRWKHPHQX,WHPVKDVKWDEO DQG ERROHDQYHJHWDULDQGRXEOHSULFH  ^ 0HQX,WHPPHQX,WHP QHZ0HQX,WHP QDPHGHVFULSWLRQYHJHWDULDQSULFH  PHQX,WHPVSXW PHQX,WHPJHW1DPH PHQX,WHP  ` WKHNH\LV WKHYDOXHLVWKHPHQX,WHPREMHFW  WKHLWHPQ DPH SXEOLF+DVKWDEOHJHW,WHPV ^ UHWXUQPHQX,WHPV ` :H·UHQRWJRLQJWRQHHGWKLVDQ\PRUH `

6KDUSHQ \RXU SHQFLO %HIRUHORRNLQJDWWKHQH[WSDJHTXLFNO\MRWGRZQWKHWKUHHWKLQJV ZHKDYHWRGRWRWKLVFRGHWR¿WLWLQWRRXUIUDPHZRUN

   

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 29

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

:M_WZSQVO\PM+INu5MV]KWLM 1V\MOZI\QVO\PM+INM5MV]QV\WW]ZNZIUM_WZSQ[MI[a?Pa'*MKI][M 0I[P\IJTMQ[WVMWN \PW[M2I^IKWTTMK\QWV[\PI\[]XXWZ\[1\MZI\WZ*]\Q\¼[VW\ Y]Q\M\PM[IUMI[)ZZIa4Q[\

&DIH0HQXLPSOHPHQWVWKH0HQX LQWHUIDFHVRWKH:DLWUHVVFDQXVH LWMXVWOLNHWKHRWKHUWZR0HQXV

SXEOLFFODVV&DIH0HQXLPSOHPHQWV0HQX^ +DVKWDEOHPHQX,WHPV QHZ+DVKWDEOH   :H·UHXVLQJ+DVKWDEOHEHFDXVHLW·VD SXEOLF&DIH0HQX ^ FRPPRQGDWDVWUXFWXUHIRUVWRULQJYDOXHV FRQVWUXFWRUFRGHKHUH OGDOVRXVHWKHQHZHU+DVK0DS \RXFRX `  SXEOLFYRLGDGG,WHP 6WULQJQDPH6WULQJGHVFULSWLRQ ERROHDQYHJHWDULDQGRXEOHSULFH  ^ 0HQX,WHPPHQX,WHP QHZ0HQX,WHP QDPHGHVFULSWLRQYHJHWDULDQSULFH  PHQX,WHPVSXW PHQX,WHPJHW1DPH PHQX,WHP  `  W,WHPV VRZHGRQ·W -XVWOLNHEHIRUHZHFDQJHWULGRIJH VWRWKH:DLWUHVV SXEOLF+DVKWDEOHJHW,WHPV ^ ,WHP PHQX RI H[SRVHWKHLPSOHPHQWDWLRQ UHWXUQPHQX,WHPV `  $QGKHUH·VZKHUHZHLPSOHPHQWWKHFUHDWH,WHUDWRU  SXEOLF,WHUDWRUFUHDWH,WHUDWRU ^ UHWXUQPHQX,WHPVYDOXHV LWHUDWRU  PHWKRG1RWLFHWKDWZH·UHQRWJHWWLQJDQ,WHUDWRU ` IRUWKHZKROH+DVKWDEOHMXVWIRUWKHYDOXHV `

&RGH8S&ORVH

+DVKWDEOHLVDOLWWOHPRUHFRPSOH[WKDQWKH$UUD\/LVWEHFDXVHLW VXSSRUWVERWKNH\VDQGYDOXHVEXWZHFDQVWLOOJHWDQ,WHUDWRU IRUWKHYDOXHV ZKLFKDUHWKH0HQX,WHPV 

SXEOLF,WHUDWRUFUHDWH,WHUDWRU ^ UHWXUQPHQX,WHPVYDOXHV LWHUDWRU  `

)LUVWZHJHWWKHYDOXHVRIWKH+DVKWDEOH ZKLFKLVMXVWDFROOHFWLRQRIDOOWKH REMHFWVLQWKHKDVKWDEOH

/XFNLO\WKDWFROOHFWLRQVXSSRUWVWKH LWHUDWRU PHWKRGZKLFKUHWXUQVD  REMHFWRIW\SHMDYDXWLO,WHUDWRU

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 30

WHVWGULYHWKHQHZPHQX

)LLQVO\PM+INu5MV]\W\PM?IQ\ZM[[
UHVVLQ

7KH&DIpPHQXLVSDVVHGLQWRWKH:DLW SXEOLFFODVV:DLWUHVV^ WKHFRQVWUXFWRUZLWKWKHRWKHUPHQXVDQGZH 0HQXSDQFDNH+RXVH0HQX 0HQXGLQHU0HQX VWDVKLWLQDQLQVWDQFHYDULDEOH 0HQXFDIH0HQX  SXEOLF:DLWUHVV 0HQXSDQFDNH+RXVH0HQX0HQXGLQHU0HQX0HQXFDIH0HQX ^ WKLVSDQFDNH+RXVH0HQX SDQFDNH+RXVH0HQX WKLVGLQHU0HQX GLQHU0HQX WKLVFDIH0HQX FDIH0HQX `  SXEOLFYRLGSULQW0HQX ^ ,WHUDWRUSDQFDNH,WHUDWRU SDQFDNH+RXVH0HQXFUHDWH,WHUDWRU  ,WHUDWRUGLQHU,WHUDWRU GLQHU0HQXFUHDWH,WHUDWRU  ,WHUDWRUFDIH,WHUDWRU FDIH0HQXFUHDWH,WHUDWRU  :H·UHXVLQJWKH&DIp·V 6\VWHPRXWSULQWOQ ³0(18?Q?Q%5($.)$67´  PHQXIRURXUGLQQHUPHQX SULQW0HQX SDQFDNH,WHUDWRU  $OOZHKDYHWRGRWRSULQW 6\VWHPRXWSULQWOQ ³?Q/81&+´  LWLVFUHDWHWKHLWHUDWRU SULQW0HQX GLQHU,WHUDWRU  6\VWHPRXWSULQWOQ ³?Q',11(5´  DQGSDVVLWWRSULQW0HQX  SULQW0HQX FDIH,WHUDWRU  7KDW·VLW `  SULYDWHYRLGSULQW0HQX ,WHUDWRULWHUDWRU ^ ZKLOH LWHUDWRUKDV1H[W ^ 1RWKLQJFKDQJHVKHUH 0HQX,WHPPHQX,WHP  0HQX,WHP LWHUDWRUQH[W  6\VWHPRXWSULQW PHQX,WHPJHW1DPH ³³  6\VWHPRXWSULQW PHQX,WHPJHW3ULFH ³³  6\VWHPRXWSULQWOQ PHQX,WHPJHW'HVFULSWLRQ  ` ` `



&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 31

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

*ZMISNI[\T]VKP)6,LQVVMZ 4M\¼[]XLI\MW]Z\M[\LZQ^M\WUISM[]ZM\PQ[ITT_WZS[

SXEOLFFODVV0HQX7HVW'ULYH^ SXEOLFVWDWLFYRLGPDLQ 6WULQJDUJV>@ ^ &UHDWHD&DIH0HQX 3DQFDNH+RXVH0HQXSDQFDNH+RXVH0HQX QHZ3DQFDNH+RXVH0HQX  'LQHU0HQXGLQHU0HQX QHZ'LQHU0HQX  &DIH0HQXFDIH0HQX QHZ&DIH0HQX  DQGSDVVLWWRWKHZDLWUHVV  :DLWUHVVZDLWUHVV QHZ:DLWUHVV SDQFDNH+RXVH0HQXGLQHU0HQXFDIH0HQX   ZDLWUHVVSULQW0HQX  1RZZKHQZHSULQWZHVKRXOGVHHDOOWKUHHPHQXV ` 

0MZM¼[\PM\M[\Z]V#KPMKSW]\\PMVM_LQVVMZUMV]NZWU\PM+INu )LOH(GLW:LQGRZ+HOS.DWK\ %HUW/LNH3DQFDNHV

MDYD'LQHU0HQX7HVW'ULYH

)LUVWZHLWHUDWH

0(18 WKURXJKWKHSDQFDNH  PHQX %5($.)$67 . %¶V3DQFDNH%UHDNIDVW3DQFDNHVZLWKVFUDPEOHGHJJVDQGWRDVW 5HJXODU3DQFDNH%UHDNIDVW3DQFDNHVZLWKIULHGHJJVVDXVDJH %OXHEHUU\3DQFDNHV3DQFDNHVPDGHZLWKIUHVKEOXHEHUULHV $QGWKHQ :DIÀHV:DIÀHVZLWK\RXUFKRLFHRIEOXHEHUULHVRUVWUDZEHUULHV

WKHGLQHU

PHQX /81&+ 9HJHWDULDQ%/7 )DNLQ¶ %DFRQZLWKOHWWXFH WRPDWRRQZKROHZKHDW %/7%DFRQZLWKOHWWXFH WRPDWRRQZKROHZKHDW 6RXSRIWKHGD\6RXSRIWKHGD\ZLWKDVLGHRISRWDWRVDODG +RWGRJ$KRWGRJZLWKVDXUNUDXWUHOLVKRQLRQVWRSSHGZLWKFKHHVH 6WHDPHG9HJJLHVDQG%URZQ5LFH6WHDPHGYHJHWDEOHVRYHUEURZQULFH 3DVWD6SDJKHWWLZLWK0DULQDUD6DXFHDQGDVOLFHRIVRXUGRXJKEUHDG ',11(5 6RXSRIWKHGD\$FXSRIWKHVRXSRIWKHGD\ZLWKDVLGHVDODG %XUULWR$ODUJHEXUULWRZLWKZKROHSLQWREHDQVVDOVDJXDFDPROH 9HJJLH%XUJHUDQG$LU)ULHV9HJJLHEXUJHURQDZKROHZKHDWEXQ OHWWXFHWRPDWRDQGIULHV



$QGILQDOO\WKH QHZFDIpPHQX DOOZLWKWKH VDPHLWHUDWLRQ FRGH

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 32

ZKDWGLGZHGR"

?PI\LQL_MLW'

)ZZIa4Q[\

:HZDQWHGWRJLYHWKH :DLWUHVVDQHDV\ZD\WR LWHUDWHRYHUPHQXLWHPV

 0H QX,WHP

DGWZR 2XUPHQXLWHPVK QWDWLRQV PH SOH LP W GLIIHUHQ W DQGWZRGLIIHUHQHUDWLQJ LW RU VI FH ID LQWHU

 0 HQX,WHP



 0H QX,WHP





 0 HQ

X,WHP



)ZZIa 

DQGZHGLGQ·WZDQWKHUWR NQRZDERXWKRZWKHPHQX LWHPVDUHLPSOHPHQWHG

 0 HQX,WHP

  0H QX,WHP

  0H QX,WHP

  0 HQ

X,WHP

?MLMKW]XTML\PM?IQ\ZM[[ 6RZHJDYHWKH:DLWUHVVDQ ,WHUDWRUIRUHDFKNLQGRI JURXSRIREMHFWVVKHQHHGHG RQHIRU WRLWHUDWHRYHU $UUD\/LVW

$UUD\/LVWKDVD EXLOWLQLWHUDWRU )ZZIa4Q[\

 0H QX,WHP

 0 HQX,WHP



 0H QX,WHP





 0 HQ

X,WHP



NEXT 



, WHUDWRU

DQGRQHIRU $UUD\

NEXT

$UUD\GRHVQ·W KDYHDEXLOWLQ ,WHUDWRUVRZH EXLOWRXURZQ

)ZZIa 

 0 HQX,WHP







 0H QX,WHP

, WHUDWRU

1RZVKHGRHVQ·WKDYHWRZRUU\DERXWZKLFK LPSOHPHQWDWLRQZHXVHGVKHDOZD\VXVHVWKHVDPH LQWHUIDFH,WHUDWRUWRLWHUDWHRYHUPHQXLWHPV 6KH·VEHHQGHFRXSOHGIURPWKHLPSOHPHQWDWLRQ 

  0H QX,WHP

  0 HQ

X,WHP

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 33

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

IVL_MUILM\PM?IQ\ZM[[UWZMM`\MV[QJTM :HHDVLO\DGGHG DQRWKHULPSOHPHQWDWLRQ RIPHQXLWHPVDQG VLQFHZHSURYLGHGDQ ,WHUDWRUWKH:DLWUHVV 0I[P\IJTM NQHZZKDWWRGR

%\JLYLQJKHUDQ,WHUDWRU ZHKDYHGHFRXSOHGKHUIURP WKHLPSOHPHQWDWLRQRIWKH PHQXLWHPVVRZHFDQHDVLO\ DGGQHZ0HQXVLIZHZDQW NEXT

, WHUDWR

:KLFKLVEHWWHUI EHFDXVHQRZVKHFDRUKHU VDPHFRGHWRLWHU QXVHWKH DQ\JURXSRIREMHFDWHRYHU LW·VEHWWHUIRUXV WV$QG WKHLPSOHPHQWDWLRQEHFDXVH DUHQ·WH[SRVHG GHWDLOV

 0 HQX,WHP

  NH\

 0H QX,WHP

  NH\

 0H QX,WHP

  NH\

 0 HQ

U





  NH\

X,WHP

0DNLQJDQ,WHUDWRU IRUWKH+DVKWDEOH YDOXHVZDVHDV\ZKHQ \RXFDOO YDOXHVLWHUDWRU  \RXJHWDQ ,WHUDWRU

*]\\PMZM¼[UWZM -DYDJLYHV\RXDORWRI ´FROOHFWLRQµFODVVHVWKDWDOORZ \RXWRVWRUHDQGUHWULHYH JURXSVRIREMHFWV)RUH[DPSOH 9HFWRUDQG/LQNHG/LVW

4QVSML4Q[\ >MK\WZ

0RVWKDYHGLIIHUHQW LQWHUIDFHV

%XWDOPRVWDOORI WKHPVXSSRUWD ZD\WRREWDLQDQ ,WHUDWRU

 0H QX,WHP

 0H QX,WHP



 0 HQX,WHP



 0H QX,WHP



 0 HQ

 0 HQX,WHP

 0H QX,WHP

 0 HQ

X,WHP

X,WHP



M

 I V LUW Z $QGLIWKH\GRQ·WVXSSRUW ,WHUDWRUWKDW·VRNEHFDXVHQRZ \RXNQRZKRZWREXLOG\RXURZQ

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 34

LWHUDWRUVDQGFROOHFWLRQV

1\MZI\WZ[IVL+WTTMK\QWV[ ?M¼^MJMMV][QVOIKW]XTMWN KTI[[M[\PI\IZMXIZ\WN \PM2I^I+WTTMK\QWV[.ZIUM_WZS MK\WZ4QVSML4Q[\;\IKSIVL8ZQWZQ\a9]M]M-IKP WN \PM[MKTI[[M[QUXTMUMV\[\PMRI^I]\QT+WTTMK\QWVQV\MZNIKM_PQKPKWV\IQV[IJ]VKPWN  ][MN]TUM\PWL[NWZUIVQX]TI\QVOOZW]X[WN WJRMK\[ 4M\¼[\ISMIY]QKSTWWSI\\PMQV\MZNIKM"

LQWHUIDFH!! &ROOHFWLRQ

DOONLQGVRIJRRG $V\RXFDQVHHWKHUH·V GDQGUHPRYH DG FDQ VWXIIKHUH
DGG DGG$OO FOHDU FRQWDLQV FRQWDLQV$OO HTXDOV KDVK&RGH

+HUH·VRXUROGIULHQGWKHLWHUDWRU  PHWKRG:LWKWKLVPHWKRG\RXFDQJHW DQ,WHUDWRUIRUDQ\FODVVWKDWLPSOHPHQWV WKH&ROOHFWLRQLQWHUIDFH

LV(PSW\ LWHUDWRU UHPRYH UHPRYH$OO UHWDLQ$OO VL]H WR$UUD\

2WKHUKDQG\PHWKRGVLQFOXGHVL] WRJHWWKHQXPEHURIHOHPHQWV H  DQGWR$UUD\ WRWXUQ\RXU  FROOHFWLRQLQWRDQDUUD\

+DVKWDEOHLVRQHRIDIHZ FODVVHVWKDWLQGLUHFWO\ VXSSRUWV,WHUDWRU$V\RXVDZ ZKHQZHLPSOHPHQWHGWKH &DIH0HQX\RXFRXOGJHWDQ ,WHUDWRUIURPLWEXWRQO\E\ ¿UVWUHWULHYLQJLWV&ROOHFWLRQ FDOOHGYDOXHV,I\RXWKLQN DERXWLWWKLVPDNHVVHQVH WKH+DVKWDEOHKROGVWZR VHWVRIREMHFWVNH\VDQG YDOXHV,IZHZDQWWRLWHUDWH RYHULWVYDOXHVZH¿UVWQHHG WRUHWULHYHWKHPIURPWKH +DVKWDEOHDQGWKHQREWDLQ WKHLWHUDWRU

7KHQLFHWKLQJDERXW&ROOHFWLRQVDQG ,WHUDWRULVWKDWHDFK&ROOHFWLRQREMHFW NQRZVKRZWRFUHDWHLWVRZQ,WHUDWRU &DOOLQJLWHUDWRU RQDQ$UUD\/LVWUHWXUQVD FRQFUHWH,WHUDWRUPDGHIRU$UUD\/LVWVEXW \RXQHYHUQHHGWRVHHRUZRUU\DERXWWKH FRQFUHWHFODVVLWXVHV\RXMXVWXVHWKH ,WHUDWRULQWHUIDFH



&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 35

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

1\MZI\WZ[IVL+WTTMK\QWV[ QV2I^I

&KHFNWKLVRXWLQ -DYDWKH\·YHDGGHG VXSSRUWIRULWHUDWLQJ RYHU&ROOHFWLRQVVRWKDW \RXGRQ·WHYHQKDYHWR DVNIRUDQLWHUDWRU

2I^IQVKT]LM[IVM_NWZUWN \PMNWZ[\I\MUMV\KITTML NWZQV\PI\TM\[aW]Q\MZI\MW^MZIKWTTMK\QWVWZIVIZZIa _Q\PW]\KZMI\QVOIVQ\MZI\WZM`XTQKQ\Ta
,WHUDWHVRYHU HDFKREMHFWLQ WKHFROOHFWLRQ

REMLVDVVLJQHGWRWKHQH[W HOHPHQWLQWKHFROOHFWLRQ HDFKWLPHWKURXJKWKHORRS

IRU 2EMHFWREMFROOHFWLRQ ^     ` 0MZM¼[PW_aW]Q\MZI\MW^MZIV)ZZIa4Q[\][QVONWZQV"

/RDGXSDQ $UUD\/LVWRI 0HQX,WHPV

$UUD\/LVWLWHPV QHZ$UUD\/LVW  LWHPVDGG QHZ0HQX,WHP ³3DQFDNHV´³GHOLFLRXVSDQFDNHV´WUXH  LWHPVDGG QHZ0HQX,WHP ³:DIÀHV´³\XPP\ZDIÀHV´WUXH  LWHPVDGG QHZ0HQX,WHP ³7RDVW´³SHUIHFWWRDVW´WUXH  IRU 0HQX,WHPLWHPLWHPV ^ 6\VWHPRXWSULQWOQ ³%UHDNIDVWLWHP³LWHP  `

,WHUDWHRYHUWKHOLVWDQGSULQW HDFKLWHP


Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 36

FRGHPDJQHWV

&RGH0DJQHWV 4HE#HEFSHAVEDECIDEDTHATTHEYWANTTOBEABLETOALTERNATETHEIRLUNCHMENUITEMSINOTHERWORDS THEYWILLOFFERSOMEITEMSON-ONDAY 7EDNESDAY &RIDAYAND3UNDAY ANDOTHERITEMSON4UESDAY 4HURSDAY AND3ATURDAY3OMEONEALREADYWROTETHECODEFORANEWh!LTERNATINGv$INER-ENU)TERATOR SOTHATITALTERNATESTHEMENUITEMS BUTTHEYSCRAMBLEDITUPANDPUTITONTHEFRIDGEINTHE$INERASA JOKE#ANYOUPUTITBACKTOGETHER3OMEOFTHECURLYBRACESFELLONTHEmOORANDTHEYWERETOOSMALL TOPICKUP SOFEELFREETOADDASMANYOFTHOSEASYOUNEED

0HQX,WHPPHQX,WHP LWHPV>[email protected] SRVLWLRQ SRVLWLRQ UHWXUQPHQX,WHP LPSRUWMDYDXWLO,WHUDWRU LPSRUWMDYDXWLO&DOHQGDU

`

SXEOLF2EMHF WQH[W ^

0HQX,WHP>@LWHPV SXEOLF$OWHUQDWLQJ'LQHU0HQX,WHUDWRU WKLVLWHPV LWHPV &DOHQGDUULJKW1RZ &DOHQGDUJHW,Q VWDQFH  SRVLWLRQ ULJKW1RZ'$
LPSOHPHQ WV,WHUD WRU

SXEOLFYRLGUHPRYH ^

0HQX,WHP>@LWHPV LQWSRVLWLRQ

`

SXEOLFFODVV$OWHUQDWLQJ'LQHU0HQX,WHUDWRU

SXEOLFERROHDQKDV1H[W ^ WKURZQHZ8QVXSSRUWHG2SHUDWLRQ([FHSWLRQ ³$OWHUQDWLQJ'LQHU0HQX,WHUDWRUGRHVQRWVXSSRUWUH PRYH ´ 

LI SRVLWLRQ! LWHPVOHQJWK__LWHPV>[email protected] UHWXUQIDOVH `HOVH^ UHWXUQWUXH `



QXOO ^

`

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 37

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

1[\PM?IQ\ZM[[ZMILaNWZXZQUM\QUM'
7KUHHFUHDWH,WHUDWRU FDOOV SXEOLFYRLGSULQW0HQX ^ ,WHUDWRUSDQFDNH,WHUDWRU SDQFDNH+RXVH0HQXFUHDWH,WHUDWRU  ,WHUDWRUGLQHU,WHUDWRU GLQHU0HQXFUHDWH,WHUDWRU  ,WHUDWRUFDIH,WHUDWRU FDIH0HQXFUHDWH,WHUDWRU  6\VWHPRXWSULQWOQ ³0(18?Q?Q%5($.)$67´  SULQW0HQX SDQFDNH,WHUDWRU  6\VWHPRXWSULQWOQ ³?Q/81&+´  SULQW0HQX GLQHU,WHUDWRU 

7KUHHFDOOVWRSULQW0HQX

6\VWHPRXWSULQWOQ ³?Q',11(5´  SULQW0HQX FDIH,WHUDWRU  `

(YHU\WLPHZHDGGRUUHPRYHDPHQXZH·UHJRLQJ WRKDYHWRRSHQWKLVFRGHXSIRUFKDQJHV 1\¼[VW\\PM?IQ\ZM[[¼NI]T\?MPI^MLWVMIOZMI\RWJWN LMKW]XTQVO\PMUMV]QUXTMUMV\I\QWVIVL M`\ZIK\QVO\PMQ\MZI\QWVQV\WIVQ\MZI\WZ*]\_M[\QTTIZMPIVLTQVO\PMUMV][_Q\P[MXIZI\MQVLMXMVLMV\ WJRMK\[·_MVMMLI_Ia\WUIVIOM\PMU\WOM\PMZ

YiX`e gfn\i 7KH:DLWUHVVVWLOOQHHGVWRPDNHWKUHHFDOOVWRSULQW0HQX RQHIRUHDFKPHQX&DQ\RXWKLQNRID ZD\WRFRPELQHWKHPHQXVVRWKDWRQO\RQHFDOOQHHGVWREHPDGH"2USHUKDSVVRWKDWRQH,WHUDWRULV SDVVHGWRWKH:DLWUHVVWRLWHUDWHRYHUDOOWKHPHQXV"

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Page 38

Return to Table of Contents

DQHZGHVLJQ"

7KLVLVQ·WVREDGDOOZH QHHGWRGRLVSDFNDJH WKHPHQXVXSLQWRDQ$UUD\/LVW DQGWKHQJHWLWVLWHUDWRUWRLWHUDWH WKURXJKHDFK0HQX7KHFRGHLQWKH :DLWUHVVLVJRLQJWREHVLPSOH DQGLWZLOOKDQGOHDQ\QXPEHURI PHQXV

;W]VL[TQSM\PMKPMN Q[WV\W[WUM\PQVO4M\¼[OQ^MQ\I\Za" SXEOLFFODVV:DLWUHVV^ 1RZZHMXVWWDNHDQ $UUD\/LVWPHQXV $UUD\/LVWRIPHQXV  SXEOLF:DLWUHVV $UUD\/LVWPHQXV ^ WKLVPHQXV PHQXV `  SXEOLFYRLGSULQW0HQX ^ ,WHUDWRUPHQX,WHUDWRU PHQXVLWHUDWRU  ZKLOH PHQX,WHUDWRUKDV1H[W ^ 0HQXPHQX  0HQX PHQX,WHUDWRUQH[W  SULQW0HQX PHQXFUHDWH,WHUDWRU  ` `  YRLGSULQW0HQX ,WHUDWRULWHUDWRU ^ ZKLOH LWHUDWRUKDV1H[W ^ 0HQX,WHPPHQX,WHP  0HQX,WHP LWHUDWRUQH[W  6\VWHPRXWSULQW PHQX,WHPJHW1DPH ³³  6\VWHPRXWSULQW PHQX,WHPJHW3ULFH ³³  6\VWHPRXWSULQWOQ PHQX,WHPJHW'HVFULSWLRQ  ` ` ` 

$QGZHLWHUDWH WKURXJKWKH PHQXVSDVVLQJHDFK PHQX·VLWHUDWRU WRWKHRYHUORDGHG SULQW0HQX PHWKRG

1RFRGH FKDQJHVKHUH




&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 39

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

2][\_PMV_M\PW]OP\Q\_I[[INM 1RZWKH\ZDQWWRDGGDGHVVHUWVXEPHQX

,MXVWKHDUGWKH 'LQHULVJRLQJWREH FUHDWLQJDGHVVHUWPHQXWKDW LVJRLQJWREHDQLQVHUWLQWR WKHLUUHJXODUPHQX

7SIaVW__PI\'6W__MPI^M\W[]XXWZ\VW\WVTaU]T\QXTM UMV][J]\UMV][_Q\PQVUMV][ 1\_W]TLJMVQKMQN _MKW]TLR][\UISM\PMLM[[MZ\UMV]IV MTMUMV\WN \PM,QVMZ5MV]KWTTMK\QWVJ]\\PI\_WV¼\_WZSI[ Q\Q[VW_QUXTMUMV\ML

:KDWZHZDQW VRPHWKLQJOLNHWKLV 

3D

H0 H

QX

)TT5MV][ QF DNH XV +R

' LQH U0HQX



 &D IH0HQX



+INu5MV]

8IVKISM5MV]

,QVMZ5MV]  0H QX,WHP



 0 HQX,WHP



 0H QX,WHP

 0 HQ



+HUH·VRXU$UUD\OLVW WKDWKROGVWKHPHQXV RIHDFKUHVWDXUDQW



X,WHP





$UUD\

  NH\

 0 HQX,WHP

  NH\

 0H QX,WHP

  NH\

 0H QX,WHP

  NH\

 0 HQ

 0 HQX,WHP

  0H QX,WHP

$UUD\/LVW

,M[[MZ\5MV] 



+DVKWDEOH

X,WHP

 0H QX,WHP

  0 HQX,WHP

  0H QX,WHP

  0H QX,WHP

  0 HQ

 0 HQ

X,WHP

:HQHHGIRU'LQHU0HQXWRKROGDVXEPHQXEXW ZHFDQ·WDFWXDOO\DVVLJQDPHQXWRD0HQX,WHP DUUD\EHFDXVHWKHW\SHVDUHGLIIHUHQWVRWKLV LVQ·WJRLQJWRZRUN

X,WHP

V N WKL %XW ·WZRU Q ZR

:HFDQ·WDVVLJQDGHVVHUWPHQXWR D0HQX,WHPDUUD\ 7LPHIRUDFKDQJH \RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Page 40

Return to Table of Contents

WLPHWRUHIDFWRU

?PI\LW_MVMML'
ƒ ?MVMML[WUMSQVLWN I\ZMM[PIXML[\Z]K\]ZM\PI\ _QTTIKKWUUWLI\MUMV][[]JUMV][IVLUMV] Q\MU[

ƒ ?MVMML\WUISM[]ZM_MUIQV\IQVI_Ia\W \ZI^MZ[M\PMQ\MU[QVMIKPUMV]\PI\Q[I\TMI[\ I[KWV^MVQMV\I[_PI\_MIZMLWQVOVW__Q\P Q\MZI\WZ[

ƒ ?MUIaVMML\WJMIJTM\ZI^MZ[M\PMQ\MU[QVI UWZMÆM`QJTMUIVVMZ.WZQV[\IVKM_MUQOP\ VMML\WQ\MZI\MW^MZWVTa\PM,QVMZ¼[LM[[MZ\UMV] WZ_MUQOP\VMML\WQ\MZI\MW^MZ\PM,QVMZ¼[MV\QZM UMV]QVKT]LQVO\PMLM[[MZ\[]JUMV]



7KHUH FRPHVDWLPH ZKHQZHPXVWUHIDFWRU RXUFRGHLQRUGHUIRULWWRJURZ 7RQRWGRVRZRXOGOHDYHXVZLWK ULJLGLQÁH[LEOHFRGHWKDWKDV QRKRSHRIHYHUVSURXWLQJ QHZOLIH

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 41

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

H 0 H QX

HU0HQ

X

 

DQGVXEPHQXV

 0H

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

 0H

'

QX,WHP

 0H

QX,WHP

 0H

H VV H HUW0

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

 0H

 0H

'

QX,WHP

QX,WHP

 0H

 0H

QX

H VV H HUW0

QX,WHP

 0H

QX,WHP

 0H

&D

'

IH0HQ

QX,WHP

X

H 0 H QX QX,WHP

 0H





QX,WHP

 0H

X



QX,WHP

 0H

QX,WHP

 0H

QX,WHP

:HDOVRQHHGWR WUDYHUVHPRUHIOEHDEOHWR LQVWDQFHRYHURQ H[LEO\IRU HPHQX 

Q 3D

 0H

HU0HQ

 0H

QX,WHP

DQGPHQXLWHPV

$O O0HQXV

 ' LQ

FD NH+RXV

IH0HQ

QX,WHP

 :HVWLOOQHHGWREHDEOHH WK WRWUDYHUVHWKHDOO LWHPVLQWKHWUHH 

&D

 0H

QX,WHP

 0H

QX

 0H

QX,WHP



 0H

 ' LQ

QX

Q 3D QX,WHP

$O O0HQXV



:HQHHGWR DFFRPRGDWH0HQXV

FD NH+RXV

 0H



X

SUHVHQW %HFDXVHZHQHHGWRUH VDQG PHQXVQHVWHGVXEPHQX XUDOO\ILW PHQXLWHPVZHFDQQDW WUXFWXUH WKHPLQDWUHHOLNHV

H VV H HUW0

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

QX,WHP

YiX`e gfn\i +RZZRXOG\RXKDQGOHWKLVQHZZULQNOHWRRXUGHVLJQUHTXLUHPHQWV"7KLQNDERXWLWEHIRUHWXUQLQJWKHSDJH

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 42

FRPSRVLWHSDWWHUQGH¿QHG


+HUH·VDWUHHVWUXFWX

?M¼ZMVW\OWQVO\WJMI\IZW]VL\PMJ][PWV\PQ[XI\\MZV _M¼ZMOWQVO\WOWIPMILIVLZWTTW]\\PMWNÅKQITLMÅVQ\QWV VW_"



   1RGH



 

 


UH

(OHPHQWVZLWK FKLOGHOHPHQWV DUHFDOOHGQRGHV

 

/ I HD

  /

   /HDI

HDI

(OHPHQWVZLWKRXWFKLOGUHQ DUHFDOOHGOHDYHV

4M\¼[\PQVSIJW]\\PQ[QV\MZU[WN W]ZUMV]["\PQ[XI\\MZV OQ^M[][I_Ia\WKZMI\MI\ZMM[\Z]K\]ZM\PI\KIVPIVLTM IVM[\MLOZW]XWN UMV][IVLUMV]Q\MU[QV\PM[IUM [\Z]K\]ZM*aX]\\QVOUMV][IVLQ\MU[QV\PM[IUM [\Z]K\]ZM_MKZMI\MIXIZ\_PWTMPQMZIZKPa#\PI\Q[I\ZMMWN  WJRMK\[\PI\Q[UILMWN XIZ\[UMV][IVLUMV]Q\MU[J]\ \PI\KIVJM\ZMI\MLI[I_PWTMTQSMWVMJQO†JMZUMV]

:HFDQUHSUHVHQW RXU0HQXDQG 0HQX,WHPVLQD WUHHVWUXFWXUH

 

HP



 

   0HQX

0H X,W Q

0

P



 

7VKM_MPI^MW]Z†JMZUMV]_MKIV][M\PQ[XI\\MZV \W\ZMI\¹QVLQ^QL]ITWJRMK\[IVLKWUXW[Q\QWV[]VQNWZUTaº ?PI\LWM[\PI\UMIV'1\UMIV[QN _MPI^MI\ZMM[\Z]K\]ZM WN UMV][[]JUMV][IVLXMZPIX[[]J[]JUMV][ITWVO_Q\P UMV]Q\MU[\PMVIVaUMV]Q[I¹KWUXW[Q\QWVºJMKI][M Q\KIVKWV\IQVJW\PW\PMZUMV][IVLUMV]Q\MU[
HQX,WH

 0 HQX,WHP

0HQXVDUHQRGHVDQG 0HQX,WHPVDUHOHDYHV



&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 43

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

O\

:HFDQFUHDWHDUELWUDUL FRPSOH[WUHHV

 0H

QX,WHP

QX,WHP

 0H

 0H

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

0HQX,WHPV

 0H

'

QX,WHP

 0H

QX,WHP

 

 0H

H VV H HUW0

 0H

QX,WHP

QX,WHP

 0H

&D

IH0HQ

X

6XEPHQX

X



 0H

HU0HQ

QX

 ' LQ

FD NH+RXV

0HQXV

$O O0HQXV



Q 3D

H 0 H QX

 

 0H

QX,WHP

QX,WHP

 0H

QX,WHP

QX,WHP

ROH

$QGWUHDWWKHPDVDZK

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

 0H

 0H

QX,WHP

0HQX,WHPV

 0H

'

QX,WHP

 0H

QX,WHP

 

 0H

H VV H HUW0

 0H

QX,WHP

QX,WHP

 0H

&D

IH0HQ

X

6XEPHQX

X



 0H

HU0HQ

QX

 ' LQ

FD NH+RXV

0HQXV

$O O0HQXV



Q 3D

H 0 H QX



 0H

QX,WHP

QX,WHP

 0H

QX,WHP

QX,WHP

RUDVSDUWV QEH 2SHUDWLRQVFD HZKROH WK R DSSOLHGW

SULQW

QX,WHP

 0H

QX,WHP

0HQX,WHPV

 0H

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

 0H

 0H

6XEPHQX '

QX,WHP

QX,WHP

 0H

 

 0H

H VV H HUW0

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

&D

8VLQJDFRPSRVLWHVWUXFWXUH ZHFDQDSSO\WKHVDPH RSHUDWLRQVRYHUERWK FRPSRVLWHVDQGLQGLYLGXDO REMHFWV,QRWKHUZRUGVLQ PRVWFDVHVZHFDQLJQRUH WKHGLIIHUHQFHVEHWZHHQ FRPSRVLWLRQVRIREMHFWVDQG LQGLYLGXDOREMHFWV

IH0HQ

QX,WHP

X

X



 0H

HU0HQ

QX

 ' LQ

FD NH+RXV

0HQXV

$O O0HQXV



Q 3D

H 0 H QX

 

7KH&RPSRVLWH3DWWHUQ DOORZVXVWREXLOGVWUXFWXUHV RIREMHFWVLQWKHIRUPRI WUHHVWKDWFRQWDLQERWK FRPSRVLWLRQVRIREMHFWVDQG LQGLYLGXDOREMHFWVDVQRGHV

 0H

QX,WHP

 0H

SULQW

QX,WHP

DUWV 2UWKHS \RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 44

FRPSRVLWHSDWWHUQFODVVGLDJUDP

7KH&OLHQWXVHVWKHFHWR &RPSRQHQWLQWHUID WVLQWKH PDQLSXODWHWKHREMHF FRPSRVLWLRQ

LQHVDQ 7KH&RPSRQHQWGHIEMHFWVLQ OR LQWHUIDFHIRUDO KWKH WKHFRPSRVLWLRQERWHDIQRGHV FRPSRVLWHDQGWKHO

7KH&RPSRQHQWPD\LPS GHIDXOWEHKDYLRUIRUDGGOHPHQWD JHW&KLOG DQGLWVRSHUD UHPRYH  WLRQV

&RPSRQHQW

&OLHQW

RSHUDWLRQ DGG &RPSRQHQW UHPRYH &RPSRQHQW

1RWHWKDWWKH/HDIDOV LQKHULWVPHWKRGVOLNHDG R UHPRYH DQGJHW&KLOG G  GRQ·WQHFHVVDULO\PDNHDO ZKLFK VHQVHIRUDOHDIQRGH: RWRI JRLQJWRFRPHEDFNWRWKH·UH LVLVVXH

JHW&KLOG LQW

/HDI

&RPSRVLWH DGG &RPSRQHQW

RSHUDWLRQ

VLWHDOVR 7KH&RPSR WKH/HDI V W LPSOHPHQ UDWLRQV UHODWHGRSHVRPHRI 1RWHWKDW RWPDNH WKHVHPD\Q RPSRVLWH VHQVHRQD&  7KH&RPSRVLWH·VUROHLVWRGHILQH VRLQWKDWFDVHDQ H E W K V LJ RQHQW P FRPS Q IWKH LRUR LR EHKDY H[FHSW KDYLQJFKLOGUHQDQGWRVWRUHFKLOG JHQHUDWHG FRPSRQHQWV UHPRYH &RPSRQHQW

$/HDIKDVQR FKLOGUHQ

JHW&KLOG LQW RSHUDWLRQ

$/HDIGHILQHVWKHEHKDYLRUIRUWKH HOHPHQWVLQWKHFRPSRVLWLRQ,WGRHV WKLVE\LPSOHPHQWLQJWKHRSHUDWLRQV WKH&RPSRVLWHVXSSRUWV

there are no

Q:

&RPSRQHQW&RPSRVLWH7UHHV" ,·PFRQIXVHG

A:

$FRPSRVLWHFRQWDLQVFRPSRQHQWV &RPSRQHQWVFRPHLQWZRIODYRUV FRPSRVLWHVDQGOHDIHOHPHQWV6RXQG UHFXUVLYH",WLV$FRPSRVLWHKROGVDVHW RIFKLOGUHQWKRVHFKLOGUHQPD\EHRWKHU FRPSRVLWHVRUOHDIHOHPHQWV



Dumb Questions :KHQ\RXRUJDQL]HGDWDLQWKLVZD\\RXHQG XSZLWKDWUHHVWUXFWXUH DFWXDOO\DQXSVLGH GRZQWUHHVWUXFWXUH ZLWKDFRPSRVLWHDWWKH URRWDQGEUDQFKHVRIFRPSRVLWHVJURZLQJXS WROHDIQRGHV

Q:

+RZGRHVWKLVUHODWHWRLWHUDWRUV"

A:

5HPHPEHUZH·UHWDNLQJDQHZ DSSURDFK:H·UHJRLQJWRUHLPSOHPHQWWKH PHQXVZLWKDQHZVROXWLRQWKH&RPSRVLWH 3DWWHUQ6RGRQ·WORRNIRUVRPHPDJLFDO WUDQVIRUPDWLRQIURPDQLWHUDWRUWRD FRPSRVLWH7KDWVDLGWKHWZRZRUNYHU\ QLFHO\WRJHWKHU
&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 45

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

,M[QOVQVO5MV][_Q\P+WUXW[Q\M ;WPW_LW_MIXXTa\PM+WUXW[Q\M8I\\MZV\WW]ZUMV]['
0HQX&RPSRQHQWUHSUHVHQWVWKHLQWHUIDFHIRU ERWK0HQX,WHPDQG0HQX:H·YHXVHGDQDEVWUDFW FODVVKHUHEHFDXVHZHZDQWWRSURYLGHGHIDXOW LPSOHPHQWDWLRQVIRUWKHVHPHWKRGV

XVHWKH VLVJRLQJWR 7KH:DLWUHV QWLQWHUIDFHWRDFFHVV 0HQX&RPSRQH QG0HQX,WHPV ERWK0HQXVD

0HQX&RPSRQHQW

:DLWUHVV

JHW1DPH

:HKDYHVRPHRIWKH VDPHPHWKRGV\RX·OO UHPHPEHUIURPRXU SUHYLRXVYHUVLRQVRI 0HQX,WHPDQG0HQX DQGZH·YHDGGHGSULQW  DGG UHPRYH DQG JHW&KLOG :H·OOGHVFULEH WKHVHVRRQZKHQZH LPSOHPHQWRXUQHZ0HQX DQG0HQX,WHPFODVVHV

JHW'HVFULSWLRQ JHW3ULFH LV9HJHWDULDQ SULQW DGG &RPSRQHQW UHPRYH &RPSRQHQW

+HUHDUHWKHPHWKRGVIRU PDQLSXODWLQJWKHFRPSRQHQWV 7KHFRPSRQHQWVDUH 0HQX,WHPDQG0HQX %RWK0HQX,WHPDQG0HQX V RYHUULGHSULQW 

JHW&KLOG LQW

0HQX,WHP

0HQX

JHW1DPH

PHQX&RPSRQHQWV

JHW'HVFULSWLRQ

JHW1DPH

JHW3ULFH

JHW'HVFULSWLRQ

LV9HJHWDULDQ

SULQW

SULQW

DGG &RPSRQHQW UHPRYH &RPSRQHQW JHW&KLOG LQW

0HQX,WHPRYHUULGHVWKH PHW VHQVHDQGXVHVWKHGHIDX KRGVWKDWPDNH 0HQX&RPSRQHQWIRUWKRV OWLPSOHPHQWDWLRQVLQ HWKDWGRQ·WPDNH VHQVH OLNHDGG LWGRHVQ DGGDFRPSRQHQWWRD0 ·WPDNHVHQVHWR DGGFRPSRQHQWVWRD0H HQX,WHPZHFDQRQO\ QX 

RGVWKDWPDNH 0HQXDOVRRYHUULGHVWKHPHWK RYHPHQXLWHPV UHP DQG GG RD \W ZD NHD VHQVHOL HQX&RPSRQHQWV RURWKHUPHQXV IURPLWVP DPH DQG HW1 ,QDGGLWLRQZH·OOXVHWKHJ UHWXUQWKHQDPH JHW'HVFULSWLRQ PHWKRGVWR DQGGHVFULSWLRQRIWKHPHQX \RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Page 46

Return to Table of Contents

LPSOHPHQWLQJFRPSRVLWHPHQXV

1UXTMUMV\QVO\PM5MV]+WUXWVMV\ 7SIa_M¼ZMOWQVO\W[\IZ\_Q\P\PM 5MV]+WUXWVMV\IJ[\ZIK\KTI[[#ZMUMUJMZ\PM ZWTMWN \PMUMV]KWUXWVMV\Q[\WXZW^QLMIV QV\MZNIKMNWZ\PMTMIN VWLM[IVL\PMKWUXW[Q\M VWLM[6W_aW]UQOP\JMI[SQVO¹1[V¼\\PM 5MV]+WUXWVMV\XTIaQVO\_WZWTM['º1\UQOP\ _MTTJMIVL_M¼TTKWUMJIKS\W\PI\XWQV\ 0W_M^MZNWZVW__M¼ZMOWQVO\WXZW^QLMILMNI]T\ QUXTMUMV\I\QWVWN \PMUM\PWL[[W\PI\QN \PM 5MV]1\MU\PMTMINWZ\PM5MV]\PMKWUXW[Q\M LWM[V¼\_IV\\WQUXTMUMV\[WUMWN \PMUM\PWL[ TQSMOM\+PQTLNWZITMIN VWLM\PMaKIVNITTJIKS WV[WUMJI[QKJMPI^QWZ"

OW 0HQX&RPSRQHQWSURYLGHVGHIDX RG HWK \P YHU RUH QVI LPSOHPHQWDWLR SXEOLFDEVWUDFWFODVV0HQX&RPSRQHQW^  SXEOLFYRLGDGG 0HQX&RPSRQHQWPHQX&RPSRQHQW ^ WKURZQHZ8QVXSSRUWHG2SHUDWLRQ([FHSWLRQ  ` SXEOLFYRLGUHPRYH 0HQX&RPSRQHQWPHQX&RPSRQHQW ^ WKURZQHZ8QVXSSRUWHG2SHUDWLRQ([FHSWLRQ  ` SXEOLF0HQX&RPSRQHQWJHW&KLOG LQWL ^ WKURZQHZ8QVXSSRUWHG2SHUDWLRQ([FHSWLRQ  `  SXEOLF6WULQJJHW1DPH ^ WKURZQHZ8QVXSSRUWHG2SHUDWLRQ([FHSWLRQ  ` SXEOLF6WULQJJHW'HVFULSWLRQ ^ WKURZQHZ8QVXSSRUWHG2SHUDWLRQ([FHSWLRQ  ` SXEOLFGRXEOHJHW3ULFH ^ WKURZQHZ8QVXSSRUWHG2SHUDWLRQ([FHSWLRQ  ` SXEOLFERROHDQLV9HJHWDULDQ ^ WKURZQHZ8QVXSSRUWHG2SHUDWLRQ([FHSWLRQ  `  SXEOLFYRLGSULQW ^ WKURZQHZ8QVXSSRUWHG2SHUDWLRQ([FHSWLRQ  ` `



$OOFRPSRQHQWVPXVWLPSOHPHQW WKH0HQX&RPSRQHQWLQWHUIDFH KRZHYHUEHFDXVHOHDYHVDQG QRGHVKDYHGLIIHUHQWUROHVZH FDQ·WDOZD\VGHÀQHDGHIDXOW LPSOHPHQWDWLRQIRUHDFK PHWKRGWKDWPDNHVVHQVH 6RPHWLPHVWKHEHVW\RXFDQGR LVWKURZDUXQWLPHH[FHSWLRQ

%HFDXVHVRPHRIWKHVHPHWKRGVRQO\PDNHVHQVH IRU0HQX,WHPVDQGVRPHRQO\PDNHVHQVHIRU 0HQXVWKHGHIDXOWLPSOHPHQWDWLRQLV 8QVXSSRUWHG2SHUDWLRQ([FHSWLRQ7KDWZD\ LI0HQX,WHPRU0HQXGRHVQ·WVXSSRUWDQ RSHUDWLRQWKH\GRQ·WKDYHWRGRDQ\WKLQJ WKH\FDQMXVWLQKHULWWKH GHIDXOWLPSOHPHQWDWLRQ

:H·YHJURXSHGWRJHWKHUWKH ´FRPSRVLWHµPHWKRGVWKDWLV PHWKRGVWRDGGUHPRYHDQGJHW 0HQX&RPSRQHQWV GV +HUHDUHWKH´RSHUDWLRQµPHWKR V ,WHP HQX H0 WK WKHVHDUHXVHGE\ D ,WWXUQVRXWZHFDQDOVRXVH DV RR QXW 0H PLQ WKH FRXSOHRI KHQ \RX·OOVHHLQDFRXSOHRISDJHVZ H FRG HQX H0 WK VKRZ ZH

SULQW LVDQ´RSHUDWLRQµPHWKRG WKDWERWKRXU0HQXVDQG0HQX,WHPV ZLOOLPSOHPHQWEXWZHSURYLGHD GHIDXOWRSHUDWLRQKHUH

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Page 47

Return to Table of Contents

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

1UXTMUMV\QVO\PM5MV]1\MU 7SIaTM\¼[OQ^M\PM5MV]1\MUKTI[[I[PW\:MUMUJMZ \PQ[Q[\PMTMIN KTI[[QV\PM+WUXW[Q\MLQIOZIUIVLQ\ QUXTMUMV\[\PMJMPI^QWZWN \PMMTMUMV\[WN \PMKWUXW[Q\M

,·PJODGZH·UHJRLQJLQ WKLVGLUHFWLRQ,·PWKLQNLQJWKLVLV JRLQJWRJLYHPHWKHÁH[LELOLW\,QHHG WRLPSOHPHQWWKDWFUrSHPHQX,·YH DOZD\VZDQWHG

SXEOLFFODVV0HQX,WHPH[WHQGV0HQX&RPSRQHQW^ 6WULQJQDPH )LUVWZHQHHGWRH[WHQG 6WULQJGHVFULSWLRQ WKH0HQX&RPSRQHQW ERROHDQYHJHWDULDQ LQWHUIDFH GRXEOHSULFH  SXEOLF0HQX,WHP 6WULQJQDPH 7KHFRQVWUXFWRUMXVWWDNHV 6WULQJGHVFULSWLRQ WKHQDPHGHVFULSWLRQHWFDQG ERROHDQYHJHWDULDQ NHHSVDUHIHUHQFHWRWKHPDOO GRXEOHSULFH  7KLVLVSUHWW\PXFKOLNHRXUROG ^ PHQXLWHPLPSOHPHQWDWLRQ WKLVQDPH QDPH WKLVGHVFULSWLRQ GHVFULSWLRQ WKLVYHJHWDULDQ YHJHWDULDQ WKLVSULFH SULFH `  SXEOLF6WULQJJHW1DPH ^ UHWXUQQDPH ` +HUH·VRXUJHWWHUPHWKRGVMXVW  OLNHRXUSUHYLRXVLPSOHPHQWDWLRQ SXEOLF6WULQJJHW'HVFULSWLRQ ^ UHWXUQGHVFULSWLRQ `  SXEOLFGRXEOHJHW3ULFH ^ UHWXUQSULFH `  7KLVLVGLIIHUHQWIURPWKHSUHYLRXVLPSOHPHQWDWLRQ SXEOLFERROHDQLV9HJHWDULDQ ^ +HUHZH·UHRYHUULGLQJWKHSULQW PHWKRGLQWKH UHWXUQYHJHWDULDQ 0HQX&RPSRQHQWFODVV)RU0HQX,WHPWKLVPHWKRG ` SULQWVWKHFRPSOHWHPHQXHQWU\QDPHGHVFULSWLRQ  SULFHDQGZKHWKHURUQRWLW·VYHJJLH SXEOLFYRLGSULQW ^ 6\VWHPRXWSULQW ³³JHW1DPH  LI LV9HJHWDULDQ ^ 6\VWHPRXWSULQW ³ Y ´  ` 6\VWHPRXWSULQWOQ ³³JHW3ULFH  6\VWHPRXWSULQWOQ ³³JHW'HVFULSWLRQ  ` ` \RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 48

FRPSRVLWHVWUXFWXUH

1UXTMUMV\QVO\PM+WUXW[Q\M5MV] 6W_\PI\_MPI^M\PM5MV]1\MU_MR][\VMML\PMKWUXW[Q\MKTI[[_PQKP_M¼ZM KITTQVO5MV]:MUMUJMZ\PMKWUXW[Q\MKTI[[KIVPWTL5MV]1\MU[WZW\PMZ5MV][ MOM\IZQIVJMKI][M\PW[MLWV¼\UISMITW\WN [MV[MNWZI5MV]

0HQXLVDOVRD0HQX&RPSRQHQW MXVWOLNH0HQX,WHP

FKLOGUHQ 0HQXFDQKDYHDQ\QXPEHURI VHDQ ·OOX ZH HQW SRQ &RP HQX H0 RIW\S VH LQWHUQDO$UUD\/LVWWRKROGWKH

SXEOLFFODVV0HQXH[WHQGV0HQX&RPSRQHQW^ $UUD\/LVWPHQX&RPSRQHQWV QHZ$UUD\/LVW  6WULQJQDPH 6WULQJGHVFULSWLRQ 7KLVLVGLIIHUHQWWKDQRXUROGLPSOHPHQWDWLRQ  ZH·UHJRLQJWRJLYHHDFK0HQXDQDPHDQGD SXEOLF0HQX 6WULQJQDPH6WULQJGHVFULSWLRQ ^ GHVFULSWLRQ%HIRUHZHMXVWUHOLHGRQKDYLQJ WKLVQDPH QDPH GLIIHUHQWFODVVHVIRUHDFKPHQX WKLVGHVFULSWLRQ GHVFULSWLRQ `  SXEOLFYRLGDGG 0HQX&RPSRQHQWPHQX&RPSRQHQW ^ PHQX&RPSRQHQWVDGG PHQX&RPSRQHQW  +HUH·VKRZ\RXDGG0HQX,WHPVRU ` RWKHU0HQXVWRD0HQX%HFDXVH  ERWK0HQX,WHPVDQG0HQXVDUH SXEOLFYRLGUHPRYH 0HQX&RPSRQHQWPHQX&RPSRQHQW ^ 0HQX&RPSRQHQWVZHMXVWQHHGRQH PHQX&RPSRQHQWVUHPRYH PHQX&RPSRQHQW  ` PHWKRGWRGRERWK 


&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 49

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

:DLWDVHF,GRQ·WXQGHUVWDQGWKH LPSOHPHQWDWLRQRISULQW ,WKRXJKW,ZDV VXSSRVHGWREHDEOHWRDSSO\WKHVDPHRSHUDWLRQVWRD FRPSRVLWHWKDW,FRXOGWRDOHDI,I,DSSO\SULQW WRD FRPSRVLWHZLWKWKLVLPSOHPHQWDWLRQDOO,JHWLVD VLPSOHPHQXQDPHDQGGHVFULSWLRQ,GRQ·WJHWD SULQWRXWRIWKH&20326,7(

-`KMTTMV\KI\KP*MKI][MUMV]Q[IKWUXW[Q\MIVLKWV\IQV[ JW\P5MV]1\MU[IVLW\PMZ5MV][Q\[XZQV\UM\PWL[PW]TL XZQV\M^MZa\PQVOQ\KWV\IQV[1N Q\LQLV¼\_M¼LPI^M\WQ\MZI\M \PZW]OP\PMMV\QZMKWUXW[Q\MIVLXZQV\MIKPQ\MUW]Z[MT^M[
.Q`QVO\PMXZQV\UM\PWL SXEOLFFODVV0HQXH[WHQGV0HQX&RPSRQHQW^ $UUD\/LVWPHQX&RPSRQHQWV QHZ$UUD\/LVW  6WULQJQDPH 6WULQJGHVFULSWLRQ  FRQVWUXFWRUFRGHKHUH  RWKHUPHWKRGVKHUH  SXEOLFYRLGSULQW ^ 6\VWHPRXWSULQW ³?Q´JHW1DPH  6\VWHPRXWSULQWOQ ³³JHW'HVFULSWLRQ  6\VWHPRXWSULQWOQ ³´   ,WHUDWRULWHUDWRU PHQX&RPSRQHQWVLWHUDWRU  ZKLOH LWHUDWRUKDV1H[W ^ 0HQX&RPSRQHQWPHQX&RPSRQHQW   0HQX&RPSRQHQW LWHUDWRUQH[W  PHQX&RPSRQHQWSULQW  ` ` `

JHWKHSULQW PHWKRG $OOZHQHHGWRGRLVFKDQO\WKHLQIRUPDWLRQDERXW WRPDNHLWSULQWQRWRQ V0HQX·VFRPSRQHQWV WKLV0HQXEXWDOORIWKL,WHPV RWKHU0HQXVDQG0HQX

/RRN:HJHWWRXVHDQ,WHUDWRU:H XVHLWWRLWHUDWHWKURXJKDOOWKH0HQX·V FRPSRQHQWVWKRVHFRXOGEHRWKHU0HQXV RUWKH\FRXOGEH0HQX,WHPV6LQFHERWK 0HQXVDQG0HQX,WHPVLPSOHPHQWSULQW ZH MXVWFDOOSULQW DQGWKHUHVWLVXSWRWKHP

127(,IGXULQJWKLVLWHUDWLRQZHHQFRXQWHUDQRWKHU0HQX REMHFWLWVSULQW PHWKRGZLOOVWDUWDQRWKHULWHUDWLRQDQGVRRQ \RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 50

WHVWGULYHWKHPHQXFRPSRVLWH

/M\\QVOZMILaNWZI\M[\LZQ^M 1\¼[IJW]\\QUM_M\WWS\PQ[KWLMNWZI\M[\LZQ^MJ]\_MVMML\W]XLI\M\PM?IQ\ZM[[KWLMJMNWZM _MLW·IN\MZITT[PM¼[\PMUIQVKTQMV\WN \PQ[KWLM"

SXEOLFFODVV:DLWUHVV^ 0HQX&RPSRQHQWDOO0HQXV  SXEOLF:DLWUHVV 0HQX&RPSRQHQWDOO0HQXV ^ WKLVDOO0HQXV DOO0HQXV `  SXEOLFYRLGSULQW0HQX ^ DOO0HQXVSULQW  ` `

LVWKLVVLPSOH
HVV

7SIaWVMTI[\\PQVOJMNWZM_M_ZQ\MW]Z\M[\LZQ^M4M\¼[OM\IVQLMIWN _PI\\PMUMV] KWUXW[Q\MQ[OWQVO\WTWWSTQSMI\Z]V\QUM"

(YHU\0HQXDQG 0HQX,WHPLPSOHPHQWVWKH 0HQX&RPSRQHQWLQWHUIDFH

7KHWRSOHYHOPHQXK PHQXVDQGLWHPV ROGVDOO

&RPSRVLWH 

$O O0HQXV

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

 0H

&RPSRVLWH

X

'

QX,WHP

 

 0H

H VV H HUW0

&D

IH0HQ

QX,WHP

X

HU0HQ



 0H

 ' LQ

QX

FD NH+RXV

(DFK0HQX KROGVLWHPV RULWHPVDQG RWKHUPHQXV



Q 3D

H 0 H QX

&RPSRVLWH

 0H

QX,WHP

 0H

QX,WHP

/HDI /HDI

 0H

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

 0H

QX,WHP

/HDI

/HDI 

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 51

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

6W_NWZ\PM\M[\LZQ^M 7SIaVW__MR][\VMMLI\M[\LZQ^M=VTQSMW]ZXZM^QW][^MZ[QWV_M¼ZMOWQVO\W PIVLTMITT\PMUMV]KZMI\QWVQV\PM\M[\LZQ^M?MKW]TLI[SMIKPKPMN \WOQ^M][ PQ[VM_UMV]J]\TM\¼[OM\Q\ITT\M[\MLÅZ[\0MZM¼[\PMKWLM"

SXEOLFFODVV0HQX7HVW'ULYH^ SXEOLFVWDWLFYRLGPDLQ 6WULQJDUJV>@ ^ /HW·VILUVWFUHDWHDOO 0HQX&RPSRQHQWSDQFDNH+RXVH0HQX  QHZ0HQX ³3$1&$.(+286(0(18´³%UHDNIDVW´  WKHPHQXREMHFWV 0HQX&RPSRQHQWGLQHU0HQX  QHZ0HQX ³',1(50(18´³/XQFK´  :HDOVRQHHGWZRWRS 0HQX&RPSRQHQWFDIH0HQX  OHYHOPHQXQRZWKDWZH·OO QHZ0HQX ³&$)(0(18´³'LQQHU´  0HQX&RPSRQHQWGHVVHUW0HQX  QDPHDOO0HQXV QHZ0HQX ³'(66(570(18´³'HVVHUWRIFRXUVH´   0HQX&RPSRQHQWDOO0HQXV QHZ0HQX ³$//0(186´³$OOPHQXVFRPELQHG´   DOO0HQXVDGG SDQFDNH+RXVH0HQX  :H·UHXVLQJWKH&RPSRVLWHDGG PHWKRGWRDGG DOO0HQXVDGG GLQHU0HQX  HDFKPHQXWRWKHWRSOHYHOPHQXDOO0HQXV DOO0HQXVDGG FDIH0HQX   1RZZHQHHGWRDGGDOO DGGPHQXLWHPVKHUH WKHPHQXLWHPVKHUH·VRQH  H[DPSOHIRUWKHUHVWORRN GLQHU0HQXDGG QHZ0HQX,WHP DWWKHFRPSOHWHVRXUFHFRGH ³3DVWD´ ³6SDJKHWWLZLWK0DULQDUD6DXFHDQGDVOLFHRIVRXUGRXJKEUHDG´ WUXH   $QGZH·UHDOVRDGGLQJDPHQXWRD  PHQX$OOGLQHU0HQXFDUHVDERXWLVWKDW GLQHU0HQXDGG GHVVHUW0HQX  HYHU\WKLQJLWKROGVZKHWKHULW·VDPHQX  LWHPRUDPHQXLVD0HQX&RPSRQHQW GHVVHUW0HQXDGG QHZ0HQX,WHP ³$SSOH3LH´ ³$SSOHSLHZLWKDÀDNH\FUXVWWRSSHGZLWKYDQLOODLFHFUHDP´ WUXH   $GGVRPHDSSOHSLHWRWKH  GHVVHUWPHQX DGGPRUHPHQXLWHPVKHUH  :DLWUHVVZDLWUHVV QHZ:DLWUHVV DOO0HQXV  2QFHZH·YHFRQVWUXFWHGRXUHQWLUH  PHQXKLHUDUFK\ZHKDQGWKHZKROH ZDLWUHVVSULQW0HQX  WKLQJWRWKH:DLWUHVVDQGDV ` ` \RX·YHVHHQLW·VHDV\DVDSSOHSLH

IRUKHUWRSULQWLWRXW

\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 52

FRPSRVLWHUHVSRQVLELOLWLHV

/M\\QVOZMILaNWZI\M[\LZQ^M 127(WKLVRXWSXWLVEDVHGRQWKHFRPSOHWHVRXUFH )LOH(GLW:LQGRZ+HOS*UHHQ(JJV 6SDP

MDYD0HQX7HVW'ULYH $//0(186$OOPHQXVFRPELQHG 

+HUH·VDOORXUPHQXVZHSULQWHGDOOWKLV

3$1&$.(+286(0(18%UHDNIDVW MXVWE\FDOOLQJSULQW RQWKHWRSOHYHOPHQX  . %¶V3DQFDNH%UHDNIDVW Y  3DQFDNHVZLWKVFUDPEOHGHJJVDQGWRDVW 5HJXODU3DQFDNH%UHDNIDVW 3DQFDNHVZLWKIULHGHJJVVDXVDJH %OXHEHUU\3DQFDNHV Y  3DQFDNHVPDGHZLWKIUHVKEOXHEHUULHVDQGEOXHEHUU\V\UXS :DIÀHV Y  :DIÀHVZLWK\RXUFKRLFHRIEOXHEHUULHVRUVWUDZEHUULHV ',1(50(18/XQFK  9HJHWDULDQ%/7 Y   )DNLQ¶ %DFRQZLWKOHWWXFH WRPDWRRQZKROHZKHDW %/7 %DFRQZLWKOHWWXFH WRPDWRRQZKROHZKHDW 6RXSRIWKHGD\ $ERZORIWKHVRXSRIWKHGD\ZLWKDVLGHRISRWDWRVDODG +RWGRJ $KRWGRJZLWKVDXUNUDXWUHOLVKRQLRQVWRSSHGZLWKFKHHVH 6WHDPHG9HJJLHVDQG%URZQ5LFH Y  6WHDPHGYHJHWDEOHVRYHUEURZQULFH 3DVWD Y  6SDJKHWWLZLWK0DULQDUD6DXFHDQGDVOLFHRIVRXUGRXJKEUHDG '(66(570(18'HVVHUWRIFRXUVH  $SSOH3LH Y  $SSOHSLHZLWKDÀDNH\FUXVWWRSSHGZLWKYDQLOODLFHFUHDP &KHHVHFDNH Y  &UHDP\1HZ
7KHQHZGHVVHUW PHQXLVSULQWHG ZKHQZHDUH SULQWLQJDOOWKH 'LQHUPHQX FRPSRQHQWV

&$)(0(18'LQQHU  9HJJLH%XUJHUDQG$LU)ULHV Y  9HJJLHEXUJHURQDZKROHZKHDWEXQOHWWXFHWRPDWRDQGIULHV 6RXSRIWKHGD\ $FXSRIWKHVRXSRIWKHGD\ZLWKDVLGHVDODG %XUULWR Y  $ODUJHEXUULWRZLWKZKROHSLQWREHDQVVDOVDJXDFDPROH  

&KDSWHU

Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 53

WKHLWHUDWRUDQGFRPSRVLWHSDWWHUQV

:KDW·VWKHVWRU\")LUVW\RXWHOOXV 2QH&ODVV2QH5HVSRQVLELOLW\DQGQRZ\RX DUHJLYLQJXVDSDWWHUQZLWKWZRUHVSRQVLELOLWLHV LQRQHFODVV7KH&RPSRVLWH3DWWHUQPDQDJHV DKLHUDUFK\$1'LWSHUIRUPVRSHUDWLRQV UHODWHGWR0HQXV


\RXDUHKHUH



Chapter 9. Well-Managed Collections Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID: [email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.

Chapter 9. Well-Managed Collections

Return to Table of Contents

Page 54

ÀDVKEDFNWRLWHUDWRU

.TI[PJIKS\W1 \MZI\WZ ?MXZWUQ[MLaW]INM_XIOM[JIKS\PI\_M¼L[PW_aW]PW_\W][M1\MZI\WZ _Q\PI+WUXW[Q\MAW]SVW_\PI\_MIZMITZMILa][QVO1\MZI\WZQVW]ZQV\MZVIT QUXTMUMV\I\QWVWN \PMXZQV\UM\PWLJ]\_MKIVIT[WITTW_\PM?IQ\ZM[[\W Q\MZI\MW^MZIVMV\QZMKWUXW[Q\MQN [PMVMML[\WNWZQV[\IVKMQN [PM_IV\[\WOW \PZW]OP\PMMV\QZMUMV]IVLX]TTW]\^MOM\IZQIVQ\MU[
0HQX&RPSRQHQW JH