22
33The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate.
44
5- A factory method
6- * handles object creation and
7- * encapsulates it in a subclass.
8-
95## Problem
106
11- * Object should be created so that subclasses can redefine which class to instantiate.
12- * A class should defer instantiation to subclasses.
7+ * You should create a particular object (e.g. AmericanCheesePizza or ItalianVeggiePizza) depending on the type.
8+ * Creating a new object using ` new ` keyword and ` if-else ` conditionals in every place where you need is very inflexible.
139
1410## Solution
1511
16- * Define a separate operation ` factory method ` for creating object.
17- * Create an object by calling a factory method.
12+ * Define a separate operation ` factory method ` in the Creator/Factory class (e.g. AmericanPizzaFactory)
13+ * Move the constructor call ( ` new AmericanCheesePizza() ` ) inside the factory method.
1814
1915** Definition**
2016``` cs
2117 // A facotry method
2218 // 1. is abstract so the subclass are counted on to handle object creation.
2319 // 2. returns a Product.
24- // 3. isolates the client from knowing what of concrete Product is actually created.
20+ // 3. isolates the client from knowing which concrete Product is actually created.
2521 // 4. may be parameterized (or not) to select among several variations of a product.
2622 protected abstract Pizza CreatePizza (PizzaType type );
2723```
2824
29- ## Benefits
30-
31- ## Drawbacks
32-
3325## Common Structure
3426
35- ![ Common structure of factory method pattern] ( http://www.dofactory.com/images/diagrams/net /factory.gif)
27+ ![ Common structure of factory method pattern] ( img /factory.gif)
3628
3729* Product (Pizza)
3830 * defines the interface of objects the factory method creates
@@ -44,7 +36,22 @@ A factory method
4436* ConcreteCreator(AmericanPizzaStore)
4537 * overrides the abstract factory method to return an instance of a ConcreteProduct (eg. AmericanCheesePizza)
4638
47- _ [ Source: http://www.dofactory.com/net/factory-method-design-pattern ] _
39+ ## Collaborations
40+
41+ ![ Collaboration] ( img/collaboration.PNG )
42+ * Source: static.dzone.com*
43+
44+ * Creator/Factory relies on its subclasses to implement the factory method so that it returns an instance of the appropriate ConcreteProduct.
45+
46+ ## Benefits
47+
48+ * Avoids tight coupling between concrete products and code that uses them.
49+ * Simplifies code due to moving all creational code to one place.
50+ * Simplifies adding new products to the program.
51+
52+ ## Drawbacks
53+
54+ * Requires extra subclasses.
4855
4956## Example Usage
5057
@@ -59,4 +66,12 @@ _[Source: http://www.dofactory.com/net/factory-method-design-pattern]_
5966 PizzaStore italianStore = new ItalianPizzaStore ();
6067 Pizza pizza = italianStore .OrderPizza (PizzaType .Cheese );
6168 Console .WriteLine (" Esposito ordered a " + pizza .Name );
62- ```
69+ ```
70+
71+ ## Relations with Other Patterns
72+
73+ * ** Abstract Factory** is often implemented with Factory Methods.
74+
75+ * ** Templated Methods** - Factory methods are usually called within Template Methods.
76+
77+ * ** Prototype** doesn't require subclassing, but it does require an ` initialize() ` operation. On the other hand, Factory Method requires subclassing, but doesn't require initialization step.
0 commit comments