[Design Pattern] Factory Method

Unity3D/C# 2015. 9. 2. 15:07
반응형

http://www.dofactory.com/net/factory-method-design-pattern


Factory Method


 Definition
 UML diagram
 Participants
 Structural code in C#
 Real-world code in C#
 .NET Optimized code 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.

  1. using System;

  2.  

  3. namespace DoFactory.GangOfFour.Factory.Structural

  4. {

  5.   /// <summary>

  6.   /// MainApp startup class for Structural

  7.   /// Factory Method Design Pattern.

  8.   /// </summary>

  9.   class MainApp

  10.   {

  11.     /// <summary>

  12.     /// Entry point into console application.

  13.     /// </summary>

  14.     static void Main()

  15.     {

  16.       // An array of creators

  17.       Creator[] creators = new Creator[2];

  18.  

  19.       creators[0] = new ConcreteCreatorA();

  20.       creators[1] = new ConcreteCreatorB();

  21.  

  22.       // Iterate over creators and create products

  23.       foreach (Creator creator in creators)

  24.       {

  25.         Product product = creator.FactoryMethod();

  26.         Console.WriteLine("Created {0}",

  27.           product.GetType().Name);

  28.       }

  29.  

  30.       // Wait for user

  31.       Console.ReadKey();

  32.     }

  33.   }

  34.  

  35.   /// <summary>

  36.   /// The 'Product' abstract class

  37.   /// </summary>

  38.   abstract class Product

  39.   {

  40.   }

  41.  

  42.   /// <summary>

  43.   /// A 'ConcreteProduct' class

  44.   /// </summary>

  45.   class ConcreteProductA : Product

  46.   {

  47.   }

  48.  

  49.   /// <summary>

  50.   /// A 'ConcreteProduct' class

  51.   /// </summary>

  52.   class ConcreteProductB : Product

  53.   {

  54.   }

  55.  

  56.   /// <summary>

  57.   /// The 'Creator' abstract class

  58.   /// </summary>

  59.   abstract class Creator

  60.   {

  61.     public abstract Product FactoryMethod();

  62.   }

  63.  

  64.   /// <summary>

  65.   /// A 'ConcreteCreator' class

  66.   /// </summary>

  67.   class ConcreteCreatorA : Creator

  68.   {

  69.     public override Product FactoryMethod()

  70.     {

  71.       return new ConcreteProductA();

  72.     }

  73.   }

  74.  

  75.   /// <summary>

  76.   /// A 'ConcreteCreator' class

  77.   /// </summary>

  78.   class ConcreteCreatorB : Creator

  79.   {

  80.     public override Product FactoryMethod()

  81.     {

  82.       return new ConcreteProductB();

  83.     }

  84.   }

  85. }

  86.  
  87.  
  88.  


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.

  1. using System;

  2. using System.Collections.Generic;

  3.  

  4. namespace DoFactory.GangOfFour.Factory.RealWorld

  5. {

  6.   /// <summary>

  7.   /// MainApp startup class for Real-World

  8.   /// Factory Method Design Pattern.

  9.   /// </summary>

  10.   class MainApp

  11.   {

  12.     /// <summary>

  13.     /// Entry point into console application.

  14.     /// </summary>

  15.     static void Main()

  16.     {

  17.       // Note: constructors call Factory Method

  18.       Document[] documents = new Document[2];

  19.  

  20.       documents[0] = new Resume();

  21.       documents[1] = new Report();

  22.  

  23.       // Display document pages

  24.       foreach (Document document in documents)

  25.       {

  26.         Console.WriteLine("\n" + document.GetType().Name + "--");

  27.         foreach (Page page in document.Pages)

  28.         {

  29.           Console.WriteLine(" " + page.GetType().Name);

  30.         }

  31.       }

  32.  

  33.       // Wait for user

  34.       Console.ReadKey();

  35.     }

  36.   }

  37.  

  38.   /// <summary>

  39.   /// The 'Product' abstract class

  40.   /// </summary>

  41.   abstract class Page

  42.   {

  43.   }

  44.  

  45.   /// <summary>

  46.   /// A 'ConcreteProduct' class

  47.   /// </summary>

  48.   class SkillsPage : Page

  49.   {

  50.   }

  51.  

  52.   /// <summary>

  53.   /// A 'ConcreteProduct' class

  54.   /// </summary>

  55.   class EducationPage : Page

  56.   {

  57.   }

  58.  

  59.   /// <summary>

  60.   /// A 'ConcreteProduct' class

  61.   /// </summary>

  62.   class ExperiencePage : Page

  63.   {

  64.   }

  65.  

  66.   /// <summary>

  67.   /// A 'ConcreteProduct' class

  68.   /// </summary>

  69.   class IntroductionPage : Page

  70.   {

  71.   }

  72.  

  73.   /// <summary>

  74.   /// A 'ConcreteProduct' class

  75.   /// </summary>

  76.   class ResultsPage : Page

  77.   {

  78.   }

  79.  

  80.   /// <summary>

  81.   /// A 'ConcreteProduct' class

  82.   /// </summary>

  83.   class ConclusionPage : Page

  84.   {

  85.   }

  86.  

  87.   /// <summary>

  88.   /// A 'ConcreteProduct' class

  89.   /// </summary>

  90.   class SummaryPage : Page

  91.   {

  92.   }

  93.  

  94.   /// <summary>

  95.   /// A 'ConcreteProduct' class

  96.   /// </summary>

  97.   class BibliographyPage : Page

  98.   {

  99.   }

  100.  

  101.   /// <summary>

  102.   /// The 'Creator' abstract class

  103.   /// </summary>

  104.   abstract class Document

  105.   {

  106.     private List<Page> _pages = new List<Page>();

  107.  

  108.     // Constructor calls abstract Factory method

  109.     public Document()

  110.     {

  111.       this.CreatePages();

  112.     }

  113.  

  114.     public List<Page> Pages

  115.     {

  116.       get { return _pages; }

  117.     }

  118.  

  119.     // Factory Method

  120.     public abstract void CreatePages();

  121.   }

  122.  

  123.   /// <summary>

  124.   /// A 'ConcreteCreator' class

  125.   /// </summary>

  126.   class Resume : Document

  127.   {

  128.     // Factory Method implementation

  129.     public override void CreatePages()

  130.     {

  131.       Pages.Add(new SkillsPage());

  132.       Pages.Add(new EducationPage());

  133.       Pages.Add(new ExperiencePage());

  134.     }

  135.   }

  136.  

  137.   /// <summary>

  138.   /// A 'ConcreteCreator' class

  139.   /// </summary>

  140.   class Report : Document

  141.   {

  142.     // Factory Method implementation

  143.     public override void CreatePages()

  144.     {

  145.       Pages.Add(new IntroductionPage());

  146.       Pages.Add(new ResultsPage());

  147.       Pages.Add(new ConclusionPage());

  148.       Pages.Add(new SummaryPage());

  149.       Pages.Add(new BibliographyPage());

  150.     }

  151.   }

  152. }

  153.  
  154.  


Output
Resume -------
 SkillsPage
 EducationPage
 ExperiencePage

Report -------
 IntroductionPage
 ResultsPage
 ConclusionPage
 SummaryPage
 BibliographyPage



반응형
: