To clone or not to clone..

L

lordy

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
 
L

lordy

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).

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
 
E

Ed Kirwan

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.
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

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.
 
L

lordy

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top