Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Hard to know what it is you're trying to do exactly with the limited information but if you're getting a bunch of "action requests" from somewhere and need to have different classes of object process them in different ways you could do something like this:</p> <pre><code>interface IActionHandler{ void HandleAction(Action action); } class Humanoid: IActionHandler{ void HandleAction(Action action){ switch(action.ActionType){ ActionType.Forward: Forward(); ...... } } ... } class Catcher: IActionHandler{ void HandleAction(Action action){ switch(action.ActionType){ ActionType.Grab: Grab(); ...... } } ... } class MainActionReceiver{ ReceiceActionRequest(Action action){ GetActioner(action.Actioner).HandleAction(action); } IActionHander GetActioner(string actioner){ if (actioner == "Humanoid"){ return humanoidObject; } return catcherObject; } } </code></pre> <p>Excuse the semi-C# style - it's what I'm working in today.</p> <p>If you wanted to avoid the switch statement in the HandleAction functions you could create ActionExecuter classes to actually execute the actions like this:</p> <pre><code>Interface IExecuter&lt;T&gt;{ bool CanExecute(Action action) void Execute(T owner, Action action); } </code></pre> <p>Then have </p> <pre><code>class ForwardExecuter&lt;Humanoid&gt;{ bool CanExecute{ return action.ActionType == forward; } Execute(Humaniod owner, Action action){ owner.Forward(); } } </code></pre> <p>register the available ActionExecuters with the classes and then loop through the in the Handler looking for an Executer that can execute the action and then pass this to the executer.</p> <pre><code>class Humanoid: IActionHandler{ void HandleAction(Action action){ foreach (IExecuter in executers){ if (executer.CanExecute(action)){ executer.Execute(this, action); } } } ... } </code></pre> <p>That's quite possibly overkill for what you are doing, but you then have all your actions and action executers cleanly encapsulated in their own classes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload