动态开关案例

时间:2015-10-13 15:44:01

标签: c# switch-statement

我正在尝试为几个不同的用户创建一个简单的switch case控制台菜单: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;
}

有没有办法将这些开关盒塑造成一个动态开关?如果我在思考或解释错误,请纠正我。

4 个答案:

答案 0 :(得分:7)

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

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

为了实现“默认”逻辑,你可以使用类似的东西:

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
}

答案 1 :(得分:3)

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

答案 2 :(得分:2)

你最好使用功能或动作的地图。

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

答案 3 :(得分:1)

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

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