Course Overview
This course provides a comprehensive overview of Design Patterns in C# and .NET from a practical perspective. This course in particular covers patterns with the use of:
Course Overview
This course provides a comprehensive overview of Design Patterns in C# and .NET from a practical perspective. This course in particular covers patterns with the use of:
This course provides an overview of all the Gang of Four (GoF) design patterns as outlined in their seminal book, together with modern-day variations, adjustments, discussions of intrinsic use of patterns in the language.
What are Design Patterns?
Design Patterns are reusable solutions to common programming problems. They were popularized with the 1994 book Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, John Vlissides, Ralph Johnson and Richard Helm (who are commonly known as a Gang of Four, hence the GoF acronym).
The original book was written using C++ and Smalltalk as examples, but since then, design patterns have been adapted to every programming language imaginable: C#, Java, PHP and even programming languages that aren't strictly object-oriented, such as JavaScript.
The appeal of design patterns is immortal: we see them in libraries, some of them are intrinsic in programming languages, and you probably use them on a daily basis even if you don't realize they are there.
What Patterns Does This Course Cover?
This course covers all the GoF design patterns. In fact, here's the full list of what is covered:
Who Is the Course For?
This course is for .NET/C# developers who want to see not just textbook examples of design patterns, but also the different variations and tricks that can be applied to implement design patterns in a modern way. For example, the introduction of the DLR allows us to use an ImpromptuObject, so that our DynamicObject exposes any interface we desire. This allows for dynamic programming, and many design patterns are presented in terms of their static and DLR-based variations.
Presentation Style
This course is presented as a (very large) series of live demonstrations being done in Microsoft Visual Studio. Most demos are single-file, so you can download the file attached to the lesson and run it in Visual Studio, Visual Studio Code, Rider or another IDE of your choice.
This course does not use UML class diagrams; all of demos are live coding. I use Visual Studio, various NuGet packages, R# unit test runner and even dotMemoryUnit.
A taste of things to come...
What are SOLID principles, where do they come from and why do we care?
A look at the Single Responsibility Principle, which states that a class should only have one reason to change. Also tied to the concept of Separation of Concerns which is basically stating the same thing.
A discussion of the Open-Closed Principle, which states that classes should be open for extension, but closed for modification. In other words, you should extend functionality using interfaces and inheritance rather than jumping back into already-written/tested code and adding to it or changing it.
The Liskov Substitution Principle states that subtypes should be substitutable for their base types.
The Interface Segregation Principle is simple: don't throw everything in the kitchen sink into an interface because then all its users will have to implement things they do not need. Instead, split the interface into several smaller ones.
Not to be confused with dependency injection, dependency inversion specifies that high-level modules should not depend on low-level ones; both should depend on abstractions. Confusing, huh?
A summary of the things we've learned in this section of the course.
A brief note about the three categories of design patterns: creational, structural and behavioral.
A discussion of the Builder design pattern and what it's used for.
A look at why you'd want to have a builder in the first place.
We implement a simple builder for constructing trees of HTML elements.
We make the builder fluent by returning this from builder methods.
Inheriting fluent interfaces is not easy because this cannot be returned in a virtual way. But we can make it work using recursive generics.
How do you enforce that builder methods are called in a specific order?
We can extend a builder without breaking OCP using a functional approach and extension methods.
We look at a more complicated builder facade that exposes several sub-builders (builder facets) for building up parts of an object in a fluent manner.
A summary of the things we've learned about the Builder pattern.
A discussion of the general concept of factories and the two design patterns: Factory Method and Abstract Factory.
A scenario where having a factory interface actually makes sense.
Implementing a factory method is easy, and you can turn a constructor into a factory method using ReSharper.
We want to perform async initialization, but constructors cannot be marked async. Factories to the rescue!
When you want all the factory methods in a separate class.
An additional benefit of factories is they can keep weak references to objects they create or even, with one level of indirection, to replace those objects entirely.
An external factory needs the created object's constructor to be public. But what if you want it to be private? The solution is simple: stick a factory into the class whose instances it creates!
Sometimes, you want abstract factories with abstract objects; we support DIP but break OCP in the process.
Can we fix an OCP violation without introducing an IoC container? Seems we can.
A summary of the things we've learned in this module.
A discussion of the Prototype factory (not to be confused with a rather good game of the same name) and what it's used for.
The .net Framework comes with an ICloneable
interface but its use is not recommended. Why not?
Another suspect approach from the land of C++. While it avoids the confusion of ICloneable, it's not clear-cut either. Plus, we still have to do things recursively, which is tiring.
Let's be clear about what we're doing.
How would you succinctly implement inheritance when deep copying is concerned?
How to make a copy of the entire object graph without writing any copy-specific code? Easy, just serialize and deserialize!
A summary of all the things we've learned about the prototype pattern.
Ahh, the much maligned Singleton? Is it really that evil? Let's find out...
Avoiding all the philosophical nonsense surrounding double-checked locking (it’s not thread-safe) and implementations involving inner static classes (with an empty static
constructor to avoid beforefieldinit
), we simply look at a safe .net 4-like way of making a lazy, thread-safe singleton.
The singleton works fine, so what's the problem? Turns out, hard reference to a type means we cannot fake it in our tests. Oops!
The only socially acceptable way of using a singleton is with a DI framework.
Typically, marking a component as a singleton is trivial. Check out my Dependency
Injection course! (link below)
A variation on a Singleton pattern, the Monostate lets the client instantiate as many copies of the singleton class as they want, but all those copies refer to the same static
data. Is this a good idea? Let’s find out!
An alternative that completely skirts the issue of thread safety.
A very common and simple design pattern.
A summary of all that we've learned about the Singleton. As you can see, it's not really that evil provided it can be substituted by a different type (polymorphism). Also, lifetime management is best left to a specialized system (i.e. a DI container).
A look at the Adapter design pattern.
We are going to build a simple adapter for the rending of vector data where only a raster renderer is available.
An adapter can generate a large number of temporary objects. Caching helps us avoid doing extra work more than once.
Unlike C++, C# does not allow us to use literal values (numbers, strings) as generic arguments. Generic Value Adapter is a pattern that helps us deal with this. This lecture also uses the Factory Method design pattern and recursive generics.
Let's take a look at how Autofac supports the creation of adapters. For more info on Dependency Injection, see my Autofac course!
A summary of all the things we've learned about the Adapter pattern.
A discussion of the Bridge pattern and what it's used for.
A simple illustration of how to build a bridge.
A summary of all the important things we've learned in this section of the course.
A discussion of what the Composite pattern is for and how it's used.
Let's implement the Composite pattern by considering individual geometric shapes as well as grouping of shapes.
Let's apply the Composite pattern to the implementation of simple neural networks (individual neurons and layers of neurons).
A look back at our OCP demo, where we use the Composite pattern to introduce a base class useful for making combinators.
A summary of all the things we've learned about the Composite design pattern.
A look at the Decorator design pattern.
StringBuilder is unfortunately sealed. Let's see how we can have its de facto inheritor as a Decorator.
Here we build a pattern which is both a decorator (over a StringBuilder) and an adapter (adapting string's operator support).
When you implement pseudo-multiple inheritance using interfaces, you quite often end up implementing the Decorator pattern.
C#8 introduces default interface members. Does this change the MI situation? Not really.
A look at how to make decorators-of-decorators.
How can you handle a decorator being applied more than once?
Can decorators be composed as nested generic type arguments? They can, but things aren't as rosy in .NET as they are in C++.
Let's take a look at how Autofac supports decorators. For more info on Dependency Injection, see my Autofac course!
A summary of all the things we've learned about the Decorator design pattern.
A look at the Facade design pattern. Also an explanation of that weird letter C.
Instead of building a clinical example, let's take a look at a real-life Facade!
A summary of the things we've learned about the Facade design pattern.
A discussion of the Flyweight design pattern and what it's used for.
So what if .NET does string interning? We can still find a scenario where string space optimization is possible.
Text formatting is a classic example of a Flyweight. Instead of keeping formatting flags for every single character in a line of text, let's implement ranges!
A summary of all the things we've learned about the Flyweight design pattern.
A look at the Proxy design pattern.
Let's implement a proxy which adds access control to the object.
One very common scenario is where developers replace ordinary properties with Property<T>-typed objects. Let's build out own implementation and discuss what it's for.
A proxy for a single value? Why would you need this?
OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.
Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.
Find this site helpful? Tell a friend about us.
We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.
Your purchases help us maintain our catalog and keep our servers humming without ads.
Thank you for supporting OpenCourser.