-1

I have a manager that read objects from a repository. The objects reference other objects of a known base class, based on a stored enumeration property.

What's a good design pattern to know which manager/repository to query on the reference object? A switch block over the stored kind of the object would get out of hand very quickly.

For example:

public class Person
{
    public Animal Pet { get; set; }
    public AnimalKind PetKind { get; set; }
}

public enum AnimalKind
{
    Dog,
    Cat
}

public abstract class Animal
{

}

public class Dog : Animal
{

}

public class Cat : Animal
{

}

public class DogManager /**/
public class CatManager /**/

public class PersonManager
{
    public IList<Person> GetPersons()
    {
        var persons = Repository.GetPersons();
        // Pattern to instantiate person.Pet with the proper class???
        // The Repository is very lightweight and does not load references.
    }
}

Could someone point me to the right direction? Any recomended book?

Edit: There is a table in the database for each type of Animal.

Villason
  • 11
  • 6

1 Answers1

0

I think the factory method pattern would be useful here.

Wikipedia Link

the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created

Also, here are a couple of articles on implementing it in C#: Factory Method, Abstract Factory

Edit: Here is a well known book on design patterns - Design Patterns: Elements of Reusable Object-Oriented Software

androo
  • 939
  • 9
  • 16