动态开关盒

Dynamic switch cases

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

管理员切换案例:

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;
}

有什么办法可以将这些开关盒合为一个动态开关吗?如果我的想法或解释有问题,请纠正我。

这是 strategy pattern 的一个很好的候选者。

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

例如:

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;
        }
    }
}

切换(没有双关语意)它会在每种情况下检查角色。然后不让做就跳过

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;
}

您最好使用函数或操作图。

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();

switch-case 的动态等价物是字典查找。例如:

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]();

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

为了实现 "default" 逻辑,您需要使用如下内容:

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
}