Factory Method Pattern
This factory method pattern usually is used to create difference objects according to subclasses which inherit the same interface or abstract class. Factory method lets a class defer instantiate to a subclass.
The UML class diagram of factory method pattern is as follows:
As follow example:
First example: Structural. The structural code is follow. This example demonstrate the basic structure of implementing the factory method pattern.
// Factory Method pattern -- Structural example
using System;
using System.Collections;
namespace DoFactory.GangOfFour.Factory.Structural
{
// MainApp test application
class MainApp
{
static void Main()
{
// An array of creators
Creator[] creators = new Creator[2];
creators[0] = new ConcreteCreatorA();
creators[1] = new ConcreteCreatorB();
// Iterate over creators and create products
foreach(Creator creator in creators)
{
Product product = creator.FactoryMethod();
Console.WriteLine("Created {0}",
product.GetType().Name);
}
// Wait for user
Console.Read();
}
}
// "Product"
abstract class Product
{
}
// "ConcreteProductA"
class ConcreteProductA : Product
{
}
// "ConcreteProductB"
class ConcreteProductB : Product
{
}
// "Creator"
abstract class Creator
{
public abstract Product FactoryMethod();
}
// "ConcreteCreator"
class ConcreteCreatorA : Creator
{
public override Product FactoryMethod()
{
return new ConcreteProductA();
}
}
// "ConcreteCreator"
class ConcreteCreatorB : Creator
{
public override Product FactoryMethod()
{
return new ConcreteProductB();
}
}
}
Second Example: real-world. The example demonstrates factory method offers flexibility in creating difference document. The derived document class Report and Resume instantiate extended version of the Document class. Here, the factory method is called in the constructor of the Document class.
// Factory Method pattern -- Real World example
using System;
using System.Collections;
namespace DoFactory.GangOfFour.Factory.RealWorld
{
// MainApp test application
class MainApp
{
static void Main()
{
// Note: constructors call Factory Method
Document[] documents = new Document[2];
documents[0] = new Resume();
documents[1] = new Report();
// Display document pages
foreach (Document document in documents)
{
Console.WriteLine("/n" + document.GetType().Name+ "--");
foreach (Page page in document.Pages)
{
Console.WriteLine(" " + page.GetType().Name);
}
}
// Wait for user
Console.Read();
}
}
// "Product"
abstract class Page
{
}
// "ConcreteProduct"
class SkillsPage : Page
{
}
// "ConcreteProduct"
class EducationPage : Page
{
}
// "ConcreteProduct"
class ExperiencePage : Page
{
}
// "ConcreteProduct"
class IntroductionPage : Page
{
}
// "ConcreteProduct"
class ResultsPage : Page
{
}
// "ConcreteProduct"
class ConclusionPage : Page
{
}
// "ConcreteProduct"
class SummaryPage : Page
{
}
// "ConcreteProduct"
class BibliographyPage : Page
{
}
// "Creator"
abstract class Document
{
private ArrayList pages = new ArrayList();
// Constructor calls abstract Factory method
public Document()
{
this.CreatePages();
}
public ArrayList Pages
{
get{ return pages; }
}
// Factory Method
public abstract void CreatePages();
}
// "ConcreteCreator"
class Resume : Document
{
// Factory Method implementation
public override void CreatePages()
{
Pages.Add(new SkillsPage());
Pages.Add(new EducationPage());
Pages.Add(new ExperiencePage());
}
}
// "ConcreteCreator"
class Report : Document
{
// Factory Method implementation
public override void CreatePages()
{
Pages.Add(new IntroductionPage());
Pages.Add(new ResultsPage());
Pages.Add(new ConclusionPage());
Pages.Add(new SummaryPage());
Pages.Add(new BibliographyPage());
}
}
}