Design Patterns Csharp Dotnet

Tags:

Discover the modern implementation of design patterns with C# and .NET

Last updated 2022-01-10 | 4.5

- Recognize and apply design patterns
- Refactor existing designs to use design patterns
- Reason about applicability and usability of design patterns

What you'll learn

Recognize and apply design patterns
Refactor existing designs to use design patterns
Reason about applicability and usability of design patterns

* Requirements

* Good understanding of C#
* Familiarity with latest C# features
* Good understanding of object-oriented design principles

Description

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:

  • The latest versions of C# and the .NET framework
  • Use of modern programming approaches: dependency injection, reactive programming and more
  • Use of modern developer tools such as ReSharper
  • Discussions of pattern variations and alternative approaches

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:

  • SOLID Design Principles: Single Responsibility Principle, Open-Closed Principle, Liskov Substitution Principle, Interface Segregation Principle and Dependency Inversion Principle
  • Creational Design Patterns: Builder, Factories (Factory Method and Abstract Factory), Prototype and Singleton
  • Structrural Design Patterns: Adapter, Bridge, Composite, Decorator, Façade, Flyweight and Proxy
  • Behavioral Design Patterns: Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Null Object, Observer, State, Strategy, Template Method and Visitor

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.


Who this course is for:

  • Beginner and experienced developers
  • Anyone interested in design patterns

Course content

28 sections • 195 lectures

Introduction Preview 07:12

A taste of things to come...

Overview Preview 01:16

What are SOLID principles, where do they come from and why do we care?

Single Responsibility Principle Preview 07:29

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.

Open-Closed Principle Preview 17:24

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.

Liskov Substitution Principle Preview 06:37

The Liskov Substitution Principle states that subtypes should be substitutable for their base types.

Interface Segregation Principle Preview 06:33

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.

Dependency Inversion Principle Preview 11:11

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?

Summary Preview 05:25

A summary of the things we've learned in this section of the course.

Gamma Categorization Preview 03:36

A brief note about the three categories of design patterns: creational, structural and behavioral.

Overview Preview 01:43

A discussion of the Builder design pattern and what it's used for.

Life Without Builder Preview 03:34

A look at why you'd want to have a builder in the first place.

Builder Preview 09:06

We implement a simple builder for constructing trees of HTML elements.

Fluent Builder Preview 01:16

We make the builder fluent by returning this from builder methods.

Fluent Builder Inheritance with Recursive Generics Preview 12:37

Inheriting fluent interfaces is not easy because this cannot be returned in a virtual way. But we can make it work using recursive generics.

Stepwise Builder Preview 09:00

How do you enforce that builder methods are called in a specific order?

Functional Builder Preview 10:19

We can extend a builder without breaking OCP using a functional approach and extension methods.

Faceted Builder Preview 11:11

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.

Builder Coding Exercise

Summary Preview 00:57

A summary of the things we've learned about the Builder pattern.

Overview Preview 02:22

A discussion of the general concept of factories and the two design patterns: Factory Method and Abstract Factory.

Point Example Preview 04:38

A scenario where having a factory interface actually makes sense.

Factory Method Preview 05:03

Implementing a factory method is easy, and you can turn a constructor into a factory method using ReSharper.

Asynchronous Factory Method Preview 05:01

We want to perform async initialization, but constructors cannot be marked async. Factories to the rescue!

Factory Preview 02:59

When you want all the factory methods in a separate class.

Object Tracking and Bulk Replacement Preview 12:01

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.

Inner Factory Preview 05:41

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!

Abstract Factory Preview 11:21

Sometimes, you want abstract factories with abstract objects; we support DIP but break OCP in the process.

Abstract Factory and OCP Preview 09:52

Can we fix an OCP violation without introducing an IoC container? Seems we can.

Factory Coding Exercise

Summary Preview 01:05

A summary of the things we've learned in this module.

Overview Preview 01:59

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.

ICloneable is Bad Preview 07:37

The .net Framework comes with an ICloneable interface but its use is not recommended. Why not?

Copy Constructors Preview 03:55

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.

