In my application, there is a concept of an interface along with multiple objects that implement this interface in various ways. These objects are created using different factory methods, with the potential for more factories to be added as the application continues to evolve.
One example of this implementation:
var Shapes={};
var Shapes.createCircle=function(radius) {
return {draw:function...}
};
var Shapes.createRectangle=function(width,height) {
return {draw:function...}
};
The goal is to serialize these shapes into files by having each object store the name of its factory (e.g. "Shapes.createRectangle") and an array of arguments (e.g [10,20]), which will then be saved as a JSON string. This approach ensures that when the objects are deserialized, they can have all their functions and properties restored.
For instance, to save an object, the plan is to store the JSON string:
{"factory":"Shapes.createRectangle","args":[10,20]}
And in future sessions, the object can be reconstructed by dynamically calling the factory method once again.
Are there any potential pitfalls or issues associated with serializing polymorphic objects in this manner?