To clone or not to clone..

Discussion in 'Java' started by lordy, Jul 7, 2006.

  1. lordy

    lordy Guest

    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, Jul 7, 2006
    #1
    1. Advertising

  2. lordy

    lordy Guest

    On 2006-07-07, lordy <> wrote:
    > 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

    > Lordy
    lordy, Jul 7, 2006
    #2
    1. Advertising

  3. lordy

    Ed Kirwan Guest

    lordy wrote:
    > On 2006-07-07, lordy <> wrote:
    >> 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


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


    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




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

    Download Fractality, free Java code analyzer:
    www.EdmundKirwan.com/servlet/fractal/frac-page130.html
    Ed Kirwan, Jul 7, 2006
    #3
  4. lordy

    lordy Guest

    On 2006-07-07, Ed Kirwan <> wrote:
    >>
    >> 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.


    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
    >
    >
    >
    lordy, Jul 7, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mdb
    Replies:
    1
    Views:
    757
  2. Konrad Eisele

    Arm clone version 0_8

    Konrad Eisele, Apr 7, 2004, in forum: VHDL
    Replies:
    0
    Views:
    660
    Konrad Eisele
    Apr 7, 2004
  3. Kohn J. Wrahn
    Replies:
    0
    Views:
    2,882
    Kohn J. Wrahn
    May 25, 2005
  4. Felix

    clone a datagrid

    Felix, Dec 4, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,864
    Felix
    Dec 4, 2003
  5. Chris Becker

    How to clone/duplicate a TableCell object

    Chris Becker, Apr 1, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    641
    Chris Becker
    Apr 1, 2004
Loading...

Share This Page