Finally, a book on software design patterns that won't
|

Real World Implementation of C# Design Patterns

Real World Implementation of C# Design Patterns

Overcome daily programming challenges using elements of reusable object-oriented software

by Bruce M. Van Horn II

with illustrations by the author

Forward by Van Symons,
  CTO Visual Storage Intelligence

Fork the code on GitHub
Phoebe, one of the characters in the book, standing in her lab coat saying AHA!
Phoebe, one of the characters in the book, standing in her lab coat saying AHA!

An AHA! moment with software design patterns


For 30 years now I have derived a great deal of satisfaction teaching new and new-ish software developers our trade. There's just something about seeing the AHA! moment when suddenly a tough or opaque concept clicks in my colleague's mind. The world for them has opened to new possibilities, even if they are small. When Packt Publishing approached me about a book on design patterns, I jumped at the chance.

Software development as a profession has been around for over 50 years now. As an industry, we’re pretty good at teaching new languages, and basic theory like algorithms, data structures, Big O notation and the like. But there is a difference between software development and software engineering. Engineers need to be able to think about more than just syntax and clever uses of linked lists. They also need to learn programming paradigms and how to use software design patterns as both a communications tool and as an advantage when designing well written, easily maintainable code.

Developers working with C# will be able to put their knowledge to work with this practical guide to design patterns. The book provides a hands-on approach to implementation and associated methodologies that will have you up-and-running, and productive in no time.

Complete with step-by-step explanations of essential concepts, practical examples, and self-assessment questions, you will begin by exploring what patterns are. Throughout the book, you'll learn how to leverage them in your daily work, and even how to recognize and correct for anti-patterns that invariably worm their way into your projects.

You’ll learn to recognize solutions to common problems in software design with C# that come up time and again regardless of where you work or what industry you serve. Rather than an academic treatment that you’ll find in “classic” books on this topic, we’ll be exploring real-world applications of design patterns that you can use in your everyday work.

By the end of this book, you’ll be able to recognize situations where you are tempted to reinvent the wheel, and quickly avoid the time and cost associated with solving problems that are common, well understood, with battle tested, and proven design patterns.

You'll wield patterns with confidence, and you do it with CLASS! And sometimes interfaces.

This is a picture of an old fuddy-duddy CS prof standing with an ancient computer.  If you are a CS prof don't be a fuddy-duddy, and don't use the classic 1994 book as your textbook.  Try this one instead!  Your students will appreciate it.
This is a picture of an old fuddy-duddy CS prof standing with an ancient computer.  If you are a CS prof don't be a fuddy-duddy, and don't use the classic 1994 book as your textbook.  Try this one instead!  Your students will appreciate it.

Why should you use this book to learn or teach software design patterns in C#?

Most books on patterns have one or more problems:

  1. They are written by university professors for other university professors. These people have never had to ship real software on a deadline. Learning from them is like learning to play a musical instrument from someone who has only ever listened to the radio. This book is written in plain English by a real (but not perfect) software developer with over 25 years of experience shipping very large software projects.
  2. Writing a book on patterns is hard! Many authors play it safe and make every example super-vague, or overly contrived. Class A inherits from Class B which depends on Class C. Unless you're making a weird alphabet app, that kind of treatment isn't going to be very useful. The examples in this book are relatable. If you've ever ridden a bicycle before, you'll understand this book.
  3. The implementation language is old, irrelevant, or pseudocode. You need to learn patterns in a language you can actually use. C# is a popular choice. The book includes an extensive Appendix to help new developers orient to the content in the book.
  4. They aren't written with real world business problems in mind. The seminal work in software design patterns includes examples on UI design in MOTIF, and algorithms for managing lines on printed circuit boards. You need a book (like this one) that focuses on the kinds of business scenarios you're actually going to see in your work.
  5. They aren't written from a real-world perspective. Does your code break? Do you sometimes run with an idea only to find it doesn't work out? Have you ever noticed that in most books, that never happens? In this book, the characters make mistakes, then learn how to solve them.
  6. They go for quantity over quality. Trying to cover every software pattern ever conceived is a noble endeavor, but it's like trying to document every feature in Excel. Did you know Excel can sum a column of numbers? Of course! Did you know there is an Excel function to convert your numbers into Roman numerals? Some of the patterns outlined in longer books are about as useful. You need a book that focuses on the patterns you'll really use. That's what this book does!

Learn to defeat antipatterns and dark forces that plague 99% our projects!

