Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

"Is A" "Has A"

Posted on 2004-10-07
12
Medium Priority
?
207 Views
Last Modified: 2013-12-16
Just trying to get some hierachy sorted out, but could use some help.

Not sure where to use inheritance or composition.

I'm basically writing a wrapper for the COM object "Scripting.FileSystem" in ColdFusion (sorry there is no OO forum for ColdFusion ;-) but whatever I am writing it in does really not matter anyway.

File System Has A Drive
Drive Has A Folder
Sub-folder Is A Folder
Folder Has A File

Any thoughts on this?
0
Comment
Question by:Tacobell777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12255740
> Sub-folder Is A Folder

why do you need special treatment for a sub-folder?
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 12255746
Sub-folder Is A Folder
File Has A Folder
Folder Has A Drive
File System Has A Drive
Drive Has A Folder

I'm lost... ...
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 12255748
I guess its one and the same object... hmmm
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 9

Expert Comment

by:doronb
ID: 12255752
It depends on who you ask, and when you ask them ;)

I don't think there is one definite answer to this.  An example:

public class A {
  public void someMethod() {
    // Some code here...
  }
}

public class B extends A {
  public void anotherMethod() {
    // More code here..
  }
}

Now, you could do this:

  A b = new B();
  b.someMethod();

But, since b was created as an object of class A, you can't do this:

  b.anotherMethod();

But, you can however do this:

  ((B)b).anotherMethod();

Now, lets say you add this:

public class C {
  private A a = new A();

  public void someMethod() {
    a.someMethod();
  }

  public void yetAnotherMethod() {
    // More code here..
  }
}

So, creating a C object goes like this:

  C c = new C();

If you do this:

  A c = new C(); // DOES NOT COMPILE

It simply won't compile!!  But, with an instance of c, you can still use both methods, like this:

  C c = new C();
  c.someMethod();  // This seems to work like before..
  c.yetAnotherMethod();

All of that would work.  The thing composition can NOT enable you to do is to pass your instances as an object of the type they contain and use, C is just NOT an A.  Another problem is that for every method A has you wish to expose and use in C, you have to write a method to invoke the composed method.

I once created an interesting project, I tried to see if I could use composition to create an object that actually inherited from TWO parent-objects.  It worked, but whenever I wanted to pass the child object as either one of its parent objects, I had to cast it myself!

So, in conclusion, use either where ever you need it, just remember that composition isn't inheritance, so casting your object won't even work (my project was a different issue, I did some tricks to get casting to work :)
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 12255753
However there is a lot of stuff that the sub folder object does and most of the time is not required by the Folder object..
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 12255819
but I need some ideas ;-)) I know what you are saying, but surely someone else must have something similiar before.
0
 
LVL 3

Accepted Solution

by:
RuadRauFlessa earned 2000 total points
ID: 12256136
Why not ahve a handler class that represents your Filesystem and then keep everything else as simple as possilbe by going the unix routhe and call all of the rest a file? You can store everything you need to know about a folder in a file as well since a file hass all the same properties as a folder with the exception of having on disk data connected to it. For example user rights to the file or folder, size of file or folder and parent of file or folder. They all have everything in common. Remember that inherently a folder is actually a type of file that contains more files within it rather than an on  disk storage area.
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 12257420
>File System Has A Drive
>Drive Has A Folder
>Sub-folder Is A Folder
>Folder Has A File

class FileSystem
{
    public Drive getDrive(String driveLetter){...}
    public Collection getDrives(){...}
   
}


public Drive
{
   /**
     *Return collection of folders on root of drive
     */
   public Collection getFolders() {...}
}


public Folder extends File
{
   public Collection getSubFolders() {...}
}

public File /*Note this is a DIFFERENT object than java.io.File*/
{
}

ColdFusions FileSystemObject is the same object used in VBScript.  Just go to any reference site (e.g. http://www.devguru.com/Technologies/vbscript/quickref/filesystemobject.html) and copy the specification into the java classes.
All of the VB properties will have to be replaced by java get/set methods.  (e.g VB FileSystem object has Drives property your Java FileSystem will have a getDrives() method [no setDrives() because this is a readonly property]).  Each method would mostly just be a wrapper to the corresponding method in File object.  For Example:
public Collection getDrives()
{
     java.io.File[] roots=java.io.File.listRoots();

     ArrayList drives=new ArrayList(roots.length);
     for(int i=0;i<roots.length;i++) {
         Drive drive=new Drive(roots[i]);
         drives.add(drive);
     }
     
     return drives;
}
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12264424
>Not sure where to use inheritance or composition.

> File System Has A Drive
> Drive Has A Folder
> Sub-folder Is A Folder
> Folder Has A File

Well to me: All has-a are "Composition implementations" and All is-a are "Inheritance implementations"

Think of it in terms of set presentation:
                         _________________________________________
                        |FileSystem                                                       |
                        |  ____________________________________      |
                        | |Drive(s)                                                   |     |
                        | |  _______________________________      |     |
                        | | |FOLDER                                          |     |     |
                        | | |  _____________                            |     |     |
                        | | | |Folder(s)  ____|__________         |     |     |
                        | | | |              |      |                  |        |     |     |
                        | | | |  file(s)    |      |    file(s)      |        |     |     |
                        | | | |              | X   |                  |        |     |     |
                        | | | |_____________|                 |       |     |     |
                        | | |                |     Subfolder(s)    |       |     |     |
                        | | |                |_______________|       |     |     |
                        | | |______________________________|     |     |
                        | |__________________________________ |     |
                        |________________________________________|

X = Common functionalities

From this diagram, we can deduct that:
_ a Filesystem has 1..n Drives
_ each drive has EXACTLY 1 FOLDER, interestingly if I've drawn Subfolder box within Folder box then we get Folder has 0..n subfolders which is TRUE. But such drawing would losing the meaning of subfolder is-a folder. To avoid this, I divided FOLDER into Folder(s) and Subfolder(s). So FOLDER is a special one-off instance of Folder like root of a tree for example.
_ FOLDER has 0..n Folders and 0..n Subfolders
_ Subfolders and Folders have common functionalities denoted by X, hence Subfolder is-a Folder.
_ Subfolders and Folders have 0..n files.

So what am I talking about? Your implementation must obey the above deduction.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12264644
Let's simplify (and think Unix):

File System Has A Drive
Drive Has A File
Folder Is A File
Folder Has A File
File Is A File
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12274035
Now isn't that what I was saying orangehead911 and that whould be the correct way of handling things since Java does it that way as well. If you havent noticed there isn't such a thing as a Folder class in Java simply cuz the File class has everything you need for a folder pluss the extras that  makes up a file. So you see you can even represend a drive as a file sinc eyou would still reference it by it's name [Windows(C: or D: or E:)] NetWare[(SYS: DATA:)] [Linux(there is only one drive or filesystem on a unix machine and that is root / since all pysical drives get mounted to a logical naming structure and if you look at the source of Linux you would notice that even that logical system is mapped as a file)]


========== Off topic ==========
By the way orangehead911 do you by any chance own a orange 911 Porche? Just asking mate.
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12274868
Glad we could help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question