java - Which strategy to use? Object Clone or a simple Map -


i need something. code of following template. assume customobject has multiple property1, property2, ..property100.

list<customobject> customobjectlist = /*<method call external api returns said list >*/ if(customobjectlist != null && customobjectlist.size() > 0){     //*** point ***<clone object>     resultlist = <some method process above list>(customobjectlist)      if(resultlist.size() > 0){         for(iterator<map.entry<customobject, externalresponse>> itr = resultlist.entryset().iterator(); itr.hasnext();) {             //code modifies properties in customobjects             //*** point b ***resetaproperty(<object clone>)         }     } } 

at point b, need 1 unmodified specific property of original object use in method. have 2 strategies this:

  1. clone object @ point a, , use cloned copy property shown in above code. @ point a, use loop , a
  2. map form associate array of object names, property original values , traverse them property initial value @ point b

avoid cloning because requires deep cloning

.clone() on list end in tears because have deep clone objects in list, referenced objects, , on.

deep cloning means have make binary copy of every last object in object graph. copying reference give shallow copy , see changes made referenced object. miss 1 property , have hell of time finding bug.

solution

what should make customobject instances immutable , don't need worry versioning, can never change, mutation involve creating new instance immutable , complete different object. never had worry versions.

of course instance variables point other objects need immutable well. same problem deep clone taking angle. more manageable angle.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -