Solved

"Is A" "Has A"

Posted on 2004-10-07
12
199 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
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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Accepted Solution

by:
RuadRauFlessa earned 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
Turn A Profile Picture Into A Cartoon Using Photoshop And Illustrator This tutorial will teach you how to make a cartoon style image out of a regular picture. I have tried to keep the tutorial as simple as possible. I used Adobe CS4 for this tuto…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The viewer will learn how to implement Singleton Design Pattern in Java.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now