Factory Method
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.
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.
2.
3. using System;
4.
5. namespace DoFactory.GangOfFour.Factory.Structural
6. {
7. /// <summary>
8. /// MainApp startup class for Structural
9. /// Factory Method Design Pattern.
10. /// </summary>
11. class MainApp
12. {
13. /// <summary>
14. /// Entry point into console application.
15. /// </summary>
16. static void Main()
17. {
18. // An array of creators
19. Creator[] creators = new Creator[2];
20.
21. creators[0] = new ConcreteCreatorA();
22. creators[1] = new ConcreteCreatorB();
23.
24. // Iterate over creators and create products
25. foreach (Creator creator in creators)
26. {
27. Product product = creator.FactoryMethod();
28. Console.WriteLine("Created {0}",
29. product.GetType().Name);
30. }
31.
32. // Wait for user
33. Console.ReadKey();
34. }
35. }
36.
37. /// <summary>
38. /// The 'Product' abstract class
39. /// </summary>
40. abstract class Product
41. {
42. }
43.
44. /// <summary>
45. /// A 'ConcreteProduct' class
46. /// </summary>
47. class ConcreteProductA : Product
48. {
49. }
50.
51. /// <summary>
52. /// A 'ConcreteProduct' class
53. /// </summary>
54. class ConcreteProductB : Product
55. {
56. }
57.
58. /// <summary>
59. /// The 'Creator' abstract class
60. /// </summary>
61. abstract class Creator
62. {
63. public abstract Product FactoryMethod();
64. }
65.
66. /// <summary>
67. /// A 'ConcreteCreator' class
68. /// </summary>
69. class ConcreteCreatorA : Creator
70. {
71. public override Product FactoryMethod()
72. {
73. return new ConcreteProductA();
74. }
75. }
76.
77. /// <summary>
78. /// A 'ConcreteCreator' class
79. /// </summary>
80. class ConcreteCreatorB : Creator
81. {
82. public override Product FactoryMethod()
83. {
84. return new ConcreteProductB();
85. }
86. }
87. }
88.
89.
90.
91.
92.
Output
Created ConcreteProductA
Created ConcreteProductB
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.
2.
3. using System;
4. using System.Collections.Generic;
5.
6. namespace DoFactory.GangOfFour.Factory.RealWorld
7. {
8. /// <summary>
9. /// MainApp startup class for Real-World
10. /// Factory Method Design Pattern.
11. /// </summary>
12. class MainApp
13. {
14. /// <summary>
15. /// Entry point into console application.
16. /// </summary>
17. static void Main()
18. {
19. // Note: constructors call Factory Method
20. Document[] documents = new Document[2];
21.
22. documents[0] = new Resume();
23. documents[1] = new Report();
24.
25. // Display document pages
26. foreach (Document document in documents)
27. {
28. Console.WriteLine("\n" + document.GetType().Name + "--");
29. foreach (Page page in document.Pages)
30. {
31. Console.WriteLine(" " + page.GetType().Name);
32. }
33. }
34.
35. // Wait for user
36. Console.ReadKey();
37. }
38. }
39.
40. /// <summary>
41. /// The 'Product' abstract class
42. /// </summary>
43. abstract class Page
44. {
45. }
46.
47. /// <summary>
48. /// A 'ConcreteProduct' class
49. /// </summary>
50. class SkillsPage : Page
51. {
52. }
53.
54. /// <summary>
55. /// A 'ConcreteProduct' class
56. /// </summary>
57. class EducationPage : Page
58. {
59. }
60.
61. /// <summary>
62. /// A 'ConcreteProduct' class
63. /// </summary>
64. class ExperiencePage : Page
65. {
66. }
67.
68. /// <summary>
69. /// A 'ConcreteProduct' class
70. /// </summary>
71. class IntroductionPage : Page
72. {
73. }
74.
75. /// <summary>
76. /// A 'ConcreteProduct' class
77. /// </summary>
78. class ResultsPage : Page
79. {
80. }
81.
82. /// <summary>
83. /// A 'ConcreteProduct' class
84. /// </summary>
85. class ConclusionPage : Page
86. {
87. }
88.
89. /// <summary>
90. /// A 'ConcreteProduct' class
91. /// </summary>
92. class SummaryPage : Page
93. {
94. }
95.
96. /// <summary>
97. /// A 'ConcreteProduct' class
98. /// </summary>
99. class BibliographyPage : Page
100.
{
101.
}
102.
103.
/// <summary>
104.
/// The 'Creator' abstract class
105.
/// </summary>
106.
abstract class Document
107.
{
108.
private List<Page> _pages = new List<Page>();
109.
110.
// Constructor calls abstract Factory
method
111.
public Document()
112.
{
113.
this.CreatePages();
114.
}
115.
116.
public List<Page> Pages
117.
{
118.
get { return _pages; }
119.
}
120.
121.
// Factory Method
122.
public abstract void CreatePages();
123.
}
124.
125.
/// <summary>
126.
/// A 'ConcreteCreator' class
127.
/// </summary>
128.
class Resume : Document
129.
{
130.
// Factory Method implementation
131.
public override void CreatePages()
132.
{
133.
Pages.Add(new SkillsPage());
134.
Pages.Add(new EducationPage());
135.
Pages.Add(new ExperiencePage());
136.
}
137.
}
138.
139.
/// <summary>
140.
/// A 'ConcreteCreator' class
141.
/// </summary>
142.
class Report : Document
143.
{
144.
// Factory Method implementation
145.
public override void CreatePages()
146.
{
147.
Pages.Add(new IntroductionPage());
148.
Pages.Add(new ResultsPage());
149.
Pages.Add(new ConclusionPage());
150.
Pages.Add(new SummaryPage());
151.
Pages.Add(new BibliographyPage());
152.
}
153.
}
154.
}
155.
156.
157.
158.
Output
Resume -------
SkillsPage
EducationPage
ExperiencePage
Report -------
IntroductionPage
ResultsPage
ConclusionPage
SummaryPage
BibliographyPage
SkillsPage
EducationPage
ExperiencePage
Report -------
IntroductionPage
ResultsPage
ConclusionPage
SummaryPage
BibliographyPage

No comments:
Post a Comment