Let's say I have a set of objects,
foo f; bar br; baz bz;
I also have a string of JSON data,
Depending on the object type of the JSON string, I need to transform it into either foo, bar, or baz. Ok, cool, I'll have a method for that.
public object parseJSONToFooBarBaz(string jsonString);
What I want to avoid is writing something like:
map<string, object> topLevelJSON = deserialized json string; if(map[foo] != null) return new foo(jsonString); else if(map[bar] != null) return new bar(jsonString); ... // And the list balloons up and is difficult to maintain
I feel like this is either a good condidate or almost a good candidate for a factory pattern, but something doesn't feel quite right. Is there a simple solution that I'm overlooking, or is a set of conditionals or a switch/case really an OK way to solve this?