Gang of Four (GOF) Design Patterns
page 2 of 8
by John Spano
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 48807/ 116

Implementing the Factory Pattern in .NET

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.

A class cannot anticipate the class of the object it must create.

A class wants its subclasses to specify the objects it creates.

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:

Listing 5

public interface IDog
{
void Bark();
void Scratch();
}

We just define a couple of simple methods for our dogs to do.

For the two actual concrete dog classes, we define a bulldog and poodle class:

Listing 6

public class CPoodle: IDog
{
  public CPoodle()
  {
    Console.WriteLine("CreatingPoodle");
  }
  public void Bark()
  {
    Console.WriteLine("Yip Yip");
  }
  public void Scratch()
  {
    Console.WriteLine("ScratchScratch");
  }
}
 
public class CBullDog: IDog
{
  public CBullDog()
  {
    Console.WriteLine("CreatingBulldog");
  }
  public void Bark()
  {
    Console.WriteLine("Wooof Wooof");
  }
  public void Scratch()
  {
    Console.WriteLine("Scratch SlobberScratch");
  }
}

Our factory class is static and contains one method to return the correct dog.

Listing 7

public class CDogFactory
{
  public enum DogType
  {
    Poodle, Bulldog
  } static CDogFactory(){}
  public static IDog CreateDog(DogTypeTypeOfDog)
  {
    switch (TypeOfDog)
    {
      case DogType.Bulldog:
        return new CBullDog();
      case DogType.Poodle:
        return new CPoodle();
      default:
        throw newArgumentException("Invalid Dog Type!");
    }
  }
}

We make the class static so we do not need an instance of the class.

To test the class, I have created a simple function.  Our test function returns the dogs and uses them.  In a more real world application, the type of dog would have been determined by the user or through program logic.

Listing 8

IDog dog1;
IDog dog2;
 
dog1 = CDogFactory.CreateDog(CDogFactory.DogType.Bulldog);
dog2 =CDogFactory.CreateDog(CDogFactory.DogType.Poodle);
 
dog1.Bark();
dog1.Scratch();
 
dog2.Bark();
dog2.Scratch();

View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-09-15 10:33:18 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search