• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A aggregration and Composition question

 
Gaurav Chikara
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose we have a document system that contains folders and each folder is a set of documents and it may also contain other folders
In such kind of scenario what type of relationship can we expect between Folder class and Document class
will Folder class will point to itself showing aggregration or both Folder and Document class will inherit from some other common parent class
And to define the operations on these 2 classes like
insert document /folder
delete document /folder
classify document /folder
Can anyone pleease give his valuable comments or suggestions on such kind of problem
Thankx in advance
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like a good job for the Gang of 4 Composite pattern. From the book the Composite pattern: "Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly".
In your case individual objects are files and compositions of objects are directories (which can contain files and other directories).

You come up with a shared interface such as
public interface FileComposite {
public void delete();
public void rename();
...
}
Implement this interface for Files
public class File implements FileComposite {
public void delete();
public void rename();
...
}

And for directories
public class Directories implements FileComposite {
public void delete();
public void rename();
public void add(FileComposite fc);
...
}
Note the Directories interface allows you to add more files and directories (i.e. subdirectories) to it. That way when taking an action like delete() you client code doesn't need to know if it is dealing with a file or directory.
See the Gang of 4 book for more info on the Composite pattern
steve - http://www.jamonapi.com
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic