CSharp: Factory Method Pattern in donet 6

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/// <summary>
 /// The Product declares the interface, which is common to all objects
 /// that can be produced by the creator <see cref="Restaurants.Common.Restaurant"/> and its subclasses.
 /// 工厂方法模式 Factory Method - Creational Pattern
 /// </summary>
 public interface IMeal
 {
 
     /// <summary>
     ///
     /// </summary>
     void ShowDescription();
 }
 
 /// <summary>
 /// The Creator class declares the factory method that returns new product objects.
 /// 工厂方法模式 Factory Method - Creational Pattern
 /// </summary>
 public abstract class Restaurant
 {
     /// <summary>
     /// The factory method that returns new product objects.
     /// It’s important that the return type of this method matches the product interface.
     /// You can declare the factory method as abstract to force all subclasses to implement their own versions of the method.
     /// As an alternative, the base factory method can return some default product type.
     /// </summary>
     /// <returns>Meal.</returns>
     public abstract IMeal PrepareMainCourse();
 
     /// <summary>
     /// Note, despite its name, product creation is not the primary responsibility of the creator.
     /// Usually, the creator class already has some core business logic related to products.
     /// The factory method helps to decouple this logic using the concrete product classes.
     /// </summary>
     public void OrderDailySpecial()
     {
         Console.WriteLine("Geovin Du 要饮料:苏打水");
 
         var mainCourse = PrepareMainCourse();
         mainCourse.ShowDescription();
     }
 }
 
/// <summary>
 /// Concrete products are different implementations of the product interface <see cref="IMeal"/>.
 /// </summary>
 public class GreenSalad : IMeal
 {
 
     /// <summary>
     ///
     /// </summary>
     public void ShowDescription()
     {
         Console.WriteLine("涂聚文叫单:蔬菜沙拉——有生菜、黄瓜和青橄榄");
     }
 }
 
 /// <summary>
 /// Concrete products are different implementations of the product interface <see cref="IMeal"/>.
 /// </summary>
 public class Hamburger : IMeal
 {
 
     /// <summary>
     ///
     /// </summary>
     public void ShowDescription()
     {
         Console.WriteLine("Geovin Du叫单:汉堡-加牛肉,伍斯特沙司和奶酪。再来一份中餐:鸳鸯火锅.");
     }
 }
 
/// <summary>
 /// Concrete creators override the base factory method so it returns a different type of product.
 /// 工厂方法模式 Factory Method - Creational Pattern
 /// </summary>
 public class VegetarianRestaurant : Restaurant
 {
 
     /// <summary>
     ///
     /// </summary>
     /// <returns></returns>
     public override IMeal PrepareMainCourse()
     {
         return new GreenSalad();
     }
 }
 
 /// <summary>
 /// Concrete creators override the base factory method so it returns a different type of product.
 /// 工厂方法模式 Factory Method - Creational Pattern
 /// </summary>
 public class FastFoodRestaurant : Restaurant
 {
 
     /// <summary>
     ///
     /// </summary>
     /// <returns></returns>
     public override IMeal PrepareMainCourse()
     {
         return new Hamburger();
     }
 }
 
 /// <summary>
 /// 工厂方法模式 Factory Method - Creational Pattern
 ///
 /// </summary>
 public class Executor : PatternExecutor
 {
 
     /// <summary>
     ///
     /// </summary>
     public override string Name => "工厂方法模式 Factory Method - Creational Pattern";
     /// <summary>
     ///
     /// </summary>
     public override void Execute()
     {
         Restaurant restaurant = InitializeRestaurant();
 
         restaurant.OrderDailySpecial();
     }
     /// <summary>
     ///
     /// </summary>
     /// <returns></returns>
     private Restaurant InitializeRestaurant()
     {
         // This is usually stored within some configuration
         var choosenType = typeof(FastFoodRestaurant).FullName;
         choosenType = typeof(VegetarianRestaurant).FullName;
         return Assembly.GetExecutingAssembly().CreateInstance(choosenType) as Restaurant;
     }
 }

  

调用:

1
2
3
4
5
6
7
8
Console.WriteLine("Hello, Geovin Du! 学习 .net 6 ");
//Geovin.Du.DuEmail.EmailExecutor.Execute();
//Geovin.Du.DuStock.StockExecutor.Execute();
// Geovin.Du.DuShoppingCart.ShoppingCartExecutor.Execute();
//Geovin.Du.DuNullObject.SmartphoneApplicationExecutor.Execute();
 
Geovin.Du.DuFactoryMethod.Executor ex = new Geovin.Du.DuFactoryMethod.Executor();
ex.Execute();

  

输出:

1
2
3
Hello, Geovin Du! 学习 .net 6
Geovin Du 要饮料:苏打水
涂聚文叫单:蔬菜沙拉——有生菜、黄瓜和青橄榄

  

 

posted @   ®Geovin Du Dream Park™  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2009-11-05 Dijkstra's Shortest Path Algorithm(最短路径算法)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示