aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes composit pattern? how to travers upwards 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 "composit pattern? how to travers upwards" Watch "composit pattern? how to travers upwards" New topic
Author

composit pattern? how to travers upwards

Nermin H.
Greenhorn

Joined: Sep 14, 2005
Posts: 2
Hi i simplified the composit Pattern from HFDP to the most basic level and this is what i came up with:



But i cant figure out how to traverse upwards in the hierarchy?
i tried everything to make a method like
mm.printfullPath(); //and or prog.printfullPath();
what should give me back a path like "c:\Programme"

is that possible if we follow the composit Pattern
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
Not sure I understand your point entirely. Are you trying to get each Component to contribute its piece to the fullpath. Couldn't you get each getName() to concatenate the next in the chain. You could either have this done at the getName() method of the composite or some other method like getFullPath().





http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Nermin H.
Greenhorn

Joined: Sep 14, 2005
Posts: 2
after i created the tree like this

Component root = new Folder("c:");
Component prog = new Folder("Programme");
Component mm = new Folder("Macromedia");
root.add(prog);
root.add(new Folder("Winnt"));
root.add(new File("win.ini"));
prog.add(mm);
mm.add(new File("flash.exe"));
mm.add(new File("freehand.exe"));

for example: mm is attached to prog and prog to root
so i need the method getFullpath
so that i can say mm.getFullpath();
ant that should return "c rogramme"
in other words it should return all parent nodes (starting from the calling node).
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
That's simply not what the Composite pattern is good for. In the typical implementation of the Composite pattern, child nodes don't know that they are part of a composite, and clients don't know that they are dealing with a composite either. Clients always only talk to the root node.

If want to call a method on a child and the childs behaviour depend on its parents, Composite simply won't help you at all.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ner min
Ranch Hand

Joined: Sep 14, 2005
Posts: 76
# tnx Ilja , that helped

i was actually trying to rewrite my navigation structur with composit pattern(just to be cool )
but that doesnt sound like the way to go. And if in composit pattern

Clients always only talk to the root node.

i m wondering what a practical exampels of composit? Can u tell me a couple of problems that composit solves?
I maen talking ONLY to u rootnood is probably very rarey a whole solution?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Ner min:
i m wondering what a practical exampels of composit? Can u tell me a couple of problems that composit solves?
I maen talking ONLY to u rootnood is probably very rarey a whole solution?


That's actually at the core of the Composite pattern - make clients work with a collection of objects whithout noticing it.

An example where I recently used it:

We have a custom dialog framework, where a dialog contains of a message about the current state of the dialog (such as an error message or a prompt to do something), a "dialog page" that contains the actual content, and buttons at the bottom.

There is an interface you have to implement which is called IDialogPage, which methods such as getContent (which returns a JComponent for the center of the dialog), getCurrentMessage and methods to determine which buttons should be active etc.

So typically what you do is implement such an IDialogPage and give it to the dialog implementation.

Recently, though, what I wanted to show several existing IDialogPage implementation in one dialog, organized in a JTabbedPane. What I did was implementing a CompositeDialogPage which was instantiated with an array of IDialogPages. The composite page created the JTabbedPane from the contents of those other pages, decided which message to show based on which tab was currently visible etc.

The dialog itself didn't need to be changed at all - it still worked on a simple IDialogPage implementation (the composite page). The fact that this single dialog was effectively working with a number of dialog pages at once was totally encapsulated by use of the Composite pattern.

Does that help?
Ner min
Ranch Hand

Joined: Sep 14, 2005
Posts: 76
hi Ilja, tnx

hmm.. i'm more servlet/jsp developer so my gui, swing etc. knowledge is pretty basic but even so i thing i got the idea. But again i thing that�s r very special cases, i mistake was that i was understood/hoped that with a composite-pattern I got the general-weapon for handling tree-like-(whole-part)structures. Ok,ok, it was just a dream
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: composit pattern? how to travers upwards
 
Similar Threads
how come int to String ?
My this jsp page is not working
Problem with BeanUtils
Couldn't get Menu Composite Iterator (HeadFirst Design Pattern) to work
need good example of hibernate with composit key