我最近在研究树结构、多个节点、多个可增加的级别以及一个 print() 方法。 起初,我认为它应该是一个 Composite,然后我写下了一些可能的设计和代码:
$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);
看起来不错,我认为并开始编码,print() 方法稍后可能会遵循 Iterator 模式。 但是在编码过程中,我觉得对于这些简单的节点来说是否太复杂了?而且我必须实例化很多具体类(超过 50+,并且还在增加)。然后我停下来并通过使用数组思考一种简单的类似方式:
-- 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']);
...
}
这两个设计看起来很相似,现在我有点困惑,复合模式的值(value)是什么,我是否错过了这个模式的一些重要内容?
最佳答案
复合的值(value)在于您可以用一些复杂性换取不破坏封装。
在你的数组版本中,你打破了封装,因为你是 测试节点是否不是叶子:
if(isset($node['children'])) $this->print($node['children']);
对于复合你可以说:
print();
然后运行时多态性将调用正确的方法。在这种情况下(我不是 PHP 程序员,所以让我使用类似 Java 的语法):
class Node {
void print() {
for (child in children) {
child.print();
}
}
...
}
class Leaf {
void print() {
// print it!
}
}
与普通数组相比的另一个优势是您隐藏了实现细节(数据结构等)
关于php - Composite 模式比 Array 给我带来什么优势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1254356/