D D< < I I:
DE DE!IIGN GN PA" "EN! I2018-1.1
DEMO VERSION
A F= %+ C?/: " D(5$ I3 D$2(& P33$2 G G < < H HF F D(5$ I3 R$%"3(& . G F G > JF HFF F
I ()('%8) 87 & 8 1= ;*), M%6%. I* 8 %(2@8 &))2 *6 )6, )6, I@( 46&%&= %) 27)( 8) 8 ) & & 7 71) 1) 30 =)% )%67 67 % %8) 8)6. 6.
4
C G GG GFF
"() - C:: "() - C: C:: : ............................................................................... ....................................................................................... ........ 4 H= : (+ "/ B B.............................................................................. .............................................................................. 6 IN"OD#C"ION "O OOP ............................................................................. 7 BF<F * ................... ...................................... ...................................... ...................................... .......................... ....... 8 *<F * ................... ...................................... ...................................... ...................................... ........................ .....13 13 +G<F BGJ BGJ =GF =GF................... ...................................... .....................................20 ..................20
IN"OD#C"ION "O DE!IGN PA""EN!.................................................23 GQFF DF< *GG?.................. GQ *GG? ..................................... ...................................... ....................... ....24 24 L H # & *GGF? *GGF? ................... ...................................... ................................28 .............28
!OF"%AE DE!IGN PINCIPLE!...........................................................29 GHF DF< .................. ..................................... ...................................... ....................... ....30 30
D P*.............................................................................34 EFHG G <F <F ................... ...................................... ............................35 .........35 N EFHG N *@ G #G, G #@@GG<.39 C@F
!OLID P*..............................................................................48 +FF<<I HFG
5
C G GG GFF
CA"ALOG OF DE!IGN PA""EN!............................................................68 C(:( D P(::...........................................................69 'G ..................................... ...................................... ............................ .........71 71 N GL 'G.................. GL................................................................87 ..................87 N AFGG GL.............................................. BH<.................................................... ...................................... ........................... ........ 103 N BH<................................. GGL ................. .................................... ...................................... ...................................... ................... 122 N *GGL <G ................. .................................... ...................................... ...................................... ................... 136 N <G
!:;*:;( D P(::.......... P(::............... ........... ............ ........... ........... ............ ........... ........... ........ 145 ...................................... ...................................... ...................................... ..................... 148 N AG ................... B<.................. ..................................... ...................................... ...................................... ........................ ..... 161 N B< C@F
B/(<( B/(< ( D P(:: ...... ............ ........... ........... ............ ........... ........... ............ ............ ......... ... 244 +FF<<
C*; ..... C*; ........... ............ ........... ........... ............ ........... ........... ............ ............ ............ ........... ........... ............ ........... ........... ......... ... 406
6
"J " J G G G >
H= : (+ "/ B G<F G F<G<F 22 FF< F< GGF @HG L G R HS ( F<@L ) < 1994. E G KF G<H G<H GG. , LH @ I G I L <>< G GGF LHQ <GFG <. 'L GGF G, F LH F<L =H@ @ G< G G< HF< H@HF F. G F F GJ G HG GG GF. # LH F G @ GG GG LH IQG F LG, =HFG > <OG J F GF G@. HF, < LH +$( (& & 3 33$ 3$2 2 L >J GG, LH G +$ <G JL.
IN"OD#C"ION "O OOP
8
#GH #G HG< G< G * / BF BF<F <F *
B(* - OOP &)'8-o6)28)( p6+6%112+
G J< <F G, I< G G GG G, <G F< HF !)$"32, J< FGHG @ FG RH<GFS, W L @@, "+22$2.
O)*:, *( D LH <> GF? # LH HF #Q GL G K< G * GF HF< I<HF G K@F.
T7 7 % ML '%77 (%+6%1. (%+6%1. $9@ $9@ 7)) % 8 * 79' (%+6%17 (%+6%17 2 8) &.
9
#GH #G HG< G< G * / BF BF<F <F *
L LH I G @ F. F. F
DG FG <F< G =GQF WF
O&)'87 O&)' 87 %6) 278%2') 278%2')77 * '%77 '%77)7. )7.
10
#GH #G HG< G< G * / BF BF<F <F *
&H, LH <QF G, H<G GG WF G FGHGH & )'87, J< G <FGF GG FF.
C( /(*/ EILG< W L J J G> HG FF. GEILG< HL, @ G<F @ G F< FF. @ "+22 22 '($ '($ "'( "'($2 $2. &GQF GF FFF @<G G JQI =HFG W,
11
#GH #G HG< G< G * / BF BF<F <F *
ML (%+6%1 * % '%77 )6%6'=. A '%77)7 2 87 (%+6%1 %6) 4%68 * 8) Animal '% '%77 77 ) )6%6 6%6'= '=..
AFFH@< GG J I G HF<FF H<@G, J I HG KGG @ FF
12
#GH #G HG< G< G * / BF BF<F <F *
C%77)7 2 % ML (%+6%1 '%2 &) 714)( * 8@7 16) 1468%28 8 7; 8)6 6)%82 6)%8277 8%2 8)6 '2 '28)287 8)287..
HFFF I< G I< @GF GG GL <
16 pages from the full book are omitted in the demo version
!OF " %A !OF" AEE DE!IGN PINCIPLE!
30
G GH HF F D DF< F<
F(:; F (:; - G+ D B J G G GH GGF, GQF
C+ ; CFG G<@ GJ G @FG IH @G<F J I< L FGJ HG. &FF G<@ < I@G @F G< G @>G @ >G < G @GG< G GG< HFG@F. C# #$ $ $ $42 42$ $
@G FGF. <GG F G , HG < K < J GKG HFHL G>F KG G. <G H< GJ @GF, <F G FFF <FG <GF, G<FO GF G@ F< G HF. "JI,
31
GH GHF F DF DF< < / C C H HF F
G< G @GF @ @<G. "QF < J
R
# F G IF HF. AG G JFG I, LH HF FFF: FF <<F, G<F, @L F@ FF RG@FS <> G</F G G <FG I. L L GL G L FGG<F FI< @, FG G@ L FFF W G<F<F GJ G@. $, K@. #G F. #G F Test , TestCase
, TestSuite G<F<F W.
A @J> <G @J>F L FHFF< F@J. L HF G F- "LJ << RQG HF, JQ LH.S @J> GF LH W LH HFG@ I<,, .
1.
E< @@ @@ K K<< <<
32
GH GHF F DF DF< < / EKG EKGF F<< <<
F F. LQ L F<G< HG J H FFF G G <GG JF. G L G @J>F. BH<< @J> F<
T
E>:):? C'&$
U
H F F I< I< @ @ < <J JF, F, HG HG J J F> F> @ IF<.
U
H G G # @ @J J> > J
U
H F< F< < << <GG - -@@ @@ J JF GH GG JH G G@ G F. E FGJ I F MF F<@< FG<F. FI FF JL G
33
GH GHF F DF DF< < / EKG EKGF F<< <<
<FG, J HFG G @ GG J FGG G FI F <> . @G< L LH G F . < @<G< G< F JF G JF F FHG F. G< F F
QF <G F<: < F@ F>F LH G F@G< < LH , GG @F F@ FG< F HG
GQF JL FF IF GQF IF GL G I< FF< HGH F J F<< <G<QF
34
DF< D F< *< << < F F
D P* G LH F GF <<F.
35
DF< DF< * *<< <<FF / EF EFHG HG G <F <F
E*(;(: %/(: $( #G<L G FGF LH <G< GG IL FG G@ @ JG FGLF G F@.
@< G > L G G @< @< ,, G G F< F<>F. %J< G <G J>< F, <@@G< <@@G< GFG< G F. FF G<@ LH F @>< F, G @ G<@ LH I <@@G< GHF.
36
DF< DF< * *<< <<FF / EF EFHG HG G <F <F
E*(;(: ( :/+ < L LHQ @>< -@@ JF
1
getOrderTotal (order order) ) is method getOrderTotal(
2
tota to tal l = 0
3
item in orde order.li r.lineIt neItems ems foreach item
4
total tot al += ite item.p m.pric rice e * ite item.q m.quan uantit tity y
5 6 7 8 9
order.co r.country untry == "US") "US") if (orde tota to tal l += to tota tal l * 0. 0.07 07 // US sal ales es tax order.co r.countr untry y == "EU") "EU"): else else if (orde tota to tal l += to tota tal l * 0. 0.20 20 // Eu Euro rope pean an VA VAT T
10 11
return total
BEFORE: 8%< '%'9%82 '%'9%82 '() 7 1<)( 1<)( ;8 8) 6)78 * 8) 1)8(@7 '().
H KG KG G G G G G GK K H HG G< < < < < <G G F F G G @G, <<
37
1
DF< DF< * *<< <<FF / EF EFHG HG G <F <F
getOrderTotal (order order) ) is method getOrderTotal(
2
tota to tal l = 0
3
item in orde order.li r.lineIt neItems ems foreach item
4
total tot al += ite item.p m.pric rice e * ite item.q m.quan uantit tity y
5 6
tota to tal l += to tota tal l * ge getT tTax axRa Rate te( (order.country order.country) )
7 8
return total
9 10 11 12 13 14 15 16
getTaxRate (country country) ) is method getTaxRate( countr ntry y == "US") "US") if (cou ale es ta tax x return 0.07 // US sal countr ntry y == "EU") "EU"): else else if (cou Euro rope pean an VA VAT T return 0.20 // Eu
else return 0
AFTER: =9 '%2 '%2 +)8 8) 8%< 6%8) &= '%2+ '%2+ % ()7+2%8)( ()7+2%8)( 1)8(.
K-G F @
E*(;(: ( *( < I G<@ LH @<G @ @ FF<< G<F @H @ F<@.
38
DF< DF< * *<< <<FF / EF EFHG HG G <F <F
BEFORE: '%'9%82+ 8%< 2
Order
'%77.
=GF G Order FF G G GK-G GK-G J> J> G F< =G GG F =HFG GG.
AFTER: 8%< '%'9%82 '%'9%82 7 (()2 *61 8) 6()6 '%77.
29 pages from the full book are omitted in the demo version
CA"ALOG OF DE!IGN DE! IGN PA PA" "EN!
69
CG C G< < DF DF< < *GG GG FF
C(:( D P(:: CG< GGF I< I<HF =G G< @
F(*:? M:/+ *I<F <G G< =GF < FHFF, HG *I<F JF FHFFF G G G GL =GF GG J< G.
A):(*: F(*:? &GF LH H @<<F G =GF J
70
CG C G< < DF DF< < *GG GG FF
B;+ &GF LH FGHG @K =GF FG L FG. GG JF LH G H <G GLF FGG<F =G HF< G F@ FGHG< .
P::? &GF LH L K< LH G G< FFF.
!: &GF LH FH GG FF F L <FG, J< I<< FF <G G G
71
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
FAC"O' ME"HOD A7 2;2 %7: !689% C27869'86
F"3 F "3 8 8 M$ M$3' 3'# #
<G G< =GF < FHFF, HG JF FHFFF G G G GL =GF GG J< G.
72
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
P) #@< GG LHQ G< F, F G H> LH
A((2+ % 2); '%77 '%77 8 8) 46+6%1 72@8 8%8 714) * 8) 6)78 * 8) '() '() 7 %6)%(= %6)%(= '94)( '94)( 8 )<782+ )<782+ '%77)7.
G JF, <G? BHG J HG G ? AG FG, @FG LH < F G G G< F. 'I,, < G LH < G G GL GF 'I G FGGG< G G , LH J< L G @> GF F <.
73
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
AF FHG, LH J< H J
!;: GL 'G GG FHFGF GG LH <G =G FGHG< F (HF< G new G) J
S9&'%77)7 '%2 %8)6 8) '%77 * &)'87 &)2+ 6)8962)( &= 8) *%'86= 1)8(.
AG WFG , G <GFF: J =HFG @I G FGHG @ G G @ G G. "JI, F< G
74
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
QF F<G <@
A 46(9'87 1978 *; 8) 7%1) 28)6*%').
K@, G Truck Ship FFF FH <@@G @ G G G Transport <G <G , , J J< < F F @G @G deliver . E FF <@@GF GF GH> =GF, JF G GL @G < G SeaLogistics FF GHF F<F. GG HFF G GL @G (G G ')28 ) FQG F < GJ G GH HGF GH L I<HF FHFFF. <G GGF G HGF F FGG Transport .
75
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
A7 2+ %7 % 46(9'8 46(9'8 '%77)7 14)1)28 % '112 '112 28)6*%'), 28)6*%'), =9 '%2 '%2 4%77 8)6 &)'87 8 8) ')28 '() ;898 &6)%2+ &6)%2+ 8.
<G >JF GG GFG =GF FHF G I G deliver @G, HG KGL KGL J J F
!:;*:;
76
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
1.
P#4"3 F G <G, J<
2.
C" "$3 $3$ $ P# #4"3 4"322 <G <@@GG<F G -
HG <G. 3.
C$3 FF F G GL @G GG GHF J HG =GF. #GQF <@GG GG G GH GL G
4.
C" "$3 $3$ $ C$ C$3 32 2 I< G F GL @G F
GHF <G GL HG.
77
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
G GG G GL @G FQG I G "$3$ J <FGF G G<@. #G F GH K
P;+*+ F"3 "3 8 8 M$ M$3' 3'# # HF
F < FF HFF <G # @GF G
T) '677-4%8* '677-4%8*61 61 (%+ )< )<%14 %14). ).
78
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
G GL @G @F <G L, LH QG G JF G G GL @G, <JF>< HGGF G F. G, G F < FF @HFG J> JF J HG G
1
// Th The e cr crea eato tor r cl clas ass s de decl clar ares es th the e fa fact ctor ory y me meth thod od th that at mu must st
2
// re retu turn rn an ob obje ject ct of a pr prod oduc uct t cl clas ass. s. Th The e cr crea eato tor' r's s su subc bcla lass sses es
3
// us usua uall lly y pr prov ovid ide e th the e im impl plem emen enta tati tion on of th this is me meth thod od. .
4
class Dialog is
5
// Th The e cr crea eato tor r ma may y al also so pr prov ovid ide e so some me de defa faul ult t im impl plem emen enta tati tion on
6
// of th the e fa fact ctor ory y me meth thod od. .
7
createButton () abstract abstract method method createButton()
8 9
// No Note te th that at, , de desp spit ite e it its s na name me, , th the e cr crea eato tor' r's s pr prim imar ary y
10
// res respon ponsib sibili ility ty isn isn't 't cre creati ating ng pro produc ducts. ts. It usu usuall ally y
79
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
11
// co cont ntai ains ns so some me co core re bu busi sine ness ss lo logi gic c th that at re reli lies es on pr prod oduc uct t
12
// ob obje ject cts s re retu turn rned ed by th the e fa fact ctor ory y me meth thod od. . Su Subc bcla lass sses es ca can n
13
// in indi dire rect ctly ly ch chan ange ge th that at bu busi sine ness ss lo logi gic c by ov over erri ridi ding ng th the e
14
// fa fact ctor ory y me meth thod od an and d re retu turn rnin ing g a di diff ffer eren ent t ty type pe of pr prod oduc uct t
15
// fr from om it it. .
16
renderWindow () is method renderWindow()
17
// Ca Call ll th the e fa fact ctor ory y me meth thod od to cr crea eate te a pr prod oduc uct t ob obje ject ct. .
18
Button But ton okB okButt utton on = cre create ateBut Button ton() ()
19
// No Now w us use e th the e pr prod oduc uct. t.
20
okButton.onClick( okButton.onClick (closeDialog closeDialog) )
21
okButton.render() okButton.render ()
22 23 24
// Co Conc ncre rete te cr crea eato tors rs ov over erri ride de th the e fa fact ctor ory y me meth thod od to ch chan ange ge th the e
25
// res result ulting ing pro produc duct's t's typ type. e.
26
class WindowsDialog extends Dialog is
27 28
createButton () is method createButton() WindowsButton () return return new WindowsButton()
29 30 31 32
class WebDialog extends Dialog is createButton () is method createButton() HTMLButton () return return new HTMLButton()
33 34 35
// Th The e pr prod oduc uct t in inte terf rfac ace e de decl clar ares es th the e op oper erat atio ions ns th that at al all l
36
// con concre crete te pro produc ducts ts mus must t imp implem lement ent. .
37
interface Button is
38
render() method render()
39
onClick(f) method onClick(
40 41
// Con Concre crete te pro produc ducts ts pro provid vide e var variou ious s imp implem lement entati ations ons of the
42
// pro produc duct t int interf erface ace. .
80
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
43
class WindowsButton implements Button is
44 45 46 47
render(a, b) is method render( // Re Rend nder er a bu butt tton on in Wi Wind ndow ows s st styl yle. e. onClick(f) is method onClick( // Bi Bind nd a na nati tive ve OS cl clic ick k ev even ent. t.
48 49 50 51 52 53
class HTMLButton implements Button is render(a, b) is method render( // Re Retu turn rn an HT HTML ML re repr pres esen enta tati tion on of a bu butt tton on. . onClick(f) is method onClick( // Bi Bind nd a we web b br brow owse ser r cl clic ick k ev even ent. t.
54 55 56 57
class Application is dialog: Di Dial alog og field dialog:
58 59
// Th The e ap appl plic icat atio ion n pi pick cks s a cr crea eato tor' r's s ty type pe de depe pend ndin ing g on th the e
60
// cur curren rent t con config figura uratio tion n or env enviro ironme nment nt set settin tings. gs.
61
initialize () is method initialize()
62
config conf ig = read readAppl Applicat icationCo ionConfigF nfigFile ile ()
63 64
config.O ig.OS S == "Windows") "Windows") then if (conf
65
dial di alog og = new WindowsDialog() WindowsDialog ()
66
config fig.OS .OS == "Web") "Web") then else else if (con
67 68 69
dial di alog og = new WebDialog() WebDialog()
else throw new Exception( Exception ("Err "Error! or! Unkn Unknown own oper operating ating syst system." em.") )
70 71
// Th The e cl clie ient nt co code de wo work rks s wi with th an in inst stan ance ce of a co conc ncre rete te
72
// cr crea eato tor, r, al albe beit it th thro roug ugh h it its s ba base se in inte terf rfac ace. e. As lo long ng as
73
// th the e cl clie ient nt ke keep eps s wo work rkin ing g wi with th th the e cr crea eato tor r vi via a th the e ba base se
74
// in inte terf rfac ace, e, yo you u ca can n pa pass ss it an any y cr crea eato tor' r's s su subc bcla lass ss. .
81
75
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
main() is method main()
76
dialog.initialize() dialog.initialize ()
77
dialog.render() dialog.render ()
A*():?
U2$ 3'$ F"38 M$3'# 6'$ 84 #;3 *6 !$%$'# 3'$ $7"3 38$2 # #$$#$"($2 % 3'$ !)$ )$""32 84 4 "#$ 2' '4 4+ # 6* 6(3'.
GL 'G FGF HG FGHG< @ G GG GHL HFF G HG.
U2$ 3'$ F"38 M$3'# 6'$ 84 63 3 5(#$ 42$2 % 84 +(!8 %,$6* 6(3' 68 3 $73$# (32 (3$+ ",$32.
#. BHG J JH G @J>
82
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
FHG< <G F< GL GL @G G L I< G. #@< GG LH J. H FH I H HGGF, HG G @J> L I<F FH F. H KG G FG Button FF FF J FF I< LH FHFF GH RoundButton =GF = GF.. J HF HF G UIWithRoundButtons F FFF < <FG FG UIFramework . A GGQF HG
U2$ 3'$ F"38 M$3'# 6'$ 84 63 3 25$ 2823$, $24 2 4""$2 !8 $ $42 42((& $7( 7(223( 3(& & ! !)$ )$"3 "322 ( (23 23$ $# # % $ $!4 !4((+# +#( (& & 3' 3'$, $, $"' $ "' 3( 3(,$ ,$..
H G K< G FHF.
83
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
&GQF G<> HG JG F G G HF K
<FG, LH LH G G G G F@ FG FG G > > G G>> G G =GF.
2.
F@ F@ H HFG FGFF = =G, G, G @ @ FH FH >> =G <F< GG .
3.
V G GH GH
4.
# G G = =GF, GF, G @ @ FH FH G G J ( GL @G.
H= : I: 1.
'> H '> HGF GF J J G F@ <G <G . . FF < IL HG.
84
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
2.
A @GL @GL G GL L @G @G <F< <F< G G G F FF. F. GH GL G @G FH @G G @@ HG <G.
3.
# G GQF W F F G HG FG FGHGF. HGF. L , G@ J GGL HL. #G @L I switch G GG <>F <>F J< HG FF G <FGG<G. BHG QG JL, JQ WK
4.
J, G J, G FG G G FH FHFF FFF F GL GL HG
5.
# G G G G @L @L HG HG GLF GLF @> FF G G FHFFF G@, LH HF G G @G @ G F FF < FHFFF. <FG, <@< GG LH I G J< <L FFF: G F Mail FF J
85
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
Train . < G AirMail FF L HFF Plane =GF, GroundMail @L J> J> J
6.
#, G G G KG KGG<F, G<F, G F GL @G F @ @GL, @GL, LH @>
P (+ C
H I< G<G H< GJ G G G G HGF.
S2+ S2 +) ) R)7 )74 427 27& & 8= 8= P6 P62' 2'4 4) ). H @I G HG -
G< <G < G @, @>< G F< G FHG.
O4)2/C O4) 2/C7) 7)( ( P6 P62' 2'4) 4). H H <GH J GLF GL F HGF
<G G @ J< K
@L @ @ @<G F< LH G <GH G J FHFFF G <@@G G GG. FG F F F< <
86
CG< C G< DF< DF< * *GG GGF F / G GL L 'G
(: =:/ O:/ P(:: U
F"3 "3 8 8 M$ M$3' 3'# # (FF @<G 'L 'L F< F< FF FG FGG G L HF HF< < F @ HFG@
U
A!23 A! 23 "3 "3 F F"3 "3 8 8 FFF FFF G F FG F"38 M$3'#2, HG LH F HF P338$ G @F G
@GF GF FFF. U
F"3 "3 8 8 M$ M$3' 3'# # J
U
P338$
J>F. G G , P688=4) H<F @<GJ>F. F"3 "3 8 8 M$ M$3' 3'# #
F"3 F "3 8 8 M$ M$3' 3'# #
320 pages from the full book are omitted in the demo version