mercredi 30 août 2017

List of similar objects - invoke different method for each element

I'm looking for a good practice/idea how to write a clean code for my solution. Let's say I have class Foo:

public class Foo
{
    public SomeEnumType Type { get;set; }
    ....
}

I got a list of these elements like:

List<Foo> Foos

Now I want to iterate over my list and invoke different methods for any EnumType. For now I'm just doing something like this:

foreach (var x in Foos)
{
     if (x.Type == XXX)
        invoke first();
     else if (x.Type == YYY)
        invoke second();
     .....
}

It looks OK if i got one or two values in my TypeEnum, but unfortunately I will have about ~~30 and as I assume it will look terrible with ~~30 else. How to make it better? Does anyone have met such a problem before?

Aucun commentaire:

Enregistrer un commentaire