The GoF Design Patterns Reference

Preparing to load PDF file. please wait...

0 of 0
100%
The GoF Design Patterns Reference

Transcript Of The GoF Design Patterns Reference

The GoF Design Patterns Reference
Version 2.0 / 01.10.2017 / Generated 12.01.2018
Copyright © 2014-2018 w3sDesign. All rights reserved.

The GoF Design Patterns Reference

w3sDesign / V 2.0 / 12.01.2018

ii

Table of Contents
Preface ......................................................................................................................... viii I. Introduction .................................................................................................................. 1
1. DESIGN PRINCIPLES ........................................................................................ 2 2. OVERVIEW ........................................................................................................ 7 II. Creational Patterns ..................................................................................................... 11 1. ABSTRACT FACTORY ..................................................................................... 12
Intent ............................................................................................................. 12 Problem ......................................................................................................... 13 Solution ......................................................................................................... 14 Motivation 1 .................................................................................................. 16 Applicability .................................................................................................. 17 Structure, Collaboration .................................................................................. 18 Consequences ................................................................................................. 19 Implementation .............................................................................................. 20 Sample Code 1 ............................................................................................... 21 Sample Code 2 ............................................................................................... 24 Sample Code 3 ............................................................................................... 26 Related Patterns ............................................................................................. 28 2. BUILDER .......................................................................................................... 29 Intent ............................................................................................................. 29 Problem ......................................................................................................... 30 Solution ......................................................................................................... 31 Motivation 1 .................................................................................................. 32 Applicability .................................................................................................. 33 Structure, Collaboration .................................................................................. 34 Consequences ................................................................................................. 35 Implementation .............................................................................................. 36 Sample Code 1 ............................................................................................... 37 Related Patterns ............................................................................................. 39 3. FACTORY METHOD ........................................................................................ 40 Intent ............................................................................................................. 40 Problem ......................................................................................................... 41 Solution ......................................................................................................... 42 Motivation 1 .................................................................................................. 43 Applicability .................................................................................................. 44 Structure, Collaboration .................................................................................. 46 Consequences ................................................................................................. 47 Implementation .............................................................................................. 48 Sample Code 1 ............................................................................................... 50 Sample Code 2 ............................................................................................... 52 Related Patterns ............................................................................................. 53 4. PROTOTYPE ..................................................................................................... 54 Intent ............................................................................................................. 54 Problem ......................................................................................................... 55 Solution ......................................................................................................... 56 Motivation 1 .................................................................................................. 57 Applicability .................................................................................................. 58 Structure, Collaboration .................................................................................. 59 Consequences ................................................................................................. 60 Implementation .............................................................................................. 61 Sample Code 1 ............................................................................................... 62 Sample Code 2 ............................................................................................... 64 Related Patterns ............................................................................................. 66

The GoF Design Patterns Reference

w3sDesign / V 2.0 / 12.01.2018 iii

5. SINGLETON ..................................................................................................... 67 Intent ............................................................................................................. 67 Problem ......................................................................................................... 68 Solution ......................................................................................................... 69 Motivation 1 .................................................................................................. 70 Applicability .................................................................................................. 71 Structure, Collaboration .................................................................................. 72 Consequences ................................................................................................. 73 Implementation .............................................................................................. 74 Sample Code 1 ............................................................................................... 75 Related Patterns ............................................................................................. 76
III. Structural Patterns .................................................................................................... 77 1. ADAPTER ......................................................................................................... 78 Intent ............................................................................................................. 78 Problem ......................................................................................................... 79 Solution ......................................................................................................... 80 Motivation 1 .................................................................................................. 81 Applicability .................................................................................................. 82 Structure, Collaboration .................................................................................. 83 Consequences ................................................................................................. 84 Implementation .............................................................................................. 85 Sample Code 1 ............................................................................................... 86 Sample Code 2 ............................................................................................... 88 Related Patterns ............................................................................................. 89 2. BRIDGE ............................................................................................................ 90 Intent ............................................................................................................. 90 Problem ......................................................................................................... 91 Solution ......................................................................................................... 92 Motivation 1 .................................................................................................. 93 Applicability .................................................................................................. 94 Structure, Collaboration .................................................................................. 95 Consequences ................................................................................................. 96 Implementation .............................................................................................. 97 Sample Code 1 ............................................................................................... 98 Related Patterns ............................................................................................. 99 3. COMPOSITE ................................................................................................... 100 Intent ........................................................................................................... 100 Problem ....................................................................................................... 101 Solution ....................................................................................................... 102 Motivation 1 ................................................................................................ 104 Applicability ................................................................................................. 105 Structure, Collaboration ................................................................................ 106 Consequences ............................................................................................... 107 Implementation ............................................................................................. 108 Sample Code 1 ............................................................................................. 109 Sample Code 2 ............................................................................................. 111 Related Patterns ............................................................................................ 113 4. DECORATOR .................................................................................................. 114 Intent ........................................................................................................... 114 Problem ....................................................................................................... 115 Solution ....................................................................................................... 116 Motivation 1 ................................................................................................ 117 Applicability ................................................................................................. 118 Structure, Collaboration ................................................................................ 119 Consequences ............................................................................................... 120

