Created
April 19, 2016 03:20
-
-
Save rohit-lakhanpal/7bb9b4da4d18e7ef745088f985bee113 to your computer and use it in GitHub Desktop.
Patterns - Factory Pattern (Real World Example): Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace DesignPatterns.Factory.RealWorld | |
{ | |
/// <summary> | |
/// MainApp startup class for Real-World | |
/// Factory Method Design Pattern. | |
/// </summary> | |
class MainApp | |
{ | |
/// <summary> | |
/// Entry point into console application. | |
/// </summary> | |
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.ReadKey(); | |
} | |
} | |
/// <summary> | |
/// The 'Product' abstract class | |
/// </summary> | |
abstract class Page | |
{ | |
} | |
/// <summary> | |
/// A 'ConcreteProduct' class | |
/// </summary> | |
class SkillsPage : Page | |
{ | |
} | |
/// <summary> | |
/// A 'ConcreteProduct' class | |
/// </summary> | |
class EducationPage : Page | |
{ | |
} | |
/// <summary> | |
/// A 'ConcreteProduct' class | |
/// </summary> | |
class ExperiencePage : Page | |
{ | |
} | |
/// <summary> | |
/// A 'ConcreteProduct' class | |
/// </summary> | |
class IntroductionPage : Page | |
{ | |
} | |
/// <summary> | |
/// A 'ConcreteProduct' class | |
/// </summary> | |
class ResultsPage : Page | |
{ | |
} | |
/// <summary> | |
/// A 'ConcreteProduct' class | |
/// </summary> | |
class ConclusionPage : Page | |
{ | |
} | |
/// <summary> | |
/// A 'ConcreteProduct' class | |
/// </summary> | |
class SummaryPage : Page | |
{ | |
} | |
/// <summary> | |
/// A 'ConcreteProduct' class | |
/// </summary> | |
class BibliographyPage : Page | |
{ | |
} | |
/// <summary> | |
/// The 'Creator' abstract class | |
/// </summary> | |
abstract class Document | |
{ | |
private List<Page> _pages = new List<Page>(); | |
// Constructor calls abstract Factory method | |
public Document() | |
{ | |
this.CreatePages(); | |
} | |
public List<Page> Pages | |
{ | |
get { return _pages; } | |
} | |
// Factory Method | |
public abstract void CreatePages(); | |
} | |
/// <summary> | |
/// A 'ConcreteCreator' class | |
/// </summary> | |
class Resume : Document | |
{ | |
// Factory Method implementation | |
public override void CreatePages() | |
{ | |
Pages.Add(new SkillsPage()); | |
Pages.Add(new EducationPage()); | |
Pages.Add(new ExperiencePage()); | |
} | |
} | |
/// <summary> | |
/// A 'ConcreteCreator' class | |
/// </summary> | |
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()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment