Monday, August 6, 2012

Factory method design pattern

There is a factory interface that declares what this factory is supposed to produce. Then, there is an implementation, which is the way how to produce. In the example below, I am using a factory that produces watches of types Elegant and Sporty. Hence I have an factory interface - IWatchFactory. This interface is implemented as WatchFactory. Now the watches are different implementation of IWatch interface.
Here is the C# implementation:
    /// <summary>
    /// Interface for common watch type
    /// </summary>
    public interface IWatch
    {
        string GetType();
    }
    /// <summary>
    /// Elegant class implementation of IWatch interface
    /// </summary>
    public class ElegantWatch : IWatch
    {
        public string GetType()
        {
            return "Elegant";
        }
    }
    /// <summary>
    /// Sporty class implementation of IWatch interface.
    /// </summary>
    public class SportyWatch : IWatch
    {
        public string GetType()
        {
            return "Sporty";
        }
    }
    /// <summary>
    /// Factory interface to generate watches
    /// </summary>
    public interface IWatchFactory
    {
        IWatch GetElegantWatch();
        IWatch GetSportyWatch();
    }
    /// <summary>
    /// Watch factory implementation of IWatchFactory interface
    /// </summary>
    public class WatchFactory : IWatchFactory
    {
        public IWatch GetElegantWatch()
        {
            return new ElegantWatch();
        }
        public IWatch GetSportyWatch()
        {
            return new SportyWatch();
        }
    }
Here is how to use it:
            IWatchFactory watchFactory = new WatchFactory();
            IWatch watch = watchFactory.GetElegantWatch();
            System.Diagnostics.Debug.WriteLine(watch.GetType());
            watch = watchFactory.GetSportyWatch();
            System.Diagnostics.Debug.WriteLine(watch.GetType());

Sunday, August 5, 2012

Singleton Pattern

In this design pattern, the number of objects for a class is restricted to one. Good example of it is thread pools and registry pools.
Following C# code implements this design pattern
public class Singleton
{
private Singleton()
{
// Any initialization
}

private static Singleton singleton;
public static Singleton Singleton
{
get
{
if (singleton == null)
{
singleton = new Singleton();
}
return singleton;
}
}

// Any further implementations...
} // End of singleton class

Observer pattern

In this design pattern, the subject has a list of objects that are dependent on it. For a certain event, the subject informs the objects that the event has occurred. In computers, task scheduler is perfect example of the same.
Here is the C# implementation followed by its class diagram:
    public class Subject
    {
        public event ObserverEvent forObserver;
        private int sleepTime;
        public EventArgs e = null;
        public delegate void ObserverEvent(EventArgs e);
        public Subject() : this((new Random()).Next() % 10000)
        {
        }
        public Subject(int interval)
        {
            this.sleepTime = interval;
        }
        public void BeginWait()
        {
            System.Diagnostics.Debug.WriteLine("Adding 10 second sleep...");
            System.Threading.Thread.Sleep(10000);
            System.Diagnostics.Debug.WriteLine("Adding {0} millisecond sleep...", this.sleepTime);
            System.Threading.Thread.Sleep(this.sleepTime);
            System.Diagnostics.Debug.WriteLine("Waking all listeners");
            if (forObserver != null)
            {
                forObserver(e);
            }
        }
    }
    class Observer
    {
        public Observer()
        {
        }
        public void SubscribeToEvent(Subject s)
        {
            s.forObserver += new Subject.ObserverEvent(EventHandler);
        }
        private void EventHandler(EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Got the event handler");
        }
    }
 static void Main(string[] args)
        {
            Program pr = new Program();
            pr.UseSingleton();
            // Now use observer pattern
            // Create the observer
            Observer o1 = new Observer();
            Observer o2 = new Observer();
            // Creare the subject
            Subject s = new Subject();
           
            // Subscribe to event
            o1.SubscribeToEvent(s);
            o2.SubscribeToEvent(s);
            // Call back
            s.BeginWait();
        }

Following is the class diagram for the observer pattern: