DiDive Inve Inttoo
DEDESSIGNIGN
PPAATTTERNTERNSS
J
A Few Words on Copyright
#= (M B5A9 =G @9L5B89F -<J9HG $A
H<9 5IH<CF C: H<9 6CC? Dive Into Design
Patterns
1
5B8 H<9 CB@=B9 7CIFG9 Dive
Into Refactoring
2
.<=G 6CC? =G :CF MCIF D9FGCB5@ IG9 CB@M
+@95G9 8CBH G<5F9 =H K=H< 5BM H<=F8
D5FH=9G 9L79DH MCIF :5A=@M A9A69FG $: MCI8 @=?9 HC G<5F9 H<9
6CC? K=H< 5 :F=9B8 CF 7C@@95;I9 6IM 5B8 G9B8 H<9A 5 B9K
7CDM 3CI 75B 5@GC 6IM 5 G=H9 @=79BG9 :CF MCIF K<C@9 H95A CF H<9
9BH=F9 7CAD5BM
@@ DFCYH :FCA H<9 G5@9 C: AM 6CC?G 5B8 7CIFG9G =G GD9BH CB
H<9 89J9@CDA9BH C: Refactoring.Guru 57< 7CDM GC@8 <9@DG
H<9 DFC>97H =AA9BG9@M 5B8 6F=B;G H<9 ACA9BH C: 5 B9K 6CC?
F9@95G95@=HH@96=H 7@CG9F
@9L5B89F-<J9HG,9:57HCF=B;"IFI
support@refactoring.guru
$@@IGHF5H=CBGA=HFM4<5FH
8=H=B;B8F9K19HACF9,<M5B-C@CACB
 Dive Into Design Patterns https://refactoring.guru/design-patterns/book
 Dive Into Refactoring https://refactoring.guru/refactoring/course
