www.it-ebooks.info
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.
www.it-ebooks.info
Chapter 1. Welcome to Design Patterns
Return to Table of Contents
Page 1
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.
www.it-ebooks.info
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.
www.it-ebooks.info
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
HG :KDW-RHDGG
IO\ 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.
www.it-ebooks.info
Chapter 1. Welcome to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DNµ
&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.
www.it-ebooks.info
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.
www.it-ebooks.info
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\
27+(5GXFNOLNHPHWKRGV
0DOODUG'XFN GLVSOD\ IO\ TXDFN
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.
www.it-ebooks.info
Chapter 1. Welcome to Design Patterns
Return to Table of Contents
Page 7
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.
www.it-ebooks.info
Chapter 1. Welcome to Design Patterns
Return to Table of Contents
Page 8
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.
www.it-ebooks.info
Chapter 1. Welcome to Design Patterns
Return to Table of Contents
Page 9
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.
www.it-ebooks.info
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
WKHVXSHUFODVV 7KH'XFNFODVVLVVWLOO HSXOOLQJRXW DU ZH EXW NV GXF RIDOO DYLRUVDQG WKHIO\DQGTXDFNEHK KHUFODVV QRW RD LQW KHP SXWWLQJW VWUXFWXUH
HDFKJHW 1RZIO\LQJDQGTXDFNLQJ WKHLURZQVHWRIFODVVHV
9DULRXVEHKDYLRU LPSOHPHQWDWLRQVDUHJRLQJ UH KH OLYH WR
3XOORXWZKDWYDULHV
' XF
NFODVV
)O\
UV LQJ%HKDYLR
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.
www.it-ebooks.info
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'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Kathy Sierra Prepared for Ann Cherkis, Safari ID:
[email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.
www.it-ebooks.info
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
'HFODULQJWKHYDULDEOH´GµDV H'R J DFRQFUHWHLPSOHPHQWDWLRQRIW\S $QLPDO IRUFHVXVWRFRGHWRDFRQFUHWH LPSOHPHQWDWLRQ
*]\XZWOZIUUQVO\WIVQV\MZNIKM[]XMZ\aXM_W]TLJM"
PDNH6RXQG
:HNQRZLW·VD
$QLPDODQLPDO QHZ'RJ ZHFDQQRZXVH'RJEXW WKHDQLPD DQLPDOPDNH6RXQG UHIHUHQFHSRO\PRUSKLFDO O
O\
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`
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.
www.it-ebooks.info
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 QLQWHUIDFH HD DY K EHKDYLRUZH TXDFN WKDWMXVWLQFOXGHVDVWREHLPSOH HG QH W KD W RG WK PH PHQWHG
FHWKDW LQWHUID QW$OO Q D H Y OHPH HKD +HUHZ JFODVVHVLPS VWQHHGWR DOOIO\LQ LQJFODVVHVMX 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·V WLRQRI WKDWFDQ·W IO\ IO\LQJIWKHLPSOHPHQW ZLQJV RUDOOGXFNVW DWLRQRI KDWKDY H
4XDFNVWKDWVTXHDN
:LWKWKLVGHVLJQRWKHUW\SHVRIREMHFWVFDQ U HXVHRXUÁ\DQGTXDFNEHKDYLRUVEHFDXVH WKHVHEHKDYLRUVDUHQRORQJHUKLGGHQDZD\ LQRXU'XFNFODVVHV
4XDFNVWKDWPDNH QRVRXQGDWDOO
HILWRI WKHEHQ OOWKHEDJ W H J H Z 6R LWKRXWD LWK 5(86(ZDWFRPHVDORQJZ 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.
www.it-ebooks.info
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"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RXVHWKH4XDFN 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.
www.it-ebooks.info
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 IO\ DQGTXDFN
,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
DFNLQJ %HKDYL
RU V
'XFN%HKDYLRUV
RPHWKLQJWKDW UHIHUHQFHWRV HUIDFH D V KD N XF ' LQW (DFK 4XDFN%HKDYLRU LPSOHPHQWVWKH NEHKDYLRU QGOLQJWKHTXDF 5DWKHUWKDQKDNREMHFWGHOHJDWHVWKDW LWVHOIWKH'XF REMHFWUHIHUHQFHGE\ EHKDYLRUWRWKH 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.
www.it-ebooks.info
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 HGWRWKH4XDFNREMHFW TXDFNLVGHOHJDWHDOTXDFN DQGZHJHWDU LWV O\:LWK:LQJVDV $QGLWXVHV) H \S W )O\%HKDYLRU
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.
www.it-ebooks.info
Chapter 1. Welcome to Design Patterns
Return to Table of Contents
Page 17
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.
www.it-ebooks.info
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 HW\SHV IDF WHU ULQ IRUWKHEHKDYLR KHVDPH $OOGXFNVXEFODVVHVLQW H KHV WW HUL SDFNDJH LQK
'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.
www.it-ebooks.info
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 SHUIRUP4XDFN PHWKR ULHFDOOVTXDFN RQWKH PDOODUGSHUIRUP)O\ YLR HKD N% XDF 4 W·V ` WKHREMHF LRUUHIHUHQFH ` N·VLQKHULWHGTXDFN%HKDY
5XQWKHFRGH )LOH(GLW:LQGRZ+HOS
GXF LQJZLWK0DOODUG'XFN·V 7KHQZHGRWKHVDPHWK 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.
www.it-ebooks.info
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%HKDYLRU4XDFN%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\SH0RGHO'XFNMDYD SXEOLFFODVV0RGHO'XFNH[WHQGV'XFN^ SXEOLF0RGHO'XFN ^ À\%HKDYLRU QHZ)O\1R:D\ TXDFN%HKDYLRU QHZ4XDFN `
URXQGHG NEHJLQVOLIHJ 2XUPRGHOGXF WRIO\ 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.
www.it-ebooks.info
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\
OHJDWHV SHUIRUP)O\ GH 7KHILUVWFDOOWRLRUREMHFWVHWLQWKH WRWKHIO\%HKDY VWUXFWRUZKLFKLVD 0RGHO'XFN·VFRQ 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·VLQKH EHKDYLRUVHWWHUPHWKRGDQGULWHG PRGHOVXGGHQO\KDVURFNHWSRYRLOD7KH ZHUHG IO\LQJFDSDELOLW\
)LOH(GLW:LQGRZ+HOS
MDYD0LQL'XFN6LPXODWRU 4XDFN ,¶PÀ\LQJ
DIWHU
,FDQ¶WÀ\ ,¶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.
www.it-ebooks.info
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
`
`
`
YLRUV EHKD µDUH H V H 7K ULWKPV 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.
www.it-ebooks.info
Chapter 1. Welcome to Design Patterns
Return to Table of Contents
Page 23
LQWURWR'HVLJQ3DWWHUQV
0);)KIVJMJM\\MZ\PIV1;)
0DVWHUDQG6WXGHQW 0DVWHU*UDVVKRSSHU WHOOPHZKDW\RXKDYH OHDUQHGRIWKH2EMHFW 2ULHQWHGZD\V 6WXGHQW0DVWHU,KDYHOHDUQHGWKDW WKHSURPLVHRIWKHREMHFWRULHQWHGZD\ LVUHXVH
'HVLJQ3ULQFLSOH
0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OKDYH 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.
www.it-ebooks.info
Chapter 1. Welcome to Design Patterns
Return to Table of Contents
Page 24
WKHVWUDWHJ\SDWWHUQ
;XMISQVOWN,M[QOV8I\\MZV[
&RQJUDWXODWLRQVRQ \RXUILUVWSDWWHUQ
\RX ZKHQ LQLWLRQ QGVDQG I H G +,6 ULH 8VH7 RLPSUHVVI XWLYHV QHHGW FHNH\H[HF 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.
www.it-ebooks.info
Chapter 1. Welcome to Design Patterns
Return to Table of Contents
Page 25
LQWURWR'HVLJQ3DWWHUQV
'HVLJQ3X]]OH *M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Head First Design Patterns By Eric Freeman, Elisabeth Freeman, Bert Bates, Kathy Sierra Prepared for Ann Cherkis, Safari ID:
[email protected] ISBN: 0596007124 Publisher: O'Reilly Print Publication Date: 2004/10/25 User number: 1673621 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.
www.it-ebooks.info
Chapter 1. Welcome to Design Patterns
Return to Table of Contents
Page 26
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.
www.it-ebooks.info
Chapter 1. Welcome to Design Patterns
Return to Table of Contents
Page 27
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´"
([DFWO\,I\RX FRPPXQLFDWHLQSDWWHUQV WKHQRWKHUGHYHORSHUVNQRZ LPPHGLDWHO\DQGSUHFLVHO\WKH GHVLJQ\RX·UHGHVFULELQJ-XVWGRQ·W JHW3DWWHUQ)HYHU\RX·OONQRZ \RXKDYHLWZKHQ\RXVWDUWXVLQJ SDWWHUQVIRU+HOOR :RUOG
5LFNZK\ GLGQ·W\RXMXVWVD\ \RXZHUHXVLQJWKH 2EVHUYHU3DWWHUQ"
\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.
www.it-ebooks.info
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\[
WRLPSOH \SDWWHUQGXFNVµ HJ W UD VW H IRXU QJWK ´:H·UHXVL YDULRXVEHKDYLRUVRDYLRUKDVEHHQ PHQWWKH RXWKHGXFNEHK WRIFODVVHV 7KLVWHOOV\HGLQWRLWVRZQVH DQGFKDQJHG HQFDSVXODWEHHDVLO\H[SDQGHG WKDWFDQ XQWLPHLIQHHGHG 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
H\RX JVKDY LQWR LQ W H 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
VKDUHGHVLJQ $V\RXUWHDPEHJLQVWRLQWHUPVRI FH LHQ LGHDVDQGH[SHU DFRPPXQLW\ SDWWHUQV\RXZLOOEXLOG UV XVH QV WHU SDW RI
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.
www.it-ebooks.info
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
YLRU
)O\%HKD IO\
\
)O\1R:D
)O\:LWK:LQJV
IO\ ^ WKLQJFD
IO\ ^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VLJQSDWWHUQVWHOOXV 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.
www.it-ebooks.info
Chapter 1. Welcome to Design Patterns
Return to Table of Contents
Page 30
ZK\GHVLJQSDWWHUQV"
3DWWHUQVDUH QRWKLQJPRUHWKDQXVLQJ 22GHVLJQSULQFLSOHV $FRPPRQPLVFRQFHSWLRQ *UDVVKRSSHUEXWLW·VPRUH VXEWOHWKDQWKDW
.ZQMVLTa 8I\\MZV[/]Z]
;SMX\QKIT,M^MTWXMZ
'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.
www.it-ebooks.info
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.
www.it-ebooks.info
Chapter 1. Welcome to Design Patterns
Return to Table of Contents
Page 32
\RXUGHVLJQWRROER[
VLFV KH22ED RXNQRZWRUSKLFDOO\ \ H P X VV P :HD JQE\ ODVVHVSRO\ RIXVLQJFULWDQFHLVOLNHGHVL ODWLRQ VX H S KRZLQK DQGKRZHQFD UXVW\ FRQWUDFW \RXDUHDOL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SVXODWHLW
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.
www.it-ebooks.info
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.
www.it-ebooks.info
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
LPSOHPHQW MHFWFRXOG 6D\D E R < 1 $ 1RWHWKDW Q%HKDYLRULQWHUIDFHVWHRUD WKH:HDSR WXEHRIWRRWKSD SDSHUFOLSDHDEDVV 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.
www.it-ebooks.info
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.
www.it-ebooks.info
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.
www.it-ebooks.info
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.
www.it-ebooks.info
Chapter 2. Keeping your Objects in the know
Return to Table of Contents
Page 2
ZHDWKHUPRQLWRULQJVWDWLRQ
&RQJUDWXODWLRQV
)NC
/ 2AMA 7EATHER 3TREET -AIN !LLEY /+ 4ORNADO
Copyright Safari Books Online #1673621 3TATEME
NT OF 7O
RK
Q WJHQHUDWLR GRXUQH[ LO X E R W G HFWH EHLQJVHO WDWLRQ ODWLRQVRQ QLWRULQJ6 R 0 J &RQJUDWX HU WK HD : HQWSHQGLQ HG DV RQRXUSDW GLWLRQV ,QWHUQHWE HG Q R DV F E H HU E WK O ZHD ZLO GOLNH VFXUUHQW HUVWDWLRQ 7KHZHDWK REMHFWZKLFKWUDFN HWULFSUHVVXUH :H· H D P KUH DW R W ' :HDWKHU LW\DQGEDU DWLQLWLDOO\SURYLGHV DQGD UHKXPLG K V X W Q LF DW R VW HU WL WL S FD WD WHP WKHUV DSSOL FUHDWHDQ HU'DWD LWLRQVZHD IRU\RXWR UUHQWFRQG OWLPHDVWKH:HDWK FX WV HQ P HD OH U H Q \ L OD HG S GLV HQWV XSGDW UHFDVWDOO VWUHFHQWPHDVXUHP VLPSOHIR WKHU2 HPR K W HV LU X WLRQ:HD HDWKHUVWD REMHFWDFT HUVFDQ Z S OH R DE HO G HY VDQH[SDQ DWRWKHUG K L W LV VR WK WLQ:H·G , K 3 HU LJ )XUWK JWKHPU HDVHDQ$ OX HO U S R G W DQ WV V 5DPDZDQ QZHDWKHUGLVSOD\ URZ FH ZULWHWKHL WKDW$3, PRGHORQ XWRVXSSO\ EXVLQHVV DW UH FK J HD D U H OLNHIRU\R R VZHKDY JHWKHPI DPDWKLQN SD\ QGWRFKDU R 5 WH W LQ J 2 H LQ HU R Z WK J :HD RNHG HDUH HUVDUHKR HVWSDUWZ WKHFXVWRP VH1RZIRUWKHE X H\ RQ GLVSOD\WK V DDSSOLFDWL FNRSWLRQ QDQGDOSK LJ HV G \RXLQVWR U X VHHLQJ\R RUZDUGWR :HORRNI 6LQFHUHO\
R\RX
(2 XUFHÀOHVW HU'DWDVR XUULFDQH& WK + \ HD Q : Q K H K -R KWLQJW HRYHUQLJ 36:HDU
&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.
www.it-ebooks.info
Chapter 2. Keeping your Objects in the know
Return to Table of Contents
Page 3
WKHREVHUYHUSDWWHUQ
LRQVLVRQHRI &XUUHQW&RQGLWWGLVSOD\V7KH HQ HU II WKUHHGL ZHDWKHUVWDWV XVHUFDQDOVRJHW VW DQGDIRUHFD
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.
www.it-ebooks.info
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
HQW PRVWUHF WXUQWKH HUDWXUHKXPLGLW\ H U V G R K W H S P P H H H W U U K R 7KHVHW HDVXUHPHQWVI HVSHFWLYHO\ KH ZHDWKHUPPHWULFSUHVVXUHU UHVHWW DULDEOHVD HWXSGDWHG Y DQGEDUR VH H K W : VKRZWRJ FDUH+2 :HGRQ·W 'DWDREMHFWNQRZWDWLRQ U 6 H U K :HDW PWKH:HDWKH LQIRIUR
HU'DWD RIWKH:HDWK 7KHGHYHORSHUV DFOXHDERXWZKDWZH V REMHFWOHIWX QHHGWRDGG 5HPHPEHUWKLV&XUUHQW&RQ 21(RIWKUHHGLIIHUHQWGLVSGLWLRQVLVMXVW 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.
www.it-ebooks.info
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.
www.it-ebooks.info
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,
R) ?MIZMKWLQVO\WKWVKZM\M
KWUUWVQV\MZNIKM
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- ?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.
www.it-ebooks.info
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.
www.it-ebooks.info
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.
www.it-ebooks.info
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
HFWFKDQJHV :KHQGDWDLQWKH6XEM LHG WLI QR DUH UV WKHREVHUYH
FWPDQDJHV 6XEMHFWREMHG DWD RI W EL VRPH
' R
LQW
FW2EMH
1HZGDWDYDOXHVDUH FRPPXQLFDWHGWRWKH REVHUYHUVLQVRPHIRUP ZKHQWKH\FKDQJH
F ' XFN2EMH
W
W &D W2EMHF
FW
6X EMH
W HF J2EM
0R
HF XVH2EM
W
2EVHUYHU2EMHFWV
DQ 7KLVREMHFWLVQ·W HVQ·W GR LW R V REVHUYHU WKH Q KH Z LHG JHWQRWLI DQJHV 6XEMHFW·VGDWDFK
\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.
www.it-ebooks.info
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
W HF
FW
J2EM
F ' XFN2EMH
W &D W2EMHF
0R
W
HF XVH2EM
W
2EVHUYHUV
6X EMH
LQW
' R
W HF J2EM
FW
$'XFNREMHFWFRPHVDORQJ DQGWHOOVWKH6XEMHFWWKDW LWZDQWVWREHFRPHDQ REVHUYHU
' R
µ PH H LE U F V LQW 6X E EMH VX FW2EMH U H LVW HJ ´U
FW2EMH
F ' XFN2EMH
0R
W
W &D W2EMHF
HF XVH2EM
W
2EVHUYHUV
6X EMH
LQW
FW
7KH6XEMHFWJHWVDQHZ GDWDYDOXH
FW2EMH
1RZ'XFNDQGDOOWKHUHVWRIWKH REVHUYHUVJHWDQRWLÀFDWLRQWKDW WKH6XEMHFWKDVFKDQJHG
' 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.
www.it-ebooks.info
Chapter 2. Keeping your Objects in the know
Return to Table of Contents
Page 11
WKHREVHUYHUSDWWHUQ
6X EMH
LQW
' R
J2EM
W HF
FW
7KH0RXVHREMHFWDVNVWREH UHPRYHGDVDQREVHUYHU
FW2EMH
7KH0RXVHREMHFWKDVEHHQ JHWWLQJLQWVIRUDJHVDQGLVWLUHG RILWVRLWGHFLGHVLW·VWLPHWR VWRSEHLQJDQREVHUYHU
´UH PR YH FW X ' QV XFN2EMH XE VFU LEH P Hµ 0R
W &D W2EMHF
HF XVH2EM
W
2EVHUYHUV
' R
LQW
W HF
FW2EMH
7KH6XEMHFWDFNQRZOHGJHVWKH 0RXVH·VUHTXHVWDQGUHPRYHVLW IURPWKHVHWRIREVHUYHUV
F ' XFN2EMH
0R
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.
www.it-ebooks.info
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.
www.it-ebooks.info
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.
www.it-ebooks.info
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.
www.it-ebooks.info
Chapter 2. Keeping your Objects in the know
Return to Table of Contents
Page 15
WKHREVHUYHUSDWWHUQ