I've been using a certain pattern to store my data objects like this :
JavaScript :
var person = {
data : {}, // json data will be populated here
getId : function(){
return this.data.id;
},
getName : function(){
return this.data.name
}
}
Java :
class Person {
HashMap<String, Object> data = new HashMap<>(); // json converted to hashmap
public String getId(){
return this.data.get("id");
}
public String getName(){
return this.data.get("name");
}
}
Benefits of using this pattern -
-
Never have to worry about serializing and deserializing
-
Can save and load the 'data' object anywhere easily(localstorage or elsewhere)
-
Never lose reference to the 'person' object anywhere in the code even though internally the object changed
-
Can have custom logic for getters and setters
-
Don't have to worry about API changes on the server - accessing properties only happens inside getter functions so if ever a property value needs to be changed or that property has been mapped to another property in an object - we can just change the property in the getter and we're done. For eg - if we've been using person.name everywhere in the code and we now need to use person.fullName - if we have the getter we can just change it in the getter and we would be good to go.
Downsides -
- The only downside I'm assuming is reading might be a bit slow
Wanted to know what other downsides there might be to following this pattern? Or any glaring red-flags as to why this should be avoided?
Aucun commentaire:
Enregistrer un commentaire