Skip to content

Commit 7157d28

Browse files
committed
[Factory method] - Add implementation of factory method.
1 parent a02f66d commit 7157d28

File tree

6 files changed

+291
-16
lines changed

6 files changed

+291
-16
lines changed

Factory Method/Factory Method.csproj

+6-1
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@
4242
<Reference Include="System.Xml" />
4343
</ItemGroup>
4444
<ItemGroup>
45-
<Compile Include="Program.cs" />
45+
<Compile Include="Program.RealWorldCode.cs" />
46+
<Compile Include="Program.Structural.cs" />
4647
<Compile Include="Properties\AssemblyInfo.cs" />
4748
</ItemGroup>
4849
<ItemGroup>
4950
<None Include="App.config" />
5051
</ItemGroup>
52+
<ItemGroup>
53+
<Content Include="factory.gif" />
54+
<Content Include="Participants.txt" />
55+
</ItemGroup>
5156
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5257
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5358
Other similar extension points exist, see Microsoft.Common.targets.

Factory Method/Participants.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
The classes and objects participating in this pattern are:
2+
3+
Product (Page)
4+
defines the interface of objects the factory method creates
5+
6+
ConcreteProduct (SkillsPage, EducationPage, ExperiencePage)
7+
implements the Product interface
8+
9+
Creator (Document)
10+
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.
11+
may call the factory method to create a Product object.
12+
13+
ConcreteCreator (Report, Resume)
14+
overrides the factory method to return an instance of a ConcreteProduct.
+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
5+
/// <summary>
6+
/// Creates an instance of several derived classes.
7+
///
8+
/// Define an interface for creating an object, but let subclasses decide which class to instantiate.
9+
/// Factory Method lets a class defer instantiation to subclasses.
10+
///
11+
/// Frequency of use: 5 high.
12+
/// </summary>
13+
namespace DoFactory.GangOfFour.Factory.RealWorld
14+
{
15+
/// <summary>
16+
/// MainApp startup class for Real-World
17+
/// Factory Method Design Pattern.
18+
/// </summary>
19+
class MainApp
20+
{
21+
/// <summary>
22+
/// Entry point into console application.
23+
/// </summary>
24+
static void Main()
25+
{
26+
// Note: constructors call Factory Method
27+
Document[] documents = new Document[2];
28+
29+
documents[0] = new Resume();
30+
documents[1] = new Report();
31+
32+
// Display document pages
33+
foreach (Document document in documents)
34+
{
35+
Console.WriteLine("\n" + document.GetType().Name + "--");
36+
foreach (Page page in document.Pages)
37+
{
38+
Console.WriteLine(" " + page.GetType().Name);
39+
}
40+
}
41+
42+
// Wait for user
43+
Console.ReadKey();
44+
}
45+
}
46+
47+
/// <summary>
48+
/// The 'Product' abstract class
49+
/// </summary>
50+
abstract class Page
51+
{
52+
}
53+
54+
/// <summary>
55+
/// A 'ConcreteProduct' class
56+
/// </summary>
57+
class SkillsPage : Page
58+
{
59+
}
60+
61+
/// <summary>
62+
/// A 'ConcreteProduct' class
63+
/// </summary>
64+
class EducationPage : Page
65+
{
66+
}
67+
68+
/// <summary>
69+
/// A 'ConcreteProduct' class
70+
/// </summary>
71+
class ExperiencePage : Page
72+
{
73+
}
74+
75+
/// <summary>
76+
/// A 'ConcreteProduct' class
77+
/// </summary>
78+
class IntroductionPage : Page
79+
{
80+
}
81+
82+
/// <summary>
83+
/// A 'ConcreteProduct' class
84+
/// </summary>
85+
class ResultsPage : Page
86+
{
87+
}
88+
89+
/// <summary>
90+
/// A 'ConcreteProduct' class
91+
/// </summary>
92+
class ConclusionPage : Page
93+
{
94+
}
95+
96+
/// <summary>
97+
/// A 'ConcreteProduct' class
98+
/// </summary>
99+
class SummaryPage : Page
100+
{
101+
}
102+
103+
/// <summary>
104+
/// A 'ConcreteProduct' class
105+
/// </summary>
106+
class BibliographyPage : Page
107+
{
108+
}
109+
110+
/// <summary>
111+
/// The 'Creator' abstract class
112+
/// </summary>
113+
abstract class Document
114+
{
115+
private List<Page> _pages = new List<Page>();
116+
117+
// Constructor calls abstract Factory method
118+
public Document()
119+
{
120+
this.CreatePages();
121+
}
122+
123+
public List<Page> Pages
124+
{
125+
get { return _pages; }
126+
}
127+
128+
// Factory Method
129+
public abstract void CreatePages();
130+
}
131+
132+
/// <summary>
133+
/// A 'ConcreteCreator' class
134+
/// </summary>
135+
class Resume : Document
136+
{
137+
// Factory Method implementation
138+
public override void CreatePages()
139+
{
140+
Pages.Add(new SkillsPage());
141+
Pages.Add(new EducationPage());
142+
Pages.Add(new ExperiencePage());
143+
}
144+
}
145+
146+
/// <summary>
147+
/// A 'ConcreteCreator' class
148+
/// </summary>
149+
class Report : Document
150+
{
151+
// Factory Method implementation
152+
public override void CreatePages()
153+
{
154+
Pages.Add(new IntroductionPage());
155+
Pages.Add(new ResultsPage());
156+
Pages.Add(new ConclusionPage());
157+
Pages.Add(new SummaryPage());
158+
Pages.Add(new BibliographyPage());
159+
}
160+
}
161+
}
162+
163+
// Output:
164+
//Resume -------
165+
// SkillsPage
166+
// EducationPage
167+
// ExperiencePage
168+
169+
//Report -------
170+
// IntroductionPage
171+
// ResultsPage
172+
// ConclusionPage
173+
// SummaryPage
174+
// BibliographyPage