(Most software developers don't do this!)


This book focuses entirely on reality, warts and all. It presents the story of two sisters, Kitty and Phoebe, who create a tech startup aimed at building the finest bicycles the world has ever seen. The girls are not professional software developers, so they have to learn as they go.  They have business objectives and very real competitive pressures just like you do.  Left to their own devices, their code will turn into a big ball of mud on a plate of spaghetti.  

We start off with some anti-patterns: those practices everybody does because either they're lazy, or they cut corners because they're under extreme pressure to ship before the code is ready.  These conditions can be corrected with regular refactoring and code reviews, but they usually aren't.  The anti-patterns we cover include:

A Big Ball of Mud

icon for a big ball of mud showing a hand filled with mud and goop

All code starts off with the best feasible design. Without patterns, devolution is inevitable.

Code Pasta

An icon showing a plate of spaghetti.

These code pasta dishes that will always lead to indigestion.

The Golden Hammer

An icon showing a dwarven forged war hammer.

When someone hands you a new hammer, everything looks like a nail!

The evil pointy haired boss who makes you ship your software without testing it first.

Prepare yourself to transform the way you write code.

Good code never happens by accident. Learning patterns without learning the best design practices will not yield good results.  After we cover antipatterns, we talk about SOLID.  SOLID represents a set of design techniques that help you create flexible object class structures. 

S

Single Responsibility Principle

O

Open-Closed
Principle

L

Liskov
Substitution

I

Interface Segregation Principle

D

Dependency Inversion Principle

Not sure what these are? Read Chapter 2! Each principle is explained in detail. Examples are included that show you how to break the rules, and later, how to use them. After that, SOLID is used to make every example including a full project in Chapter 7.

Meet the heroes!

The following story is fictitious.

Any resemblance to actual persons, alive or dead, is a mere coincidence (and a real shame!).

Kitty and Phoebe with a bicycle prorotype and two robots

Meet Kitty and Phoebe – Texas-born sisters who share a love for riding bicycles. Phoebe is studying engineering at Southern Methodist University, a private university in the sisters’ hometown of Dallas, Texas. Kitty is studying industrial design at Sul Ross University in the town of Alpine located in west Texas. Purely through kismet, they both won a summer internship at MegaBikeCorp, a large multi-national bicycle manufacturing company.

After three months of soul crushging big-company bureaucracy, the girls decided they could do better. Kitty designs a new line of incredibly innovative bicycles. Her sister has the idea to create an automated robotic assembly line. The sisters find two abandoned warehouses: one in Dallas, the other in the city of Alpine. The two set up shop with the help of their father's credit card. (Don't worry, they pay him back!)

Throughout the story, the sisters are working on building the software systems that will control their robotic manufacturing facilities. You'll be working alongside them, learning as they do, and occasionally making mistakes, and then correcting them.

This is the most fun you'll have reading a technical book.

Kitty and Phoebe with a bicycle prorotype and two robots

Master the most important patterns you'll use every day.

Chapter 3 through 6 describe what I believe are the most useful patterns for every day, real-world projects in C#.

The Simple Factory

Simple Factory

Not really a pattern, but many developers think it is.

The Factory Method

The Factory Method Pattern

This is the factory pattern most widely used by developers.

The Abstract Factory

Abstract Factory Pattern

Don't confuse this with Neo-Classical expressionist factories!

The Builder

Builder Pattern

Rome could have been built in a day with this pattern!

The Object Pool

Object Pool Pattern

When objects are costly to instantiate, put them in a pool!

The Singleton

The Singleton Pattern

There can be only one. But one is all you need.

The Decorator

Decorator pattern

It's like behavioral bling for closed classes.

The Façade

the Facade Pattern

Code castle on the outside, code trailer park on the inside.

The Composite

Composite Pattern

Use recursion on your object graphs by using recursion.

The Bridge

The bridge pattern

Manage complexity between two related object graphs.

The Command

Command Pattern

Package and send your commands like a boss.

The Observer

Observer Pattern

See object. See object run. Now do something.

The Iterator

Iterator Pattern

It never eats. It never sleeps. It just keeps going.

The Strategy

Strategy Pattern

Action without strategy is a nightmare.

Chapters 7 & 8: PLOT TWIST!

The plot of our story takes a dire turn and the heroic sisters must find a way to adapt so they can save their family and their business.


In the real world, your business needs to be able to pivot on a dime to capture new markets and new opportunities. If your software architecture isn't resilient and flexible, you could miss out. The only way to have a flexible architecture is to follow SOLID principles and utilize patterns. Stretched thin by their current business committements, the girls have to recruit some help from a skilled developer most companies probably wouldn't hire. Will this unlikely hero save they day using the patterns learned in the earlier chapters?

That's all we're going to say! No spoilers! I'm not even going to tell you what that red thing is. It looks like a tank, but it isn't. That's all you're getting! Now go buy the book!

What the heck is that thing?  Read Chapter 7 to find out.