This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Java in General and the fly likes Creating objects question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating objects question" Watch "Creating objects question" New topic
Author

Creating objects question

David Pantale
Ranch Hand

Joined: Mar 16, 2010
Posts: 32
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
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4003
    
  18

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.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating objects question
 
jQuery in Action, 3rd edition