You are on page 1of 54

Think ahead. Act now.

Design Patterns

Think ahead. Act now.


Overall training 1. Design Patterns
2. Enterprise Design Patterns
program 3. Scalable architectures
4. Microservices & Web API
5. Cloud native architectures
6. Container technology

Think ahead. Act now.


Agenda •


Introductions

VM setup

• Design patterns

• Creational patterns
• Abstract Factory

• Singleton

• Hands-on Labs 1-2

• Structural patterns
• Adapter

• Decorator

• Façade

• Hands-on Labs 3-5

• Behavioral patterns
• Chain of Responsibility

• Observer

• Strategy

• Hands-on Labs 6-8

Think ahead. Act now.


Introductions

• Who are you?


• What do you (like to) do?
• Are you familiar with design patterns?

Think ahead. Act now.


VM access
1. Azure DevTest lab: Centric
2. Claimable virtual machines -> Claim
3. My virtual machines -> Connect
4. Credentials: centric / Centric123!
5. Wait
6. Start VS2017
7. Sign in with VS Account
8. Solutions are located in C:\Repos\

Think ahead. Act now.


Design patterns
Think ahead. Act now.
What is a design pattern?
• Description of a solution for a
well known problem

Published in 1994!
Think ahead. Act now.
Four elements of a design pattern
• Pattern name
Identifies the problem, solution(s) and consequences

• Problem
When to apply the pattern

• Solution
Describes elements of the design

• Consequences
The result and trade-offs of applying the pattern

Think ahead. Act now.


Why do we use design patterns?
• Improve communication

• Apply proven solutions for well known problems

Be careful!

• Applying design patterns is a means to achieve a goal, it’s not a goal by


itself.

Think ahead. Act now.


Design pattern categories
• Creational

• Structural

• Behavioral

Think ahead. Act now.


Creational design patterns
Think ahead. Act now.
Abstract Factory
Intent
Provide an interface for creating families of
related or dependent objects without
specifying their concrete classes.

Think ahead. Act now.


Abstract Factory
Consequences
+ Provides good isolation of concrete classes and client

+ Easy to exchange factories

+ Promotes consistency between products

- Supporting very different products requires a lot of changes

Think ahead. Act now.


Abstract Factory
In .NET Framework
DbProviderFactory

Can create Commands, Connections, DataAdapters.

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dbproviderfactories

Think ahead. Act now.


Abstract Factory
Demo

Xpirit.DesignPatterns

Xpirit.DesignPatterns.Creational

AbstractFactory

Think ahead. Act now.


Singleton
Intent
Ensure a class has only one instance and
provide a global point of access to it.

Think ahead. Act now.


Singleton
Consequences
+ Controlled access to one instance

- Can be easily misused

Think ahead. Act now.


Singleton
In the .NET Framework
WCF service instance
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class CalculatorService : ICalculatorInstance
{
...
}

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/sessions-instancing-and-concurrency

Think ahead. Act now.


Singleton
Demo

Xpirit.DesignPatterns

Xpirit.DesignPatterns.Creational

Singleton

Think ahead. Act now.


Hands-on Lab: 1
1. Open solution: DesignPatterns-Creational-Labs.sln.
2. Examine the files in the AbstractFactory folder in the Xpirit.DesignPatterns.Creational project.
3. Refactor the code to implement the AbstractFactory pattern.
4. Ensure the unit tests marked with [Trait(“Creational”, “AbstractFactory“)] all pass.

Think ahead. Act now.


Hands-on Lab: 2
1. Open solution: DesignPatterns-Creational-Labs.sln.
2. Examine the files in the Singleton folder in the Xpirit.DesignPatterns.Creational project.
3. Refactor the code to implement the Singleton pattern.
4. Ensure the unit tests marked with [Trait(“Creational”, “Singleton“)] all pass.

Think ahead. Act now.


Structural design patterns
Think ahead. Act now.
Adapter
Intent
Convert the interface of a class into another
interface clients expect.

Adapter lets classes work together that


couldn't otherwise because of incompatible
interfaces.

Think ahead. Act now.


Adapter
Consequences
+ Adapter could adapt different Adaptees

Think ahead. Act now.


Adapter
In .NET Framework
DbDataAdapters

SqlDataAdapter custAdapter = new SqlDataAdapter( customerQuery, customerConnection);


OleDbDataAdapter ordAdapter = new OleDbDataAdapter( orderQuery, orderConnection);

DataSet customerOrders = new DataSet();

custAdapter.Fill(customerOrders, "Customers");
ordAdapter.Fill(customerOrders, "Orders");

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/populating-a-dataset-from-a-dataadapter

Think ahead. Act now.


Adapter
Demo

Xpirit.DesignPatterns

Xpirit.DesignPatterns.Structural

Adapter

Think ahead. Act now.


Decorator
Intent
Attach additional responsibilities to an
object dynamically.

Decorators provide a flexible alternative to


subclassing for extending functionality.

Think ahead. Act now.


Decorator
Consequences
+ More flexible than inheritance

+ Avoids large classes which implement all features

- Many small classes

Think ahead. Act now.


Decorator
In the .NET Framework
BufferedStream, CryptoStream

The constructor accepts another Stream and functionality is extended with


buffering/cryptography.

Think ahead. Act now.


Decorator
Demo

