Skip to content

Commit 0d0746d

Browse files
committed
Documentation for AbstractFactory
1 parent 665dd40 commit 0d0746d

File tree

6 files changed

+88
-41
lines changed

6 files changed

+88
-41
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
language: csharp
2+
solution: DesignPatterns.sln

CreationalPatterns/AbstractFactory/Pizza.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void box()
3535
}
3636
}
3737

38-
// American Pizzas
38+
// Cheese Pizzas
3939
public class CheesePizza : Pizza{
4040
private string description = "Cheese Pizza";
4141
private PizzaIngredientFactory ingredientFactory;
Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,92 @@
1-
# Factory Method
1+
# Abstract Factory
22

3-
The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate.
3+
The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.
44

5-
A factory method
6-
* handles object creation and
7-
* encapsulates it in a subclass.
5+
**Definition** - AbstractFactory and ConcreteFactory
6+
```cs
7+
public interface PizzaIngredientFactory
8+
{
9+
Dough CreateDough();
10+
}
11+
12+
public class AmericanPizzaIngredientFactory : PizzaIngredientFactory
13+
{
14+
public Dough CreateDough()
15+
{
16+
return new ThickCrustDough();
17+
}
18+
}
19+
20+
public class ItalianPizzaIngredientFactory : PizzaIngredientFactory
21+
{
22+
public Dough CreateDough()
23+
{
24+
return new ThinCrustDough();
25+
}
26+
}
27+
```
828

9-
**Definition**
29+
**Definition** - AbstractProduct and ConcreteProduct
1030
```cs
11-
// A facotry method
12-
// 1. is abstract so the subclass are counted on to handle object creation.
13-
// 2. returns a Product.
14-
// 3. isolates the client from knowing what of concrete Product is actually created.
15-
// 4. may be parameterized (or not) to select among several variations of a product.
16-
protected abstract Pizza CreatePizza(PizzaType type);
31+
public interface Dough
32+
{
33+
string GetName();
34+
}
35+
36+
public class ThickCrustDough : Dough
37+
{
38+
public string GetName()
39+
{
40+
return "Extra thick crust dough";
41+
}
42+
}
43+
44+
public class ThinCrustDough : Dough
45+
{
46+
public string GetName()
47+
{
48+
return "Thin crust dough";
49+
}
50+
}
1751
```
1852

19-
![Pizza Store with Factory Method](/Diagrams/FactoryMethod.png)
53+
![Pizza Store with Factory Method](../../Diagrams/AbstractFactory.png)
2054

2155
**Usage**
2256
```cs
23-
PizzaStore americanStore = new AmericanPizzaStore();
24-
Pizza pizza = americanStore.OrderPizza(PizzaType.Veggie);
25-
Console.WriteLine("Esposito ordered a " + pizza.Name);
26-
27-
// Italian Pizza Store
28-
PizzaStore italianStore = new ItalianPizzaStore();
29-
Pizza pizza = italianStore.OrderPizza(PizzaType.Cheese);
30-
Console.WriteLine("Esposito ordered a " + pizza.Name);
57+
public class AmericanPizzaStore : PizzaStore
58+
{
59+
// AmericanPizzaStore creates the corresponding ingriedient factory.
60+
private PizzaIngredientFactory ingredientFactory = new AmericanPizzaIngredientFactory();
61+
62+
protected override Pizza CreatePizza(PizzaType type)
63+
{
64+
switch (type)
65+
{
66+
case PizzaType.Cheese:
67+
// the correct ingredientFactory is transferred when creating a pizza
68+
return new CheesePizza(ingredientFactory);
69+
case PizzaType.Veggie:
70+
return new VeggiePizza(ingredientFactory);
71+
default:
72+
throw new NotImplementedException();
73+
}
74+
}
75+
}
3176
```
3277

3378
## Common Structure
3479

35-
![Common structure of factory method pattern](http://www.dofactory.com/images/diagrams/net/factory.gif)
80+
![Common structure of abstract factoring pattern](https://upload.wikimedia.org/wikipedia/commons/thumb/9/9d/Abstract_factory_UML.svg/677px-Abstract_factory_UML.svg.png)
3681

37-
* Product (Pizza)
38-
* defines the interface of objects the factory method creates
39-
* ConcreteProduct (ItalianCheesePizza, AmericanCheesePizza etc.)
40-
* implements the Product interface
41-
* Creator (PizzaStore)
42-
* declares the abstract factory method which returns an instance of type Product.
43-
* may call the factory method to return an instance (OrderPizza calls CreatePizza)
44-
* ConcreteCreator(AmericanPizzaStore)
45-
* overrides the abstract factory method to return an instance of a ConcreteProduct (eg. AmericanCheesePizza)
82+
* AbstractFactory (PizzaIngredientFactory)
83+
* declares an interface for operations that create abstract products
84+
* ConcreteFactory (ItalianPizzaIngredientFactory, AmericanPizzaIngredientFactory)
85+
* implements the operations to create concrete product objects
86+
* AbstractProduct (Dough)
87+
* declares an interface for a type of product object
88+
* ConcreteProduct(ThinCrustDough, ThickCrustDough)
89+
* defines a product object to be created by the corresponding concrete factory
90+
* implements the AbstractProduct interface
4691

47-
_[Source: http://www.dofactory.com/net/factory-method-design-pattern]_
92+
_[Source: http://www.dofactory.com/net/abstract-factory-design-pattern]_

CreationalPatterns/doc/AbstractFactory.cd

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<ClassDiagram MajorVersion="1" MinorVersion="1">
33
<Class Name="CreationalPatterns.AbstractFactory.ItalianPizzaIngredientFactory">
4-
<Position X="4.5" Y="5.25" Width="2.25" />
4+
<Position X="4.5" Y="5.5" Width="2.25" />
55
<TypeIdentifier>
66
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAABAA=</HashCode>
77
<FileName>AbstractFactory\PizzaIngredientFactory.cs</FileName>
88
</TypeIdentifier>
99
<Lollipop Position="0.2" />
1010
</Class>
1111
<Class Name="CreationalPatterns.AbstractFactory.AmericanPizzaIngredientFactory">
12-
<Position X="7.25" Y="5.25" Width="2.5" />
12+
<Position X="7.25" Y="5.5" Width="2.5" />
1313
<TypeIdentifier>
1414
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAABAA=</HashCode>
1515
<FileName>AbstractFactory\PizzaIngredientFactory.cs</FileName>
@@ -47,15 +47,15 @@
4747
</ShowAsAssociation>
4848
</Class>
4949
<Class Name="CreationalPatterns.AbstractFactory.ThickCrustDough">
50-
<Position X="5" Y="12.25" Width="1.5" />
50+
<Position X="7.75" Y="7.5" Width="1.5" />
5151
<TypeIdentifier>
5252
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAABAA=</HashCode>
5353
<FileName>AbstractFactory\Dough.cs</FileName>
5454
</TypeIdentifier>
5555
<Lollipop Position="0.2" />
5656
</Class>
5757
<Class Name="CreationalPatterns.AbstractFactory.ThinCrustDough">
58-
<Position X="7.5" Y="12.25" Width="1.5" />
58+
<Position X="5.25" Y="7.5" Width="1.5" />
5959
<TypeIdentifier>
6060
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAABAA=</HashCode>
6161
<FileName>AbstractFactory\Dough.cs</FileName>
@@ -115,14 +115,14 @@
115115
</TypeIdentifier>
116116
</Interface>
117117
<Interface Name="CreationalPatterns.AbstractFactory.Dough">
118-
<Position X="6.5" Y="10.25" Width="1.5" />
118+
<Position X="6.5" Y="10" Width="1.5" />
119119
<TypeIdentifier>
120120
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAA=</HashCode>
121121
<FileName>AbstractFactory\Dough.cs</FileName>
122122
</TypeIdentifier>
123123
</Interface>
124124
<Enum Name="CreationalPatterns.FactoryMethod.PizzaType">
125-
<Position X="1.75" Y="12.5" Width="1.5" />
125+
<Position X="3.75" Y="11.25" Width="1.5" />
126126
<TypeIdentifier>
127127
<HashCode>AAAAgAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
128128
<FileName>FactoryMethod\Pizza.cs</FileName>

Diagrams/AbstractFactory.png

-3.19 KB
Loading

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ Design Patterns in C# / .NET
33

44
![Language C#](https://img.shields.io/badge/language-c%23-blue.svg)
55
![status in progress](https://img.shields.io/badge/status-in%20progress-brightgreen.svg)
6-
![number of patterns](https://img.shields.io/badge/patterns-4-red.svg)
6+
![number of patterns](https://img.shields.io/badge/patterns-5-red.svg)
77

88
## Creational Patterns
99

1010
| Pattern | SE1
1111
---|--- | ---
12-
| Abstract Factory
12+
:heavy_check_mark: | [Abstract Factory](/CreationalPatterns/AbstractFactory/)
1313
| Builder
1414
:heavy_check_mark: | [Factory Method](/CreationalPatterns/FactoryMethod/) | :warning:
1515
| Lazy Initialization

0 commit comments

Comments
 (0)