动态开关机箱

本文关键字:机箱 开关 动态 | 更新日期: 2023-09-27 18:17:34

我正在尝试为几个不同的用户制作一个简单的开关箱控制台菜单:admin, moderatoruseradmincreate, delete, modify, show函数、moderator - create, modify, show函数和user - create, show函数可供选择。

Admin switch case:

if(userType == "admin")
{
    string i = Console.ReadLine();
    switch(i):
    case "create": Console.WriteLine("Created");
                   break;
    case "modify": Console.WriteLine("Modified");
                   break;
    case "delete":Console.WriteLine("Deleted");
                  break;
    case "show":Console.WriteLine("Showed");
                break;
    default: Console.WriteLine("Default");
             break;
}

主持人切换案例:

if(userType == "moderator")
{
    string i = Console.ReadLine();
    switch(i):
    case "create": Console.WriteLine("Created");
                   break;
    case "modify": Console.WriteLine("Modified");
                   break;
    case "show": Console.WriteLine("Showed");
                 break;
    default: Console.WriteLine("Default");
             break;
}

用户切换案例:

if(userType == "user")
{
    string i = Console.ReadLine();
    switch(i):
    case "create": Console.WriteLine("Created");
                   break;
    case "show": Console.WriteLine("Showed");
                 break;
    default: Console.WriteLine("Default");
             break;
}

有没有办法把这些开关箱做成一个动态开关?如果我的想法或解释有误,请纠正我。

动态开关机箱

与切换情况动态等价的是字典查找。例如:

Dictionary<string, Action> userActions = {
        { "create", () => Console.WriteLine("created") },
        { "show", () => Console.WriteLine("showed") } };
Dictionary<string, Action> adminActions = {
        { "create", () => Console.WriteLine("created") },
        { "show", () => Console.WriteLine("showed") },
        { "delete", () => Console.WriteLine("deleted") } };
Dictionary<string, Dictionary<string, Action>> roleCapabilities = {
        { "user", userActions },
        { "administrator", adminActions } };
roleCapabilities[userType][action]();

在运行时,您可以轻松地为每个角色(组)添加和删除允许的操作。

为了实现"默认"逻辑,您可以使用如下内容:

Action actionCall;
if (roleCapabilities[userType].TryGetValue(action, out actionCall)) {
   actionCall();
}
else {
   // this is the "default" block, the specified action isn't valid for that role
}

这是策略模式的一个很好的候选。

在策略模式中,功能由可以传递的接口对象表示。不同的实现允许动态改变行为。

例如:

interface ConsoleInteractor
{
    void performAction(string action);
}
class UserConsoleInteractor : ConsoleInteractor
{
    public void performAction(string action)
    {
        switch(i)
        {
            case "create": 
                Console.WriteLine("Created");
                break;
            case "show":
                Console.WriteLine("Showed");
                break;
            default: 
                Console.WriteLine("Default");
                break;
        }
    }
}

您最好使用功能或操作的映射。

var actionsAdmin = new Dictionary<string, Action>{
  {"create", ()=>Console.WriteLine("create")}
  {"modify", ()=>Console.WriteLine("modify")}
}
var actionsUser = new Dictionary<string, Action>{
  {"show", ()=>Console.WriteLine("show")}
  {"foodle", ()=>Console.WriteLine("foodle")}
}

然后选择正确的映射并执行指定的函数

var action = actionUser[verb];
action();

切换(没有双关语的意思),在每个情况下都检查角色。然后,如果不允许这样做,则跳过它。

string i = Console.ReadLine();
if (allowed(userType, i)){
  switch(i):
  case "create": Console.WriteLine("Created");
      handleCreate();
  break;
  case "show":Console.WriteLine("Showed");
      handleShow();
  break;
  case "delete":Console.WriteLine("Deleted");
      handleDelete();
  break;
  default: Console.WriteLine("Default");
    handleDefault(userType);
  break;
}