Design Patterns are nothing but tested best practices for Common solutions.
Classification of Design Patterns based on their purpose:
Creational Patterns : defines the way of creating instance(object) of classes
Structural Patterns : To define relationship between entities
Behavioral Patterns : These patterns defines the way of communication between objects
Concurrency Patterns : These patterns defines the best designs for Concurrency managment in multi threaded Environment.
Now I am going to discuss some commonly used patterns starting with creational Patterns
Types of Creational Paterns:
Singleton
Factory Method
Abstract Factory
Builder pattern
Lazy initialization
Object pool
Prototype
SingleTon Pattern
Before starting Singleton pattern, I am going to discuss some points, so that one can easily understand the pattern detail.
Thread-safe: It means only single thread can access the same line of code at same time. to make a thread safe we can put locks in C#
Type Safe:
PatternName: Singleton
General Scenerio: Where we required at most one instance of Class.
Characterstics of SingleTon Pattern:
1. Only one instance of class can be created
2. Thread safety ( To make compatible with multithreaded environment
3 Lazy Initialzation ( To initialzation the object when first time accessed, not at application initialzation)
Real World Scenerios:
1. If you were to interface with hardware resource like Serial Port or external devices,
its mandatory to have a single point of access to the device,
through which the entire communication happen to pass on with.
2. In web application if you required Counter for number of visitor
Mapping Problem to Code:
In each language SingleTon pattern mapped to code with differnt ways.The best code for singleton pattern in c# is given below.
using System;
public sealed class Singleton
{
private static volatile Singleton instance;
private static object syncRoot = new Object();
private Singleton() {}
public static Singleton Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
}
Code Understanding Points:
1. The class is marked sealed to prevent derivation, which could add more instances.
2. The variable is declared to be volatile to ensure that assignment to the instance
variable completes before the instance variable can be accessed.
3. uses a syncRoot instance to lock on, rather than locking on the type itself, to avoid deadlocks.
4. This double-check locking approach solves the thread concurrency problems while avoiding an exclusive
lock in every call to the Instance property method
5. Delay instantiation until the object is first accessed using property.
References
http://msdn.microsoft.com/en-us/library/ff650316.aspx ( core c# approach)
http://www.yoda.arachsys.com/csharp/singleton.html ( Java based approach)
Factory Method
1. http://aspalliance.com/1751_Exemplifying_the_Factory_Method_Pattern_inside_the_NET_Framework.2
2. http://www.blackwasp.co.uk/FactoryMethod.aspx (Factory parameterized pattern)
3. http://msdn.microsoft.com/en-us/library/ee817667.aspx
Abstract FactoryPattern
Dependency Injection Pattern:
DI(IOC)
http://www.codeproject.com/KB/aspnet/IOCDI.aspx
http://en.wikipedia.org/wiki/Dependency_injection
http://msdn.microsoft.com/en-us/magazine/cc163739.aspx#S1