Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns For Dummies. The Factory Method Pattern

Posted in Design Patterns
This post has been read 3436 times

In continuation of my posts on Creational design patterns, I will be discussing about the Factory Method pattern in this post. You can read about the other patterns from the following links.

  1. Prototype Pattern

You can read about Structural Patterns from here.

The Factory Method pattern deals with creation of objects in which the subclasses decide which class's object has to be created. Various subclasses may implement the same interface, but the Factory Method creates the appropriate object based on some supplied information.

    1 public class FactoryPattern

    2 {

    3     public interface IWorkLocation

    4     {

    5         string WorkingFrom();

    6     }

    7 

    8     public class Developer : Person, IWorkLocation

    9     {

   10         public Developer( string name, string designation )

   11         {

   12             this.Name = name;

   13             this.Designation = designation;

   14         }

   15 

   16         public string WorkingFrom()

   17         {

   18             return "Name : " + this.Name + ", Work Location :  Home";

   19         }

   20     }

   21 

   22     public class Tester : Person, IWorkLocation

   23     {

   24         public Tester( string name, string designation )

   25         {

   26             this.Name = name;

   27             this.Designation = designation;

   28         }

   29 

   30         public string WorkingFrom()

   31         {

   32             return "Name : " + this.Name + ", Work Location :  Office";

   33         }

   34     }

   35 

   36     public class Person : IWorkLocation

   37     {

   38         public string Designation { get; set; }

   39         public string Name { get; set; }

   40 

   41         public Person()

   42         {

   43             Name = "Base Person";

   44             Designation = "";

   45         }

   46 

   47         public Person( string name, string designation )

   48         {

   49             Designation = designation;

   50             Name = name;

   51         }

   52 

   53         public string WorkingFrom()

   54         {

   55             return "Name : " + this.Name + ", Work Location :  Not Available";

   56         }

   57     }

   58 

   59     //Creator

   60     public class Creator

   61     {

   62         public IWorkLocation FactoryMethod( Person per )

   63         {

   64             if ( per.Designation.Equals( "Developer" ) )

   65                 return new Developer( per.Name, per.Designation );

   66             else if ( per.Designation.Equals( "Tester" ) )

   67                 return new Tester( per.Name, per.Designation );

   68             else

   69                 return new Person();

   70         }

   71     }

   72 

   73     static void Main()

   74     {

   75         Creator crtr = new Creator();

   76         var person = crtr.FactoryMethod( new Person( "Person1", "Developer" ) );

   77         Console.WriteLine( person.WorkingFrom() );

   78         //Output: Name : Person1, Work Location :  Home

   79 

   80         person = crtr.FactoryMethod( new Person( "Person2", "Tester" ) );

   81         Console.WriteLine( person.WorkingFrom() );

   82         //Output: Name : Person2, Work Location :  Office

   83 

   84         person = crtr.FactoryMethod( new Person() );

   85         Console.WriteLine( person.WorkingFrom() );

   86         //Output:  Name : Base Person, Work Location :  Not Available

   87 

   88         Console.ReadLine();

   89     }

   90 }

As seen in the above example, the instantiation of objects is handled by the FactoryMethod of the Creator class. In this method, the information being passed helps the method in deciding which object to create. We must use the Factory method pattern when we want the flexibility of creating objects based on some criteria or the objects can be extended in subclasses.

In my next post I will talking about the Singleton pattern.




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll