Dynamic switch cases

后端 未结 4 729
孤独总比滥情好
孤独总比滥情好 2021-01-07 04:08

I\'m trying to make a simple switch case console menu for a few different users: admin, moderator, and user. admin would

4条回答
  •  渐次进展
    2021-01-07 04:24

    This is a good candidate for the strategy pattern.

    In the strategy pattern, functionality is represented by an object of an interface that can be passed around. Different implementations allow the behaviour to change dynamically.

    For example:

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

提交回复
热议问题