31 July, 2012

Factory Design Pattern


Factory design pattern implements the concept of real world factories. Factory pattern is a creational design pattern. It deals with creating object without specifying exact class. In general, actors of factory patterns are a client, a factory and a product. Client is an object that requires another object for some purposes. Rather than creating the product instance directly, the client delegates this responsibility to the factory. The factory then creates a new instance of the product, passing it back to the client. Figure shows the whole process.

 
Application
For an example, a banking application works with accounts. In this application there are different types of account like saving account and checking account. All accounts are derived from an abstract account name IAccount. The IAccount defines the withdraw, deposit and interest rate which must be implemented by the concrete accounts (saving account, checking account). If clients want to know the interest rate of the Saving Account. It just invoke the factory to create an instance of Saving account.  Being invoked factory, it creates an instance of the saving account and then client just get the interest rate by invoking interest method. The client uses the object as casted to the abstract class without being aware of the concrete object type. Over all implementation of this scenario is given below.

The advantage is in here that new account can be added without changing a single line of code in the client code. Here an object is created without exposing instantiation logic to the client. The object generation is centralized here.

Benefits:
  • Factory Pattern is the mostly used pattern.
  •  Decoupled the classes i.e eliminates the need to bind application specific class
  • The code only deals with the Interface.
  •  Factory Pattern provides the way to create multiple instances of classes.
  •  This provides a hook so that we can derive a sub-class to create different controls to display the data.
  •   Factory method connects the class hierarchies with minimum coupling.
  •  Product implementation may change over time but client remains unchanged.

Implementation
Step 1: Create an Interface - IAccount
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryPattern
{
    public interface IAccount
    {
        string Withdraw(int amount);
        string Deposit(int amount);

        double InterestRate();
    }
} 

Step 2: Create concrete classes

Saving account
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryPattern
{
    /// 
    /// Concrete class SavingsAccount
    /// 
    public class SavingsAccount : IAccount
    {
        #region IAccount Members

        public string Withdraw(int amount)
        {
            throw new NotImplementedException();
        }

        public string Deposit(int amount)
        {
            throw new NotImplementedException();
        }

        public double InterestRate()
        {
            return 12.5;
        }

        #endregion
    }
}


Checking Account


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryPattern
{

    /// 
    /// Concrete class CheckingAccount
    /// 
    public class CheckingAccount : IAccount
    {
        #region IAccount Members

        public string Withdraw(int amount)
        {
            throw new NotImplementedException();
        }

        public string Deposit(int amount)
        {
            throw new NotImplementedException();
        }

        public double InterestRate()
        {
            return 10.24;
        }

        #endregion
    }
} 


Step 3: Create a Factory Object Enum
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryPattern
{
    /// 
    /// FactoryObject Enum to configure object
    /// 
    public enum FactoryObject
    {
        SavingAccount,
        CheckingAccount
    }
}

Step 4: Create a factory class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryPattern
{
    /// 
    /// Factory class to create object
    /// 
    public static class Factory
    {
        public static IAccount CreateObject(FactoryObject factoryObject)
        {
            IAccount objIAccount = null;

            switch (factoryObject)
            {
                case FactoryObject.SavingAccount:
                    objIAccount = new SavingsAccount();
                    break;

                case FactoryObject.CheckingAccount:
                    objIAccount = new CheckingAccount();
                    break;

                default:
                    break;
            }

            return objIAccount;
        }
    }
}


Step 5: Access from client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryPattern
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //Create object by factory pattern
            IAccount objSavingAccount = Factory.CreateObject(FactoryObject.SavingAccount);
            IAccount objCheckingAccount = Factory.CreateObject(FactoryObject.CheckingAccount);

            //Access object
            Console.WriteLine("Saving Account Interest Rate: " + objSavingAccount.InterestRate());
            Console.WriteLine("Checking Account Interest Rate: " + objCheckingAccount.InterestRate());


            Console.ReadLine();
            
        }
    }
}

