Skip to content

Commit 85bb057

Browse files
committed
update factory method
1 parent 34dbd9c commit 85bb057

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

CreationalPatterns/FactoryMethod/README.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ A factory method
66
* handles object creation and
77
* encapsulates it in a subclass.
88

9+
## Problem
10+
11+
* Object should be created so that subclasses can redefine which class to instantiate.
12+
* A class should defer instantiation to subclasses.
13+
14+
## Solution
15+
16+
* Define a separate operation `factory method` for creating object.
17+
* Create an object by calling a factory method.
18+
919
**Definition**
1020
```cs
1121
// A facotry method
@@ -16,19 +26,9 @@ A factory method
1626
protected abstract Pizza CreatePizza(PizzaType type);
1727
```
1828

19-
![Pizza Store with Factory Method](/Diagrams/FactoryMethod.png)
29+
## Benefits
2030

21-
**Usage**
22-
```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);
31-
```
31+
## Drawbacks
3232

3333
## Common Structure
3434

@@ -45,3 +45,18 @@ A factory method
4545
* overrides the abstract factory method to return an instance of a ConcreteProduct (eg. AmericanCheesePizza)
4646

4747
_[Source: http://www.dofactory.com/net/factory-method-design-pattern]_
48+
49+
## Example Usage
50+
51+
![Pizza Store with Factory Method](/Diagrams/FactoryMethod.png)
52+
53+
```cs
54+
PizzaStore americanStore = new AmericanPizzaStore();
55+
Pizza pizza = americanStore.OrderPizza(PizzaType.Veggie);
56+
Console.WriteLine("Esposito ordered a " + pizza.Name);
57+
58+
// Italian Pizza Store
59+
PizzaStore italianStore = new ItalianPizzaStore();
60+
Pizza pizza = italianStore.OrderPizza(PizzaType.Cheese);
61+
Console.WriteLine("Esposito ordered a " + pizza.Name);
62+
```

0 commit comments

Comments
 (0)