I have a Person base class and three derivatives. On the edit page I have a section with general person data (name, address, etc.).
I also want a section per subclass. So if a Person is a User I want to add a section with the specific user data. If a Person is also a CardHolder I want to show the card holder section.
I am struggling on how to implement this. What I have in mind is to ask the UserRepository if the specified Person is a User. If so, the method will return a User object; otherwise null. I am planning to do this for all the subclasses.
However, I have this feeling that this can be done a better way, but I can’t find that way.
EDIT:
I have the following scenario. I have an overview with Person objects (not the derivatives!). When a row is clicked, I want to show the details page. On this page I have the different sections as described above. At this point I want to know if the ‘Person’ is also a User or a ‘Cardholder’.
I’d like to apply polymorphism, but I don’t see how…
Have an abstract function on the Person class called something like “
ReturnValidSectionsForPerson()“. In each subclass, override that function to return “UserData” or “CardHolder” as appropriate. You can use an enumeration to hold the various types of sections to make it easier to read and maintain.