Explicit Deep Copy Interface Preview 02:34

Let's be clear about what we're doing.

Prototype Inheritance Preview 20:24

How would you succinctly implement inheritance when deep copying is concerned?

Copy Through Serialization Preview 09:05

How to make a copy of the entire object graph without writing any copy-specific code? Easy, just serialize and deserialize!

Prototype Coding Exercise

Summary Preview 01:08

A summary of all the things we've learned about the prototype pattern.

Overview Preview 02:43

Ahh, the much maligned Singleton? Is it really that evil? Let's find out...

Singleton Implementation Preview 08:40

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.

Testability Issues Preview 07:22

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!

Singleton in Dependency Injection Preview 08:58

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)

Monostate Preview 03:46

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!

Per-Thread Singleton Preview 04:25

An alternative that completely skirts the issue of thread safety.

Ambient Context Preview 12:26

A very common and simple design pattern.

Singleton Coding Exercise

Summary Preview 02:15

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).

Overview Preview 02:36

A look at the Adapter design pattern.

Vector/Raster Demo Preview 08:35

We are going to build a simple adapter for the rending of vector data where only a raster renderer is available. 

Adapter Caching Preview 06:04

An adapter can generate a large number of temporary objects. Caching helps us avoid doing extra work more than once.

Generic Value Adapter Preview 25:18

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.

Adapter in Dependency Injection Preview 09:07

Let's take a look at how Autofac supports the creation of adapters. For more info on Dependency Injection, see my Autofac course!

Adapter Coding Exercise

Summary Preview 01:10

A summary of all the things we've learned about the Adapter pattern.

Overview Preview 02:50

A discussion of the Bridge pattern and what it's used for.

Bridge Preview 09:49

A simple illustration of how to build a bridge.

Bridge Coding Exercise

Summary Preview 01:33

A summary of all the important things we've learned in this section of the course.

Overview Preview 01:53

A discussion of what the Composite pattern is for and how it's used.

Geometric Shapes Preview 07:33

Let's implement the Composite pattern by considering individual geometric shapes as well as grouping of shapes.

Neural Networks Preview 08:00

Let's apply the Composite pattern to the implementation of simple neural networks (individual neurons and layers of neurons).

Composite Specification Preview 05:58

A look back at our OCP demo, where we use the Composite pattern to introduce a base class useful for making combinators.

Composite Coding Exercise

Summary Preview 01:11

A summary of all the things we've learned about the Composite design pattern.

Overview Preview 02:34

A look at the Decorator design pattern.

Custom String Builder Preview 06:19

StringBuilder is unfortunately sealed. Let's see how we can have its de facto inheritor as a Decorator.

Adapter-Decorator Preview 06:33

