Tuesday, 7 June 2011

Factory Method Design Pattern


The Factory Method pattern is way of creating objects, but letting subclasses decided exactly which class to instantiate. All factory method encapsulates creation of object. Creator class creates the object of the product based on values supplied by the client.
In this example participant are:
Product Interface and product classes.
IColorProduct
RedColore
GreenColor
BlueColor

Creator Class
ColorCreator
UML Diagram 
FactoryDesignPattern
 
Code Explanation
I created IColorProduct Interface. This interface implemented by 3 classes RedColor, GreenColor and BlueColor. Based on passed values by client, Creator class (ColorCreator) creates different classes.
Code Sample
   1: interface IColorProduct
   2:     {
   3:         Color GetColor();
   4:     }
   5:  
   6:     public class RedColor : IColorProduct
   7:     {
   8:  
   9:         public Color GetColor()
  10:         {
  11:             return Color.Red;
  12:         }
  13:     }
  14:  
  15:     public class GreenColor : IColorProduct
  16:     {
  17:         public Color GetColor()
  18:         {
  19:             return Color.Green;
  20:         }
  21:     }
  22:     public class BlueColor : IColorProduct
  23:     {
  24:         public Color GetColor()
  25:         {
  26:             return Color.Blue;
  27:         }
  28:     }
  29:  
  30: class ColorCreator
  31:    {
  32:        IColorProduct colorProduct;
  33:        public Color  GetColorFactory(string  ColorCode)
  34:        {
  35:            
  36:  
  37:            switch (ColorCode)
  38:            {
  39:                case "rdoBlue":
  40:                    colorProduct = new BlueColor();
  41:                    break;
  42:                case "rdoRed":
  43:                    colorProduct = new RedColor();
  44:                    break;
  45:                case "rdoGreen":
  46:                    colorProduct = new GreenColor();
  47:                    break;
  48:                default:
  49:                    colorProduct = new GreenColor();
  50:                    break;
  51:  
  52:            }
  53:            return colorProduct.GetColor(); 
  54:        }
  55:    }
  56: private void rdo_CheckedChanged(object sender, EventArgs e)
  57:         {
  58:             RadioButton radioButton = (RadioButton)sender;
  59:             string strSelected="";
  60:             if(radioButton.Checked==true)
  61:                  strSelected = radioButton.Name;
  62:             ColorCreator creator = new ColorCreator();
  63:             panel1.BackColor = creator.GetColorFactory(strSelected);
  64:  
  65:         }
  66: //For three radio button only one event handler
  67: this.rdoBlue.CheckedChanged += new System.EventHandler(this.rdo_CheckedChanged);
  68: this.rdoRed.CheckedChanged += new System.EventHandler(this.rdo_CheckedChanged);
  69: this.rdoGreen.CheckedChanged += new System.EventHandler(this.rdo_CheckedChanged);

Achievement

  1. By using factory method we centralized creation of the objects in one location.
  2. Client who ever using factory method responsibility is only passing values. Based on values different color objects are returning. Client no need to know internal logic of creation of object.
  3. In future if we add new color class there is no impact on client code.
  4. Hinding object creation logic inside Factory class.

 


No comments:

Post a Comment

Post a Comment