|
1 | | -# Factory Method |
| 1 | +# Abstract Factory |
2 | 2 |
|
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. |
4 | 4 |
|
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 | +``` |
8 | 28 |
|
9 | | -**Definition** |
| 29 | +**Definition** - AbstractProduct and ConcreteProduct |
10 | 30 | ```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 | + } |
17 | 51 | ``` |
18 | 52 |
|
19 | | - |
| 53 | + |
20 | 54 |
|
21 | 55 | **Usage** |
22 | 56 | ```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 | + } |
31 | 76 | ``` |
32 | 77 |
|
33 | 78 | ## Common Structure |
34 | 79 |
|
35 | | - |
| 80 | + |
36 | 81 |
|
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 |
46 | 91 |
|
47 | | -_[Source: http://www.dofactory.com/net/factory-method-design-pattern]_ |
| 92 | +_[Source: http://www.dofactory.com/net/abstract-factory-design-pattern]_ |
0 commit comments