Factory Method Pattern In C#


Definition

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

 

Frequency of use:
High

 


 

UML class diagram

 

 


 

Participants

 

The classes and objects participating in this pattern are:

  • Product  (Page)
    • defines the interface of objects the factory method creates
  • ConcreteProduct  (SkillsPage, EducationPage, ExperiencePage)
    • implements the Product interface
  • Creator  (Document)
    • declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.
    • may call the factory method to create a Product object.
  • ConcreteCreator  (Report, Resume)
    • overrides the factory method to return an instance of a ConcreteProduct.

 


 

Structural code in C#

 

This structural code demonstrates the Factory method offering great flexibility in creating different objects. The Abstract class may provide a default object, but each subclass can instantiate an extended version of the object.

static void Main()
{
Creator[] creators = new Creator[2];
creators[0] = new ConcreateCreatorA();
creators[1] = new ConcreateCreatorB();
foreach (Creator item in creators)
{
Product product = item.FactoryMethod();
Console.WriteLine(“Created {0}”,
product.GetType().Name);
}

Console.WriteLine(” Main factory method”);
Console.ReadKey();
}

abstract class Product
{

}

class ConcreateProductA : Product
{

}
class ConcreateProductB : Product
{

}

abstract class Creator
{
public abstract Product FactoryMethod();

}

class ConcreateCreatorA : Creator
{
public override Product FactoryMethod()
{
return new ConcreateProductA();
}
}

class ConcreateCreatorB : Creator
{
public override Product FactoryMethod()
{
return new ConcreateProductB();
}
}

Output
Created ConcreteProductA
Created ConcreteProductB

 


 

Real-world code in C#

 

This real-world code demonstrates the Factory method offering flexibility in creating different documents. The derived Document classes Report and Resume instantiate extended versions of the Document class. Here, the Factory Method is called in the constructor of the Document base class.

static void Main()
{
Document[] docs = new Document[2];
docs[0] = new Resume();
docs[1] = new Report();
foreach (Document item in docs)
{
foreach (Page page in item.Pages)
{
Console.WriteLine(” Page :” + page.GetType().Name);
}
}

Console.WriteLine(” Main factory method”);
Console.ReadKey();
}
abstract class Page
{

}
class EducationPage : Page
{

}
class SkillPage : Page
{

}
class ExperiencePage : Page
{

}
class IntroductionPage : Page
{

}
class ResultPage : Page
{

}
class ConclusionPage : Page
{

}
class SumaryPage : Page
{

}
class BibliographyPage : Page
{

}
abstract class Document
{
private List<Page> _pages = new List<Page>();
public Document()
{
this.CreatePage();
}
public abstract void CreatePage();
public List<Page> Pages
{
get { return this._pages; }
}
}
class Resume : Document {
public override void CreatePage()
{
this.Pages.Add(new SkillPage());
this.Pages.Add(new EducationPage());
this.Pages.Add(new ExperiencePage());
}
}
class Report : Document
{
public override void CreatePage()
{
this.Pages.Add(new IntroductionPage());
this.Pages.Add(new ResultPage());
this.Pages.Add(new ConclusionPage());
}
}

 

Output
SkillsPage
EducationPage
ExperiencePage
IntroductionPage
ResultsPage
ConclusionPage
SummaryPage
BibliographyPage
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s