The GoF Design Patterns Reference

w3sDesign / V 2.0 / 12.01.2018 iv

Implementation ............................................................................................. 121 Sample Code 1 ............................................................................................. 122 Sample Code 2 ............................................................................................. 124 Related Patterns ............................................................................................ 125 5. FACADE ......................................................................................................... 126 Intent ........................................................................................................... 126 Problem ....................................................................................................... 127 Solution ....................................................................................................... 128 Motivation 1 ................................................................................................ 129 Applicability ................................................................................................. 130 Structure, Collaboration ................................................................................ 131 Consequences ............................................................................................... 132 Implementation ............................................................................................. 133 Sample Code 1 ............................................................................................. 134 Related Patterns ............................................................................................ 136 6. FLYWEIGHT ................................................................................................... 137 Intent ........................................................................................................... 137 Problem ....................................................................................................... 138 Solution ....................................................................................................... 139 Motivation 1 ................................................................................................ 140 Applicability ................................................................................................. 141 Structure, Collaboration ................................................................................ 142 Consequences ............................................................................................... 144 Implementation ............................................................................................. 145 Sample Code 1 ............................................................................................. 146 Related Patterns ............................................................................................ 148 7. PROXY ........................................................................................................... 149 Intent ........................................................................................................... 149 Problem ....................................................................................................... 150 Solution ....................................................................................................... 151 Motivation 1 ................................................................................................ 152 Applicability ................................................................................................. 153 Structure, Collaboration ................................................................................ 154 Consequences ............................................................................................... 155 Implementation ............................................................................................. 156 Sample Code 1 ............................................................................................. 157 Related Patterns ............................................................................................ 158 IV. Behavioral Patterns ................................................................................................. 159 1. CHAIN OF RESPONSIBILITY ........................................................................ 160 Intent ........................................................................................................... 160 Problem ....................................................................................................... 161 Solution ....................................................................................................... 162 Motivation 1 ................................................................................................ 163 Applicability ................................................................................................. 164 Structure, Collaboration ................................................................................ 165 Consequences ............................................................................................... 166 Implementation ............................................................................................. 167 Sample Code 1 ............................................................................................. 168 Related Patterns ............................................................................................ 170 2. COMMAND .................................................................................................... 171 Intent ........................................................................................................... 171 Problem ....................................................................................................... 172 Solution ....................................................................................................... 173 Motivation 1 ................................................................................................ 174 Applicability ................................................................................................. 175

The GoF Design Patterns Reference

w3sDesign / V 2.0 / 12.01.2018

v

Structure, Collaboration ................................................................................ 176 Consequences ............................................................................................... 177 Implementation ............................................................................................. 178 Sample Code 1 ............................................................................................. 179 Related Patterns ............................................................................................ 181 3. INTERPRETER ............................................................................................... 182 Intent ........................................................................................................... 182 Problem ....................................................................................................... 183 Solution ....................................................................................................... 184 Motivation 1 ................................................................................................ 186 Applicability ................................................................................................. 187 Structure, Collaboration ................................................................................ 188 Consequences ............................................................................................... 189 Implementation ............................................................................................. 190 Sample Code 1 ............................................................................................. 192 Sample Code 2 ............................................................................................. 194 Related Patterns ............................................................................................ 198 4. ITERATOR ...................................................................................................... 199 Intent ........................................................................................................... 199 Problem ....................................................................................................... 200 Solution ....................................................................................................... 201 Motivation 1 ................................................................................................ 202 Applicability ................................................................................................. 203 Structure, Collaboration ................................................................................ 204 Consequences ............................................................................................... 205 Implementation ............................................................................................. 206 Sample Code 1 ............................................................................................. 207 Sample Code 2 ............................................................................................. 209 Related Patterns ............................................................................................ 213 5. MEDIATOR ..................................................................................................... 214 Intent ........................................................................................................... 214 Problem ....................................................................................................... 215 Solution ....................................................................................................... 216 Motivation 1 ................................................................................................ 217 Applicability ................................................................................................. 218 Structure, Collaboration ................................................................................ 219 Consequences ............................................................................................... 220 Implementation ............................................................................................. 221 Sample Code 1 ............................................................................................. 222 Related Patterns ............................................................................................ 225 6. MEMENTO ..................................................................................................... 226 Intent ........................................................................................................... 226 Problem ....................................................................................................... 227 Solution ....................................................................................................... 228 Motivation 1 ................................................................................................ 229 Applicability ................................................................................................. 230 Structure, Collaboration ................................................................................ 231 Consequences ............................................................................................... 232 Implementation ............................................................................................. 233 Sample Code 1 ............................................................................................. 234 Related Patterns ............................................................................................ 236 7. OBSERVER ..................................................................................................... 237 Intent ........................................................................................................... 237 Problem ....................................................................................................... 238 Solution ....................................................................................................... 239

