Skip to content

Commit 1892e11

Browse files
committed
Factory Pattern added
1 parent 81afb04 commit 1892e11

9 files changed

Lines changed: 289 additions & 3 deletions

File tree

CreationalPatterns/CreationalPatterns.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,19 @@
4343
<Reference Include="System.Xml" />
4444
</ItemGroup>
4545
<ItemGroup>
46+
<Compile Include="FactoryMethod\Pizza.cs" />
47+
<Compile Include="FactoryMethod\PizzaStore.cs" />
48+
<Compile Include="FactoryMethod\TestPizzaFactoryMethod.cs" />
4649
<Compile Include="Program.cs" />
4750
<Compile Include="Properties\AssemblyInfo.cs" />
4851
<Compile Include="Singleton\ChocolateBoiler_StaticInit.cs" />
4952
<Compile Include="Singleton\ChocolateBoiler_ThreadSafe.cs" />
5053
</ItemGroup>
5154
<ItemGroup>
5255
<None Include="App.config" />
56+
<None Include="doc\FactoryMethod.cd" />
5357
<None Include="doc\Singleton.cd" />
58+
<None Include="FactoryMethod\README.md" />
5459
</ItemGroup>
5560
<ItemGroup>
5661
<Folder Include="AbstractFactory\" />
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CreationalPatterns.FactoryMethod
8+
{
9+
public enum PizzaType
10+
{
11+
Cheese,
12+
Veggie
13+
};
14+
15+
public class Pizza
16+
{
17+
public string Name { get; set; }
18+
public string Dough { get; set; }
19+
20+
public void Prepare()
21+
{
22+
Console.WriteLine("Preparing " + Name);
23+
Console.WriteLine("Tossing dough..." + Dough);
24+
}
25+
26+
public void Bake()
27+
{
28+
Console.WriteLine("Bake for 15 minutes");
29+
}
30+
31+
public void cut()
32+
{
33+
Console.WriteLine("Cutting the pizza into 8 slices");
34+
}
35+
36+
public void box()
37+
{
38+
Console.WriteLine("Pack the pizza in a box");
39+
}
40+
}
41+
42+
// American Pizzas
43+
public class AmericanCheesePizza : Pizza{
44+
public AmericanCheesePizza()
45+
{
46+
this.Name = "American Style Cheese Pizza";
47+
this.Dough = "Extra thick crust dough";
48+
}
49+
}
50+
51+
public class AmericanVeggiePizza : Pizza
52+
{
53+
public AmericanVeggiePizza()
54+
{
55+
this.Name = "American Vegetarian Pizza";
56+
this.Dough = "Extra thick crust dough";
57+
}
58+
}
59+
60+
// Italian Pizzas
61+
public class ItalianCheesePizza : Pizza
62+
{
63+
public ItalianCheesePizza()
64+
{
65+
this.Name = "Italian Style Cheese Pizza";
66+
this.Dough = "Thin crust dough";
67+
}
68+
}
69+
70+
public class ItalianVeggiePizza : Pizza
71+
{
72+
public ItalianVeggiePizza()
73+
{
74+
this.Name = "Italian Vegetarian Pizza";
75+
this.Dough = "Thin crust dough";
76+
}
77+
}
78+
79+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CreationalPatterns.FactoryMethod
8+
{
9+
10+
public abstract class PizzaStore
11+
{
12+
public Pizza OrderPizza(PizzaType type)
13+
{
14+
// calls the factory method to create pizza
15+
Pizza pizza = CreatePizza(type);
16+
17+
pizza.Prepare();
18+
pizza.Bake();
19+
pizza.cut();
20+
pizza.box();
21+
22+
return pizza;
23+
}
24+
25+
// A facotry method
26+
// 1. is abstract so the subclass are counted on to handle object creation.
27+
// 2. returns a Product.
28+
// 3. isolates the client from knowing what of concrete Product is actually created.
29+
// 4. may be parameterized (or not) to select among several variations of a product.
30+
protected abstract Pizza CreatePizza(PizzaType type);
31+
}
32+
33+
public class AmericanPizzaStore : PizzaStore
34+
{
35+
protected override Pizza CreatePizza(PizzaType type)
36+
{
37+
switch (type)
38+
{
39+
case PizzaType.Cheese:
40+
return new AmericanCheesePizza();
41+
case PizzaType.Veggie:
42+
return new AmericanVeggiePizza();
43+
default:
44+
throw new NotImplementedException();
45+
}
46+
}
47+
}
48+
49+
public class ItalianPizzaStore : PizzaStore
50+
{
51+
protected override Pizza CreatePizza(PizzaType type)
52+
{
53+
switch (type)
54+
{
55+
case PizzaType.Cheese:
56+
return new ItalianCheesePizza();
57+
case PizzaType.Veggie:
58+
return new ItalianVeggiePizza();
59+
default:
60+
throw new NotImplementedException();
61+
}
62+
}
63+
}
64+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Factory Method
2+
3+
The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate.
4+
5+
A factory method
6+
* handles object creation and
7+
* encapsulates it in a subclass.
8+
9+
**Usage**
10+
```cs
11+
abstract Product factoryMethod(String type);
12+
```
13+
14+
15+
![Duck App with Strategy Pattern](/Diagrams/Strategy.png)
16+
17+
**Usage**
18+
```cs
19+
Duck mallard = new MallardDuck();
20+
mallard.PerformQuack();
21+
mallard.PerformFly();
22+
23+
// change the flying behavior dynamically
24+
Duck model = new ModelDuck();
25+
model.PerformFly(); // default behavior
26+
model.FlyBehavior = new FlyRocketPowered(); // set a different flying behavior at runtime
27+
model.PerformFly();
28+
```
29+
30+
## Common Structure
31+
32+
![Common structure of strategy pattern](http://www.dofactory.com/images/diagrams/net/strategy.gif)
33+
34+
* Strategy (SortStrategy)
35+
* declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy
36+
* ConcreteStrategy (QuickSort, ShellSort, MergeSort)
37+
* implements the algorithm using the Strategy interface
38+
* Context (SortedList)
39+
* is configured with a ConcreteStrategy object
40+
* maintains a reference to a Strategy object
41+
* may define an interface that lets Strategy access its data.
42+
43+
_[Source: http://www.dofactory.com/net/strategy-design-pattern]_
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CreationalPatterns.FactoryMethod
8+
{
9+
public class TestPizzaFactoryMethod
10+
{
11+
public static void run()
12+
{
13+
PizzaStore americanStore = new AmericanPizzaStore();
14+
Pizza pizza = americanStore.OrderPizza(PizzaType.Veggie);
15+
Console.WriteLine("Esposito ordered a " + pizza.Name);
16+
Console.ReadLine();
17+
}
18+
}
19+
}

CreationalPatterns/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using CreationalPatterns.FactoryMethod;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -10,7 +11,7 @@ class Program
1011
{
1112
static void Main(string[] args)
1213
{
13-
14+
TestPizzaFactoryMethod.run();
1415
}
1516
}
1617
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ClassDiagram MajorVersion="1" MinorVersion="1">
3+
<Class Name="CreationalPatterns.FactoryMethod.PizzaStore">
4+
<Position X="1.75" Y="1" Width="1.5" />
5+
<TypeIdentifier>
6+
<HashCode>AAAAAAAAAAAAAAAAEAAAAAAAAAAEAAAAAAAAAAAAAAA=</HashCode>
7+
<FileName>FactoryMethod\PizzaStore.cs</FileName>
8+
</TypeIdentifier>
9+
</Class>
10+
<Class Name="CreationalPatterns.FactoryMethod.AmericanPizzaStore">
11+
<Position X="2.75" Y="2.75" Width="1.75" />
12+
<TypeIdentifier>
13+
<HashCode>AAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
14+
<FileName>FactoryMethod\PizzaStore.cs</FileName>
15+
</TypeIdentifier>
16+
</Class>
17+
<Class Name="CreationalPatterns.FactoryMethod.ItalianPizzaStore">
18+
<Position X="0.5" Y="2.75" Width="1.75" />
19+
<TypeIdentifier>
20+
<HashCode>AAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
21+
<FileName>FactoryMethod\PizzaStore.cs</FileName>
22+
</TypeIdentifier>
23+
</Class>
24+
<Class Name="CreationalPatterns.FactoryMethod.Pizza">
25+
<Position X="4" Y="4.5" Width="1.5" />
26+
<TypeIdentifier>
27+
<HashCode>AAAAAAAEAAAAQAAAAAAAAgQAAAAAAAAAAAAAAAAAQQA=</HashCode>
28+
<FileName>FactoryMethod\Pizza.cs</FileName>
29+
</TypeIdentifier>
30+
</Class>
31+
<Class Name="CreationalPatterns.FactoryMethod.AmericanCheesePizza">
32+
<Position X="2.75" Y="7.25" Width="1.5" />
33+
<TypeIdentifier>
34+
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
35+
<FileName>FactoryMethod\Pizza.cs</FileName>
36+
</TypeIdentifier>
37+
</Class>
38+
<Class Name="CreationalPatterns.FactoryMethod.AmericanVeggiePizza">
39+
<Position X="7.5" Y="7.25" Width="1.5" />
40+
<TypeIdentifier>
41+
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
42+
<FileName>FactoryMethod\Pizza.cs</FileName>
43+
</TypeIdentifier>
44+
</Class>
45+
<Class Name="CreationalPatterns.FactoryMethod.ItalianCheesePizza">
46+
<Position X="0.5" Y="7.25" Width="1.5" />
47+
<TypeIdentifier>
48+
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
49+
<FileName>FactoryMethod\Pizza.cs</FileName>
50+
</TypeIdentifier>
51+
</Class>
52+
<Class Name="CreationalPatterns.FactoryMethod.ItalianVeggiePizza">
53+
<Position X="5.25" Y="7.25" Width="1.5" />
54+
<TypeIdentifier>
55+
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
56+
<FileName>FactoryMethod\Pizza.cs</FileName>
57+
</TypeIdentifier>
58+
</Class>
59+
<Enum Name="CreationalPatterns.FactoryMethod.PizzaType">
60+
<Position X="6.75" Y="4.5" Width="1.5" />
61+
<TypeIdentifier>
62+
<HashCode>AAAAgAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
63+
<FileName>FactoryMethod\Pizza.cs</FileName>
64+
</TypeIdentifier>
65+
</Enum>
66+
<Font Name="Segoe UI" Size="9" />
67+
</ClassDiagram>

DesignPrinciples.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,12 @@
44

55
* Program to an interface, not an implementation.
66

7-
* Favor composition over inheritance.
7+
* Favor composition over inheritance.
8+
9+
* Designs should be open for extension but closed for modification.
10+
11+
* _Dependency Inversion Principle_: Depend upon abstractions. Do not depend upon concrete classes.
12+
** No variable should hold a reference to a concrete class. (eg. using _new_.) --> Use a factory to get around that.
13+
** No class should derive from a concrete class. --> Derive from an interface or an abstract class
14+
** No method should override an implemented method of any of its base classes.
15+

Diagrams/FactoryMethod.png

48.4 KB
Loading

0 commit comments

Comments
 (0)