Suppose we have an object representing the configuration of a piece of equipment. For argumentation - the temperature controller (TempController). It contains one property, a given temperature.
I need to save this configuration to a file for use on some other device. The file format (FormatA) is set to stone. I do not want the TempController object to know about the file format ... it just does not apply to this object. Therefore, I create another "FormatAExporter" object that converts the TempController to the desired result.
In a year, we are creating a new temperature controller, we will allow it to "AdvancedTempController", which has not only a preset value, but also has speed control, which means one or two more properties. A new file format has also been invented to preserve these properties ... let it be called FormatB.
Both file formats can represent both devices (suppose AdvancedTempController has reasonable defaults if it lacks settings).
So, here is the problem: not using "isa" or any other "fraudulent" way to find out what type of object I have, how does FormatBExporter handle both cases?
My first instinct is to have a method in every temperature controller that can provide a client exporter for this class, for example TempController.getExporter () and AdvancedTempController.getExporter (). It does not support multiple file formats.
The only other approach that comes to mind is to have a method in each temperature controller that returns a list of properties and their values, and then the formatter can decide how to output them. This will work, but it seems confusing.
UPDATE: With further work, this latter approach does not work very well. If all your types are simple, perhaps, but if your objects are objects, you end up just pushing the problem to the level ... you are forced to return a pair of String, Object values, and the exporter will need to know that the Objects should actually use them. Thus, it simply pushes the problem to a different level.
Are there any suggestions on how I can maintain this flexibility?