Przeglądaj źródła

工厂模式

master
QYYEE 5 lat temu
rodzic
commit
508ac275bb

BIN
实践/后端/项目/imgs/工厂方法模式.png Wyświetl plik


BIN
实践/后端/项目/imgs/简单工厂.png Wyświetl plik


+ 156
- 0
实践/后端/项目/工厂模式.md Wyświetl plik

@@ -0,0 +1,156 @@
1
+>简单工厂
2
+- 简单/简易工厂并不是一个设计模式, 更多是一个编程习惯. 但是使用的非常广泛.
3
+- 例如披萨店的实例
4
+![](./imgs/简单工厂.png)
5
+
6
+> 用C#/.net core实现简单工厂
7
+- Pizza的父类
8
+```
9
+using System;
10
+using System.Collections.Generic;
11
+
12
+namespace SimpleFactory.Pizzas
13
+{
14
+    public abstract class Pizza
15
+    {
16
+        public string Name { get; protected set; }
17
+        public string Dough { get; protected set; }
18
+        public string Sauce { get; protected set; }
19
+        protected List<string> Toppings = new List<string>();
20
+
21
+        public void Prepare()
22
+        {
23
+            Console.WriteLine($"Preparing: {Name}");
24
+            Console.WriteLine($"Tossing: {Dough}");
25
+            Console.WriteLine($"Adding sauce: {Sauce}");
26
+            Console.WriteLine("Adding toppings: ");
27
+            Toppings.ForEach(x => Console.WriteLine($"  {x}"));
28
+        }
29
+
30
+        public void Bake()
31
+        {
32
+            Console.WriteLine("Bake for 25 minutes");
33
+        }
34
+
35
+        public void Cut()
36
+        {
37
+            Console.WriteLine("Cutting the pizza into diagnol slices");
38
+        }
39
+
40
+        public void Box()
41
+        {
42
+            Console.WriteLine("Placing pizza in official PizzaStore box......");
43
+        }
44
+    }
45
+}
46
+```
47
+- 各种Pizza
48
+```
49
+namespace SimpleFactory.Pizzas
50
+{
51
+    public class CheesePizza: Pizza
52
+    {
53
+        public CheesePizza()
54
+        {
55
+            Name = "Cheese Pizza";
56
+            Dough = "Think Dough";
57
+            Sauce = "Salad";
58
+            Toppings.Add("Grated Reggiano Cheese");
59
+        }
60
+    }
61
+}
62
+
63
+namespace SimpleFactory.Pizzas
64
+{
65
+    public class ClamPizza: Pizza
66
+    {
67
+        public ClamPizza()
68
+        {
69
+            Name = "Clam Pizza";
70
+            Sauce = "Tomato sauce";
71
+            Dough = "Soft dough";
72
+            Toppings.Add("Shrimp meat");
73
+        }
74
+    }
75
+}
76
+
77
+namespace SimpleFactory.Pizzas
78
+{
79
+    public class PepperoniPizza: Pizza
80
+    {
81
+        public PepperoniPizza()
82
+        {
83
+            Name = "Pepperoni Pizza";
84
+            Dough = "Thin dough";
85
+            Sauce = "Black pepper";
86
+            Toppings.Add("Beef Granules");
87
+            Toppings.Add("Niblet");
88
+        }
89
+    }
90
+}
91
+```
92
+- 简单工厂
93
+```
94
+using SimpleFactory.Pizzas;
95
+
96
+namespace SimpleFactory
97
+{
98
+    public class SimplePizzaFactory
99
+    {
100
+        public Pizza CreatePizza(string type)
101
+        {
102
+            Pizza pizza = null;
103
+            switch (type)
104
+            {
105
+                case "cheese":
106
+                    pizza = new CheesePizza();
107
+                    break;
108
+                case "pepperoni":
109
+                    pizza = new PepperoniPizza();
110
+                    break;
111
+                case "clam":
112
+                    pizza = new ClamPizza();
113
+                    break;
114
+            }
115
+
116
+            return pizza;
117
+        }
118
+    }
119
+}
120
+```
121
+- PizzaStore
122
+```
123
+using SimpleFactory.Pizzas;
124
+
125
+namespace SimpleFactory
126
+{
127
+    public class PizzaStore
128
+    {
129
+        private readonly SimplePizzaFactory _factory;
130
+
131
+        public PizzaStore(SimplePizzaFactory factory)
132
+        {
133
+            _factory = factory;
134
+        }
135
+
136
+        public Pizza OrderPizza(string type)
137
+        {
138
+            var pizza = _factory.CreatePizza(type);
139
+            pizza.Prepare();
140
+            pizza.Bake();
141
+            pizza.Cut();
142
+            pizza.Box();
143
+            return pizza;
144
+        }
145
+    }
146
+}
147
+```
148
+
149
+> 工厂方法模式
150
+
151
+- 工厂方法模式定义了一个创建对象的接口, 但是让子类来决定具体创建的是哪一个对象. 工厂方法让一个类延迟实例化, 直到子类的出现.
152
+- ![](./imgs/工厂方法模式.png)
153
+
154
+> 设计原则
155
+- DIP (Dependency Inversion Principle) 依赖反转原则.
156
+- 进一步解释就是: 高级别的组件不应该依赖于低级别的组件, 它们都应该依赖于抽线.

Ładowanie…
Anuluj
Zapisz