Design Patterns in C#: A Hands-On Guide With Real-World Examples 2nd Edition Vaskaran Sarcar 2024 Scribd Download
Design Patterns in C#: A Hands-On Guide With Real-World Examples 2nd Edition Vaskaran Sarcar 2024 Scribd Download
com
https://textbookfull.com/product/design-patterns-
in-c-a-hands-on-guide-with-real-world-
examples-2nd-edition-vaskaran-sarcar/
https://textbookfull.com/product/java-design-patterns-a-hands-on-
experience-with-real-world-examples-2nd-edition-vaskaran-sarcar/
textbookfull.com
https://textbookfull.com/product/java-design-patterns-a-tour-
of-23-gang-of-four-design-patterns-in-java-1st-edition-vaskaran-
sarcar/
textbookfull.com
https://textbookfull.com/product/java-design-patterns-a-tour-
of-23-gang-of-four-design-patterns-in-java-1st-edition-vaskaran-
sarcar-2/
textbookfull.com
The War on Kids How American Juvenile Justice Lost Its Way
Cara H. Drinan
https://textbookfull.com/product/the-war-on-kids-how-american-
juvenile-justice-lost-its-way-cara-h-drinan/
textbookfull.com
Principles of Tensor Calculus Tensor Calculus First
Edition Taha Sochi
https://textbookfull.com/product/principles-of-tensor-calculus-tensor-
calculus-first-edition-taha-sochi/
textbookfull.com
https://textbookfull.com/product/the-boundary-element-method-for-
engineers-and-scientists-theory-and-applications-2nd-edition-john-t-
katsikadelis/
textbookfull.com
https://textbookfull.com/product/the-oxford-handbook-of-health-care-
management-1st-edition-ewan-ferlie/
textbookfull.com
https://textbookfull.com/product/desires-for-reality-radicalism-and-
revolution-in-western-european-film-1st-edition-benjamin-halligan/
textbookfull.com
Internal Gravity Waves in the Shallow Seas 1st Edition
Stanis■aw R. Massel (Auth.)
https://textbookfull.com/product/internal-gravity-waves-in-the-
shallow-seas-1st-edition-stanislaw-r-massel-auth/
textbookfull.com
Vaskaran Sarcar
Design Patterns in C#
A Hands-on Guide with Real-world Examples
2nd ed.
The publisher, the authors and the editors are safe to assume that the
advice and information in this book are believed to be true and accurate
at the date of publication. Neither the publisher nor the authors or the
editors give a warranty, expressed or implied, with respect to the
material contained herein or for any errors or omissions that may have
been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
—Priya Shimanthoor
Test Architect
Bangalore, India
June 3, 2020
Preface
Welcome to your journey through Design Patterns in C# Second Edition.
This book is an introductory guide to the design patterns that you
want to use in C#. You probably know that the concept of design
patterns became extremely popular with the Gang of Four’s famous
book Design Patterns: Elements of Reusable Object-Oriented Software
(Addison-Wesley, 1994). That book was primarily focused on C++, but
these concepts still apply in today’s programming world.
C# had its first major release (C# 2.0) in 2005. Since then, it has
become rich with new features and is now a popular programming
language. In 2015, I wrote the book Design Patterns in C#: Computer
Science Interview Series. In 2018, Design Patterns in C#: A Hands-on
Guide with Real-World Examples was born. In these books, my core
intention was to implement each of the 23 Gang of Four (GoF) design
patterns with C# implementations. I wanted to present each pattern
with simple examples. One thing was always on my mind when writing:
I wanted to use the most basic constructs of C# so that the code would
be compatible with both the upcoming version and the legacy version
of C#. I have found this method helpful in the world of programming.
In the last few years, I got a lot of constructive feedback from my
readers. This fully revised and updated version was created with that
feedback in mind. I took the opportunity to update the formatting and
correct some typos from the previous version of the book and add new
content to this new edition. In this book, I focus on another important
area; I call it the “doubt-clearing sessions.” I knew that if I could add
more information, such as alternative ways to write the
implementations, the pros and cons of the patterns, when to choose one
approach over another, and so on, readers would find this book even
more helpful.
In this updated version of the original, the “Q&A Session” sections in
each chapter are further enhanced. These sessions can help you learn
about each pattern in more depth. In addition, you see more code
explanations for all the programs, and in many cases, the programs are
further simplified, and new programs are added for the patterns. To
learn about the most important enhancements in this edition, refer to
Appendix D at the end of this book.
How Is the Book Organized?
This book has three major parts.
Part I consists of the first 23 chapters, which discusses and
implements all the GoF design patterns.
In the world of programming, there is no shortage of patterns, and
each has its own significance. Part II discusses some additional design
patterns (Simple Factory, Null Object, and MVC) that are equally
important in today’s world of programming. In this second edition, I
dropped discussions of memory leaks, but I include several patterns
from asynchronous programming. In modern applications, these
patterns are very common.
Part III discusses the criticism of design patterns and overviews
antipatterns, which are important when you implement the concepts of
design patterns in your applications.
Each chapter is divided into six major parts: a definition (which is
basically the intent in the GoF book), a core concept, a real-world
example, a computer/coding–world example, a sample program with
various output, and the “Q&A Session” section. These sections help you
learn about each pattern in more depth.
Please remember that you have just started this journey. As you
learn the concepts, try to write your own code; only then will you
master an area.
You will be able to download all the book’s source code from the
Apress website. I plan to maintain the errata, and if necessary, I will
also make updates and announcements there.
Prerequisite Knowledge
This book’s target readers are those who are familiar with C# basic
language constructs and pure object-oriented concepts, like
polymorphism, inheritance, abstraction, encapsulation, and most
importantly, how to compile or run a C# application in Visual Studio.
This book does not invest time in easily available topics, such as how to
install Visual Studio on your system, or how to write a “Hello World”
program in C#, or how can you use an if-else statement or a while loop,
and so forth. This book was written using the most basic features of C#,
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
so for most of the programs in this book, you do not need to be familiar
with C# advanced topics. The examples are simple and straightforward.
I believe that the examples are written in such a way that even if you
are familiar with another popular language, such as Java or C++, you
can still easily grasp the concepts in this book.
Who Is This Book For?
In short, you want this book if your answer is “yes” to all of the
following questions.
Are you familiar with basic constructs in C# and object-oriented
concepts like polymorphism, inheritance, abstraction, and
encapsulation?
Do you know how to set up your coding environment?
Do you want to explore the design patterns in C# step by step?
Do you want to explore GoF design patterns?
Are you interested in learning about Simple Factory, Null Object,
MVC, and asynchronous programming patterns?
Do you want to know how the core constructs of C# work behind
these patterns?
You probably don’t want this book if the answer is “yes” to any of
the following questions.
Are you new to C#?
Are you looking for advanced concepts in C#, excluding the topics
mentioned previously?
Are you interested in exploring a book where the focus is on GoF
patterns (and the patterns listed in the previous section)?
Do you dislike a book that uses Q&A sessions?
“I do not like Windows, Visual Studio, and .NET Core. I want to learn
and use C# without them.” Is this statement true for you?
Figure P-1 Download link for Visual Studio 2019 and Visual Studio Code
Note At the time of this writing, this link works fine, and the
information is correct. But the link and policies may change in the
future.
...
[Joseph] posts: How are you? Last message posted
15-05-2020 11:30:52
Amit has received a message from Joseph
saying: How are you?
Final Words
You are showing interest in a subject that can assist you throughout
your career. If you are a developer/programmer, you need these
concepts. If you are an architect at a software company, you need these
concepts. If you are a college student, you need these concepts, not only
to score well on exams, but to enter the corporate world. Even if you are
a tester who needs to take care of white-box testing, or needs to know
the code paths of a product, knowing these concepts will help you. So, I
suggest that you should not demotivate yourself, if in the first attempt,
you do not understand everything in a particular chapter. It’s perfectly
natural. Based on your C# knowledge, you may find one pattern easier
than other. In that case, move on to the next chapter, learn from it, gain
confidence, and come back to the old chapter.
This book is designed to help you develop an adequate knowledge
of design patterns in C#, and more importantly, help you learn how to
go further. I hope that this book will help you and you will value the
effort.
Acknowledgments
First, I thank the Almighty. I sincerely believe that I could complete this
book only with His blessings. I extend my deepest gratitude and thanks
to the following people.
Ratanlal Sarkar and Manikuntala Sarkar: My dear parents, only
with your blessings could I complete this work.
Indrani, my wife; Ambika, my daughter; Aryaman, my son:
Sweethearts, once again, without your love, I could not proceed at all. I
know that we need to limit many social gatherings and invitations to
complete my books on time, and each time I promise you that I’ll take a
long break and spend more time with you.
Sambaran, my brother: Thank you for your constant
encouragement.
Carsten: I know that whenever I was in need, your support was
there. Thank you once more.
Sekhar: I know this time you helped only in the incremented
version of the book, but thank you once more.
Ankit, my technical advisor in the first edition of this book: I always
acknowledge your contribution and help. I know that your valuable
comments were some of the key foundations for this enhanced edition.
Priya, my ex-colleague cum senior: A special thanks to you for
investing your time to write the forewords for both editions of this
book. When experts like you agree to write for me, I get the additional
motivation to enhance the quality of my work.
Celestin, Laura, Smriti: Thanks for giving me another opportunity
to work with you and Apress.
Shrikant: Thank you for your exceptional support to beautify my
work.
The production team—Krishnan Sathyamurthy, Sherly, Ramraj,
Selvakumar, and MathaRajamohan: Thank you guys; your efforts are
extraordinary.
Lastly, I extend my deepest gratitude to my publisher, the editorial
board members, and everyone who directly or indirectly supports this
book.
Table of Contents
Part I: Gang of Four Design Patterns
Chapter 1:Singleton Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration 1
Output
Analysis
Q&A Session
Alternative Implementation
Analysis
Q&A Session
Chapter 2:Prototype Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration 1
Output
Modified Implementation
Class Diagram
Demonstration 2
Output
Analysis
Q&A Session
Shallow Copy vs.Deep Copy
Demonstration 3
Output from a Shallow Copy
Analysis
Output from Deep Copy
Analysis
Q&A Session
Demonstration 4
Output
Analysis
Chapter 3:Builder Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration 1
Output
Analysis
Q&A Session
An Alternative Implementation
Class Diagram
Solution Explorer View
Demonstration 2
Output
Analysis
Q&A Session
Chapter 4:Factory Method Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration 1
Output
Modified Implementation 1
Partial Demonstration 1
Output
Analysis
Q&A Session
Modified Implementation 2
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Partial Demonstration 2
Output
Chapter 5:Abstract Factory Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration 1
Output
Q&A Session
Chapter 6:Proxy Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration 1
Output
Q&A Session
Demonstration 2
Output
Chapter 7:Decorator Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration
Output
Q&A Session
Chapter 8:Adapter Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration 1
Output
Analysis
Types of Adapters
Object Adapters
Class Adapters
Q&A Session
Demonstration 2
Output
Analysis
Q&A Session
Chapter 9:Facade Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration
Output
Q&A Session
Chapter 10:Flyweight Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration 1
Output
Analysis
Q&A Session
Demonstration 2
Output
Analysis
Chapter 11:Composite Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration
Output
Q&A Session
Chapter 12:Bridge Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration 1
Output
Additional Implementation
Class Diagram
Demonstration 2
Output
Q&A Session
Chapter 13:Visitor Pattern
GoF Definition
Concept
Real-World Example
Computer-World Example
Implementation
Class Diagram
Solution Explorer View
Demonstration 1
Output
Q&A Session
Using Visitor Pattern and Composite Pattern Together
Step 1
Step 2
Step 3
Step 4
Step 5
Demonstration 2
Output
Chapter 14:Observer Pattern
GoF Definition
Concept
Random documents with unrelated
content Scribd suggests to you:
SPHENOPHYLLUM
(Plate 2)
The name Sphenophyllum refers to both stems and leaves of this
extinct genus, which was related to the scouring rushes—note its
resemblance to Annularia.
The cones of this group also are slender, delicate structures, bearing
a number of sporangia, and are correctly called Bowmanites,
although they also have been called Sphenophyllostachys. These
fossil cones frequently are found in Illinois.
14
FERNS
(Plates 1 and 3)
True ferns, like those living in today’s woodlands, were common in
the Pennsylvanian forests. Some species attained heights of 30 to 40
feet. Their fronds (compound leaves divided into segments or
leaflets) commonly were five to six feet long.
True ferns do not produce cones or seeds, but spores, which develop
in cases called sporangia. The sporangia frond showing are attached
in clusters (sori) to the lower side or margins of the leaves. In
modern ferns the sporangia may also occur on fertile spikes.
The shape and position of the sori are used to identify modern ferns,
but because leaves that bear sori (“fertile” leaves) are rare among
fossil specimens, the number, shape, and attachment of the leaflets
and the pattern of the veins are more commonly used for
identification.
Reconstruction of Megaphyton
(after Hirmer)
Because fossils of complete fern plants have not yet been found,
separate names have been adopted for detached leaves, stems, and
other parts. For example, the fossil stems of some Pennsylvanian
ferns found in Illinois have been referred to two genera,
Megaphyton, whose leaf attachment scars are arranged in two
vertical rows, one on either side of the stem, and Caulopteris, 15
whose leaf scars are arranged in a steep spiral that becomes
progressively flatter upward until near the top they appear to be
whorled. When the stem is a petrifaction, with internal structures
preserved, it is called Psaronius. The fronds are referred to a number
of genera, but those most commonly found in Illinois are Pecopteris,
Asterotheca, and Ptychocarpus.
Pecopteris
Asterotheca
Ptychocarpus
SEED FERNS
(Plate 4)
Seed ferns resembled true ferns in general, but they produced
seeds, borne on modified leaves. Where spore sacs and seeds are
absent, the leaves of seed ferns are difficult to distinguish from
those of spore ferns, although individual seed fern leaflets, called
pinnae, are somewhat larger.
Medullosa Reconstruction and original drawing by Wilson N. Stewart
Seed ferns included vinelike plants in the undergrowth and trees
such as Medullosa. Some tree genera were very tall, with 16
trunks more than two feet in diameter. Unlike the true ferns,
still living today, seed ferns declined steadily after the close of the
Pennsylvanian Period and finally became extinct during Jurassic time.
During Pennsylvanian time, however, they were much more
numerous and varied than true ferns.
Alethopteris
Odontopteris
Mariopteris
Neuropteris
Linopteris
18
Mazon Creek Strip Mine Area Showing Distribution of Spoil Heaps. The small
circular areas represent waste from underground mines.
Of all the fossils that have been found in Illinois, the most famous
are the plant remains from the world-renowned Mazon Creek area in
the northeastern part of the state. In this area in Grundy and Will
Counties, plant fossils are found in ironstone concretions in the lower
part of the Francis Creek Shale directly overlying the Colchester (No.
2) Coal.
Calamites ⅓×
Stigmaria ⅖×
Lepidodendron ⅖×
Calamites ¹/₁₀×
Fern Stem ⅔×
Caulopteris ¼×
Sigillaria ⅖×
Megaphyton ⅗×
Calamites ⅗×
Artisia ⅗×
Sigillaria sub-bark ⅗×
21
The concretions generally are oval to elongate and range from less
than an inch to a foot or more in maximum dimension. Only about
one nodule in ten contains plant remains.
In earlier years good collections were made from the spoil heaps of
underground mines. Especially notable are the mine dumps of the
Wilmington Star No. 7 mine, 2¼ miles west of Coal City, and Skinner
No. 2 mine, two miles northeast of Braidwood.
Sphenophyllum ⅗×
Lepidostrobophyllum ⅗×
Annularia ⅗×
Sphenophyllum 1×
Lycopodites ⅗×
Cordaites ⅗×
Asterophyllites ⅓×
Bureau County
Knox County
Pecopteris 1×
Ptychocarpus ⅗×
Pecopteris ⅗×
Mariopteris ⅗×
Asterotheca ⅗×
Spiropteris ⅗×
Pecopteris ⅗×
In the same general area but about three miles due south of 25
Alexis, fossil plants also may be found in the clay pits of the
Hydraulic-Press Brick Company and the Northwestern Clay
Manufacturing Company.
Fulton County
Fern and cordaitean leaves have been collected along Mill Creek
about a mile northeast of Pleasantview where the fossils occur in the
shale overlying the Babylon Coal. In the same general area,
impressions and casts of Stigmaria, Lepidodendron, and Cordaites
have been found in the Babylon Sandstone.
Three miles north of Pleasantview, a quarter of a mile northwest of
Union School, several species of leaves have been collected from the
Browning Sandstone where it is exposed in a roadcut.
Numerous fern impressions also are found in shale beds above the
Herrin (No. 6) Coal along the Middle Branch of Copperas Creek, six
miles west of Glasford. Other specimens may be found in these beds
elsewhere in the area.
McDonough County
In some of the small underground mines near Colchester, the shale
overlying the Colchester (No. 2) Coal contains ironstone concretions
similar to those from northeastern Illinois. More than 50 species of
plant fossils have been reported, but they were collected many years
ago from spoil heaps at the mines. Beds of the same age crop out
widely in other localities in western Illinois and may contain plant
fossils.
26
Plate 4
Sphenopteris ⅗×
Linopteris ⅗×
Neuropteris ½×
Odontopteris ⅗×
Cycopteris ½×
Alethopteris ⅗×
Alethopteris ⅗×
Odontopteris ½×
Alethopteris ⅗×
27
Vermilion County