Here we build a pattern which is both a decorator (over a StringBuilder) and an adapter (adapting string's operator support).

Multiple Inheritance with Interfaces Preview 08:40

When you implement pseudo-multiple inheritance using interfaces, you quite often end up implementing the Decorator pattern.

Multiple Inheritance with Default Interface Members Preview 07:44

C#8 introduces default interface members. Does this change the MI situation? Not really.

Dynamic Decorator Composition Preview 07:39

A look at how to make decorators-of-decorators.

Detecting Decorator Cycles Preview 22:02

How can you handle a decorator being applied more than once?

Static Decorator Composition Preview 09:30

Can decorators be composed as nested generic type arguments? They can, but things aren't as rosy in .NET as they are in C++.

Decorator in Dependency Injection Preview 06:10

Let's take a look at how Autofac supports decorators. For more info on Dependency Injection, see my Autofac course!

Decorator Coding Exercise

Summary Preview 02:02

A summary of all the things we've learned about the Decorator design pattern.

Overview Preview 03:08

A look at the Facade design pattern. Also an explanation of that weird letter C.

Façade Preview 07:59

Instead of building a clinical example, let's take a look at a real-life Facade!

Facade Coding Exercise

Summary Preview 01:25

A summary of the things we've learned about the Facade design pattern.

Overview Preview 05:24

A discussion of the Flyweight design pattern and what it's used for.

Repeating User Names Preview 12:29

So what if .NET does string interning? We can still find a scenario where string space optimization is possible.

Text Formatting Preview 08:53

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!

Flyweight Coding Exercise

Summary Preview 00:58

A summary of all the things we've learned about the Flyweight design pattern.

Overview Preview 03:12

A look at the Proxy design pattern.

Protection Proxy Preview 03:11

Let's implement a proxy which adds access control to the object.

Property Proxy Preview 09:24

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.

Value Proxy Preview 12:04

A proxy for a single value? Why would you need this?

Composite Proxy: SoA/AoS Preview 11:30

A mixture of the Composite and Proxy design patterns used to solve the 'array of structures' problem.

Composite Proxy with Array-Backed Properties Preview 06:42

We build a boolean composite property proxy and then refactor the code to use array-backed properties.

Dynamic Proxy for Logging Preview 11:50

A dynamic proxy is created at runtime, and saves us from having to replicate every single interface member individually. Let's implement such a proxy for logging.

Proxy vs. Decorator Preview 01:27

Almost like "Alien vs. Predator" — a comparison of the Proxy and Decorator design patterns.

ViewModel Preview 08:42

A proxy/decorator approach to helping UI bind to data while keeping notifications separate to comply with SRP. Part of the MVVM paradigm.

Proxy Coding Exercise

Bit Fragging Preview 25:04

A fun proxy/adapter implementation that shows up in many scenarios. In this case, we iterate all the combinations of several operators using a simple ++ increment.

Summary Preview 00:55

A summary of the things we've learned about the Proxy design pattern.

Overview Preview 03:33

A look at the Chain of Responsibility design pattern and discussion of what it's used for.

Command Query Separation Preview 01:28

Brief discussion of the concept of Command Query Separation (CQS).

Method Chain Preview 12:15

Chain of Responsibility implemented by chaining method calls together.

Broker Chain Preview 13:50

A more sophisticated approach to implementing the chain of responsibility.

Chain of Responsibility Coding Exercise

Summary Preview 01:18

A summary of the things we've learned about the Chain of Responsibility design pattern.

Overview Preview 03:06

A look at the Command design pattern.

Command Preview 07:47

Let's implement the Command design pattern in a simple scenario.

Undo Operations Preview 06:05

A simple demonstration of how to implement Undo functionality while using Command.

Composite Command Preview 12:11

Composite commands (a.k.a. macros) are very common.

Command Coding Exercise

Summary Preview 01:09

A summary of all the things we've learned about the Command design pattern.

Overview Preview 04:00

An overview of the Interpreter design pattern, which actually brings with it a whole field of Computer Science typically called Compiler Theory.

Handmade Interpreter: Lexing Preview 07:52

Lexing is the process of splitting textual input into lexical tokens.

Handmade Interpreter: Parsing Preview 12:05

Parsing is the process of converting a series of tokens into an Abstract Syntax Tree (AST).

ANTLR Preview 02:44

Parsers are typically made with specialized parser frameworks and ANTLR is one such example.

Interpreter Coding Exercise

Summary Preview 01:03

A summary of the things we've learned about the Interpreter design pattern.

Overview Preview 01:42

A look at the Iterator design pattern.

Iterator Object Preview 11:49

A look at how to build a handmade iterator. This is the 'C++ Way', so isn't really recommended in C#.

Iterator Method Preview 06:57

A much more natural way of providing iteration functionality.

Iterators and Duck Typing Preview 04:06

Can you iterate an object that doesn’t even implement IEnumerable<T>? You sure can.
foreach works on duck typing, i.e. finding elements by name and type. If they’re all there, it works. Let’s reuse our old iterator, then!

Array-Backed Properties Preview 05:51

What happens if you want to iterate the value of every single property (or a specific range of properties) in your application? Well, this is rather easy: just make a single array-typed backing field for every single property!

Iterator Coding Exercise

Summary Preview 01:28

A summary of all the things we've learned about iterators in .NET.

Overview Preview 01:13

A look at the Mediator design pattern.

Chat Room Preview 10:42

A classic example of the Mediator pattern is a chat room. Well, let's build one!

Event Broker Preview 15:30

A more advanced Mediator scenario, making use of Reactive Extensions and Dependency Injection (Autofac). Check out the Reactive Extensions course (link below).

Introduction to MediatR Preview 13:20

A look at Jimmy Bogard's shrink-wrapped Mediator package.

Mediator Coding Exercise

Summary Preview 01:16

A summary of the things we've learned about the Mediator design pattern.

Overview Preview 01:41

A look at the Memento design pattern.

Memento Preview 06:04

Let's turn back to our classic BankAccount example and implement the Memento pattern.

Undo and Redo Preview 07:15

If we keep every single change as a Memento stored internally, we can easily implement Undo/Redo operations.

Memento for Interop Preview 06:38

One less intuitive reason to use Memento is in interop, for example when you are using C++ code from C#. Why? Because only simple data (scalars and arrays) can go through P/Invoke. Classes, unfortunately, cannot.

Memento Coding Exercise

Summary Preview 01:17

A summary of the things we've learned about the Memento design pattern.

Overview Preview 01:57

A discussion on what the Null Object is useful for. Note this is not a GoF pattern.

Null Object Preview 08:53

A demonstration of a very simple implementation of a Null Object.

Null Object Singleton Preview 05:08

How can we completely encapsulate a singleton Null Object so that the client never gets to see it?

Dynamic Null Object Preview 06:11

If the interface is too complicated, building a Null Object on it is a bit tedious. So instead, what you can do is generate a DLR-based Null Object that conforms to a specific interface. Warning: use of dynamic programming implies a big performance hit on method calls. Probably not the best approach for production code, but adequate for testing.

Null Object Coding Exercise

Summary Preview 00:41

A summary of all the things we've learned about the Null Object design pattern.

Overview Preview 02:10

A look at the Observer design pattern.

Observer via the 'event' Keyword Preview 07:09

The Observer pattern is baked into C# with the event keyword.

Weak Event Pattern Preview 08:25

If an object subscribes to an event on an object that lives longer, the object may end up staying alive for a lot more than necessary, even after there are no references to it. This can cause a de facto memory leak. We look at how this problem can be solved.

Observer via Special Interfaces Preview 18:28

What if our subscriptions were separate, disposable objects? Oh wait, that's exactly what Reactive Extensions try to do.

Observable Collections Preview 09:45

A look at how the Observer design pattern is implemented for collections and sequences. More info about observable collections in my Reactive Extensions course!

Bidirectional Observer Preview 14:48

One-way binding is easy, but what if we want two members of two different classes to bind their values together?

Property Dependencies Preview 13:30

It's easy to mutually bind two properties if they have setters. How about a more complicated scenario — a property that affects one or more other (possibly read-only) properties?

Declarative Event Subscriptions with Interfaces Preview 27:08

Using an IoC container, a declarative approach to event subscriptions is also feasible.

Observer Coding Exercise

Summary Preview 00:56

A summary of all the things we've learned about the Observer design pattern.

Overview Preview 03:08

A look at the State design pattern.

Classic Implementation Preview 12:32

The classic implementation of state machine. Bulky, unreadable and generally not recommended.

Handmade State Machine Preview 06:44

Let's hand-roll a simple finite state machine.

Switch-Based State Machine Preview 06:38

You can implement an entire state machine as a single switch statement.

Switch Expressions Preview 08:49

C#8 switch expressions allow for very informal definition of simple state machines.

State Machine with Stateless Preview 05:37

A look at how to make state machines with the excellent Stateless library.

State Coding Exercise

Summary Preview 01:00

A summary of the things we've learned about the State design pattern.

Overview Preview 01:50

A look at the strategy design pattern.

Dynamic Strategy Preview 08:25

An implementation of dynamic strategy pattern which lets us change strategies at runtime.

Static Strategy Preview 04:07

A static implementation of the Strategy pattern forces us to choose the strategy to use at compile time.

Equality and Comparison Strategies Preview 07:41

The .NET BCL uses the Strategy pattern for custom equality and comparison operations.

Strategy Coding Exercise

Summary Preview 00:26

A summary of the things we've learned about the Strategy design pattern.

Overview Preview 01:29

A look at the Template Method design pattern.

Template Method Preview 07:22

Let's write a template method for a typical board game.

Functional Template Method Preview 03:38

You can construct a template method without the use of classes and inheritance by instead using functions passed as parameters to the template method.

Template Method Coding Exercise

Summary Preview 00:45

A summary of the things we've learned about the Template Method design pattern.

Overview Preview 04:46

A look at the Visitor design pattern.

Intrusive Visitor Preview 04:41

Let's break OCP to support expression printing.

Reflective Visitor Preview 09:10

Another approach to printing expressions, this time by using reflection and checking against types. Not very efficient!

Classic Visitor (Double Dispatch) Preview 10:21

Finally, we implement the classic Visitor pattern using double dispatch.

Reductions and Transforms Preview 14:24

Why don't Accept()/Visit() methods return any values? Turns out there's a way to leverage return value functionality instead of relying on side effects.

Dynamic Visitor via the DLR Preview 07:11

The DLR allows us to choose an overload to call based on argument type, providing the argument is actually dynamic. This means we take a massive performance hit but can do without altering the hierarchy of types.

Acyclic Visitor Preview 12:01

An alternative to the GoF Visitor implementation, courtesy of Robert C Martin (a.k.a. Uncle Bob).

Visitor Coding Exercise

Summary Preview 01:09

A summary of the things we've learned about the Visitor design pattern.

Creational Paterns Summary Preview 04:41

A summary of all the Creational patterns that we've met in this course: Builder, Factory (Factory Method, Abstract Factory), Singleton and Prototype.

Structural Patterns Summary Preview 06:13

A summary of all the Structural design patterns we've met in this course: Adapter, Bridge, Composite, Decorator, Facade, Flyweight and Proxy.

Behavioral Patterns Summary Preview 08:48

A summary of all the Behavioral design patterns we've met in this course: Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Null Object, Observer, State, Strategy, Template Method and Visitor.

End of Course Preview 00:54

A few last words before we end the course... I'm so sad it's over. Ah well, there's always cake.

An ASCII C# String Preview 13:58

A more real-life example of an ASCII-specialized .NET string.

Continuation Passing Style Preview 11:06

CPS is a pattern of algorithmic decomposition. Typically associated with JavaScript, it can also be used in C# development. Let's take a look!

Local Inversion of Control Preview 17:42

Did you think that Inversion of Control is only relevant to Dependency Injection? You are wrong! Here is an example of local inversion of
control — nothing to do with DI, but also a lot of fun!

DI Container and Event Broker Integration Preview 10:11

We looked at both a ‘plain’ event broker as well as one based on Reactive Extensions. Time for the heavy artillery! We are now going to implement a declarative event broker (where event subscriptions are done using attributes) using the Unity DI framework.

Beyond the Elvis Operator Preview 14:46

C# 6 introduced the ?. (Elvis) operator for chaining checks against null. But what if we also want other types of checks (e.g., if) involved in those chains? Let's see how we can implement this with a bit of functional programming.

CQRS and Event Sourcing Preview 26:42

An introductory look at CQRS and Event Sourcing.

Overview Preview 05:10

An overview of the design patterns we're going to implement in a functional way.

Builder Preview 04:57

Let's use F# list support to build HTML in a more natural way.

Decorator Preview 04:39

Function decorators done in a functional way.

Factory Preview 08:15

F# lets us create class-less implementations of interfaces in place. Let's see how to use this to make factories.

Interpreter Preview 09:58

A real-life example of parsing data into F# discriminated unions. Warning: this demo is rather complicated.

Strategy Preview 05:38

The F# way of the Strategy pattern is to use higher-order functions.

Template Method Preview 10:06

The F# way for the Template Method is to also use higher-order functions.

Summary Preview 02:55

A summary of the things we've learned about pattern implementations in F#.