CSharp: null object 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/// <summary>
    /// 空对象模式 null object Pattern
    ///
    /// </summary>
    public abstract class Application
    {
 
        /// <summary>
        ///
        /// </summary>
        private static readonly NullApplication NullApplication = new NullApplication();
        /// <summary>
        ///
        /// </summary>
        /// <param name="processId"></param>
        public Application(Guid processId)
        {
            ProcessId = processId;
        }
        /// <summary>
        ///
        /// </summary>
        public static NullApplication Default
        {
            get
            {
                return NullApplication;
            }
        }
        /// <summary>
        ///
        /// </summary>
        public abstract string Name { get; }
        /// <summary>
        ///
        /// </summary>
        public Guid ProcessId { get; }
        /// <summary>
        ///
        /// </summary>
        public virtual void Open()
        {
            Console.WriteLine($"应用程序 {Name} 已经打开.");
        }
        /// <summary>
        ///
        /// </summary>
        public virtual void Close()
        {
            Console.WriteLine($"应用程序 {Name} 已经关闭.");
        }
    }
 
 
 
    /// <summary>
    /// 空对象模式 null object Pattern
    /// </summary>
    public class NullApplication : Application
    {
 
        /// <summary>
        ///
        /// </summary>
        public NullApplication()
            : base(Guid.Empty)
        {
        }
        /// <summary>
        ///
        /// </summary>
        public override string Name => string.Empty;
        /// <summary>
        ///
        /// </summary>
        public override void Open()
        {
        }
        /// <summary>
        ///
        /// </summary>
        public override void Close()
        {
        }
    }
 
 
    /// <summary>
    ///
    /// </summary>
    public class Gmail : Application
    {
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="processId"></param>
        public Gmail(Guid processId)
            : base(processId)
        {
        }
        /// <summary>
        ///
        /// </summary>
        public override string Name => "Gmail";
    }
 
   /// <summary>
    ///
    /// </summary>
    public class SubwaySurfers : Application
    {
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="processId"></param>
        public SubwaySurfers(Guid processId)
            : base(processId)
        {
        }
        /// <summary>
        ///
        /// </summary>
        public override string Name => "Subway Surfers";
    }
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Geovin.Du.DuNullObject.Applications.Common;
 
 
namespace Geovin.Du.DuNullObject.Applications
{
 
    /// <summary>
    ///
    /// </summary>
    public class GeovinDu : Application
    {
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="processId"></param>
        public GeovinDu(Guid processId)
            : base(processId)
        {
        }
        /// <summary>
        ///
        /// </summary>
        public override string Name => "GeovinDu";
    }
}
 
   /// <summary>
    ///
    /// </summary>
    public class ApplicationRepository
    {
 
        /// <summary>
        ///
        /// </summary>
        private readonly Dictionary<Guid, Application> _installedApplications;
 
 
        /// <summary>
        ///
        /// </summary>
        public ApplicationRepository()
        {
            var gmailProcessId = new Guid("6a3852ab-0973-4d1b-82d8-edd131c5f0a9");
            var subwaySurfersProcessId = new Guid("c8bc60c6-f1d2-484f-91ee-a6e6dec9e782");
            var geovinduProcessId = new Guid("2b8ba9a3-4f83-4f86-96a8-94daf79fa4bd");
 
            _installedApplications = new Dictionary<Guid, Application>
            {
                { gmailProcessId, new Gmail(gmailProcessId) },
                { subwaySurfersProcessId, new SubwaySurfers(subwaySurfersProcessId) },
                { geovinduProcessId, new GeovinDu(youtubeProcessId) },
            };
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="processId"></param>
        /// <returns></returns>
        public Application Find(Guid processId)
        {
            if (_installedApplications.TryGetValue(processId, out Application application))
            {
                return application;
            }
 
            return Application.Default;
        }
    }
 
    /// <summary>
    /// 空对象模式 null object Pattern
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public static class SmartphoneApplicationExecutor
    {
 
        /// <summary>
        ///
        /// </summary>
        public static void Execute()
        {
 
            ConsoleExtension.WriteSeparator("小手机应用程序 Demo");
 
            var applicationRepository = new ApplicationRepository();
 
            var validProcessId = new Guid("6a3852ab-0973-4d1b-82d8-edd131c5f0a9");
            var invalidProcessId = Guid.Empty;
 
            var gmail = applicationRepository.Find(validProcessId);
            gmail.Open();
            gmail.Close();
 
            // Null exception won't be thrown if an application with the specified process ID can't be found.
            // In that case, default (NULL) object will be returned.
            var geovindu = applicationRepository.Find(invalidProcessId);
            geovindu.Open();
            geovindu.Close();
        }
    }

  

调用:

1
2
3
4
5
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();

  

输出:

1
2
3
4
5
6
Hello, Geovin Du! 学习 .net 6
 
小手机应用程序 Demo
--------------------------------------------------
应用程序 Gmail 已经打开.
应用程序 Gmail 已经关闭.

  

 

 

 

 

 

 

 

 

 

 

吾富有钱时,妇儿看我好。吾若脱衣裳,与吾叠袍袄。吾出经求去,送吾即上道。将钱入舍来,见吾满面笑。绕吾白鸽旋,恰似鹦鹉鸟。邂逅暂时贫,看吾即貌哨。人有七贫时,七富还相报。图财不顾人,且看来时道。
--【唐】王梵志《吾富有钱时》

 

 

 

 

posted @   ®Geovin Du Dream Park™  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2011-11-04 Csharp 打印Word文件默認打印機或選擇打印機設置代碼
2011-11-04 WordLocalView (转)
< 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
点击右上角即可分享
微信分享提示