|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- >简单工厂
- - 简单/简易工厂并不是一个设计模式, 更多是一个编程习惯. 但是使用的非常广泛.
- - 例如披萨店的实例
- ![](./imgs/简单工厂.png)
-
- > 用C#/.net core实现简单工厂
- - Pizza的父类
- ```
- using System;
- using System.Collections.Generic;
-
- namespace SimpleFactory.Pizzas
- {
- public abstract class Pizza
- {
- public string Name { get; protected set; }
- public string Dough { get; protected set; }
- public string Sauce { get; protected set; }
- protected List<string> Toppings = new List<string>();
-
- public void Prepare()
- {
- Console.WriteLine($"Preparing: {Name}");
- Console.WriteLine($"Tossing: {Dough}");
- Console.WriteLine($"Adding sauce: {Sauce}");
- Console.WriteLine("Adding toppings: ");
- Toppings.ForEach(x => Console.WriteLine($" {x}"));
- }
-
- public void Bake()
- {
- Console.WriteLine("Bake for 25 minutes");
- }
-
- public void Cut()
- {
- Console.WriteLine("Cutting the pizza into diagnol slices");
- }
-
- public void Box()
- {
- Console.WriteLine("Placing pizza in official PizzaStore box......");
- }
- }
- }
- ```
- - 各种Pizza
- ```
- namespace SimpleFactory.Pizzas
- {
- public class CheesePizza: Pizza
- {
- public CheesePizza()
- {
- Name = "Cheese Pizza";
- Dough = "Think Dough";
- Sauce = "Salad";
- Toppings.Add("Grated Reggiano Cheese");
- }
- }
- }
-
- namespace SimpleFactory.Pizzas
- {
- public class ClamPizza: Pizza
- {
- public ClamPizza()
- {
- Name = "Clam Pizza";
- Sauce = "Tomato sauce";
- Dough = "Soft dough";
- Toppings.Add("Shrimp meat");
- }
- }
- }
-
- namespace SimpleFactory.Pizzas
- {
- public class PepperoniPizza: Pizza
- {
- public PepperoniPizza()
- {
- Name = "Pepperoni Pizza";
- Dough = "Thin dough";
- Sauce = "Black pepper";
- Toppings.Add("Beef Granules");
- Toppings.Add("Niblet");
- }
- }
- }
- ```
- - 简单工厂
- ```
- using SimpleFactory.Pizzas;
-
- namespace SimpleFactory
- {
- public class SimplePizzaFactory
- {
- public Pizza CreatePizza(string type)
- {
- Pizza pizza = null;
- switch (type)
- {
- case "cheese":
- pizza = new CheesePizza();
- break;
- case "pepperoni":
- pizza = new PepperoniPizza();
- break;
- case "clam":
- pizza = new ClamPizza();
- break;
- }
-
- return pizza;
- }
- }
- }
- ```
- - PizzaStore
- ```
- using SimpleFactory.Pizzas;
-
- namespace SimpleFactory
- {
- public class PizzaStore
- {
- private readonly SimplePizzaFactory _factory;
-
- public PizzaStore(SimplePizzaFactory factory)
- {
- _factory = factory;
- }
-
- public Pizza OrderPizza(string type)
- {
- var pizza = _factory.CreatePizza(type);
- pizza.Prepare();
- pizza.Bake();
- pizza.Cut();
- pizza.Box();
- return pizza;
- }
- }
- }
- ```
-
- > 工厂方法模式
-
- - 工厂方法模式定义了一个创建对象的接口, 但是让子类来决定具体创建的是哪一个对象. 工厂方法让一个类延迟实例化, 直到子类的出现.
- - ![](./imgs/工厂方法模式.png)
-
- > 设计原则
- - DIP (Dependency Inversion Principle) 依赖反转原则.
- - 进一步解释就是: 高级别的组件不应该依赖于低级别的组件, 它们都应该依赖于抽线.
|