The GoF Design Patterns Reference

w3sDesign / V 2.0 / 12.01.2018 vi

Motivation 1 ................................................................................................ 240 Applicability ................................................................................................. 241 Structure, Collaboration ................................................................................ 242 Consequences ............................................................................................... 243 Implementation ............................................................................................. 244 Sample Code 1 ............................................................................................. 245 Sample Code 2 ............................................................................................. 247 Sample Code 3 ............................................................................................. 249 Sample Code 4 ............................................................................................. 252 Related Patterns ............................................................................................ 254 8. STATE ............................................................................................................. 255 Intent ........................................................................................................... 255 Problem ....................................................................................................... 256 Solution ....................................................................................................... 257 Motivation 1 ................................................................................................ 258 Applicability ................................................................................................. 259 Structure, Collaboration ................................................................................ 260 Consequences ............................................................................................... 261 Implementation ............................................................................................. 262 Sample Code 1 ............................................................................................. 263 Sample Code 2 ............................................................................................. 265 Related Patterns ............................................................................................ 267 9. STRATEGY ..................................................................................................... 268 Intent ........................................................................................................... 268 Problem ....................................................................................................... 269 Solution ....................................................................................................... 270 Motivation 1 ................................................................................................ 272 Applicability ................................................................................................. 273 Structure, Collaboration ................................................................................ 275 Consequences ............................................................................................... 276 Implementation ............................................................................................. 277 Sample Code 1 ............................................................................................. 278 Sample Code 2 ............................................................................................. 280 Sample Code 3 ............................................................................................. 283 Related Patterns ............................................................................................ 289 10. TEMPLATE METHOD ................................................................................... 291 Intent ........................................................................................................... 291 Problem ....................................................................................................... 292 Solution ....................................................................................................... 293 Motivation 1 ................................................................................................ 294 Applicability ................................................................................................. 295 Structure, Collaboration ................................................................................ 296 Consequences ............................................................................................... 297 Implementation ............................................................................................. 298 Sample Code 1 ............................................................................................. 299 Sample Code 2 ............................................................................................. 300 Related Patterns ............................................................................................ 301 11. VISITOR ........................................................................................................ 302 Intent ........................................................................................................... 302 Problem ....................................................................................................... 303 Solution ....................................................................................................... 304 Motivation 1 ................................................................................................ 305 Applicability ................................................................................................. 306 Structure, Collaboration ................................................................................ 307 Consequences ............................................................................................... 308

The GoF Design Patterns Reference

w3sDesign / V 2.0 / 12.01.2018 vii

Implementation ............................................................................................. 309 Sample Code 1 ............................................................................................. 310 Sample Code 2 ............................................................................................. 312 Related Patterns ............................................................................................ 317 V. GoF Design Patterns Update ..................................................................................... 318 1. DEPENDENCY INJECTION ............................................................................ 319 Intent ........................................................................................................... 319 Problem ....................................................................................................... 320 Solution ....................................................................................................... 321 Motivation 1 ................................................................................................ 322 Applicability ................................................................................................. 323 Structure, Collaboration ................................................................................ 324 Consequences ............................................................................................... 325 Implementation ............................................................................................. 326 Sample Code 1 ............................................................................................. 327 Related Patterns ............................................................................................ 329 A. Bibliography ............................................................................................................ 330

Preface

w3sDesign / V 2.0 / 12.01.2018 viii

Preface
In software engineering, design patterns describe how to solve recurring design problems to design flexible and reusable object-oriented software.
w3sDesign presents the up-to-date version of the well-known GoF¹ design patterns in a compact and memory friendly way so that they can be learned and memorized as fast as possible.
We use a simple and consistent language and repeat important phrases whenever appropriate. Because a picture is worth a thousand words, each section of each design pattern starts with UML diagrams to quickly communicate the key aspects of the design under discussion.
New design patterns that are widely used today but not included in the original twenty-three GoF design patterns will be added. This release starts with the Dependency Injection design pattern, and others will follow in next releases.
By working through individual design patterns, you will learn how to design objects that are easier to implement, change, test, and reuse. Simple, ready-to-run code samples show how to implement design patterns by using objectoriented programming languages such as Java.
At w3sDesign you will find all you need to know, and you will get the skills that software developers need today.
It's all for free, and it's pretty fast. Enjoy it!

¹ Design Patterns: Elements of Reusable Object-Oriented Software. Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides. Published October 1994. Copyright © 1995 by Addison-Wesley. (The authors of the book are commonly referred to as "GoF" or "Gang of Four".)

Introduction

w3sDesign / V 2.0 / 12.01.2018

1

Part I. Introduction

DESIGN PRINCIPLES

w3sDesign / V 2.0 / 12.01.2018

2
Design PatternsDesignGof Design Patterns