Solved

"Is A" "Has A"

Posted on 2004-10-07
12
203 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 88
expectj telnet failing 5 45
how to see all occupied ports on windows 10 laptop 15 76
Java Eclipse Loop 3 21
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

838 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