I dedicate this book to my wife, Maria. If it
hadn't been for her, I'd probably have Bnished
the book some 30 years later.
Table of Contents
Table of Contents .................................................................................................. 4
How to Read This Book......................................................................................... 6
INTRODUCTION TO OOP ........................................................................................7
5G=7GC:**+
+=@@5FGC:**+ 
,9@5H=CBG9HK99B*6>97HG 
INTRODUCTION TO DESIGN PATTERNS........................................................... 26
1<5HG59G=;B+5HH9FB 
1<M-<CI@8$'95FB+5HH9FBG 
SOFTWARE DESIGN PRINCIPLES ..................................................................... 32
!95HIF9GC:"CC89G=;B 
Design Principles........................................................................................... 37
O B75DGI@5H91<5H05F=9G 
O +FC;F5AHC5B$BH9F:579BCH5B$AD@9A9BH5H=CB  
O !5JCFCADCG=H=CB*J9F$B<9F=H5B79 
SOLID Principles .............................................................................................51
O -=B;@9,9GDCBG=6=@=HM+F=B7=D@9 
O *D9B@CG98+F=B7=D@9 
O '=G?CJ-I6GH=HIH=CB+F=B7=D@9 
O $BH9F:579-9;F9;5H=CB+F=B7=D@9 
O 9D9B89B7M$BJ9FG=CB+F=B7=D@9  
.56@9C:CBH9BHG
#156629
sevenbookyue@gmail.com (#156629)
CATALOG OF DESIGN PATTERNS...................................................................... 71
Creational Design Patterns......................................................................... 72
O !57HCFM(9H<C8  
O 6GHF57H!57HCFM  
O I=@89F 
O +FCHCHMD9 
O -=B;@9HCB 
Structural Design Patterns....................................................................... 147
O 85DH9F
O F=8;9 
O CADCG=H9 
O 97CF5HCF
O !57589 
O !@MK9=;<H
O +FCLM 
Behavioral Design Patterns ...................................................................... 247
O <5=BC:,9GDCBG=6=@=HM
O CAA5B8 
O $H9F5HCF
O (98=5HCF
O (9A9BHC
O *6G9FJ9F
O -H5H9 
O -HF5H9;M
O .9AD@5H9(9H<C8
O 0=G=HCF
Conclusion ......................................................................................................... 410
.56@9C:CBH9BHG
#156629
sevenbookyue@gmail.com (#156629)
How to Read This Book
.<=G 6CC? 7CBH5=BG H<9 89G7F=DH=CBG C:  7@5GG=7 89G=;B D5H
H9FBG :CFAI@5H98 6M H<9 T"5B; C: !CIFU CF G=AD@M "C! =B 
57< 7<5DH9F 9LD@CF9G 5 D5FH=7I@5F D5HH9FB .<9F9:CF9 MCI 75B
F958 :FCA 7CJ9F HC 7CJ9F CF 6M D=7?=B; H<9 D5HH9FBG MCISF9 =BH9F
9GH98 =B
(5BM D5HH9FBG 5F9 F9@5H98 GC MCI 75B 95G=@M >IAD :FCA HCD=7
HC HCD=7 IG=B; BIA9FCIG 5B7<CFG .<9 9B8 C: 957< 7<5DH9F <5G
5 @=GH C: @=B?G 69HK99B H<9 7IFF9BH D5HH9FB 5B8 CH<9FG $: MCI
G99 H<9 B5A9 C: 5 D5HH9FB H<5H MCI <5J9BSH G99B M9H >IGH ?99D
F958=B;QH<=G=H9AK=@@5DD95F=BCB9C:H<9B9LH 7<5DH9FG
9G=;B D5HH9FBG 5F9 IB=J9FG5@ .<9F9:CF9 5@@ 7C89 G5AD@9G =B
H<=G 6CC? 5F9 KF=HH9B =B DG9I8C7C89 H<5H 8C9GBSH 7CBGHF5=B H<9
A5H9F=5@HC5D5FH=7I@5FDFC;F5AA=B; @5B;I5;9
+F=CF HC GHI8M=B; D5HH9FBG MCI 75B F9:F9G< MCIF A9ACFM 6M
;C=B; CJ9F H<9 key terms of object-oriented programming
.<5H 7<5DH9F 5@GC 9LD@5=BG H<9 65G=7G C: /(' 8=5;F5AG K<=7<
=G IG9:I@ 6975IG9 H<9 6CC? <5G HCBG C: H<9A *: 7CIFG9 =: MCI
5@F958M ?BCK 5@@ C: H<5H MCI 75B DFC7998 HC learning patterns
F=;<H 5K5M
#CKHCF958H<=G6CC?
#156629
sevenbookyue@gmail.com (#156629)
INTRODUCTION
TO OOP
#156629
sevenbookyue@gmail.com (#156629)
Basics of OOP
Object-oriented programming =G 5 D5F58=;A 65G98 CB H<9 7CB
79DH C: KF5DD=B; D=979G C: 85H5 5B8 69<5J=CF F9@5H98 HC H<5H
85H5 =BHC GD97=5@ 6IB8@9G 75@@98 objects K<=7< 5F9 7CBGHFI7H
98 :FCA 5 G9H C: T6@I9DF=BHGU 89YB98 6M 5 DFC;F5AA9F 75@@98
classes
Objects, classes
C MCI @=?9 75HG $ <CD9 MCI 8C 6975IG9 $S@@ HFM HC 9LD@5=B H<9
**+7CB79DHGIG=B;J5F=CIG75H 9L5AD@9G
This is a UML class diagram. You’ll see a lot of such diagrams in the book.
$BHFC8I7H=CBHC**+5G=7GC:**+
#156629
sevenbookyue@gmail.com (#156629)
-5M MCI <5J9 5 75H B5A98 *G75F *G75F =G 5B C6>97H 5B =BGH5B79
C: H<9
Cat
7@5GG J9FM 75H <5G 5 @CH C: GH5B85F8 5HHF=6IH9G
B5A9 G9L 5;9 K9=;<H 7C@CF :5JCF=H9 :CC8 9H7 .<9G9 5F9 H<9
7@5GGSG Belds
@@ 75HG 5@GC 69<5J9 G=A=@5F@M H<9M 6F95H<9 95H FIB G@99D 5B8
A9CK .<9G9 5F9 H<9 7@5GGSG methods C@@97H=J9@M Y9@8G 5B8
A9H<C8G75B69F9:9F9B7985GH<9 members C:H<9=F 7@5GG
5H5 GHCF98 =BG=89 H<9 C6>97HSG Y9@8G =G C:H9B F9:9F9B798
5G state5B85@@H<9C6>97HSGA9H<C8G89YB9=HG behavior
Objects are instances of classes.
$BHFC8I7H=CBHC**+5G=7GC:**+
#156629
sevenbookyue@gmail.com (#156629)
'IB5 MCIF :F=9B8SG 75H =G 5@GC 5B =BGH5B79 C: H<9
Cat
7@5GG
$H <5G H<9 G5A9 G9H C: 5HHF=6IH9G 5G *G75F .<9 8=::9F9B79 =G =B
J5@I9G C: H<9G9 5HHF=6IH9G =HG G9L =G :9A5@9 =H <5G 5 8=::9F9BH
7C@CF5B8K9=;<G @9GG
-C 5 class =G @=?9 5 6@I9DF=BH H<5H 89YB9G H<9 GHFI7HIF9 :CF
objectsK<=7<5F97CB7F9H9=BGH5B79GC:H<5H 7@5GG
Class hierarchies
J9FMH<=B;SG YB9 5B8 85B8M K<9B K9 H5@? 56CIH CB9 7@5GG )5H
IF5@@M 5 F95@ DFC;F5A 7CBH5=BG ACF9 H<5B 5 G=B;@9 7@5GG -CA9
C: H<9G9 7@5GG9G A=;<H 69 CF;5B=N98 =BHC class hierarchies '9HSG
YB8CIHK<5HH<5H A95BG
-5M MCIF B9=;<6CF <5G 5 8C; 75@@98 !=8C $H HIFBG CIH 8C;G
5B8 75HG <5J9 5 @CH =B 7CAACB B5A9 G9L 5;9 5B8 7C@CF 5F9
5HHF=6IH9G C: 6CH< 8C;G 5B8 75HG C;G 75B 6F95H<9 G@99D 5B8
FIB H<9 G5A9 K5M 75HG 8C -C =H G99AG H<5H K9 75B 89YB9 H<9
65G9
Animal
7@5GG H<5H KCI@8 @=GH H<9 7CAACB 5HHF=6IH9G 5B8
69<5J=CFG
D5F9BH 7@5GG @=?9 H<9 CB9 K9SJ9 >IGH 89YB98 =G 75@@98 5
superclass $HG 7<=@8F9B 5F9 subclasses -I67@5GG9G =B<9F=H GH5H9
5B8 69<5J=CF :FCA H<9=F D5F9BH 89YB=B; CB@M 5HHF=6IH9G CF
69<5J=CFG H<5H 8=::9F .<IG H<9
Cat
7@5GG KCI@8 <5J9 H<9
meow
A9H<C85B8H<9
Dog
7@5GGH<9
bark
A9H<C8
 $BHFC8I7H=CBHC**+5G=7GC:**+
#156629
sevenbookyue@gmail.com (#156629)
UML diagram of a class hierarchy. All classes in this diagram are part of
the
Animal
class hierarchy.
GGIA=B; H<5H K9 <5J9 5 F9@5H98 6IG=B9GG F9EI=F9A9BH K9 75B
;C 9J9B :IFH<9F 5B8 9LHF57H 5 ACF9 ;9B9F5@ 7@5GG :CF 5@@ @=J
=B;
Organisms
K<=7< K=@@ 697CA9 5 GID9F7@5GG :CF
Animals
5B8
Plants
-I7< 5 DMF5A=8 C: 7@5GG9G =G 5 hierarchy $B GI7<
5 <=9F5F7<M H<9
Cat
7@5GG =B<9F=HG 9J9FMH<=B; :FCA 6CH< H<9
Animal
5B8
Organism
7@5GG9G
 $BHFC8I7H=CBHC**+5G=7GC:**+
#156629
sevenbookyue@gmail.com (#156629)
Classes in a UML diagram can be simpliBed if it’s more important to show
their relations than their contents.
-I67@5GG9G 75B CJ9FF=89 H<9 69<5J=CF C: A9H<C8G H<5H H<9M
=B<9F=H :FCA D5F9BH 7@5GG9G GI67@5GG 75B 9=H<9F 7CAD@9H9
@M F9D@579 H<9 89:5I@H 69<5J=CF CF >IGH 9B<5B79 =H K=H< GCA9
9LHF5 GHI::
 $BHFC8I7H=CBHC**+5G=7GC:**+
#156629
sevenbookyue@gmail.com (#156629)
Pillars of OOP
*6>97HCF=9BH98 DFC;F5AA=B; =G 65G98 CB :CIF D=@@5FG 7CB
79DHGH<5H8=::9F9BH=5H9=H:FCACH<9FDFC;F5AA=B; D5F58=;AG
Abstraction
(CGH C: H<9 H=A9 K<9B MCISF9 7F95H=B; 5 DFC;F5A K=H< **+
MCI G<5D9 C6>97HG C: H<9 DFC;F5A 65G98 CB F95@KCF@8 C6>97HG
#CK9J9F C6>97HG C: H<9 DFC;F5A 8CBSH F9DF9G9BH H<9 CF=;=
B5@G K=H<  577IF57M 5B8 =HSG F5F9@M F9EI=F98 H<5H H<9M 8C
$BGH958 MCIF C6>97HG CB@M model 5HHF=6IH9G 5B8 69<5J=CFG C:
F95@C6>97HG=B5GD97=Y77CBH9LH=;BCF=B;H<9 F9GH
!CF 9L5AD@9 5B
Airplane
7@5GG 7CI@8 DFC656@M 9L=GH =B 6CH<
5 Z=;<H G=AI@5HCF 5B8 5 Z=;<H 6CC?=B; 5DD@=75H=CB IH =B H<9
:CFA9F 75G9 =H KCI@8 <C@8 89H5=@G F9@5H98 HC H<9 57HI5@ Z=;<H
K<9F95G =B H<9 @5HH9F 7@5GG MCI KCI@8 75F9 CB@M 56CIH H<9 G95H
A5D5B8K<=7<G95HG5F9 5J5=@56@9
 $BHFC8I7H=CBHC**++=@@5FGC:**+
#156629
sevenbookyue@gmail.com (#156629)
Different models of the same real-world object.
Abstraction =G 5 AC89@ C: 5 F95@KCF@8 C6>97H CF D<9BCA9BCB
@=A=H98 HC 5 GD97=Y7 7CBH9LH K<=7< F9DF9G9BHG 5@@ 89H5=@G F9@9
J5BHHCH<=G7CBH9LHK=H<<=;<577IF57M5B8CA=HG5@@H<9 F9GH
Encapsulation
.C GH5FH 5 75F 9B;=B9 MCI CB@M B998 HC HIFB 5 ?9M CF DF9GG 5
6IHHCB 3CI 8CBSH B998 HC 7CBB97H K=F9G IB89F H<9 <CC8 FCH5H9
H<9 7F5B?G<5:H 5B8 7M@=B89FG 5B8 =B=H=5H9 H<9 DCK9F 7M7@9 C:
H<9 9B;=B9 .<9G9 89H5=@G 5F9 <=889B IB89F H<9 <CC8 C: H<9
75F 3CI <5J9 CB@M 5 G=AD@9 =BH9F:579 5 GH5FH GK=H7< 5 GH99F=B;
K<99@ 5B8 GCA9 D985@G .<=G =@@IGHF5H9G <CK 957< C6>97H <5G
5B interfaceQ5 DI6@=7 D5FH C: 5B C6>97H CD9B HC =BH9F57H=CBG
K=H<CH<9F C6>97HG
 $BHFC8I7H=CBHC**++=@@5FGC:**+
#156629
sevenbookyue@gmail.com (#156629)
Encapsulation =G H<9 56=@=HM C: 5B C6>97H HC <=89 D5FHG C: =HG
GH5H9 5B8 69<5J=CFG :FCA CH<9F C6>97HG 9LDCG=B; CB@M 5 @=A=H
98=BH9F:579HCH<9F9GHC:H<9 DFC;F5A
To encapsulate GCA9H<=B; A95BG HC A5?9 =H
private
5B8 H<IG
5779GG=6@9 CB@M :FCA K=H<=B H<9 A9H<C8G C: =HG CKB 7@5GG
.<9F9SG 5 @=HH@9 6=H @9GG F9GHF=7H=J9 AC89 75@@98
protected
H<5H
A5?9G5A9A69FC:57@5GG5J5=@56@9HCGI67@5GG9G 5G K9@@
$BH9F:579G 5B8 56GHF57H 7@5GG9GA9H<C8G C: ACGH DFC;F5AA=B;
@5B;I5;9G 5F9 65G98 CB H<9 7CB79DHG C: 56GHF57H=CB 5B8 9B75D
GI@5H=CB $B AC89FB C6>97HCF=9BH98 DFC;F5AA=B; @5B;I5;9G
H<9 =BH9F:579 A97<5B=GA IGI5@@M 897@5F98 K=H< H<9
interface
CF
protocol
?9MKCF8 @9HG MCI 89YB9 7CBHF57HG C: =BH9F57
H=CB 69HK99B C6>97HG .<5HSG CB9 C: H<9 F95GCBG K<M H<9 =BH9F
:579G CB@M 75F9 56CIH 69<5J=CFG C: C6>97HG 5B8 K<M MCI 75BSH
897@5F95Y9@8=B5B =BH9F:579
.<9 :57H H<5H H<9 KCF8 interface GH5B8G :CF 5 DI6@=7 D5FH
C: 5B C6>97H K<=@9 H<9F9SG 5@GC H<9
interface
HMD9 =B
ACGH DFC;F5AA=B; @5B;I5;9G =G J9FM 7CB:IG=B; $SA K=H<
MCI CB H<5H
$A5;=B9 H<5H MCI <5J9 5
FlyingTransport
=BH9F:579 K=H< 5
A9H<C8
fly(origin, destination, passengers)
1<9B 89G=;B
=B; 5B 5=F HF5BGDCFH5H=CB G=AI@5HCF MCI 7CI@8 F9GHF=7H H<9
Airport
7@5GG HC KCF? CB@M K=H< C6>97HG H<5H =AD@9A9BH H<9
FlyingTransport
=BH9F:579 :H9F H<=G MCI 75B 69 GIF9 H<5H 5BM
 $BHFC8I7H=CBHC**++=@@5FGC:**+
#156629
sevenbookyue@gmail.com (#156629)
C6>97H D5GG98 HC 5B 5=FDCFH C6>97H K<9H<9F =HSG 5B
Airplane
5
Helicopter
CF 5 :F95?=B;
DomesticatedGryphon
KCI@8 69 56@9
HC5FF=J9CF89D5FH:FCAH<=GHMD9C: 5=FDCFH
UML diagram of several classes implementing an interface.
3CI 7CI@8 7<5B;9 H<9 =AD@9A9BH5H=CB C: H<9
fly
A9H<C8 =B
H<9G9 7@5GG9G =B 5BM K5M MCI K5BH G @CB; 5G H<9 G=;B5HIF9
C: H<9 A9H<C8 F9A5=BG H<9 G5A9 5G 897@5F98 =B H<9 =BH9F:579
5@@ =BGH5B79G C: H<9
Airport
7@5GG 75B KCF? K=H< MCIF ZM=B;
C6>97HG>IGH YB9
 $BHFC8I7H=CBHC**++=@@5FGC:**+
#156629
sevenbookyue@gmail.com (#156629)
Inheritance
Inheritance =G H<9 56=@=HM HC 6I=@8 B9K 7@5GG9G CB HCD C: 9L=GH
=B; CB9G .<9 A5=B 69B9YH C: =B<9F=H5B79 =G 7C89 F9IG9 $: MCI
K5BH HC 7F95H9 5 7@5GG H<5HSG G@=;<H@M 8=::9F9BH :FCA 5B 9L=GH=B;
CB9 H<9F9SG BC B998 HC 8ID@=75H9 7C89 $BGH958 MCI 9LH9B8 H<9
9L=GH=B; 7@5GG 5B8 DIH H<9 9LHF5 :IB7H=CB5@=HM =BHC 5 F9GI@H=B;
GI67@5GGK<=7<=B<9F=HGY9@8G5B8A9H<C8GC:H<9 GID9F7@5GG
.<9 7CBG9EI9B79 C: IG=B; =B<9F=H5B79 =G H<5H GI67@5GG9G <5J9
H<9 G5A9 =BH9F:579 5G H<9=F D5F9BH 7@5GG 3CI 75BSH <=89 5
A9H<C8 =B 5 GI67@5GG =: =H K5G 897@5F98 =B H<9 GID9F7@5GG 3CI
AIGH 5@GC =AD@9A9BH 5@@ 56GHF57H A9H<C8G 9J9B =: H<9M 8CBSH
A5?9G9BG9:CFMCIF GI67@5GG
UML diagram of extending a single class versus implementing multiple
interfaces at the same time.
$B ACGH DFC;F5AA=B; @5B;I5;9G 5 GI67@5GG 75B 9LH9B8 CB@M
CB9 GID9F7@5GG *B H<9 CH<9F <5B8 5BM 7@5GG 75B =AD@9A9BH
G9J9F5@ =BH9F:579G 5H H<9 G5A9 H=A9 IH 5G $ A9BH=CB98 69:CF9
 $BHFC8I7H=CBHC**++=@@5FGC:**+
#156629
sevenbookyue@gmail.com (#156629)
=: 5 GID9F7@5GG =AD@9A9BHG 5B =BH9F:579 5@@ C: =HG GI67@5GG9G
AIGH5@GC=AD@9A9BH =H
Polymorphism
'9HSG @CC? 5H GCA9 5B=A5@ 9L5AD@9G (CGH
Animals
75B A5?9
GCIB8G 19 75B 5BH=7=D5H9 H<5H 5@@ GI67@5GG9G K=@@ B998 HC CJ9F
F=89 H<9 65G9
makeSound
A9H<C8 GC 957< GI67@5GG 75B 9A=H
H<9 7CFF97H GCIB8 H<9F9:CF9 K9 75B 897@5F9 =H
abstract
F=;<H
5K5M .<=G @9HG IG CA=H 5BM 89:5I@H =AD@9A9BH5H=CB C: H<9
A9H<C8 =B H<9 GID9F7@5GG 6IH :CF79 5@@ GI67@5GG9G HC 7CA9 ID
K=H<H<9=F CKB
$A5;=B9 H<5H K9SJ9 DIH G9J9F5@ 75HG 5B8 8C;G =BHC 5 @5F;9 65;
.<9B K=H< 7@CG98 9M9G K9 H5?9 H<9 5B=A5@G CB96MCB9 CIH C:
 $BHFC8I7H=CBHC**++=@@5FGC:**+
#156629
sevenbookyue@gmail.com (#156629)
H<9 65; :H9F H5?=B; 5B 5B=A5@ :FCA H<9 65; K9 8CBSH ?BCK
:CF GIF9 K<5H =H =G #CK9J9F =: K9 7I88@9 =H <5F8 9BCI;< H<9
5B=A5@ K=@@ 9A=H 5 GD97=Y7 GCIB8 C: >CM 89D9B8=B; CB =HG 7CB
7F9H9 7@5GG
.<9 DFC;F5A 8C9GBSH ?BCK H<9 7CB7F9H9 HMD9 C: H<9 C6>97H 7CB
H5=B98 =BG=89 H<9
a
J5F=56@9 6IH H<5B?G HC H<9 GD97=5@ A97<
5B=GA 75@@98 polymorphism H<9 DFC;F5A 75B HF579 8CKB H<9
GI67@5GG C: H<9 C6>97H K<CG9 A9H<C8 =G 69=B; 9L97IH98 5B8
FIBH<95DDFCDF=5H9 69<5J=CF
Polymorphism =G H<9 56=@=HM C: 5 DFC;F5A HC 89H97H H<9 F95@
7@5GG C: 5B C6>97H 5B8 75@@ =HG =AD@9A9BH5H=CB 9J9B K<9B =HG
F95@HMD9=GIB?BCKB=BH<97IFF9BH 7CBH9LH
3CI 75B 5@GC H<=B? C: DC@MACFD<=GA 5G H<9 56=@=HM C: 5B C6>97H
HC TDF9H9B8U HC 69 GCA9H<=B; 9@G9 IGI5@@M 5 7@5GG =H 9LH9B8G CF
5B =BH9F:579 =H =AD@9A9BHG $B CIF 9L5AD@9 H<9 8C;G 5B8 75HG
=BH<965;K9F9DF9H9B8=B;HC69;9B9F=7 5B=A5@G
bag = [newnew Cat(), newnew Dog()];1
2
foreachforeach (Animal a : bag)3
a.makeSound()4
5
// Meow!6
// Woof!7
 $BHFC8I7H=CBHC**++=@@5FGC:**+
#156629
sevenbookyue@gmail.com (#156629)

Preview text:

Dive Into DESIGN PATTERNS v2023-2.44 A Few Words on Copyright
Hi! My name is Alexander Shvets. I'm
the author of the book Dive Into Design
Patterns
1 and the online course Dive Into Refactoring2.
This book is for your personal use only.
Please don't share it with any third
parties except your family members. If you'd like to share the
book with a friend or colleague, buy and send them a new
copy. You can also buy a site license for your whole team or the entire company.
All profit from the sale of my books and courses is spent on
the development of Refactoring.Guru. Each copy sold helps
the project immensely and brings the moment of a new book release a little bit closer.
 Alexander Shvets, Refactoring.Guru, 2022
support@refactoring.guru
 Illustrations: Dmitry Zhart
 Editing: Andrew Wetmore, Rhyan Solomon
1. Dive Into Design Patterns: https://refactoring.guru/design-patterns/book
2. Dive Into Refactoring: https://refactoring.guru/refactoring/course
I dedicate this book to my wife, Maria. If it
hadn't been for her, I'd probably have finished
the book some 30 years later. 4 Table of Contents #156629 Table of Contents
Table of Contents .................................................................................................. 4
How to Read This Book......................................................................................... 6
INTRODUCTION TO OOP ........................................................................................ 7
Basics of OOP........................................................................................8
Pillars of OOP..................................................................................... 13
Relations Between Objects............................................................ 20
INTRODUCTION TO DESIGN PATTERNS........................................................... 26
What's a Design Pattern?................................................................ 27
Why Should I Learn Patterns?....................................................... 31
SOFTWARE DESIGN PRINCIPLES ..................................................................... 32
Features of Good Design................................................................ 33
Design Principles........................................................................................... 37
§ Encapsulate What Varies........................................................ 38
§ Program to an Interface, not an Implementation ......... 42
§ Favor Composition Over Inheritance................................. 47
SOLID Principles .............................................................................................51
§ Single Responsibility Principle............................................ 52
§ Open/Closed Principle............................................................ 54
§ Liskov Substitution Principle................................................ 57
§ Interface Segregation Principle........................................... 64
§ Dependency Inversion Principle ......................................... 67
sevenbookyue@gmail.com (#156629) 5 Table of Contents #156629
CATALOG OF DESIGN PATTERNS...................................................................... 71
Creational Design Patterns......................................................................... 72
§ Factory Method ......................................................................... 74
§ Abstract Factory........................................................................ 90
§ Builder........................................................................................105
§ Prototype ...................................................................................124
§ Singleton ...................................................................................138
Structural Design Patterns....................................................................... 147
§ Adapter.......................................................................................150
§ Bridge .........................................................................................164
§ Composite .................................................................................179
§ Decorator...................................................................................193
§ Facade ........................................................................................211
§ Flyweight...................................................................................221
§ Proxy ...........................................................................................235
Behavioral Design Patterns ...................................................................... 247
§ Chain of Responsibility.........................................................251
§ Command ..................................................................................269
§ Iterator........................................................................................290
§ Mediator.....................................................................................305
§ Memento....................................................................................321
§ Observer.....................................................................................337
§ State............................................................................................353
§ Strategy......................................................................................369
§ Template Method....................................................................382
§ Visitor..........................................................................................394
Conclusion ......................................................................................................... 410
sevenbookyue@gmail.com (#156629) 6 How to read this book #156629 How to Read This Book
This book contains the descriptions of 22 classic design pat-
terns formulated by the “Gang of Four” (or simply GoF) in 1994.
Each chapter explores a particular pattern. Therefore, you can
read from cover to cover or by picking the patterns you’re inter- ested in.
Many patterns are related, so you can easily jump from topic
to topic using numerous anchors. The end of each chapter has
a list of links between the current pattern and others. If you
see the name of a pattern that you haven’t seen yet, just keep
reading—this item will appear in one of the next chapters.
Design patterns are universal. Therefore, all code samples in
this book are written in pseudocode that doesn’t constrain the
material to a particular programming language.
Prior to studying patterns, you can refresh your memory by
going over the key terms of object-oriented programming.
That chapter also explains the basics of UML diagrams, which
is useful because the book has tons of them. Of course, if you
already know all of that, you can proceed to learning patterns right away.
sevenbookyue@gmail.com (#156629) #156629 INTRODUCTION TO OOP
sevenbookyue@gmail.com (#156629) 8
Introduction to OOP / Basics of OOP #156629 Basics of OOP
Object-oriented programming is a paradigm based on the con-
cept of wrapping pieces of data, and behavior related to that
data, into special bundles called objects, which are construct-
ed from a set of “blueprints”, defined by a programmer, called classes. Objects, classes
Do you like cats? I hope you do because I’ll try to explain the
OOP concepts using various cat examples.
This is a UML class diagram. You’ll see a lot of such diagrams in the book.
sevenbookyue@gmail.com (#156629) 9
Introduction to OOP / Basics of OOP #156629
Say you have a cat named Oscar. Oscar is an object, an instance
of the Cat class. Every cat has a lot of standard attributes:
name, sex, age, weight, color, favorite food, etc. These are the class’s fields.
All cats also behave similarly: they breathe, eat, run, sleep and
meow. These are the class’s methods. Collectively, fields and
methods can be referenced as the members of their class.
Data stored inside the object’s fields is often referenced
as state, and all the object’s methods define its behavior.
Objects are instances of classes.
sevenbookyue@gmail.com (#156629) 10
Introduction to OOP / Basics of OOP #156629
Luna, your friend’s cat, is also an instance of the Cat class.
It has the same set of attributes as Oscar. The difference is in
values of these attributes: its sex is female, it has a different color, and weighs less.
So a class is like a blueprint that defines the structure for
objects, which are concrete instances of that class. Class hierarchies
Everything’s fine and dandy when we talk about one class. Nat-
urally, a real program contains more than a single class. Some
of these classes might be organized into class hierarchies. Let’s find out what that means.
Say your neighbor has a dog called Fido. It turns out, dogs
and cats have a lot in common: name, sex, age, and color are
attributes of both dogs and cats. Dogs can breathe, sleep and
run the same way cats do. So it seems that we can define the
base Animal class that would list the common attributes and behaviors.
A parent class, like the one we’ve just defined, is called a
superclass. Its children are subclasses. Subclasses inherit state
and behavior from their parent, defining only attributes or
behaviors that differ. Thus, the Cat class would have the meow
method, and the Dog class the bark method.
sevenbookyue@gmail.com (#156629) 11
Introduction to OOP / Basics of OOP #156629
UML diagram of a class hierarchy. All classes in this diagram are part of
the Animal class hierarchy.
Assuming that we have a related business requirement, we can
go even further and extract a more general class for all liv-
ing Organisms which will become a superclass for Animals
and Plants . Such a pyramid of classes is a hierarchy. In such
a hierarchy, the Cat class inherits everything from both the Animal and Organism classes.
sevenbookyue@gmail.com (#156629) 12
Introduction to OOP / Basics of OOP #156629
Classes in a UML diagram can be simplified if it’s more important to show
their relations than their contents.
Subclasses can override the behavior of methods that they
inherit from parent classes. A subclass can either complete-
ly replace the default behavior or just enhance it with some extra stuff.
sevenbookyue@gmail.com (#156629) 13
Introduction to OOP / Pillars of OOP #156629 Pillars of OOP
Object-oriented programming is based on four pillars, con-
cepts that differentiate it from other programming paradigms. Abstraction
Most of the time when you’re creating a program with OOP,
you shape objects of the program based on real-world objects.
However, objects of the program don’t represent the origi-
nals with 100% accuracy (and it’s rarely required that they do).
Instead, your objects only model attributes and behaviors of
real objects in a specific context, ignoring the rest.
For example, an Airplane class could probably exist in both
a flight simulator and a flight booking application. But in the
former case, it would hold details related to the actual flight,
whereas in the latter class you would care only about the seat
map and which seats are available.
sevenbookyue@gmail.com (#156629) 14
Introduction to OOP / Pillars of OOP #156629
Different models of the same real-world object.
Abstraction is a model of a real-world object or phenomenon,
limited to a specific context, which represents all details rele-
vant to this context with high accuracy and omits all the rest. Encapsulation
To start a car engine, you only need to turn a key or press a
button. You don’t need to connect wires under the hood, rotate
the crankshaft and cylinders, and initiate the power cycle of
the engine. These details are hidden under the hood of the
car. You have only a simple interface: a start switch, a steering
wheel and some pedals. This illustrates how each object has
an interface—a public part of an object, open to interactions with other objects.
sevenbookyue@gmail.com (#156629) 15
Introduction to OOP / Pillars of OOP #156629
Encapsulation is the ability of an object to hide parts of its
state and behaviors from other objects, exposing only a limit-
ed interface to the rest of the program.
To encapsulate something means to make it private , and thus
accessible only from within the methods of its own class.
There’s a little bit less restrictive mode called protected that
makes a member of a class available to subclasses as well.
Interfaces and abstract classes/methods of most programming
languages are based on the concepts of abstraction and encap-
sulation. In modern object-oriented programming languages,
the interface mechanism (usually declared with the interface
or protocol keyword) lets you define contracts of interac-
tion between objects. That’s one of the reasons why the inter-
faces only care about behaviors of objects, and why you can’t
declare a field in an interface.
The fact that the word interface stands for a public part
of an object, while there’s also the interface type in
most programming languages, is very confusing. I’m with you on that.
Imagine that you have a FlyingTransport interface with a
method fly(origin, destination, passengers) . When design-
ing an air transportation simulator, you could restrict the
Airport class to work only with objects that implement the
FlyingTransport interface. After this, you can be sure that any
sevenbookyue@gmail.com (#156629) 16
Introduction to OOP / Pillars of OOP #156629
object passed to an airport object, whether it’s an Airplane , a
Helicopter or a freaking DomesticatedGryphon would be able
to arrive or depart from this type of airport.
UML diagram of several classes implementing an interface.
You could change the implementation of the fly method in
these classes in any way you want. As long as the signature
of the method remains the same as declared in the interface,
all instances of the Airport class can work with your flying objects just fine.
sevenbookyue@gmail.com (#156629) 17
Introduction to OOP / Pillars of OOP #156629 Inheritance
Inheritance is the ability to build new classes on top of exist-
ing ones. The main benefit of inheritance is code reuse. If you
want to create a class that’s slightly different from an existing
one, there’s no need to duplicate code. Instead, you extend the
existing class and put the extra functionality into a resulting
subclass, which inherits fields and methods of the superclass.
The consequence of using inheritance is that subclasses have
the same interface as their parent class. You can’t hide a
method in a subclass if it was declared in the superclass. You
must also implement all abstract methods, even if they don’t make sense for your subclass.
UML diagram of extending a single class versus implementing multiple
interfaces at the same time.
In most programming languages a subclass can extend only
one superclass. On the other hand, any class can implement
several interfaces at the same time. But, as I mentioned before,
sevenbookyue@gmail.com (#156629) 18
Introduction to OOP / Pillars of OOP #156629
if a superclass implements an interface, all of its subclasses must also implement it. Polymorphism
Let’s look at some animal examples. Most Animals can make
sounds. We can anticipate that all subclasses will need to over-
ride the base makeSound method so each subclass can emit
the correct sound; therefore we can declare it abstract right
away. This lets us omit any default implementation of the
method in the superclass, but force all subclasses to come up with their own.
Imagine that we’ve put several cats and dogs into a large bag.
Then, with closed eyes, we take the animals one-by-one out of
sevenbookyue@gmail.com (#156629) 19
Introduction to OOP / Pillars of OOP #156629
the bag. After taking an animal from the bag, we don’t know
for sure what it is. However, if we cuddle it hard enough, the
animal will emit a specific sound of joy, depending on its con- crete class. 1 bag = [new Cat(), new Dog()]; 2 3 foreach (Animal a : bag) 4 a.makeSound() 5 6 // Meow! 7 // Woof!
The program doesn’t know the concrete type of the object con-
tained inside the a variable; but, thanks to the special mech-
anism called polymorphism, the program can trace down the
subclass of the object whose method is being executed and run the appropriate behavior.
Polymorphism is the ability of a program to detect the real
class of an object and call its implementation even when its
real type is unknown in the current context.
You can also think of polymorphism as the ability of an object
to “pretend” to be something else, usually a class it extends or
an interface it implements. In our example, the dogs and cats
in the bag were pretending to be generic animals.
sevenbookyue@gmail.com (#156629)