Suppose you have several types each representing a collection of fields. Each type's fields are different, but some types share some fields.
Let us consider IceCream types. All IceCream types implement a common interface which requires that a method, getFlavor() be exposed. However, each IceCream type also exposes several other methods. IceCream types include things like Gelato, WaterIce, FrozenCustard, ChilledCream, and Traditional
Some IceCream types expose a method to get the manufacturer, but other IceCream types have no manufacturer and thus do not expose a method for getting the manufacturer.
Further, the IceCream types do not expose the getManufacturer() method in the same way - some call it getManufacturer(), while others call it getMaker(), getChef(), or getOwner(). Additionally, a single IceCream type may have multiple separate methods that each return the same (or different) manufacturers.
Given some set of IceCream types, some with methods that return a manufacturer (the manufacturer will always be the same data type) how might one abstract over these IceCream types so that they can write generic logic for dealing with the IceCream types instead of duplicating all of the code that is used to retrieve the manufacturer for every IceCream type separately?
Unfortunately, these classes cannot be modified.
It seems like this would require some kind of enumeration containing a mapping from Type -> String, and the Strings are used to reflectively resolve the class methods. Is there another generic way to implement this?
Aucun commentaire:
Enregistrer un commentaire