String- Function dictionary c# where functions have different arguments

后端 未结 9 2811
温柔的废话
温柔的废话 2021-02-19 09:13

Basically I\'m trying to make a string to function dictionary in c#, I\'ve seen it done like this:

Dictionary>
<         


        
9条回答
  •  情深已故
    2021-02-19 09:44

    One way to address this issue is to introduce an argument container/command class that would contain the super set of function arguments, including the action type itself (the key from the dictionary in the above question):

    public class ActionCommand
    {
        public ActionType ActionType { get; set; } // enum instead of string
        public int Distance { get; set; }
        public DirectionType DirectionType { get; set; }
        // More properties as needed
    }
    

    Then, for each ActionType implement a strategy class:

    public interface IActionHandlerStrategy
    {
        bool AppliesTo(ActionCommand actionCommand);
        string Apply(ActionCommand actionCommand);
    }
    
    public class WalkActionHandlerStrategy : IActionHandlerStrategy
    {
        public bool AppliesTo(ActionCommand actionCommand)
        {
            return ActionCommand.ActionType == ActionType.Walk;
        }
    
        public ActionResult Apply(ActionCommand actionCommand)
        {
            // Do something here and return the result of Walk action
            return new ActionResult(...); // Container for results
        }
    }
    
    public class JumpActionHandlerStrategy : IActionHandlerStrategy
    {
        public bool AppliesTo(ActionCommand actionCommand)
        {
            return ActionCommand.ActionType == ActionType.Jump;
        }
    
        public ActionResult Apply(ActionCommand actionCommand)
        {
            // Do something here and return the result of Jump action
            return new ActionResult(...); // Container for results
        }
    }
    

    Add more strategies as needed and implement a class that would apply them:

    public class ActionHandler
    {
        // Register strategies with the handler; typically done with DI
        protected IList ActionHandlerStrategies = new List
            {
                new JumpActionHandlerStrategy(),
                new WalkActionHandlerStrategy(),
                // More strategies here...
            };
    
        public ActionResult Handle(ActionCommand actionCommand)
        {
            var actionHandlerStrategy = ActionHandlerStrategies.FirstOrDefault(ahs => ahs.AppliesTo(actionCommand);
            if (actionHandlerStrategy == null)
            {
                throw new Exception($"No strategy found for action type {actionCommand.ActionType}");
            }
            return actionHandlerStrategy.Apply(actionCommand);
        }
    }
    

    The end result should allow for relatively simple addition of new actions without much impact on existing logic.

提交回复
热议问题