Brief Overview of Gof Design Patterns: Appendix A
Brief Overview of Gof Design Patterns: Appendix A
439
© Vaskaran Sarcar 2018
V. Sarcar, Design Patterns in C#, https://doi.org/10.1007/978-1-4842-3640-6
Appendix A Brief Overview of GoF Design Patterns
The first concept of a real-life design pattern came from the building architect
Christopher Alexander. During his lifetime, he discovered that many of the problems
he faced were similar in nature. So, he tried to address those issues with similar types of
solutions.
Each pattern describes a problem, which occurs over and over again in our
environment, and then describes the core of the solution to that problem, in
such a way that you can use this solution a million times over, without ever
doing it the same way twice.
—Christopher Alexander
The GoF assured us that though these patterns were described for buildings and towns,
the same concepts can be applied to patterns in object-oriented design. We can substitute
the original concepts of walls and doors with objects and interfaces. The common thing in
both fields is that, at their cores, patterns are solutions to common problems.
The GoF discussed the original concepts in the context of C++. C# 1.0 was released
in 2002, and at the time of this writing, C# 7.0 is available with Visual Studio 2017. In this
book, I’ll examine the GoF’s original design patterns but with C#. The book is written in
C#, but if you are familiar with any other popular programming languages such as Java,
C++, and so on, you will be able to relate because I focus on the design patterns and not
on the latest features of C#. In fact, I have purposely chosen simple examples to help you
to understand these concepts easily.
Key Points
The following are key points about design patterns:
440
Appendix A Brief Overview of GoF Design Patterns
Singleton pattern
Prototype pattern
Factory Method pattern
Builder pattern
Abstract Factory pattern
B. Structural patterns
441
Appendix A Brief Overview of GoF Design Patterns
C. Behavioral patterns
Observer pattern
Strategy pattern
Command pattern
Iterator pattern
Memento pattern
State pattern
Mediator pattern
Visitor pattern
Interpreter pattern
442
Appendix A Brief Overview of GoF Design Patterns
For a quick reference, you can refer to the following table that was
introduced by the GoF:
• In this book, you can start with any pattern you like. I have chosen
simple examples so that you can pick up the basic ideas quickly. Read
about each pattern, practice, try to solve other problems with the
these patterns, and then just keep coding. This process will help you
to master the subject quickly.
Q&A Session
1. What are differences between class patterns and object patterns?
443
Appendix A Brief Overview of GoF Design Patterns
Creational Can defer object creation to subclasses Can defer object creation to another
object
Structural Focuses on the composition of classes Focuses on the different ways of
(primarily uses the concept of inheritance) composition of objects
Behavioral Describes the algorithms and execution Describes how different objects can
flows work together and complete a task
The GoF clearly excludes these concepts, saying that they are not
complex, domain-specific designs for an entire application or
subsystem. They can be encoded in classes and reused as is. So, they
are not your concern in this book.
444
APPENDIX B
447
© Vaskaran Sarcar 2018
V. Sarcar, Design Patterns in C#, https://doi.org/10.1007/978-1-4842-3640-6
Index
A object, 109
Output, 103
Abstract Factory pattern
Solution Explorer
Class Diagram, 58
View, 100–101
concept, 55
user interface and
GoF definition, 55
database, 98
illustration, 57
Anti-patterns
implementation, 60–63
catalog, 394
Output, 63
causes, 393
PetAnimalFactory, 57
concepts, 394
real-life example, 56
definition, 391
Solution Explorer View, 58–59
refactoring, 395
structure, 56, 57
software developers, 392
WildAnimalFactory, 57
symptoms, 393
Adapter pattern
class, 110
Class Diagram, 99 B
concept, 97 Bridge pattern
Directed Graph advantages, 164
Document, 100 challenges, 164
drawbacks, 111 Class Diagram, 157–158
electrical outlet, 97–98 concept, 155
GetArea() method, 99 electronic items, 156–157
GoF definition, 97 GoF definition, 155
implementation, 102–103 GUI frameworks, 156
modified illustration implementation, 159–162
characteristics, 104 Output, 163
implementation, 106–108 simple subclassing, 164
Output, 109 Solution Explorer View, 159
Solution Explorer View, 105 State pattern, 163
449
© Vaskaran Sarcar 2018
V. Sarcar, Design Patterns in C#, https://doi.org/10.1007/978-1-4842-3640-6
Index
450
Index
451
Index
G, H concept, 315
GoF definition, 315
Garbage collection (GC)
grammar rules, 316
allocated objects, 400
implementation, 318–325
automatic memory management, 412
Java compiler, 315
characteristics, 404
Output, 325
coding
Solution Explorer View, 318
comment lines, 417
translator, foreign language, 315
compile, 413, 415
Iterator pattern
destructor method, 417
challenges, 254
developer command
Class Diagram, 245
prompt, 417–418
collection objects, 243, 254
invoking Dispose(), 418
concept, 243
prediction, output, 415–416
data structure, student records, 244
Common Language Runtime (CLR), 398
Directed Graph Document, 246
compaction, 406
First(), Next(), IsDone(), and
destructors, 399, 405
CurrentItem() methods, 244
dirty objects, 397
GoF definition, 243
Dispose() method, 409–411
implementation, 248–255
Finalize() method, 405
Output, 253
finalizers, drawbacks, 412
Solution Explorer View, 247
GC.Collect(), 407
GC.SuppressFinalize(this), 411
IDisposable interface, 408–409 M
implementation, 400–403
Mediator pattern
longer-lived objects, 398
advantages, 300–301
low-priority thread, 397
airplanes, 283
managed heap, 405
analysis, 292
memory consumption rate, 400
Class Diagram, 286
Output, 403
concept, 283
short-lived objects, 398
disadvantages, 301
System.GC.Collect() method, 400
vs. Facade pattern, 301, 436
GoF definition, 283
I, J, K, L implementation, 288–292
Interpreter pattern interconnections, objects, 301
advantages, 326 modified implementation, 294–299
challenges, 326 Modified Output, 299–300
Class Diagram, 316–317 vs. Observer pattern, 435
452
Index
453
Index
454
Index
T, U
Template Method pattern W, X, Y, Z
advantages, 222 Windows Presentation Foundation
BasicEngineering class, 217–218 (WPF), 224
455