lundi 28 mars 2016

How should I select which concrete implementation should be instantiated based on the user choice?

I have an interface Fruit with two implementations Apple and Banana. I want to create a Fruit instance. The choice whether the concrete implementation should be an Apple or a Banana should be made by the user. I did not yet design the user interface, so there is no restriction how this choice is made by the user.

I know there are the following options:

  1. usage of the abstract factory pattern
  2. usage of reflection to create an instance from a given class name
  3. usage of reflection to create an instance from a given class object

What are the pros and cons of these options?


Please note that while there are several similar questions that discuss the one or the other approach, I did not find a single comparison.

Here is a list of related questions:





Aucun commentaire:

Enregistrer un commentaire