Saturday, June 4, 2016

Factory Pattern

Factory pattern is used to create objects. This pattern falls under creational patterns of GOF (Gang of Four) Patterns. In simple term factory used to create products and in software term factory used to create objects.

When to use –


When applications grow, we create more and more objects which increases complexity and becomes more difficult to maintain. To reduce complexity, we need to create objects in structured manner so that we can easily maintain them. Factory pattern provides easy way to create objects as per your need and also help you to write flexible and easy to maintain code.

Code –
public enum ResourceType
{
    DotNet,
    Java
}
public interface IResource
{
    string DoWork();
}
public class DotNet : IResource
{
    public string DoWork()
    {
        return "I'm writing Dotnet code.";
    }
}
public class Java : IResource
{
    public string DoWork()
    {
        return "I'm writing Java code.";
    }
}
public static class ResourceFactory
{
    public static IResource GetResource(ResourceType resourceType)
    {
        IResource resource;
        switch (resourceType)
        {
            case ResourceType.DotNet:
                resource = new DotNet();
                return resource;
            case ResourceType.Java:
                resource = new Java();
                return resource;
            default:
                resource = null;
                break;
        }
        return resource;
    }
}
class Program
{
    static void Main(string[] args)
    {
        IResource myResource = ResourceFactory.GetResource(ResourceType.DotNet);
        Console.WriteLine(myResource.DoWork());
        myResource = ResourceFactory.GetResource(ResourceType.Java);
        Console.WriteLine(myResource.DoWork());
        Console.ReadLine();
    }
}


Output –










Above example demonstrates how Factory Pattern used to get different types of resources as per customers need. This example can easily be extended by adding more resources.

Advantages –

  • Factory patterns implementation provides loose coupling between application and factory products.
  • Easy to extend and maintain products. (Open-Closed Principle)

Disadvantages –

  • Sometimes it makes code more difficult to read.
  • Increases complexity if incorrectly used.


Hope this post helps you to understand Factory Pattern. Please leave your feedback in comments below.

References –

See Also –

Wednesday, June 1, 2016

Thread-safe Singleton Pattern (Multi-threaded Singleton Pattern)

In my previous post Singleton Pattern, I explained various ways to implement singleton pattern but those were not thread-safe. In multi-threaded environment it’s quite possible that multiple thread enters into Instance property at the same time and more than one object of Singleton class may be created. In this article I'll show you various ways to create thread-safe singleton pattern.

Simple version of thread-safe singleton pattern via locking.


Code -
public sealed class Singleton
{
    //private static member.
    private static Singleton instance;
    private static object objLock = new object();

    //Private constructor prevents instance creation of class from outside.
    private Singleton() { }

    //This static property responsible for creation of instance of this class.
    public static Singleton Instance
    {
        get
        {
            lock (objLock)
            {
                if (instance == null)
                {
                    Console.WriteLine("New Instance created");
                    instance = new Singleton();
                }
            }
            return instance;
        }
    }

    public void Display()
    {
        Console.WriteLine("Display Method called from Singleton Class...");
    }
}


class Program
{
    static void Main(string[] args)
    {
        //array of threads
        Thread[] thrd = new Thread[4];

        for (int i = 0; i < 4; i++)
        {
            thrd[i] = new Thread(new ThreadStart(() =>
            {
                Singleton singleObject = Singleton.Instance;
                singleObject.Display();
            }));
            thrd[i].Start();
        }
        Console.Read();
    }
}
  

Output – 

You can see “New Instance created” message displayed only once since new instance created only once across multiple thread calls. The above example allows only single thread to enter instance creation code when no instance created. The lock block allows only single thread to enter and create an instance. 

In this approach lock is acquired every time the instance of class is requested by user so it will impact performance. To avoid an exclusive locking in every call of Instance property, you can use double-check locking approach.

Double-check locking approach


Code –
public sealed class Singleton
{
    //private static member.
    private static volatile Singleton instance;
    private static object objLock = new object();

    //Private constructor prevents instance creation of class from outside.
    private Singleton() { }

    //This static method responsible for creation of instance of this class.
    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                lock (objLock)
                {
                    if (instance == null)
                    {
                        Console.WriteLine("New Instance created");
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }

    public void Display()
    {
        Console.WriteLine("Display Method called from Singleton Class...");
    }
}
   
class Program
{
    static void Main(string[] args)
    {
        //array of threads
        Thread[] thrd = new Thread[4];

        for (int i = 0; i < 4; i++)
        {
            thrd[i] = new Thread(new ThreadStart(() =>
            {
                Singleton singleObject = Singleton.Instance;
                singleObject.Display();
            }));
            thrd[i].Start();
        }
        Console.Read();
    }
}

Output - 

Same as above example.

In double-check locking approach, Only first thread acquires lock and creates object while other threads will not be able to acquire lock since I added check before lock statement. So this way we can reduce acquiring lock for each and every thread who calls Instance property and improve performance. I have declared instance variable as volatile so it will read from physical memory every time instead of thread's internal cache.

Thread-safe without using locks via static initialization


As per above example, we can implement thread-safe singleton pattern using locks but there is another way to create thread-safe singleton pattern without using locks but via static member initialization. This approach does not support lazy initialization of an object like above example does.

Code –
namespace ThreadSafeSingleton
{
    public sealed class Singleton
    {
        private static Singleton instance = new Singleton();
       
        private Singleton() { }

        public static Singleton Instance
        {
            get
            {
                return instance;
            }
        }

        public void Display()
        {
            Console.WriteLine("Display Method called from Singleton Class...");
        }
    }
}

namespace ThreadSafeSingleton
{
    class Program
    {
        static void Main(string[] args)
        {
            //array of threads
            Thread[] thrd = new Thread[4];

            for (int i = 0; i < 4; i++)
            {
                thrd[i] = new Thread(new ThreadStart(() =>
                {
                    Singleton singleObject = Singleton.Instance;
                    singleObject.Display();
                }));
                thrd[i].Start();
            }
            Console.Read();
        }
    }
}

Output – 









With this approach, new instance is created only once when any static member or method of this class is referenced. This uses static variable initializer approach and CLR takes care of it. In this approach you have very less control over the creation of the object.

Hope this article helps you to understand different ways to implement thread-safe singleton pattern. Please leave your feedback.

References –

See Also –