Factory Method/Program.Structural.cs

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
3+
/// <summary>
4+
/// Creates an instance of several derived classes.
5+
///
6+
/// Define an interface for creating an object, but let subclasses decide which class to instantiate.
7+
/// Factory Method lets a class defer instantiation to subclasses.
8+
///
9+
/// Frequency of use: 5 high.
10+
/// </summary>
11+
namespace DoFactory.GangOfFour.Factory.Structural
12+
{
13+
/// <summary>
14+
/// MainApp startup class for Structural
15+
/// Factory Method Design Pattern.
16+
/// </summary>
17+
class MainApp
18+
{
19+
/// <summary>
20+
/// Entry point into console application.
21+
/// </summary>
22+
static void Main()
23+
{
24+
// An array of creators
25+
Creator[] creators = new Creator[2];
26+
27+
creators[0] = new ConcreteCreatorA();
28+
creators[1] = new ConcreteCreatorB();
29+
30+
// Iterate over creators and create products
31+
foreach (Creator creator in creators)
32+
{
33+
Product product = creator.FactoryMethod();
34+
Console.WriteLine("Created {0}",
35+
product.GetType().Name);
36+
}
37+
38+
// Wait for user
39+
Console.ReadKey();
40+
}
41+
}
42+
43+
/// <summary>
44+
/// The 'Product' abstract class
45+
/// </summary>
46+
abstract class Product
47+
{
48+
}
49+
50+
/// <summary>
51+
/// A 'ConcreteProduct' class
52+
/// </summary>
53+
class ConcreteProductA : Product
54+
{
55+
}
56+
57+
/// <summary>
58+
/// A 'ConcreteProduct' class
59+
/// </summary>
60+
class ConcreteProductB : Product
61+
{
62+
}
63+
64+
/// <summary>
65+
/// The 'Creator' abstract class
66+
/// </summary>
67+
abstract class Creator
68+
{
69+
public abstract Product FactoryMethod();
70+
}
71+
72+
/// <summary>
73+
/// A 'ConcreteCreator' class
74+
/// </summary>
75+
class ConcreteCreatorA : Creator
76+
{
77+
public override Product FactoryMethod()
78+
{
79+
return new ConcreteProductA();
80+
}
81+
}
82+
83+
/// <summary>
84+
/// A 'ConcreteCreator' class
85+
/// </summary>
86+
class ConcreteCreatorB : Creator
87+
{
88+
public override Product FactoryMethod()
89+
{
90+
return new ConcreteProductB();
91+
}
92+
}
93+
}
94+
95+
//Outputter:
96+
//Created ConcreteProductA
97+
//Created ConcreteProductB

Factory Method/Program.cs

-15
This file was deleted.

Factory Method/factory.gif

5.47 KB
Loading

0 commit comments

Comments
 (0)