CSharp: Proxy Pattern in donet.core 6.0

 

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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Geovin.Du.DuProxy.DuProtectionProxy
{
 
    /// <summary>
    /// 代理模式 Proxy  Pattern
    /// </summary>
    public class Employee
    {
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="role"></param>
        public Employee(string username, string password, Role role)
        {
            Username = username;
            Password = password;
            Role = role;
        }
        /// <summary>
        /// 账户/用户名
        /// </summary>
        public string Username { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }
        /// <summary>
        /// 角色
        /// </summary>
        public Role Role { get; set; }
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Geovin.Du.DuProxy.DuProtectionProxy
{
    /// <summary>
    /// 代理模式 Proxy  Pattern
    /// 角色
    /// </summary>
    public enum Role
    {
        /// <summary>
        /// 工作人员
        /// </summary>
        Worker,
        /// <summary>
        /// 管理者
        /// </summary>
        Manager,
        /// <summary>
        /// 高层
        /// </summary>
        CEO,
    }
 
}
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Geovin.Du.DuProxy.DuProtectionProxy
{
 
    /// <summary>
    /// 代理模式 Proxy  Pattern
    /// </summary>
    public interface ISharedFolder
    {
        /// <summary>
        ///
        /// </summary>
        void Access();
    }
 
}
 
 
using Geovin.Du.DuProxy.DuProtectionProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Geovin.Du.DuProxy.DuProtectionProxy
{
    /// <summary>
    /// 代理模式 Proxy  Pattern
    /// </summary>
    public class SharedFolder : ISharedFolder
    {
        /// <summary>
        ///
        /// </summary>
        public void Access() => Console.WriteLine("打开共享文件夹.");
    }
 
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Geovin.Du.DuProxy.DuProtectionProxy
{
    /// <summary>
    ///
    /// </summary>
    public class SharedFolderProxy : ISharedFolder
    {
 
        /// <summary>
        /// 代理模式 Proxy  Pattern
        /// </summary>
        private readonly Employee _employee;
        /// <summary>
        ///
        /// </summary>
        private readonly SharedFolder _sharedFolder;
        /// <summary>
        ///
        /// </summary>
        /// <param name="employee"></param>
        /// <param name="sharedFolder"></param>
        public SharedFolderProxy(Employee employee, SharedFolder sharedFolder)
        {
            _employee = employee;
            _sharedFolder = sharedFolder;
        }
        /// <summary>
        /// 权限 管理者和高层
        /// </summary>
        public void Access()
        {
            if (_employee.Role is Role.Manager or Role.CEO)
            {
                _sharedFolder.Access();
                return;
            }
 
            Console.WriteLine($"具有该角色的员工 '{_employee.Role}' 没有访问共享文件夹的权限");
        }
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Geovin.Du.BuildingBlocks;
 
 
namespace Geovin.Du.DuProxy.DuProtectionProxy
{
    /// <summary>
    /// 代理模式 Proxy  Pattern
    /// </summary>
    public static class ProtectionProxyExecutor
    {
 
        /// <summary>
        ///
        /// </summary>
        public static void Execute()
        {
            ConsoleExtension.WriteSeparator("代理模式 Protection proxy pattern demo");
 
            TryToAccessTheSharedFolder("GeovinDu", "password", Role.Worker);
            TryToAccessTheSharedFolder("GeovinTu", "password", Role.CEO);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="role"></param>
        private static void TryToAccessTheSharedFolder(string username, string password, Role role)
        {
            Console.WriteLine(
                $"\n使用用户名: '{username}'和角色: '{role}' " +
                $"正在尝试访问共享文件夹...");
 
            var employee = new Employee(username, password, role);
            var sharedFolder = new SharedFolder();
            var folderProxy = new SharedFolderProxy(employee, sharedFolder);
 
            folderProxy.Access();
        }
    }
}

  

调用:

1
2
3
Geovin.Du.DuProxy.DuProtectionProxy.ProtectionProxyExecutor.Execute();
 
Console.WriteLine();

  

输出:

1
2
3
4
5
6
7
8
9
10
Hello, Geovin Du! 学习 .net 6
 
代理模式 Protection proxy pattern demo
--------------------------------------------------
 
使用用户名: 'GeovinDu'和角色: 'Worker' 正在尝试访问共享文件夹...
具有该角色的员工 'Worker' 没有访问共享文件夹的权限
 
使用用户名: 'GeovinTu'和角色: 'CEO' 正在尝试访问共享文件夹...
打开共享文件夹.

  

posted @   ®Geovin Du Dream Park™  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2021-12-17 java: MySql Connection using JDK 14.02
2021-12-17 java: Lamdba
2015-12-17 css:Media Queries
< 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
点击右上角即可分享
微信分享提示