This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
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 Building Microservices this week in the Design 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: 862
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
Have you checked out Aspose?
subject: A aggregration and Composition question
It's not a secret anymore!