28 July, 2012

Singleton Design Pattern

Design Pattern
 Design pattern is a solution of known problems. These are strategies of solving commonly occurring problems. A design pattern is not a finish design. It is like a template to solve a problem.  

Singleton Design Pattern
Singleton is a software design pattern. It is restrict to create object more than once. This is actually needed when one object can perform its action in whole system. We frequently use a database connection object to connect with database. We don’t need multiple objects to create connection with database and close it. We can use singleton in this scenario.


Implementation of Singleton by C#

Step 1: Create EmployeeInfo class
This class is to hold employee information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Singleton
{
    public class EmployeeInfo
    {
        public string EmpName { get; set; }
        public string EmpDesignation { get; set; }
        public int MonthlySalary { get; set; }
    }
}


Step 2: Create a singleton class
 Here EmployeeService is a singleton class. Constructor of this class is private so that nobody can create its instance from outside.  Instance() is a static method which creates instance of the singleton class. It actually forces that only one instance of the object will be created. Lock() is used to create instance of the singleton pattern in thread safe manner in multi threaded environment. The other two methods are used to add employee information in list and get employee salary. These two are as usual method. Don’t mix up Singleton class and Static class. Keep in mind; in static class everything must be static like Method, constructor, properties. But in singleton class it is not required. Hope, you will be clear after the following example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Singleton
{
    /// 
    /// EmployeeService is a singleton class
    /// 
    public class EmployeeService
    {

        //Static object of singleton class
        private static EmployeeService instance;

        private List lstEmployeeInfo = null;

        /// 
        /// Restrict to create object of Singleton class
        /// 
        private EmployeeService()
        {
            if (lstEmployeeInfo == null)
            {
                lstEmployeeInfo = new List();
            }
        }


        /// 
        /// The static method to provide global access to the singleton object.
        /// 
        /// Singleton object of EmployeeService class
        public static EmployeeService Instance()
        {
            
            if (instance == null)
            {
                //Thread safe singleton

                lock (typeof(EmployeeService))
                {
                    instance = new EmployeeService();
                }
            }

            return instance;
        }


        /// 
        /// Add employee information to the Employee information list
        /// 
        /// 

        public void AddEmployeeInfo(EmployeeInfo objEmployeeInfo)
        {
            lstEmployeeInfo.Add(objEmployeeInfo);
        }


        /// 
        /// Get Salary by Name
        /// 
        /// 
        /// Salary of Employee
        
        public int GetEmployeeSalaryByName(string name)
        {
            int monthlySalary = 0;
            foreach (EmployeeInfo objEmployeeInfo in lstEmployeeInfo)
            {
                if (objEmployeeInfo.EmpName.Contains(name))
                    monthlySalary = objEmployeeInfo.MonthlySalary;
            }
            return monthlySalary;
        }

    }
}




Step 3: Access singleton class
This class creates an instance of singleton class by EmployeeService objEmployeeService = EmployeeService.Instance(); and access singleton class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Singleton
{
    class Program
    {
        static void Main(string[] args)
        {
            EmployeeInfo objEmpInfo1 = new EmployeeInfo() { EmpName = "Mahedee", EmpDesignation = "Senior Software Engineer", MonthlySalary = 00000 };
            EmployeeInfo objEmpInfo2 = new EmployeeInfo() { EmpName = "Kamal", EmpDesignation = "Software Engineer", MonthlySalary = 30000 };


            //Create a singleton object
            EmployeeService objEmployeeService = EmployeeService.Instance();
            

            objEmployeeService.AddEmployeeInfo(objEmpInfo1);
            objEmployeeService.AddEmployeeInfo(objEmpInfo2);

            Console.WriteLine(objEmpInfo2.EmpName + " : " + objEmployeeService.GetEmployeeSalaryByName("Kamal"));

            Console.ReadLine();

        }
    }
}