This article is related to creational design pattern in c# programming language
Introduction to design pattern.And why it needed ?.
Let me start with very interesting quote” It’s easy to walk
on water and develop software from specification- if both are frozen.” –means what?
Yes, I think you already guess it. I am
talking about requirement change of software development. And for that design
pattern is very necessary.It will help you to construct very strong foundation
in software project.
In this article I am going to explain few very popular design
patterns in software development process. I will explain and show code in C#
but you may use your favorite programming language to implement them. Basically
there are three categories in design pattern
1) Creational
2) Behavioral
3) Structural
Let’s get common fundamental idea about them.
Creational:- It deals with object creation in your project .
Define how your object will get create? When it will get create? where it will
get create?.
Behavioral:- It defines behavior of your code or class.
Means it will deal with how one object will communicate with other. How we will
preserver object’s state any many more.
Structural:- Basically
structural design pattern deals with or define structure of your project
. How to implement de couple relation within class? How to implement common
pattern or naming convention throughout project?etc.
And here I am going to explain Creational design pattern
Few popular example of creational design pattern are
Factory design pattern
Prototype design pattern
Singleton design pattern
Now let’s discuss one by one with example.
Factory Design
pattern
Factory design pattern is one of the common design patterns in
software project. Let’s understand the basics of Factory design pattern.
Before starting the discussion, let’s learn the advantage of Factory
design pattern.
<!--[if !supportLists]-->1)
<!--[endif]-->In client code here and there we create object
using new keyword within much more if – else condition. If you implement
factory design pattern to reduce new keyword in client.
<!--[if !supportLists]-->2)
<!--[endif]-->When you want to abstract your business class
from client, then implement factory pattern in your project in project.
In below code below, we see how to implement factory design pattern
in project. Here we have two types of employee class in my code and I have
created a factory class EmployeeFactory to supply object of appropriate class
depending upon class request . Now if you add one more type of employee class in
your business logic the client should not bother at all. Just tell your client
to send proper code to get object of newly added class.
So basically our factory class is making an abstraction over our
business class.
using System;
using System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Collections;
using
System.Data.SqlClient;
using
System.Data;
using
System.Diagnostics;
namespace
BlogProject
{
// Factory Design patern
public interface
IEmployee
{
void ShowEmployee();
}
public class
Developer:IEmployee
{
public void
ShowEmployee()
{
Console.Write("I ma .NET Developer");
}
}
public class
HR : IEmployee
{
public void
ShowEmployee()
{
Console.Write("I am Technical HR");
}
}
public class
EmployeeFactory
{
// Factory class to generate object of
concrite class
public IEmployee
GetObject(int a)
{
if
(a == 1)
{
return
new Developer();
}
else
if (a == 2)
{
return
new HR();
}
else
return
null;
}
}
class Program
{
static void
Main(string[] args)
{
Console.WriteLine("Enter 1 to create Developer class object and 2 to
create HR class object"+"\n");
int
Type = Convert.ToInt32(Console.ReadLine());
EmployeeFactory
objFactory = new EmployeeFactory();
IEmployee
objemp = objFactory.GetObject(Type);
objemp.ShowEmployee();
Console.ReadLine();
}
}
}
Singleton
design pattern
By seeing the name you probably guess
that it’s something related to single object, right? If you guess so, then you
are master of design pattern, just kidding. OK, I
know you are learning design pattern and one day you will be among the best.
So coming to our discussion again.
Singleton design pattern is one of very essential design pattern in day to day
programming life. Are you thinking, where it needs to be implemented? If you still do
not find any example let me give one.
In website you might be aware of hit
count feature. So whenever any user visits the website (with hit counter
feature) the hit count variable increase by one. It’s one wonderful example of
singleton pattern.
So what happens exactly? In whole
project only single object of this class (Yes only single object) get create
and all user use it. So the object is global to all user, it’s not someone’s
property.
Let’s have a look of below code.
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Collections;
using System.Data.SqlClient;
using
System.Data;
using
System.Diagnostics;
namespace
BlogProject
{
//Singletone Design patern in C#
public class
SingleTone
{
private SingleTone()
{
}
private static
SingleTone Single;
public Int32
Pagecount{get;set;}
public static
SingleTone ReturnObject()
{
if
(Single != null)
{
return
Single;
}
else
{
Single = new SingleTone();
return
Single;
}
}
}
class Program
{
static void
Main(string[] args)
{
SingleTone
s = SingleTone.ReturnObject();
s.Pagecount = 100;
SingleTone
s1 = SingleTone.ReturnObject();
s1.Pagecount = 200;
Console.WriteLine(s.Pagecount);
Console.ReadLine();
}
}
}
Prototype
Design pattern
Prototype design pattern is used to make copy of any object. Now question
may come in your mind, what we will do by making a copy of one object? Let me
explain one scenario where prototype pattern comes in play.
You want to send invitation request to all your friend in your
birthday and I am assuming that all your friends stays near to your house, not
much far. And as all are near to your house there address will be very similar
or slightly different to each other. Now I am assuming in address you will
mention
Country, State, City, PIN etc.
And
those will be same for all of your friend. Now if you make one primary copy and
copy all other from primary one then your task will reduce by 70%. Yes, I know
you will tell me what about friend name. Yes only that attribute you have to
set for each and every object, That’s why I said 70% otherwise I would say 100
% J .
Here is the hero for Prototype pattern implementation.
public Location Clone(Location
objLoc)
{
return
(Location)objLoc.MemberwiseClone();
}
using System;
using System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Collections;
using
System.Data.SqlClient;
using
System.Data;
using
System.Diagnostics;
namespace
BlogProject
{
// Prototype Design patern in C#
class Location
{
public string
CountryName { get; set;
}
public string
StateName { get; set;
}
public String
DistrictName { get; set;
}
public string
RepecientName { get; set;
}
public Location
Clone(Location objLoc)
{
return
(Location)objLoc.MemberwiseClone();
}
}
class Program
{
static void
Main(string[] args)
{
Location
L1 = new Location();
L1.CountryName = "India";
L1.StateName = "West Bengal";
L1.DistrictName = "Howrah";
L1.RepecientName = "Sourav Kayal";
Location
L2 = new Location();
L2 = L1.Clone(L1);
L2.RepecientName = "Prasanta Kumar Kayal";
Console.WriteLine("Country Name:- "+ L2.CountryName +"\n");
Console.WriteLine("State Name:- " + L2.StateName + "\n");
Console.WriteLine("District Name:- " + L2.DistrictName + "\n");
Console.WriteLine("Recipient Name:- " + L2.RepecientName +
"\n");
Console.ReadLine();
}
}
}
Conclusion
Hope this article helps understand and get fundamental idea of design pattern. Proper design pattern has to be implemented after careful consideration.