Register | Login 
View Article  

Current Articles | Categories | Search | Syndication

Implementing the Factory Pattern in .Net

By John Spano on Tuesday, February 21, 2006 :: 944 Views :: 0 Comments :: Design Patterns

Level: Beginner + to Object Oriented Programming; Beginner + with .Net and C#
 
            The factory design pattern is very simple.  Several other patterns build off of it though, so it is a common base pattern.  You use this pattern when one or more of the following are true:
 
1.      A class can’t anticipate the class of object it much create.
2.      A class wants its subclasses to specify the objects it creates.
3.      Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.[GOF]
 
The class is easy to implement and consists of an identifier, either named constants or an enum and a switch statement.  For our example we will be creating dog objects.  As with any good OO design we start with an interface for our related objects.
 
The IDog interface
 
[code]
public interface IDog
{
      void Bark();
void Scratch();
}
[/code]
 
We just define a couple of simple methods for our dogs to do.
 
            Now for the two actual concrete dog classes.  We define a bulldog and poodle class:
[code]
public class CPoodle : IDog
      {
            public CPoodle()
            {
                  Console.WriteLine("Creating Poodle");
            }
            public void Bark()
            {
                  Console.WriteLine("Yip Yip");
            }
            public void Scratch()
            {
                  Console.WriteLine("Scratch Scratch");
            }
      }
 
public class CBullDog : IDog
      {
            public CBullDog()
            {
                  Console.WriteLine("Creating Bulldog");
            }
            public void Bark()
            {
                  Console.WriteLine("Wooof Wooof");
            }
            public void Scratch()
            {
                  Console.WriteLine("Scratch Slobber Scratch");
            }
      }
[/code]
 
            Now for our factory class.  It’s static and contains one method to return the correct dog.
 
[code]
      public class CDogFactory
      {
            public enum DogType
            {
                  Poodle,Bulldog
            }
            static CDogFactory()
            {
            }
            public static IDog CreateDog(DogType TypeOfDog)
            {
                  switch (TypeOfDog)
                  {
                        case DogType.Bulldog:
                              return new CBullDog();
                        case DogType.Poodle:
                              return new CPoodle();
                        default:
                              throw new ArgumentException("Invalid Dog Type!");
                  }
            }
      }
[/code]
We make the class static so we don’t need an instance of the class.
 
      To test the class, I have created a simple function.  Our test function just return the dogs and uses them.  In a more real world app, the type of dog would have been determined by the user or through program logic.
 
[code]
            IDog dog1;
            IDog dog2;
 
            dog1 = CDogFactory.CreateDog(CDogFactory.DogType.Bulldog);
            dog2 = CDogFactory.CreateDog(CDogFactory.DogType.Poodle);
 
            dog1.Bark();
            dog1.Scratch();
 
            dog2.Bark();
            dog2.Scratch();
[/code]

Previous Page | Next Page

COMMENTS

Currently, there are no comments. Be the first to post one!
Click here to post a comment

Copyright (c) 2008 GSP Developers
Walling Info Systems | Terms Of Use | Privacy Statement