Search content within the blog

Saturday, May 7, 2011

Design Patterns - Abstract factory pattern

What Is An Abstract Factory

An abstract factory contains a set of methods that create families of objects. For example you might have a DogFactory class that has various methods for creating different kinds of dogs like CreatePoodle, CreateBeagle, CreateGoldenRetriever, and CreateChocolateLab.
Abstract Class Versus Interface

Before we get too deep into this pattern it is important to note that an abstract class that has only abstract methods is the same as an interface. The benefit of using an abstract class comes when you want to implement functionality in the base class so each derived class doesn’t need to repeat it.

Let’s say you have the following interface.

public interface ICalculatable {
int Add(int num1, int num2);
int Multiply(int num1, int num2);
}

This is a very trivial example but it will be fine for this illustration. If both those methods are going to be the same for each instance of the interface it seems silly to keep repeating the code over and over. It would make more sense to create an abstract class with two non abstract methods so you don’t have to write the code for each instance.

public abstract class Calculatable {
public int Add(int num1, int num2) {
return num1 + num2;
}

public int Multiply(int num1, int num2) {
return num1 * num2;
}
}

Then you could create multiple concreate classes that inherit from the Calculatable abstract class and not have to write any code for the Add and Multiply methods.
Program Requirements

We’ll be continuing on with our fictional city council that has commissioned us to develop an application that lets them manage their yearly dog registrations. Since the application is all about managing dogs and their owners it makes sense to have a DogFactory class that will create the different breeds of dogs that will be stored in the application.

Our application will have a factory that creates different breeds of dogs and each dog breed will inherit from the IDog interface.
The Pattern

First we’ll setup our abstract factory class that will serve as our contract or interface for a concrete factory class.

public abstract class AbstractDogFactory {
public abstract Poodle CreatePoodle();
public abstract Beagle CreateBeagle();
public abstract GoldenRetriever CreateGoldenRetriever();
public abstract ChocolateLab CreateChocolateLab();
}

Then we create a conrete class of our factory contract.
public class DogFactory : AbstractDogFactory {

public DogFactory() { }

public overrride Poodle CreatePoodle() {
return new Poodle();
}

public overrride Beagle CreateBeagle() {
return new Beagle();
}

public overrride GoldenRetriever CreateGoldenRetriever() {
return new GoldenRetriever();
}

public overrride ChocolateLab CreateChocolateLab() {
return new ChocolateLab();
}
}

Now we have our factory in place it is very simple to use. Obviously the above implementation is very basic but it illustrates the point.

AbstractDogFactory dogFactory = new DogFactory();
Poodle mittens = dogFactory.CreatePoodle();
mittens.Name = “Mittens”;

No comments:

Post a Comment