鼎鼎知识库
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

13.工厂模式.md 3.7KB

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