mercredi 5 juillet 2017

Java polymorphism: finding the right design pattern

Disclaimer: I know there are a lot of questions about polymorphism out there, but I couldn't find a suitable answer for my problem. If your Google-fu is better than mine, please forgive the dupe.

I have a model using inheritance, such as in the example below.

public abstract class Base {
// ...
}

public class ConcreteA extends Base {
// ...
}

public class ConcreteB extends Base {
// ...
}

And I also have a List<Base>, which is composed of objects that are either ConcreteAs or ConcreteBs.

I need to generate a graphical view for each object in the list, but the resulting element is not the same for ConcreteAs and ConcreteBs.

How can I achieve this using OO principles?

Aucun commentaire:

Enregistrer un commentaire