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:
ISBN: 0596007124 Publisher: O'Reilly
Print Publication Date: 2004/10/25
User number: 1673621 Copyright 2008, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates 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:
ISBN: 0596007124 Publisher: O'Reilly
Print Publication Date: 2004/10/25
User number: 1673621 Copyright 2008, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates 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
L]KS[XMKQM[[_QUUQVOIVLUISQVOY]IKSQVO[W]VL[
LM[QOVMZ[WN \PM[a[\MU][ML[\IVLIZL77\MKPVQY]M[IVLKZMI\ML
WVM,]KS[]XMZKTI[[NZWU_PQKPITTW\PMZL]KS\aXM[QVPMZQ\
'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
]XKWUQVO[PIZMPWTLMZ[UMM\QVOQV5I]QVM`\_MMS
&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:
ISBN: 0596007124 Publisher: O'Reilly
Print Publication Date: 2004/10/25
User number: 1673621 Copyright 2008, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates 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
[QU]TI\WZVMML[\WJTW_I_Ia\PMW\PMZL]KS[QUKWUXM\Q\WZ[
)VLWN KW]Z[M2WM¼[UIVIOMZ\WTL\PMUQ\¼TTJMVWXZWJTMU
NWZ2WM\WR][\_PQX[WUM\PQVO]XQVI_MMS¹)N\MZITTº[IQL
2WM¼[JW[[¹PM¼[IV77XZWOZIUUMZPW_PIZLKIVQ\JM'º
,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:
ISBN: 0596007124 Publisher: O'Reilly
Print Publication Date: 2004/10/25
User number: 1673621 Copyright 2008, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates 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 4
VRPHWKLQJZHQWZURQJ
*]\[WUM\PQVO_MV\PWZZQJTa_ZWVO
-RH,·PDWWKH
VKDUHKROGHU·VPHHWLQJ
7KH\MXVWJDYHDGHPRDQGWKHUH
ZHUHUXEEHUGXFNLHVÁ\LQJDURXQG
WKHVFUHHQ:DVWKLV\RXULGHDRI
DMRNH"
VRPHWLPHRQ0RQVWHUFRP
:KDWKDSSHQHG"
2WMNIQTML\WVW\QKM\PI\VW\ITT
[]JKTI[[M[WN ,]KS[PW]TLÆa?PMV
2WMILLMLVM_JMPI^QWZ\W\PM
,]KS[]XMZKTI[[PM_I[IT[WILLQVO
JMPI^QWZ\PI\_I[VW\IXXZWXZQI\M
NWZ[WUM,]KS[]JKTI[[M[0MVW_
PI[ÆaQVOQVIVQUI\MWJRMK\[QV\PM
;QU=,]KSXZWOZIU
2.VRWKHUH·VDVOLJKW
ÁDZLQP\GHVLJQ,
GRQ·WVHHZK\WKH\FDQ·W
MXVWFDOOLWD´IHDWXUHµ
,W·VNLQGRIFXWH
:KDWKHWKRXJKW
ZDVDJUHDWXVH
RILQKHULWDQFH
IRUWKHSXUSRVH
RIUHXVHKDVQ·W
WXUQHGRXWVRZHOO
ZKHQLWFRPHVWR
PDLQWHQDQFH
)TWKITQbML]XLI\M\W\PMKWLMKI][MLIVWV
TWKIT[QLMMNNMK\ÆaQVOZ]JJMZL]KS[
'XFN
WKH
\
LQ IO\LQJ
O
I
J
JDYH
XWWLQ
%\S UFODVVKH /GXFNV
/
H
S
$
W
X
V
\WR HWKD
DELOLW GLQJWKRV
LQFOX OGQ·W
VKRX
TXDFN
VZLP
GLVSOD\