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 the
houses
Set? Do I just implementHouse.clone()
and callhouses.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?)