4.41 sur 5
4.41

Design Patterns en C# et .NET

Découvrez la mise en œuvre moderne des modèles de conception avec C# et .NET
Instructeur :
Mahefa Abel
English [Auto] En savoir plus
Reconnaître et appliquer des modèles de conception
Refactoriser les conceptions existantes pour utiliser des modèles de conception
Raison de l'applicabilité et de la facilité d'utilisation des modèles de conception

Présentation du cours

Ce cours fournit un aperçu complet des Modèles de conception en C# et .NET de une perspective pratique. Ce cours couvre en particulier les modèles avec l’utilisation de :

  • Les dernières versions de C# et le framework .NET
  • Utilisation d’approches de programmation modernes : injection de dépendances, programmation réactive, etc.
  • Utilisation d’outils de développement modernes tels que ReSharper
  • Discussions sur le modèle variantes et approches alternatives

Ce cours donne un aperçu de toute la conception du Gang of Four (GoF) modèles tels que décrits dans leur livre séminal, ainsi que des variations modernes, des ajustements, des discussions sur l’utilisation intrinsèque des modèles dans la langue.

Quoi sont des Design Patterns ?

Les Design Patterns sont des solutions réutilisables aux problèmes de programmation courants. Ils ont été popularisés avec le livre de 1994  Design Patterns : Elements of Reusable Object-Oriented Software par Erich Gamma, John Vlissides, Ralph Johnson et Richard Helm (qui sont communément connus sous le nom de Gang of Four, d’où l’acronyme GoF).

Le livre original a été écrit en utilisant C++ et Smalltalk comme exemples, mais depuis lors, les modèles de conception ont été adaptés à tous les langages de programmation imaginables : C#, Java, PHP et même les langages de programmation qui ne sont pas strictement orientés objet, comme JavaScript.

L’attrait des modèles de conception est immortel : nous les voyons dans les bibliothèques , certains d’entre eux sont intrinsèques aux langages de programmation, et vous les utilisez probablement quotidiennement même si vous ne vous rendez pas compte qu’ils sont là.

Quels modèles ce cours couvre-t-il ?

Ce cours couvre tous les modèles de conception du GoF.                                                                                                                                                                                                                                          . En fait, voici la liste complète de ce qui est couvert :

  • Principes de conception SOLIDES : principe de responsabilité unique, ouverture -Principe fermé, principe de substitution de Liskov, principe de ségrégation d’interface et principe d’inversion de dépendance
  • Modèles de conception créatifs : constructeur, usines (méthode d’usine et usine abstraite), prototype et singleton</li >
  • Modèles de conception structurelle : adaptateur, pont, composite, décorateur, façade, poids mouche et proxy
  • Modèles de conception comportementale : chaîne de responsabilité, Commande, interprète, itérateur, médiateur, mémento, objet nul, observateur, état, stratégie, méthode de modèle et visiteur

Qui Le cours est-il pour ?

Ce cours est destiné aux développeurs .NET/C# qui veulent voir non seulement des exemples de manuels de modèles de conception, mais également les différentes variantes et astuces qui peuvent être appliquées pour mettre en œuvre les modèles de conception de manière moderne. Par exemple, l’introduction du DLR nous permet d’utiliser un ImpromptuObject, de sorte que notre DynamicObject expose n’importe quelle interface que nous désirons. Cela permet une programmation dynamique et de nombreux modèles de conception sont présentés en termes de variations statiques et basées sur le DLR.

Style de présentation

Ce cours est présenté comme une (très grande) série de démonstrations en direct effectuées dans Microsoft Visual Studio. La plupart des démos sont à fichier unique, vous pouvez donc télécharger le fichier joint à la leçon et l’exécuter dans Visual Studio, Visual Studio Code, Rider ou un autre IDE de votre choix.

Ce cours n’utilise pas de diagrammes de classes UML ; toutes les démos sont codées en direct. J’utilise Visual Studio, divers packages NuGet, le lanceur de tests unitaires R# et même dotMemoryUnit.

 

1
Introduction

A taste of things to come...

The SOLID Design Principles

1
Overview

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

2
Single Responsibility Principle

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.

