• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to create composite objects

 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'd like to get some views on the best way(s) to create composite objects.
Let's say you need to instantiate class A which is declared in three classes - Composite1, Composite2 and Composite3.
I like Composite1 as member A is valid inside the constructor because it has been initialized at the point of definition.
I think I also like Composite2 (assuming that the init method is invoked after Composite2 has been instantiated). The drawback is that you can't guarantee that init will always be called.
I'm really not sure about Composite3. It will work for a trivial example like this, but is it guaranteed to always work?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Composite1 should be used when the member class A doesn't need parameterised initialization.
Composite2 should be strongly discouraged.
Composite3 is perfect for the situation where the member class A needs a parameter for it's initialization.
reply
    Bookmark Topic Watch Topic
  • New Topic