Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

To clone or not to clone..

Thread view: 
lordy - 07 Jul 2006 03:26 GMT
If I have some code that does this during in initialisation

1. Create lots of Object A

2. Create lots of Object B that each refers to a subset of instances of A.

Then I later realise that the A's must be distinct for each B. Is it
better to use cloning or should I re-organise the code so that B's are created
first and then unique A's can be generated as required.

I'm tempted to think the latter. Cloning looks easy to make mistakes
(esp if someone later adds a new field without checking if it needs to
be deep-copied etc).

Lordy
lordy - 07 Jul 2006 03:54 GMT
> If I have some code that does this during in initialisation
>
[quoted text clipped - 9 lines]
> (esp if someone later adds a new field without checking if it needs to
> be deep-copied etc).

It turns out that some comments in my 'A' class already suggested
refactoring to introduce an AFactory object for other reasons. So perhaps a third
approach would be to have

1. Create lots of Object AFactory

2. Create lots of Object B that each calls AFactory to create unique As

> Lordy
Ed Kirwan - 07 Jul 2006 12:53 GMT
>> If I have some code that does this during in initialisation
>>
[quoted text clipped - 3 lines]
>>
>> Then I later realise that the A's must be distinct for each B. Is it

Do some of your Bs use their subsets of As before they later reduce to
depending on only one A?

If not (i.e., all Bs don't use As until they know one specific A they
must depend on) then it seems wasteful to have Bs depend on entire
subsets of A instances.

>> better to use cloning or should I re-organise the code so that B's are created
>> first and then unique A's can be generated as required.
[quoted text clipped - 10 lines]
>
> 2. Create lots of Object B that each calls AFactory to create unique As

This sounds like a better solution, though I'm not sure why you need
lots of AFactory objects; I presume you'll use a factory method to
create your As, so just one instance of AFactory should suffice.

>> Lordy

Signature

www.EdmundKirwan.com - Home of The Fractal Class Composition.

Download Fractality, free Java code analyzer:
www.EdmundKirwan.com/servlet/fractal/frac-page130.html

lordy - 07 Jul 2006 17:30 GMT
>> It turns out that some comments in my 'A' class already suggested
>> refactoring to introduce an AFactory object for other reasons. So perhaps a third
[quoted text clipped - 7 lines]
> lots of AFactory objects; I presume you'll use a factory method to
> create your As, so just one instance of AFactory should suffice.

Yes. Without some more context from me its probably a little confusing.
Rather than a factory object it would be better termed a Template or a
Type object. There a several of them. An each B requires its own unique
instances of an A derived from an arbitrary subset of ATemplates
(configurable)

Thanks,

Lordy


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.