Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Classes and parameter passing 1

Status
Not open for further replies.

SpankySpanky

Programmer
May 25, 2005
21
0
0
AU
Hi All

I am new to Object Oriented concepts and patterns. I have a question about how one should handle passing parameters between objects.

Lets say I have a heirarchy of items, Computer which contains Drives, Drives contains Folders, Folders contain Files.

What do I do if in the code of the File, I need to find out the path to the file, which is made up of sections contained in each of the parent parts, ie. I would need Computer\Drive\Folder to give me the complete path.

Do I have to do the following:

Computer creates new Drive object and passes it a string with the computer name.
Then Drive creates a new Folder and passes the Computer name and Drive name to the Folder object.
Then the Folder creates a new file object and passes all this stuff to be stored in the File object.

It seems like good programming practice for each object to hold its own name and not the names of all the parent objects. It doesnt seem logical to duplicate all these items in the File, when it could conceivably ask its parents for it. Is there a way to ask the parent objects for this information?

I primarily use VB .net

Thanks for any advice.

 
When the parent object creates the child objects, you can pass Me to the child object constructor, rather than a String. This gets stored in a private member variable in the child. Then the child object has a reference to its parent, and can ask it for information if required.

This gives you what you want while still encapsulating the information in the object it belongs to.

Steve
 
A great idea Steve, thanks for the reply. Is this standard way that is used by object oriented programmers or should I design my solution differently somehow so this is not necessary?
 
It's pretty standard, when you have a hierarchy of objects that *genuinely* need to know who their parents are. If they don't need to know it, don't pass it in.

In your example, your hierarchy nodes could all inherit from a common class with a read-only Path property. The getter method pseudocode looks something like:
Code:
if parent not null {
   return parent.Path + 
   Me.pathseparator +
   Me.nameString 
}
else {
   return Me.pathseparator +
   Me.nameString 
}
where pathseparator is set in the constructor and is '\\' for Computers and '\' for every other kind of node.

 
You need to consider the difference between an "is a" versus "has a" relationship.

For example, a crocodile [blue]is a[/blue] reptile.
whereas a folder [blue]has a[/blue] file (or more probably many files).
The file doesn't have to be directly related to the folder but they can use each other's characteristics.
In your case I think you might also like to create a [blue]path[/blue] object that might [blue]have[/blue] folders and maybe even drives. Then the file could [blue]have a[/blue] path.
Access to all data in the objects possibly should be protected via access methods but some may argue that these objects could be considered [blue]friends[/blue] and therefore access each others data directly. Personally I prefer to keep everything separate wherever possible.

I suggest you do a little reading on the subject. I'm sure you'll find lots to read with google.


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top