Categories
Uncategorized

Design Patterns – creational – factory pattern

Factory design pattern:

As the name suggests, this mode is used to produce the object. In object-oriented design patterns, all things are objects, if the object is created using new, will have a strong coupling to the object, we need to join to replace the object, then the object using the object need to be modified, which is obviously contrary to the open closed principle (OCP). If we use the factory to produce objects, we only need to deal with this plant on it, without caring about the specific object, the purpose of decoupling.

Next we proceed from actual case, never factories to distinct plant.

Pizzeria to order a pizza, the first kind of pizza a lot (CheesePizza, GreekPizza, DurianPizza {etc), pizza production process there prepare, bake, cut, box.

The traditional model:

   

 1 internal class Program
 2 {
 3     private static void Main(string[] args)
 4     {
 5         new OrderPizza();
 6     }
 7 }
 8 
 9 internal class OrderPizza
10 {
11     public OrderPizza()
12     {
13         Pizza pizza = null;
14         string orderType = "";
15         do
16         {
17             orderType = Console.ReadLine();
18             if (orderType == "cheese")
19             {
20                 pizza = new CheesePizza();
21                 pizza.setName("

Cheese Pizza

"); 22 } 23 else if (orderType == "greek") 24 { 25 pizza = new GreekPizza(); 26 pizza.setName("

Greek Pizza

"); 27 } 28 else 29 { 30 Console.WriteLine("

Order failed

"); 31 break; 32 } 33 //

Began making

34 pizza.prepare(); 35 pizza.bake(); 36 pizza.cut(); 37 pizza.box(); 38 } while (true); 39 } 40 } 41 42 internal abstract class Pizza 43 { 44 private string name; 45 46 public abstract void prepare(); 47 48 public void bake() 49 { 50 Console.WriteLine($"

{This.name} baking

"); 51 } 52 53 public void cut() 54 { 55 Console.WriteLine($"

{This.name} pruning

"); 56 } 57 58 public void box() 59 { 60 Console.WriteLine($"

{This.name} package

"); 61 } 62 63 public void setName(string name) 64 { 65 this.name = name; 66 } 67 } 68 69 internal class CheesePizza : Pizza 70 { 71 public override void prepare() 72 { 73 Console.WriteLine("

Cheese pizza preparation

"); 74 } 75 } 76 77 internal class GreekPizza : Pizza 78 { 79 public override void prepare() 80 { 81 Console.WriteLine("

Greek pizza preparation

"); 82 } 83 }

view code

The advantages and disadvantages of the traditional model:

1, is better understood, easy to operate

2, OCP violation of the principle that open for extension, but closed for modification.

3, where only ordered two pizza, if now a new breed DurianPizza, this time you need to add the class, and modify OrderPizza code. As can be seen from the figure above, OrderPizza rely on abstract and concrete implementation class, then how can we cut OrderPizza and dependencies between classes yet. So we thought, we can define a factory, order pizza does not need to know the specific production process, as long as I need until what type of pizza to order on the line.

 

Simple factory pattern:

   

  1 internal class Program
  2 {
  3     private static void Main(string[] args)
  4     {
  5         new OrderPizza();
  6     }
  7 }
  8 
  9 internal class OrderPizza
 10 {
 11     public OrderPizza()
 12     {
 13         Pizza pizza = null;
 14         string orderType = "";
 15         do
 16         {
 17             Console.Write("

Enter the Order type:

"); 18 orderType = Console.ReadLine(); 19 pizza = SimpleFactory.createPizza(orderType); 20 if (pizza == null) 21 { 22 Console.WriteLine("

Order failed

"); 23 break; 24 } 25 //

Began making

26 pizza.prepare(); 27 pizza.bake(); 28 pizza.cut(); 29 pizza.box(); 30 } while (true); 31 } 32 } 33 34 internal static class SimpleFactory 35 { 36 public static Pizza createPizza(string orderType) 37 { 38 Pizza pizza = null; 39 do 40 { 41 if (orderType == "cheese") 42 { 43 pizza = new CheesePizza(); 44 pizza.setName("

Cheese Pizza

"); 45 } 46 else if (orderType == "greek") 47 { 48 pizza = new GreekPizza(); 49 pizza.setName("

Greek Pizza

"); 50 } 51 else if (orderType == "durian") 52 { 53 pizza = new DurianPizza(); 54 pizza.setName("

Durian Pizza

"); 55 } 56 return pizza; 57 } while (true); 58 } 59 } 60 61 internal abstract class Pizza 62 { 63 private string name; 64 65 public abstract void prepare(); 66 67 public void bake() 68 { 69 Console.WriteLine($"

{This.name} baking

"); 70 } 71 72 public void cut() 73 { 74 Console.WriteLine($"

{This.name} pruning

"); 75 } 76 77 public void box() 78 { 79 Console.WriteLine($"

{This.name} package

"); 80 } 81 82 public void setName(string name) 83 { 84 this.name = name; 85 } 86 } 87 88 internal class CheesePizza : Pizza 89 { 90 public override void prepare() 91 { 92 Console.WriteLine("

Cheese pizza preparation

"); 93 } 94 } 95 96 internal class GreekPizza : Pizza 97 { 98 public override void prepare() 99 { 100 Console.WriteLine("

Greek pizza preparation

"); 101 } 102 } 103 104 internal class DurianPizza : Pizza 105 { 106 public override void prepare() 107 { 108 Console.WriteLine("

Durian pizza preparation

"); 109 } 110 }

view code

Simple factory pattern advantages and disadvantages:

1, it can be seen by the code, while reducing code changes due to demand caused by a simple factory pattern to some extent changed, but actually still contrary to the principle of OCP.

2, a simple factory pattern is only suitable for relatively small objects product, and the product fixed demand, the vagaries of demand for the product is obviously not suitable.

 

Factory Method design pattern:

Pizza project requirements change, you can order pizza tastes different when the customer point of pizza.

   

  1 internal class Program
  2 {
  3     private static void Main(string[] args)
  4     {
  5         new BJOrderPizza();
  6     }
  7 }
  8 
  9 internal abstract class OrderPizza
 10 {
 11     public OrderPizza()
 12     {
 13         Pizza pizza = null;
 14         string orderType = "";
 15         do
 16         {
 17             Console.Write("

Enter the Order type:

"); 18 orderType = Console.ReadLine(); 19 pizza = createPizza(orderType); 20 if (pizza == null) 21 { 22 Console.WriteLine("

Order failed

"); 23 break; 24 } 25 //

Began making

26 pizza.prepare(); 27 pizza.bake(); 28 pizza.cut(); 29 pizza.box(); 30 } while (true); 31 } 32 33 public abstract Pizza createPizza(string orderType); 34 } 35 36 internal class BJOrderPizza : OrderPizza 37 { 38 public override Pizza createPizza(string orderType) 39 { 40 Pizza pizza = null; 41 if (orderType == "cheese") 42 { 43 pizza = new BJCheesePizza(); 44 } 45 else if (orderType == "greek") 46 { 47 pizza = new BJGreekPizza(); 48 } 49 return pizza; 50 } 51 } 52 53 internal class LDOrderPizza : OrderPizza 54 { 55 public override Pizza createPizza(string orderType) 56 { 57 Pizza pizza = null; 58 if (orderType == "cheese") 59 { 60 pizza = new LDCheesePizza(); 61 } 62 else if (orderType == "greek") 63 { 64 pizza = new LDGreekPizza(); 65 } 66 return pizza; 67 } 68 } 69 70 internal abstract class Pizza 71 { 72 private string name; 73 74 public abstract void prepare(); 75 76 public void bake() 77 { 78 Console.WriteLine($"

{This.name} baking

"); 79 } 80 81 public void cut() 82 { 83 Console.WriteLine($"

{This.name} pruning

"); 84 } 85 86 public void box() 87 { 88 Console.WriteLine($"

{This.name} package

"); 89 } 90 91 public void setName(string name) 92 { 93 this.name = name; 94 } 95 } 96 97 internal class BJCheesePizza : Pizza 98 { 99 public override void prepare() 100 { 101 Console.WriteLine("

Beijing cheese pizza preparation

"); 102 } 103 } 104 105 internal class BJGreekPizza : Pizza 106 { 107 public override void prepare() 108 { 109 Console.WriteLine("

Greek pizza ready in Beijing

"); 110 } 111 } 112 113 internal class LDCheesePizza : Pizza 114 { 115 public override void prepare() 116 { 117 Console.WriteLine("

London cheese pizza preparation

"); 118 } 119 } 120 121 internal class LDGreekPizza : Pizza 122 { 123 public override void prepare() 124 { 125 Console.WriteLine("

Greek pizza ready to London

"); 126 } 127 }

view code

Factory method pattern of strengths and weaknesses:

1, so that the parent implementation delay to subclasses to reduce the judgment.

2, a new name, and simple factory pattern similar products are generally applicable to relatively small objects, and a fixed product demand.

3, factory methods to alleviate to some extent the responsibility of the plant will refine responsibilities, avoid factory class can not function properly and cause the program to crash.

Reference: https: //www.jianshu.com/p/38493eb4ffbd

Leave a Reply