3
Open-Closed Principle

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.

4
Liskov Substitution Principle

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

5
Interface Segregation Principle

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.

6
Dependency Inversion Principle

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?

7
Summary

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

Builder

1
Gamma Categorization

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

2
Overview

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

3
Life Without Builder

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

4
Builder

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

5
Fluent Builder

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

6
Fluent Builder Inheritance with Recursive Generics

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

7
Functional Builder

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

8
Faceted Builder

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.

9
Builder Coding Exercise
10
Summary

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

Factories

1
Overview

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

2
Point Example

A scenario where having a factory interface actually makes sense.

3
Factory Method

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

4
Asynchronous Factory Method

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

5
Factory

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

6
Inner Factory

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!

7
Abstract Factory

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

8
Abstract Factory and OCP

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

9
Factory Coding Exercise
10
Summary

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

Prototype

1
Overview

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.

2
ICloneable is Bad

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

3
Copy Constructors

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.

4
Explicit Deep Copy Interface

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

5
Prototype Inheritance

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

6
Copy Through Serialization

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

7
Prototype Coding Exercise
8
Summary

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

Singleton

1
Overview

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

2
Singleton Implementation

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.

3
Testability Issues

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!

4
Singleton in Dependency Injection

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)

5
Monostate

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!

6
Per-Thread Singleton

An alternative that completely skirts the issue of thread safety.

7
Ambient Context

A very common and simple design pattern.

8
Singleton Coding Exercise
9
Summary

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

Adapter

1
Overview

A look at the Adapter design pattern.

2
Vector/Raster Demo

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

3
Adapter Caching

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

4
Generic Value Adapter

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.

5
Adapter in Dependency Injection

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

6
Adapter Coding Exercise
7
Summary

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

Bridge

1
Overview

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

2
Bridge

A simple illustration of how to build a bridge.

3
Bridge Coding Exercise
4
Summary

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

Composite

1
Overview

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

2
Geometric Shapes

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

3
Neural Networks

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

4
Composite Specification

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

5
Composite Coding Exercise
6
Summary

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

Decorator

1
Overview

A look at the Decorator design pattern.

2
Custom String Builder

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

3
Adapter-Decorator

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

4
Multiple Inheritance with Interfaces

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

5
Multiple Inheritance with Default Interface Members

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

6
Dynamic Decorator Composition

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

7
Detecting Decorator Cycles

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

8
Static Decorator Composition

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

9
Decorator in Dependency Injection

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

10
Decorator Coding Exercise
11
Summary

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

Façade

1
Overview

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

2
Façade

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

3
Facade Coding Exercise
4
Summary

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

Flyweight

1
Overview

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

2
Repeating User Names

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

3
Text Formatting

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!

4
Flyweight Coding Exercise
5
Summary

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

Proxy

1
Overview

A look at the Proxy design pattern.

2
Protection Proxy

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

3
Property Proxy

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.

4
Value Proxy

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

5
Composite Proxy: SoA/AoS

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

6
Composite Proxy with Array-Backed Properties

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

7
Dynamic Proxy for Logging

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.

8
Proxy vs. Decorator

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

9
ViewModel

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

10
Proxy Coding Exercise
11
Bit Fragging

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.

12
Summary

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

Chain of Responsibility

1
Overview

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

2
Command Query Separation

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

3
Method Chain

Chain of Responsibility implemented by chaining method calls together.

4
Broker Chain

A more sophisticated approach to implementing the chain of responsibility.

5
Chain of Responsibility Coding Exercise
6
Summary

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

Command

1
Overview

A look at the Command design pattern.

2
Command

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

3
Undo Operations

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

4
Composite Command

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

5
Command Coding Exercise
6
Summary

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

Interpreter

1
Overview

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

2
Handmade Interpreter: Lexing

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

3
Handmade Interpreter: Parsing

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

4
ANTLR

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

5
Interpreter Coding Exercise
6
Summary

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

Iterator

1
Overview

A look at the Iterator design pattern.

2
Iterator Object

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

3
Iterator Method

A much more natural way of providing iteration functionality.

4
Iterators and Duck Typing

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!

