[非專業翻譯] Mapster - 基本使用
阿新 • • 發佈:2021-06-22
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace DesignPattern.CreationalPatern
8 {
9 #region 工廠方法模式要點
10
11 //定義抽象工廠,將建立具體物件的過程推遲到其具體工廠中進行;
12 //具體工廠必須實現的抽象介面,系統就可以再不修改工廠類邏輯的情況下來新增新產品。
13 // 主要遵循開放封閉原則,依賴倒轉原則
14
15 #endregion
16 public abstract class FactoryMethodPattern
17 {
18 public abstract Food MakeFood();
19 }
20
21 //在工廠方法模式中,工廠類與具體產品類具有平行的等級結構,它們之間是一一對應的。
22 public class IceCreamFactory : FactoryMethodPattern
23 {
24 public override Food MakeFood()
25 {
26 return new IceCream();
27 }
28 }
29
30 public class CakeFactory : FactoryMethodPattern
31 {
32 public override Food MakeFood()
33 {
34 return new Cake();
35 }
36 }
37
38 /************************************需要建立的物件*************************************** */
39
40 public abstract class Food
41 {
42 public abstract void PrintName();
43 }
44
45
46 public class IceCream : Food
47 {
48 public override void PrintName()
49 {
50 Console.WriteLine("冰激凌");
51 }
52 }
53
54 public class Cake : Food
55 {
56 public override void PrintName()
57 {
58 Console.WriteLine("蛋糕");
59 }
60 }
61
62 }