Reputation: 3582
Say I have a java object like (just as a simple example)
class Town{
Public String name;
Public Set<Business> Businesses;
Public Set<Person> people;
Public Set<House> houses;
//...
}
Houses, person, business etc being complex objects which in turn have their own objects, which have their own objects etc - some with composition and some with aggregation type relationships.
Now, I need to add a clone
method to the Town class, and I have two questions:
How do I clone thehouses
Set? Do I just implement House.clone()
and call houses.clone()
. Will set call the clone method of each of it's members?
Assuming this was a non-trivial problem (houses 'has' 10 sets of aggregated complex objects, and each of them 'has' 10 sets of aggregated complex objects and so on), is there a better way to implement the clone method for Town
?
Or do I just have to spend the time implementing 100 .clone()
methods on sub objects, and sub-sub-objects etc? (Would that be more or less work than implementing serialize on all the sub objects and doing it that way?)
Upvotes: 2
Views: 1500
Reputation: 6550
You have a couple of options:
Serializable
that has one instance var which decodes information about the object. More info here: http://docs.oracle.com/javase/tutorial/jndi/objects/serial.htmlMore information on Deep Copy libraries: Java deep copy library
Upvotes: 1