lundi 15 avril 2019

What is a good aproach handling interface specific methods?

Lets say i have:

public class Buff {
  ... code ...
}

Then im implementing triggers for my Buff

public interface IBuffOnActionTrigger {
   void OnPlayerAction(Player p);
}

public interface IBuffOnPlayerDie {
   void OnPlayerDie(Player p);
}

So now i can implement those methods on a buff.

I have the following method:

public void TriggerBuffs<TriggerType>(Player p) {
    p.Buffs.Where(b => b is TriggerType).ForEach(b => b.<CALLMETHOD>);
}

In this case, im using a Switch case

switch typeof(TriggerType):
   case IBuffOnPlayerDie:
      buff.OnPlayerDie(p);
   ... etc  

But i was looking a way to do this more elegantly. Would any1 know a good design for this ?

Cheers !

Aucun commentaire:

Enregistrer un commentaire