• 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

Creating objects question

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an object orientation question, which I'm not sure there really is a good general answer to (and which nobody may care about!). But here goes, when creating an object that will be contained by another object, what is the best way to create the inner object? Should you create the inner object outside the containing object and use a setObject method to place it in the containing object, or create it from within the container object(after passing in any data necessary for the creation)? Obviously this could often be situation dependent, but does anyone have an opinion as to a general preferred method?

Thanks
Dave
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like you said, it depends. If the relation between the objects is a simple association or an aggregation, then I generally prefer to create the second object externally. However, in a composition it's better to create it inside the first object.

Classes should be easy to use. If a class can do all the work by creating its own internals, it should definitely do so.
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic