I have the following code snippet
class Vehicle{
public String brand;
public double price;
public int productionYear;
public String toString(String formatType) {
switch(formatType) {
case "JSON": // JSON formatting here
return jsonFormattedString;
break;
case "XML": // XML formatting here
return xmlFormattedString;
break;
default: // default formatting
return defaultFormattedString;
}
}
I believe that the problem with this approach is the need of changing the source code if the behaviour changes (another formatting type); and maybe other SOLID violations that I missed.
How can this be better implemented?