Wednesday, April 13, 2016

Factory design pattern in .NET

Design patterns provide standard and efficient solution to complex problems. We can develop robust software applications and code can be reused. All design patterns are used to solve some specific problems. Each should be used carefully while designing the software applications.
    These design patterns developed based on the problems faced by many software designers and developers. We can create our own design patterns also. There are 3 categories of design patterns creational, behavioural and structural.
                   Factory method comes under creational design patterns. It is used to solve the problem of creating multiple objects without specifying exact class. Interfaces are used for most of design patterns. For factory design pattern also interfaces are used. Factory design pattern, Factory method design pattern and Abstract factory design patterns are different, even though name looks same.
Example:
1. Create a windows form application from Visual studio.
File->New-> Project
The window will be opened. Select windows form application. Provide some name like FactoryDesignPattern.
image
2. Drag combo box from toolbox and name it cbProduct. Drag and Drop 3 labels for select product, price and price value label (60000 mention here).
image
3. Code behind
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FactoryPatternTest
{
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
         
         private void Form1_Load(object sender, EventArgs e)
         {
             cbProducts.Items.Add(“SwiftDzire”);
             cbProducts.Items.Add(“Alto800”);
             cbProducts.Items.Add(“IndigoCS”);
         }
        //Interface for Factory pattern         

          public interface ICar
         {
             long GetPrice();
         }
        
//SwiftDzire Class implements ICar interface
         public class SwiftDzire : ICar
          {
          //Set price of SwiftDzire car
           public long GetPrice()
             {
                 return 600000;
             }
         }
        
 //Alto800 class implement ICar        
        
          public class Alto800 : ICar
         {
             public long GetPrice()
             {
                 return 350000;
             }
         }
      //IndigoCS class implement ICar
        public class IndigoCS : ICar
         {
            public long GetPrice()
             {
                 return 70000;
             }
         }
     //Factory class to create object         
         public class CarFactory
         {
         //Method which returns object
            public ICar CreateCarObject(int carIndex)
             {
                 try
                 {
                     switch (carIndex)
                     {
                         case 0: return (new SwiftDzire());
                         case 1: return (new Alto800());
                         case 3: return (new IndigoCS());
                         default:
                             return null;
                    }
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                     return null;
                 }
                
             }
             
         }
          private void cbProducts_SelectedIndexChanged(object sender, EventArgs e)
         {
             //Create object of class CarFactory
             CarFactory objFactory = new CarFactory();
             ICar car = objFactory.CreateCarObject(cbProducts.SelectedIndex);
             lblPriceValue.Text = car.GetPrice().ToString();
        }
    }
}
4. I have taken example of Car factory to explain this. The above mention is car factory application. Customers are selecting car to know the price of car.
5. ICar  is interface used here. It has a method GetPrice(). It is common method for all cars. SwiftDezire, Alto800 and IndigoCS are car class here. All these classes has implemented ICar interface. Multiple cars can implement this interface which has to show price.
6. CarFactory is factory method. It has a method CreateCarObject which has assign duty to creating object based on selection of objects.
7. Create object of CarFactory in selected index change event of combobox. Create object ofCarFactory. Select car from combo box. Selected index is passed to CreateCarObject and object is create and assign to ICar interface. Method GetPrice is called to get price of selected Car.
CarFactory objFactory = new CarFactory();
ICar car = objFactory.CreateCarObject(cbProducts.SelectedIndex);
lblPriceValue.Text = car.GetPrice().ToString();

Write a program to reverse a string? using System; namespace ConsoleApp1 {     class Program     {         static void Main(string[] args)  ...