DOT NET TRICKS - Design Patterns in C# PDF
DOT NET TRICKS - Design Patterns in C# PDF
Design Patterns in C#
Posted by Abhishek Sur on Wednesday, May 19, 2010
Labels: .NET, .NET 3.5, .NET Memory Management, Finalize, IDisposable, WPF
23 Comments
When the word design pattern comes into mind, the first thing that one
may think is the classical book on Design Pattern "Gangs of Four" which
was published by Erich Gamma, Richard Helm, Ralph Johnson, and John
Vlissides. In this book, it is first discussed capabilities and pitfalls of Object
http://www.abhisheksur.com/2010/05/design-patterns.html 1/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
or
CREATIONAL PATTERNS
Singleton Pattern
Implementation
/// <summary>
/// Implementation of Singleton Pattern
/// </summary>
http://www.abhisheksur.com/2010/05/design-patterns.html 2/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
# endregion
}
In the above code you can see I have intentionally made the constructor
as private. This will make sure that the class cant be instantiated from
outside. On the other hand, you also need to make a property which will
return the static instance of the object present within the class itself. Hence
the object will be shared between all the external entities.
Factory Pattern
Factory pattern deals with the instantiation of object without exposing the
instantiation logic. In other words, a Factory is actually a creator of object
which has common interface.
http://www.abhisheksur.com/2010/05/design-patterns.html 3/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
Implementation
#endregion
}
http://www.abhisheksur.com/2010/05/design-patterns.html 4/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
#endregion
}
/// <summary>
/// Implementation of Factory - Used to create objects
/// </summary>
public class Factory
{
public IPeople GetPeople(PeopleType type)
{
IPeople people = null;
switch (type)
{
case PeopleType.RURAL :
people = new Villagers();
break;
case PeopleType.URBAN:
people = new CityPeople();
break;
default:
break;
}
return people;
}
}
In the above code you can see I have created one interface called IPeople
and implemented two classes from it as Villagers and CityPeople. Based
on the type passed into the factory object, I am sending back the original
concrete object as the Interface IPeople.
http://www.abhisheksur.com/2010/05/design-patterns.html 5/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
Factory Method
IMPLEMENTATION
http://www.abhisheksur.com/2010/05/design-patterns.html 6/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
#endregion
}
You might also write your custom logic after getting the object in the
concrete Factory Method. The GetObject is made abstract in the Factory
interface.
Abstract Factory
http://www.abhisheksur.com/2010/05/design-patterns.html 7/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
IMPLEMENTATION
http://www.abhisheksur.com/2010/05/design-patterns.html 8/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
Builder Pattern
This pattern creates object based on the Interface, but also lets the
subclass decide which class to instantiate. It also has finer control over the
construction process.
IMPLEMENTATION
http://www.abhisheksur.com/2010/05/design-patterns.html 9/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
string RunBuilderTask2();
}
#endregion
}
#endregion
}
http://www.abhisheksur.com/2010/05/design-patterns.html 10/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
In case of Builder pattern you can see the Director is actually using
CreateBuilder to create the instance of the builder. So when the Bulder is
actually created, we can also invoke a few common task in it.
Prototype Pattern
This pattern creates the kind of object using its prototype. In other words,
while creating the object of Prototype object, the class actually creates a
clone of it and returns it as prototype.
IMPLEMENTATION
// normal implementation
http://www.abhisheksur.com/2010/05/design-patterns.html 11/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
You can see here, I have used MemberwiseClone method to clone the
prototype when required.
STRUCTURAL PATTERN
Adapter Pattern
Adapter pattern converts one instance of a class into another interface which
client expects. In other words, Adapter pattern actually makes two classes
compatible.
http://www.abhisheksur.com/2010/05/design-patterns.html 12/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
IMPLEMENTATION
}
}
/// <summary>
/// Implements MyClass2 again to ensure they are in same
format.
/// </summary>
public class Adapter : IAdapter
{
private MyClass2 _class2 = new MyClass2();
http://www.abhisheksur.com/2010/05/design-patterns.html 13/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
Bridge Pattern
IMPLEMENTATION
http://www.abhisheksur.com/2010/05/design-patterns.html 14/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
#endregion
}
#endregion
}
# endregion
# region Abstraction
public interface IAbstractBridge
{
void CallMethod1();
void CallMethod2();
}
http://www.abhisheksur.com/2010/05/design-patterns.html 15/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
#endregion
}
# endregion
Thus you can see the Bridge classes are the Implementation, which uses
the same interface oriented architecture to create objects. On the other
hand the abstraction takes an object of the implementation phase and runs
its method. Thus makes it completely decoupled with one another.
Decorator Pattern
http://www.abhisheksur.com/2010/05/design-patterns.html 16/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
IMPLEMENTATION
This is the same parent child relationship where the child class adds up
new feature called Method2 while other characteristics is derived from the
parent.
Composite Pattern
http://www.abhisheksur.com/2010/05/design-patterns.html 17/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
IMPLEMENTATION
/// <summary>
/// Treats elements as composition of one or more element,
so that components can be separated
/// between one another
/// </summary>
public interface IComposite
{
void CompositeMethod();
}
#endregion
}
/// <summary>
/// Elements from IComposite can be separated from others
/// </summary>
public class NormalComposite : IComposite
{
http://www.abhisheksur.com/2010/05/design-patterns.html 18/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
#endregion
Flyweight Pattern
Flyweight allows you to share bulky data which are common to each
object. In other words, if you think that same data is repeating for every
object, you can use this pattern to point to the single object and hence can
easily save space.
IMPLEMENTATION
/// <summary>
/// Defines Flyweight object which repeats iteself.
/// </summary>
public class FlyWeight
{
public string Company { get; set; }
http://www.abhisheksur.com/2010/05/design-patterns.html 19/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
Memento Pattern
Memento pattern allows you to capture the internal state of the object
without violating encapsulation and later on you can undo/ revert the
changes when required.
http://www.abhisheksur.com/2010/05/design-patterns.html 20/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
IMPLEMENTATION
Here you can see the Memento Object is actually used to Revert the
changes made in the object.
BEHAVIOURAL PATTERN
Mediator Pattern
http://www.abhisheksur.com/2010/05/design-patterns.html 21/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
Mediator pattern ensures that the components are loosely coupled, such that
they don't call each others explicitly, rather they always use a separate Mediator
IMPLEMENTATION
#endregion
}
http://www.abhisheksur.com/2010/05/design-patterns.html 22/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
#endregion
}
Here you can see the mediator Registers all the Components within it and
then calls its method when required.
Observer Pattern
http://www.abhisheksur.com/2010/05/design-patterns.html 23/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
IMPLEMENTATION
http://www.abhisheksur.com/2010/05/design-patterns.html 24/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
#endregion
#endregion
}
}
}
You can definitely got the idea that after you Register for the Notification,
you will get it when ChangeState is called.
Iterator Pattern
http://www.abhisheksur.com/2010/05/design-patterns.html 25/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
IMPLEMENTATION
http://www.abhisheksur.com/2010/05/design-patterns.html 26/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
#endregion
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
foreach (Element arr in this.array)
yield return arr;
}
#endregion
}
OR
Conclusion
These are the basic design patterns. There are still few design patterns left
to be implemented. Stay tuned for those updates.
Thank you for reading.
Shout it Submit this story to DotNetKicks
Like 28 people like this. Sign Up to see what your friends like.
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
http://www.abhisheksur.com/2010/05/design-patterns.html 27/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
Would be great if you can create a post which explores the real time
scenarios where each pattern might be used...
Regards,
Kunal
1△ ▽ • Reply • Share ›
DOT NET TRICKS: .NET Book : DOT NET TRICKS: .NET Book :
Visual Studio 2013 Expert Visual Studio 2012 and .NET 4.5
Home
Newer Post Older Post
Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF,
Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5,
JQuery and many more...
Grab it now !!!
http://www.abhisheksur.com/2010/05/design-patterns.html 30/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
Blog Subscription
Microsoft® Translator
My friend Shivprasad
Koirala who is also a
Microsoft ASP.NET
MVP has released
Learn MVC 5 step by
step video series. It
starts right from basics
of MVC and goes to a
level until you become a
professional. You can
start taking the course
for free using the below
youtube video.
Learn ASP.NE
My Awards
http://www.abhisheksur.com/2010/05/design-patterns.html 31/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
Codeproject MVP
Codeproject Associate
Dotnetfunda MVP
Hit Counter
Memory Management,
Internals, Visual
Studio, HTML5,
JQuery and many
more...
The Programmers
Newspaper
http://www.abhisheksur.com/2010/05/design-patterns.html 33/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
Blogs I follow
Blog Archive
► 2015 (2)
► 2014 (1)
► 2013 (5)
► 2012 (6)
► 2011 (58)
▼ 2010 (90)
► December (6)
► November (7)
► October (8)
http://www.abhisheksur.com/2010/05/design-patterns.html 34/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
► September (11)
► August (11)
► July (10)
► June (10)
▼ May (8)
WPF Tutorial :
Beginning to
Layout, Content,
Trans...
Strange
UserControl
Issue (Struck
with the
easiest...
Article Selected
@WindowsClien
t.net
Creating Splash
Screen (Without
Code)
Design Patterns in
C#
Object Notifiers
using
INotifyPropertyC
hanged, INo...
New WPF
Learning Series
How to escape {}
in XAML
► April (7)
► March (11)
► January (1)
► 2009 (5)
► 2008 (4)
Labels
http://www.abhisheksur.com/2010/05/design-patterns.html 35/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
ADO.NET ALM
architecture ASP.NET
4.0 asp.net4.5 async Auto
property initialization await
Azure
beyondrelational
blogger tips book C# C#
6.0 C#5.0
CodeProject
Configuration cookbook
Custom Control Database
debugging design pattern
Developer Conference DLR
dotnetfunda.com
exception filters expression
bodies functions Extention
Finalize free GC Geolocator
gesture gift giveaways html5
IDisposable internals
IObservable. Rx
IsolatedStorage jquery
kinect LOH MEF Memory
Allocation Metro
multithreading MVP MVVM
nameof null condition Online
Session Patterns PDC10
Prism push technology
Reflection Regex scripting
silverlight SOH static class
string interpolation struct
Teched Testing TFS
Threading tips TPL TPL
Data Flows Unity Visual Studio
VS2012 WCF
WeakReference Windows
Phone Windows Phone7
Windows8
windowsclient.net
WinForms WinRT
WPF XAML
Popular Posts
Working with
CollectionView in
http://www.abhisheksur.com/2010/05/design-patterns.html 36/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
WPF(Filter,
Sort,
Group,
Navigate)
If you are working with
WPF for long, you might
already have come
across with
ICollectionView. It is the
primary Data object for
any WPF lis...
Design
Patterns in
C#
As I am
doing a lot of
architecture stuffs, lets
discuss the very basics
of designing a good
architecture. To begin
with this, you must
star...
Forum
Guys, Here is a forum
for you. Just drop any
Suggestion, Query,
Problem anything here. I
will try to solve it. Please
make sure that you pr...
All about
.NET
Timers - A
Compariso
n
Threads and Timers are
the most common things
that you need for your
application. Any work
that needs to be done in
background without
inter...
http://www.abhisheksur.com/2010/05/design-patterns.html 37/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
Introducing
Ribbon UI
Control for
WPF
Introduction After
reading Pete Brown in
his post on 2nd Aug
announcing the new
RibbonUI feature in his
post,
Announcing:Microsoft
Ribbon ...
Writing a
Reusable
Custom
Control in
WPF
In my previous post , I
have already defined
how you can inherit from
an existing control and
define your own
reusable chunk. The
reusable X...
Writing a
Custom
Configurati
onSection
to handle a Collection
Configuration is one of
the major thing that you
need to keep in mind
http://www.abhisheksur.com/2010/05/design-patterns.html 38/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
WPF
Tutorial
WPF, a.k.a
Windows
Presentation Foundation
provides an unified
model for producing
high end graphical
business application
easily using norm...
Pages
Home
About Me
My Skills
Achievements
People I Admire
My Publication
Frequently Asked
Questions
ABHISHEK SUR
Follow 638
http://www.abhisheksur.com/2010/05/design-patterns.html 39/40
15/12/2018 DOT NET TRICKS: Design Patterns in C#
Microsoft
MVP, Client
Dot Net Tricks
412 likes
App Dev
Codeprojec
t MVP,
Associate | Dotnetfunda Like Page
MVP | Kolkata .NET Star
| Writer | Technology Be the first of your friends to like this
Evangelist | Technology
Lover | Geek | Speaker Follow me @abhi2434
VIEW MY COMPLETE PROF ILE
Copyright @DOT NET TRICKS all rights reserved. About Contact Disclaimer Notice Home Top
http://www.abhisheksur.com/2010/05/design-patterns.html 40/40