Xpirit.DesignPatterns

Xpirit.DesignPatterns.Structural

Decorator

Think ahead. Act now.


Facade
Intent
Provide a unified interface to a set of
interfaces in a subsystem.

Facade defines a higher-level interface that


makes the subsystem easier to use.

Think ahead. Act now.


Facade
Consequences
+ Shields clients from knowing about subsystems

+ Promotes weak coupling between clients and subsystems.

Think ahead. Act now.


Facade
In .NET Framework
XmlSerializer
Performs (de)serialization of objects, creates readers, writers and custom serializers.

https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer

Think ahead. Act now.


Facade
Demo

Xpirit.DesignPatterns

Xpirit.DesignPatterns.Structural

Facade

Think ahead. Act now.


Hands-on Lab: 3
1. Open solution: DesignPatterns-Structural-Labs.sln.
2. Examine the files in the Adapter folder in the Xpirit.DesignPatterns.Structural project.
3. Refactor the code to implement the Adapter pattern.
4. Ensure the unit tests marked with [Trait(“Structural”, “Adapter“)] all pass.

Think ahead. Act now.


Hands-on Lab: 4
1. Open solution: DesignPatterns-Structural-Labs.sln.
2. Examine the files in the Decorator folder in the Xpirit.DesignPatterns.Structural project.
3. Refactor the code to implement the Decorator pattern.
4. Ensure the unit tests marked with [Trait(“Structural”, “Decorator“)] all pass.

Think ahead. Act now.


Hands-on Lab: 5
1. Open solution: DesignPatterns-Structural-Labs.sln.
2. Examine the files in the Facade folder in the Xpirit.DesignPatterns.Structural project.
3. Refactor the code to implement the Facade pattern.
4. Ensure the unit tests marked with [Trait(“Structural”, “Facade“)] all pass.

Think ahead. Act now.


Behavioral design patterns
Think ahead. Act now.
Chain of Responsibility
Intent
Avoid coupling the sender of a request to its
receiver by giving more than one object a
chance to handle the request.

Chain the receiving objects and pass the


request along the chain until an object
handles it.

Think ahead. Act now.


Chain of Responsibility
Consequences
+ Reduced coupling

- No guarantee of receipt

Think ahead. Act now.


Chain of Responsibility
In .NET Framework
Routed Events in WPF
An event raised in a child element can be handled by a parent element.

https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/routed-events-overview

Think ahead. Act now.


Chain of Responsibility
Demo
Xpirit.DesignPatterns

Xpirit.DesignPatterns.Behavioural

ChainOfResponsibility

Think ahead. Act now.


Observer
Intent
Define a one-to-many dependency between
objects so that when one object changes
state, all its dependents are notified and
updated automatically.

Think ahead. Act now.


Observer
Consequences
+ Abstract coupling between Subject and Observer

+ Supports broadcasting of notifications

- Cascading updates

Think ahead. Act now.


Observer
In .NET Framework
IObservable<T> interface

Defines a Subscribe method to register observers.

https://docs.microsoft.com/en-US/dotnet/api/system.iobservable-1

Think ahead. Act now.


Observer
Demo

Xpirit.DesignPatterns

Xpirit.DesignPatterns.Behavioural

Observer

Think ahead. Act now.


Strategy
Intent
Define a family of algorithms, encapsulate
each one, and make them interchangeable.

Strategy lets the algorithm vary


independently from clients that use it.

Think ahead. Act now.


Strategy
Consequences
+ Good alternative to subclassing the context

+ Eliminates conditional statements

- Clients must be aware of available Strategies

Think ahead. Act now.


Strategy
In .NET Framework
IEquatable<T> and IComparable<T> interfaces

Can be used to change behavior of equality and sorting of collections.

https://docs.microsoft.com/en-us/dotnet/standard/collections/comparisons-and-sorts-within-collections

Think ahead. Act now.


Strategy
Demo

Xpirit.DesignPatterns

Xpirit.DesignPatterns.Behavioural

Strategy

Think ahead. Act now.


Hands-on Lab: 6
1. Open solution: DesignPatterns-Behavioural-Labs.sln.
2. Examine the files in the ChainOfResponsibility folder in the Xpirit.DesignPatterns.Behavioral project.
3. Refactor the code to implement the Chain of Responsibility pattern.
4. Ensure the unit tests marked with [Trait(“Behavioral”, “Chain of Responsibility“)] all pass.

Think ahead. Act now.


Hands-on Lab: 7
1. Open solution: DesignPatterns-Behavioural-Labs.sln.
2. Examine the files in the Observer folder in the Xpirit.DesignPatterns.Behavioral project.
3. Refactor the code to implement the Observer pattern.
4. Ensure the unit tests marked with [Trait(“Behavioral”, “Observer“)] all pass.

Think ahead. Act now.


Hands-on Lab: 8
1. Open solution: DesignPatterns-Behavioural-Labs.sln.
2. Examine the content in the Strategy folder in the Xpirit.DesignPatterns.Behavioral project.
3. Refactor the code to implement the Strategy pattern.
4. Ensure the unit tests marked with [Trait(“Behavioral”, “Strategy“)] all pass.

Think ahead. Act now.


Think ahead. Act now.

Marc Duiker
mduiker@xpirit.com
@marcduiker

Think ahead. Act now.

You might also like