@@ -6,6 +6,16 @@ A factory method
6
6
* handles object creation and
7
7
* encapsulates it in a subclass.
8
8
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
+
9
19
** Definition**
10
20
``` cs
11
21
// A facotry method
@@ -16,19 +26,9 @@ A factory method
16
26
protected abstract Pizza CreatePizza (PizzaType type );
17
27
```
18
28
19
- ![ Pizza Store with Factory Method ] ( /Diagrams/FactoryMethod.png )
29
+ ## Benefits
20
30
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
32
32
33
33
## Common Structure
34
34
@@ -45,3 +45,18 @@ A factory method
45
45
* overrides the abstract factory method to return an instance of a ConcreteProduct (eg. AmericanCheesePizza)
46
46
47
47
_ [ 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