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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Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID:
[email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the 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\RXUDSSOLFDWLRQVZHSXW 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
SXOORXWZKDWYDULH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WYDULHV
' 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\SHFR 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\VRIWHQZK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["
&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'XFNFODVVRUVXEFODVV +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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WHU
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'XFNFODVVIURPWZRSDJHVEDFN0DOODUG'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 $OOGXFNVXEFODVVHVLQ 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 FODVVHV4XDFNMDYD0XWH4XDFNMDYDDQG6THDNMDYD 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^ SXEOLFVWDWLFYRLGPDLQ6WULQJ>@DUJV ^ 'XFNPDOODUG QHZ0DOODUG'XFN XFN·VLQKHULWHG 7KLVFDOOVWKH0DOODUG' LFKWKHQGHOHJDWHVWR PDOODUGSHUIRUP4XDFN ZK G WKR PH KH SHUIRUP4XDFN PDOODUGSHUIRUP)O\ ULHFDOOVTXDFN 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[IVL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eman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID:
[email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the 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
&KDQJHWKHWHVWFODVV0LQL'XFN6LPXODWRUMDYD DGGWKH 0RGHO'XFNDQGPDNHWKH0RGHO'XFNURFNHWHQDEOHG
SXEOLFFODVV0LQL'XFN6LPXODWRU^ SXEOLFVWDWLFYRLGPDLQ6WULQJ>@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\%HKDYLRUQHZ)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 FDOOVTXDFNV 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^MTWXMZ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WLRQPD\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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 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
NT OF 7O
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\SHVFXUUHQWFRQGLWLRQV 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 PHWKRGVDOUHDG\LPSOHPHQWHG
FXUUHQW&RQGLWLRQV'LVSOD\XSGDWHWHPSKXPLGLW\SUHVVXUH VWDWLVWLFV'LVSOD\XSGDWHWHPSKXPLGLW\SUHVVXUH IRUHFDVW'LVSOD\XSGDWHWHPSKXPLGLW\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\XSGDWHWHPSKXPLGLW\SUHVVXUH VWDWLVWLFV'LVSOD\XSGDWHWHPSKXPLGLW\SUHVVXUH IRUHFDVW'LVSOD\XSGDWHWHPSKXPLGLW\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