I want to instantiate a set of object from a json list.
Consider the json list
[
{
...
"class":"object1",
"area":45
...
},
{
...
"class":"object2",
"colour":"red"
},
{
...
"class":"object3",
"height":90
...
}
]
Surely there are there fields that define each object. For each object I created a record
public record object1 (String area, ...) {}
public record object2 (String colour, ...) {}
public record object3 (String height, ...) {}
In general I could create a switch case that checks the property class and the chooses the correct record constructor. This seems to me very bad style. In the past I used polymorphism with a fitting super class. and used an enum that created the object. In this case the record
class only has object as super.
I want to stick with record since its I am only holding simple data inside this object.
There I was wondering how to handle this. What is the best practice to handle something like this? What pattern can be used to solve this?
Aucun commentaire:
Enregistrer un commentaire