File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes A aggregration and Composition question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "A aggregration and Composition question" Watch "A aggregration and Composition question" New topic

A aggregration and Composition question

Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
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

SCJP,SCWCD,SCBCD<br />If Opportunity doesn't knock then build the door
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
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 - - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
I agree. Here's the link:
subject: A aggregration and Composition question
Similar Threads
Package Statement in Java
Final packaging
Windows Problem
applet path
Association, Composition and Aggregration