+6  A: 

the value of composite is that you trade some complexity for being able to not break encapsulation.

In your array version you are breaking encapsulation since you are testing if the node is not a leaf:

if(isset($node['children'])) $this->print($node['children']);

with composite you can say:

print();

then run-time polymorphism will call the right method. In this case (I'm not a PHP programmer so let me to use a Java-like syntax):

class Node {

   void print() {
       for (child in children) {
          child.print();
       } 
   }

   ...
}

class Leaf {

   void print() {
       // print it! 
   }
}

another advantage over plain array is that you are hiding your implementation details (data structure, etc)

dfa
I tried to understand, but still cannot catch the spirit. Below is my thoughts, feel free to point my mistakes, i also corrected some mistakes in my codes.1. "break encapsulation", what i'm confused is if i'm not treat the node as an entity, is it still a encapsulation breaking? and what problem will it cause at last?2. i do think implementation details are hidden in class Structure, is it not so? 3. I still can't find the advantage of Composite, no matter how i modify this tree, the prices of code changes took by two ways seems the same.
Edward
now the code looks better: 1. with composite you don't distinguish operations on nodes from operations on leafs 2. now it is clear that your array is encapsulated in a class; in your original question it isn't
dfa
+4  A: 

The point of the composite pattern is to be able to treat a collection of objects as if it were a single object (e.g. for displaying it or writing it to a file). As you write yourself "print() method may follow Iterator pattern later" - well, the point of the composite pattern would be that you could call print() without having to worry about whether you're printing a single Component or have to iterate through an entire Tree.

But it looks as though you're unclear about object-oriented programming in general, since you're considering using nested arrays instead. The value of using objects instead of hashes (which PHP arrays are) for everything is type safety, which makes it much easier to debug and maintain programs.

Michael Borgwardt
You are right, i was a long time C programmer, when in OO programming, i follow many principle and pattern cos i know they are important, but i don't really know why they are important. In my understanding, OO is mostly the way to make program easy to maintain to change, but usually i can't see, even don't know if it is effective. in this example, for e.g., i still can't see how lower price which Composite is supposed to provide, add/remove nodes, change order, aren't the two of codes changes seems the same?
Edward
Think of it like this: using arrays instead of objects is similar to a C program that uses void* for all method parameters.
Michael Borgwardt
I feel from you a different way of thinking, think about the Array implement, i think it is an OO implement, just not Composite, is it not?I can understand the example you said, but it's no help for me to understand by using objects instead of array, in this example, the benefits?
Edward
As I said: the benefit of using objects is type safety. The array-based implementation has the same structure, but it completely loses type safety and thereby most benefits of OO as well as the Composite pattern. It probably doesn't matter much in such a simple case, but when structures get more complex, using arrays for everything means that you're more likely to make mistakes (since one array looks like any other to you, the compiler, and the runtime) and they're far harder to debug.
Michael Borgwardt
Wow, great point, by searching it on google, i find some interesting discussions on JAVA arrays, PHP type-safety. and this, "PHP is a loosely typed language. This means that any variable (e.g. $x) can be of ANY type. On one line, it might be an integer, but on the next line you could treat it as string (i often do so in my program). Loose typing, while arguably less ‘safe’ from the standpoint of runtime integrity, allows PHP to do lots of neat tricks with arrays that make them very efficient - in some situations." Thanks for emphasize such valuable point, i now learn more about it, and PHP
Edward
A: 

Doesn't the array implementation look a lot more complex to you? If I'm looking at it, I have to study it closer to understand what you're doing. You're dealing with indexes, assigning entries to specific indexes etc...it looks really complex.

Your implementation of the composite pattern on the other site looks simple and natural when you're looking at the code where you're using it. You have a node, and you attach other nodes or leafs to it. Nothing complex, strange, I don't have to care/remember indexes etc...That's why the composite pattern is useful in your case. Of course, its implementation may look a bit complex to you, if you're not accustomed to it, but the important point (as also other stated) is that you're hiding details. This is very important. It a simple example it may still work with your arrays, but when you implement such code in a production environment where possibly also other developers are editing/maintaining your code. If someone has to modify your "array" solution, it is much more probable that he'll introduce new bugs.

Juri
I corrected some mistakes in former codes, now it should be more close to my mind.
Edward
A: 

can u post the code for Tree structure implementation in java using composite design pattern.

Deepak