Table Of Content1ST EDITION
Real-World Implementation of
C R
# e
C# Design Patt erns a
D
l
-
e W
s
i o
g
r
n
l
d
As a software developer, you need to learn new languages and simultaneously get familiarized with the P
a I
programming paradigms and methods of leveraging patt erns, as both a communications tool and an m
tt
advantage when designing well-writt en, easy-to-maintain code. Design patt erns, being a collection of best p
e
practices, provide the necessary wisdom to help you overcome common sets of challenges in object-oriented l
r e
design and programming. n m 1ST EDITION
s
This practical guide to design patt erns helps C# developers put their programming knowledge to work. The e
book takes a hands-on approach to introducing patt erns and anti-patt erns, elaborating on 14 patt erns along n
t
with their real-world implementations. Throughout the book, you’ll understand the implementation of each a
patt ern, as well as fi nd out how to successfully implement those patt erns in C# code within the context of a t
i Real-World
real-world project. o
n
By the end of this design patt erns book, you’ll be able to recognize situations that tempt you to reinvent the
o
wheel, and quickly avoid the time and cost associated with solving common and well-understood problems f
with batt le-tested design patt erns. Implementation of
WHAT YOU WILL LEARN
B C# Design Patt erns
R
• Get to grips with patterns, and discover how to conceive and document them U
C
• Explore common patterns that may come up in your everyday work E
• Recognize common anti-patterns early in the process M
.
• Use creational patterns to create flexible and robust object structures
• Enhance class designs with structural patterns V Overcome daily programming challenges using
A
• Simplify object interaction and behavior with behavioral patterns N elements of reusable object-oriented software
H
O
R
N
I
BRUCE M. VAN HORN II
I
Foreword by Van Symons, CTO Visual Storage Intelligence
Re a l -Wo r l d I m p l e m e n t a t i o n o f
C # D e s i g n Pa t te r n s
Real-World Implementation of C#
Design Patterns
Overcome daily programming challenges using elements of
reusable object-oriented software
Bruce M. Van Horn II
BIRMINGHAM—MUMBAI
Real-World Implementation of C# Design Patterns
Copyright © 2022 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted
in any form or by any means, without the prior written permission of the publisher, except in the case
of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information
presented. However, the information contained in this book is sold without warranty, either express
or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable
for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and
products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot
guarantee the accuracy of this information.
Group Product Manager: Gebin George
Publishing Product Manager: Gebin George
Senior Editor: Kinnari Chohan
Technical Editor: Jubit Pincy
Copy Editor: Safis Editing
Project Coordinator: Manisha Singh
Proofreader: Safis Editing
Indexer: Subalakshmi Govindhan
Production Designer: Roshan Kawale
Marketing Coordinator: Sonakshi Bubbar
First published: September 2022
Production reference: 1270922
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-80324-273-6
www.packt.com
For my beautiful wife Karina, and my children Kitty and Phoebe, who helped
me truly understand Ruth 1:16. In memory and gratitude to my mother and
father, who paid for my first three computers, back when normal people didn’t
own them. In memory of Dr. Charles Gettys for motivating me by telling
me I would probably fail, and then teaching me everything he knew about
computers. For Van Symons, who saw past my wheelchair and gambled on me
when nobody else would, and who gave me a place and the opportunity to do
the best work of my career. For my students at Southern Methodist University
and Richland College, from whom I’ve learned as much as I’ve taught. For my
work family at Clear Technologies/Visual Storage Intelligence. In memory of
my student, Tom, who really could type ActionScript code with his feet. These
are the people who inspire me and for whom I am deeply grateful. Above all, I
am thankful to my Lord and Savior Jesus Christ who makes all things possible
(Philippians 4:13), along with His Father who invented all the electrons
and silicon, without which none of this computer stuff would exist
(Genesis 1:14–19).
Fo r e wo r d
I have known and worked with Bruce M. Van Horn II for the past 10 years. Bruce has been the lead
software developer for Visual Storage Intelligence, an analytical software technology for analyzing
infrastructure hardware and software to ensure the optimum use of on-premises and cloud resources
is maximized. Bruce is a leader in creating and leveraging innovative software technologies to meet
demanding customer needs in a fast-paced IT environment.
In this book, Bruce teaches you how to leverage software engineering techniques known as design
patterns to solve real-world problems in an efficient and effective manner. Using patterns will help you
solve problems effectively without reinventing the wheel. It will also keep your code healthy and stable.
Learning patterns are a must-have requirement in the career of any software engineer that we’ve hired
at our start-ups. There is no question as to whether you should learn patterns; the question is really,
where can you find a resource that teaches patterns in a way that is easy to understand and apply?
There are many books on patterns in the marketplace but most have a very academic focus. This book
is very different and somewhat unique. Instead of a dry treatment that reads like a doctoral thesis,
Bruce presents you with the story of two sisters who form a tech start-up. Problems are encountered,
requirements change, and at one point, so does the direction of the business. These are all normal
and frequent occurrences, and the software that runs the company must be able to adapt. Most books
cover development topics in a vacuum. All the designs are perfect, and the code always runs on the
first try. That isn’t real life.
The story in this book follows a more realistic arc. The characters get into jams and use patterns to get
out of them. Oftentimes, knowing when, where, and how to make use of these patterns can be critical
to business success, and that’s what you’re going to learn in this book.
Bruce is not only a brilliant software engineer but also one of the best technology educators I have
met in my 45-year career. He has the unique ability to not only understand the technology but also
understand both the best places to make use of it, and also the places where specific technologies are
not appropriate for the issues being analyzed.
Bruce shows you how to leverage the most important Gang of Four patterns in simple-to-understand
examples, as well as how to use the patterns to solve problems in your daily work. Reading this book
made software design patterns come to life and helped me understand how to leverage patterns in a
real-world business context.
Van Symons
CTO Visual Storage Intelligence
Co n t r i b u t o r s
About the author
Bruce M. Van Horn II is an architect and lead developer for Clear Technology’s Visual Storage Intelligence
SaaS product. He has over 30 years’ experience writing software and over 25 years’ experience teaching
software development at a university level. He currently teaches full stack software development at
Southern Methodist University’s code boot camp. He is a Certified ScrumMaster (CSM). Ten years
ago, he was diagnosed with dermatomyositis and was told he would never walk, speak, or eat nachos
again. Through faith and hard work, he beat the odds and today enjoys riding bicycles with his
daughters, Katherine (Kitty) and Phoebe. He eats nachos regularly with Karina, the love of his life.
About the reviewers
Alexej Sommer is a professional .NET developer with expertise in a variety of technologies, including
ASP.NET Core, WPF, UWP, Xamarin, and Azure.
He is also MCP/MCSD- and Azure-certified. He’s a winner of Microsoft’s Most Valuable Professional
2016–2019 in the Windows Development category. At present, he sometimes participates in conferences
as a speaker.
Sarita Nag was born in India and she earned her master's degree in computer science from KIIT
University, Bhubaneswar, Orissa.
Sarita began her career as Software Engineer from Thomson Reuters and since then, she worked on
many multinational companies and is currently working at FISERV.
She is an experienced software engineer with passion for developing innovative programs that expedite
the efficiency and effectiveness of organizational success. Well-versed in technology and writing code
to create systems that are reliable and user-friendly.
She has 10+ years of experience in various phases of Software Development Life cycle (SDLC) and
Agile methodologies such as Analysis, Design, Development, Testing, Deployment, and Maintenance in
various domains like Tax and accounting, Financial, Communication Media, and Customer relationship.
Table of Contents
Preface xv
Part 1: Introduction to Patterns (Pasta) and
Antipatterns (Antipasta)
1
There's a Big Ball of Mud on Your Plate of Spaghetti 3
Technical requirements 6 Complexity 12
No battle plan survives first Change 12
contact with the enemy 7 Scale 12
The Stovepipe system 7 The Golden Hammer 12
The Big Ball of Mud 9 A throwaway code example 15
Time 11 How can patterns help? 29
Cost 11 Summary 31
Experience 11 Questions 32
Skill 11
Further reading 32
Visibility 12
2
Prepping for Practical Real-World Applications of Patterns in C# 33
Becoming a prepper 33 Spaghetti code 35
Technical requirements 34 Lasagna code 36
Ravioli – the ultimate in pasta code 40
Spaghetti < lasagna < ravioli –
software evolution explained The foundational principle – writing
with pasta 35 clean code 41