What is the use of a composite template will only lead me to an array?

Recently, I worked on a tree structure, several nodes, several and increasing levels, and the print () method. At first I thought it should be Composite, then I wrote some possible designs and codes:

alt text

$struc = new Node('name0', 'id0', 'desc0'); $node1 = new Node('node1', 'id1', 'desc1'); $node2 = new Node('node2', 'id2', 'desc2'); $node3 = new Node('node3', 'id3', 'desc3'); $leaf1 = new Leaf('leaf1', 'ld1', 'lesc1'); $leaf2 = new Leaf('leaf2', 'ld2', 'lesc2'); $leaf3 = new Leaf('leaf3', 'ld3', 'lesc3'); $leaf4 = new Leaf('leaf4', 'ld4', 'lesc4'); $struc.add($node1); $struc.add($node3); $node1.add($leaf1); $node1.add($leaf2); $node1.add($node2); $node2.add($leaf3); $node3.add($leaf4); 

It looks good, I think and start coding, the print () method can follow the Iterator template later. But during coding, do I feel it is too complicated for these simple nodes? And I have to create a lot of specific classes (more than 50+ and increase). Then I stopped and thought in a simple way using an array:

 -- Structure Class -- //To be more readable and clear, array here could be //divided to 3 arrays(root/nodes/leafs), then connect //in a similar way Composite does. $struc = array('name0', 'id0', 'desc0', 'children'=>array( array('node1', 'id1', 'desc1', 'children' => array( array('leaf1', 'ld1', 'lesc1'), array('leaf2', 'ld2', 'lesc2'), array('node2', 'id2', 'desc2', 'children'=>array(array('leaf3', 'ld3', 'lesc3')) ) ) ), array('node3', 'id3', 'desc3', 'children' => array(array('leaf4', 'ld4', 'lesc4')) ) ) ); function print($node = $this->struct) { ... if(isset($node['children'])) $this->print($node['children']); ... } 

The two projects look very similar, now I'm a little confused, what is the value of the Composite pattern, have I missed something important from this template?

+6
php design-patterns
source share
3 answers

A compound structure point must be able to process a collection of objects as if it were a single object (for example, to display it or write it to a file). When you write yourself, the print () method may follow the Iterator template later "- well, the point of the composite template would be that you could call print () without worrying about whether you are printing a single component or iterating through whole tree.

But it looks like you didn’t understand object-oriented programming at all, as you plan to use nested arrays instead. The value of using objects instead of hashes (for which all PHP arrays are) for all types of security , which greatly facilitates debugging and maintenance of programs.

+6
source share

the meaning of the composite value is that you trade with some complexity so as not to break encapsulation .

In your version of the array, you break encapsulation, since if you node is not a leaf:

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

with composite you can say:

 print(); 

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

 class Node { void print() { for (child in children) { child.print(); } } ... } class Leaf { void print() { // print it! } } 

Another advantage over a simple array is that you hide the details of your implementation (data structure, etc.)

+7
source share

Isn't array implementation much more complicated for you? If I look at this, I need to study it closer to understand what you are doing. You deal with indexes, assign entries to certain indexes, etc ... it looks very complex.

Your implementation of the composite template on another site looks simple and natural when you look at the code in which you use it. You have a node, and you attach other nodes or sheets to it. Nothing complicated, strange, I don't need to care / remember indexes, etc. This is why a compound template is useful in your case. Of course, its implementation may look a little more complicated if you are not used to it, but the important point (like the others stated) is that you hide the details. This is very important. This is a simple example that can still work with your arrays, but when you implement such code in a production environment where, perhaps, other developers can edit / maintain your code. If someone needs to change your array decision, it is much more likely that he will introduce new errors.

0
source share

All Articles