Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

composit pattern? how to travers upwards

 
Nermin H.
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().



 
Nermin H.
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ner min
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
# 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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic