I think you need a view class related to factory.
Dictionary<Func<Animal, bool>, Func<Animal, AnimalView>> factories;
factories.Add(item => item is Dog, item => new DogView(item as Dog));
factories.Add(item => item is Pig, item => new PigView(item as Pig));
Then your DogView and PigView inherit an AnimalView that looks something like this:
class AnimalView {
abstract void DoStuff();
}
You end up with something like:
foreach (animal in list)
foreach (entry in factories)
if (entry.Key(animal)) entry.Value(animal).DoStuff();
I think you could also say that this is an implementation of a strategy template.
source
share