5
Array-Backed Properties

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!

6
Iterator Coding Exercise
7
Summary

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

Mediator

1
Overview

A look at the Mediator design pattern.

2
Chat Room

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

3
Event Broker

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

4
Introduction to MediatR

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

5
Mediator Coding Exercise
6
Summary

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

Memento

1
Overview

A look at the Memento design pattern.

2
Memento

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

3
Undo and Redo

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

4
Memento for Interop

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.

5
Memento Coding Exercise
6
Summary

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

Null Object

1
Overview

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

2
Null Object

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

3
Null Object Singleton

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

4
Dynamic Null Object

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.

5
Null Object Coding Exercise
6
Summary

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

Observer

1
Overview

A look at the Observer design pattern.

2
Observer via the 'event' Keyword

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

3
Weak Event Pattern

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.

4
Observer via Special Interfaces

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

5
Observable Collections

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

6
Bidirectional Observer

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

7
Property Dependencies

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?

8
Declarative Event Subscriptions with Interfaces

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

9
Observer Coding Exercise
10
Summary

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

State

1
Overview

A look at the State design pattern.

2
Classic Implementation

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

3
Handmade State Machine

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

4
Switch-Based State Machine

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

5
Switch Expressions

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

6
State Machine with Stateless

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

7
State Coding Exercise
8
Summary

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

Strategy

1
Overview

A look at the strategy design pattern.

2
Dynamic Strategy

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

3
Static Strategy

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

4
Equality and Comparison Strategies

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

5
Strategy Coding Exercise
6
Summary

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

Template Method

1
Overview

A look at the Template Method design pattern.

2
Template Method

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

3
Functional Template Method

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

4
Template Method Coding Exercise
5
Summary

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

Visitor

1
Overview

A look at the Visitor design pattern.

2
Intrusive Expression Printing

Let's break OCP to support expression printing.

3
Reflection-Based Printing

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

4
Classic Visitor (Double Dispatch)

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

5
Dynamic Visitor via the DLR

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.

6
Acyclic Visitor

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

7
Visitor Coding Exercise
8
Summary

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

Course Summary

1
Creational Paterns Summary

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

2
Structural Patterns Summary

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

3
Behavioral Patterns Summary

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.

4
End of Course

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

Bonus Lectures: Related Concepts

1
An ASCII C# String

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

2
Continuation Passing Style

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

3
Local Inversion of Control

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!

4
DI Container and Event Broker Integration

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.

5
Beyond the Elvis Operator

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.

6
CQRS and Event Sourcing

An introductory look at CQRS and Event Sourcing.

7
Bonus Lecture: Other Courses at a Discount

Bonus Lectures: Functional Patterns in F#

1
Overview

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

2
Builder

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

3
Decorator

Function decorators done in a functional way.

4
Factory

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

5
Interpreter

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

6
Strategy

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

7
Template Method

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

8
Summary

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

Vous pouvez afficher et revoir les supports de cours indu00e9finiment, comme une chau00eene u00e0 la demande.
Absolumentu00a0! Si vous disposez d'une connexion Internet, les cours sur Udemy sont disponibles u00e0 tout moment sur n'importe quel appareil. Si vous n'avez pas de connexion Internet, certains instructeurs permettent u00e9galement u00e0 leurs u00e9tudiants de tu00e9lu00e9charger les cours. instructeur cependant, alors assurez-vous d'u00eatre de leur bon cu00f4tu00e9u00a0!
4.4
4.4 sur 5
Notes7397

Détails des Notes

Étoiles 5
3840
Étoiles 4
2509
Étoiles 3
777
Étoiles 2
148
Étoiles 1
123
Suivre un cours
Garantie de remboursement de 30 jours

Inclut

20 heures de vidéo à la demande
articles 1
Accès complet à vie
Accès sur le mobile et la télévision
Certificat d'achèvement

Archive

Working hours

Monday 9:30 am - 6.00 pm
Tuesday 9:30 am - 6.00 pm
Wednesday 9:30 am - 6.00 pm
Thursday 9:30 am - 6.00 pm
Friday 9:30 am - 5.00 pm
Saturday Closed
Sunday Closed