C#C
C#4y ago
Matt

❔ Better way of handling this?

switch (syncMessage.type)
{
  case "Obj1": 
    Obj1.HandleUpdate(syncMessage.data);
    break;
  case "Obj2": 
    Obj2.HandleUpdate(syncMessage.data);
    break;
  case "Obj2": 
    Obj2.HandleUpdate(syncMessage.data); 
    break;
  default: 
    break;
}

Is there a better way of handling this? The objects all inherit from the same base abstract class which contains the HandleUpdate method. The syncMessage.type is a string and I don't think I can change that (not 100% sure)
It feels like the strategy pattern might be what I'm after but this implementation already uses 3 classes per object and I'd rather not add more. Any ideas?

Edit: Cleaned code up to remove information irrelevant to the question
Was this page helpful?