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(); } } }
调用:
Geovin.Du.DuProxy.DuProtectionProxy.ProtectionProxyExecutor.Execute(); Console.WriteLine();
输出:
Hello, Geovin Du! 学习 .net 6 代理模式 Protection proxy pattern demo -------------------------------------------------- 使用用户名: 'GeovinDu'和角色: 'Worker' 正在尝试访问共享文件夹... 具有该角色的员工 'Worker' 没有访问共享文件夹的权限 使用用户名: 'GeovinTu'和角色: 'CEO' 正在尝试访问共享文件夹... 打开共享文件夹.
标签:core,donet,Pattern,System,Geovin,Role,using,Du,public From: https://www.cnblogs.com/geovindu/p/16989399.html