Factory Method .NET Design Pattern in C# and VB - Dofactory
Factory Method .NET Design Pattern in C# and VB - Dofactory
com
(/)
Factory Method
Denition Structural code in C#
UML diagram Real-world code in C#
Participants .NET Optimized code in C#
Denition
Dene an interface for creating an object, but let subclasses decide which class to
instantiate. Factory Method lets a class defer instantiation to subclasses.
Participants
http://www.dofactory.com/net/factory-method-design-pattern 1/10
24/08/2017 Factory Method .NET Design Pattern in C# and VB - dofactory.com
Participants
Product(Page)
denes 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 dene 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 oering great exibility in creating dierent objects.
The Abstract class may provide a default object, but each subclass can instantiate an extended version of the
object.
http://www.dofactory.com/net/factory-method-design-pattern 2/10
24/08/2017 Factory Method .NET Design Pattern in C# and VB - dofactory.com
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.
http://www.dofactory.com/net/factory-method-design-pattern 3/10
24/08/2017 Factory Method .NET Design Pattern in C# and VB - dofactory.com
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
Real-world code in C#
This real-world code demonstrates the Factory method oering exibility in creating dierent 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.
http://www.dofactory.com/net/factory-method-design-pattern 4/10
24/08/2017 Factory Method .NET Design Pattern in C# and VB - dofactory.com
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
http://www.dofactory.com/net/factory-method-design-pattern 5/10
24/08/2017 Factory Method .NET Design Pattern in C# and VB - dofactory.com
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.
http://www.dofactory.com/net/factory-method-design-pattern 6/10
24/08/2017 Factory Method .NET Design Pattern in C# and VB - dofactory.com
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
http://www.dofactory.com/net/factory-method-design-pattern 7/10
24/08/2017 Factory Method .NET Design Pattern in C# and VB - dofactory.com
SummaryPage
BibliographyPage
The .NET optimized code demonstrates the same real-world situation as above but uses modern, built-in .NET
features, such as, generics, reection, object initializers, automatic properties, etc. You can nd an example on
our Singleton (/net/singleton-design-pattern#net) pattern page.
All other patterns (and much more) are available in our .NET Design Pattern Framework 4.5.
Not only does the .NET Design Pattern Framework 4.5 cover GOF and Enterprise patterns, it
also includes .NET pattern architectures that reduce the code you need to write by up to 75%.
This unique package will change your .NET lifestyle -- for only $79. Here's what is included:
(/products/net-design-pattern-framework)
http://www.dofactory.com/net/factory-method-design-pattern 8/10
24/08/2017 Factory Method .NET Design Pattern in C# and VB - dofactory.com
Company
About Us (/about)
Our Story (/story)
Services (/services)
Training (/training)
Contact Us (/contact)
Privacy (/privacy)
Terms (/terms)
Licensing (/licensing)
Customers
Community
Questions (/topic/search.aspx)
Ask Question (/topic/add.aspx)
Explore (/topic/topics.aspx)
Tags (/tag/tags.aspx)
Users (/user/users.aspx)
Reference Guides
Our Products
http://www.dofactory.com/net/factory-method-design-pattern 9/10
24/08/2017 Factory Method .NET Design Pattern in C# and VB - dofactory.com
2017 - Data & Object Factory, LLC. dofactory.com. All rights reserved.
http://www.dofactory.com/net/factory-method-design-pattern 10/10