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?