Учитывая лист и его родительский TD, сделайте печать:
$parent = new Node('td');
$child = new Leaf('Text:', 'Value');
$parent->add($child);
$parent->print();
требование к печати:
sometimes <td>Text: Value</td>
sometimes <td>Text:</td><td>Value</td>
Пока что я построил 3 решения, но ни одно не удовлетворило меня, интересно, какое из них больше ОО? И есть ли 4-й выбор?
-- Solution 1 --
// Divide the leaf object to two leaf objects
$leafText = new Leaf('Text: ');
$leafvalue = new Leaf('Value');
$parent->add($leafText);
$parent->add($leafValue);
$parent->print();
-- Solution 2 --
// Change leaf print() logic, if leaf's parent is TD, output "Text: Value",
// otherwise output "<td>Text:</td><td>Value</td>"
-- Solution 3 --
// Change parent add() logic, give leaf a variable $separate to describe if it
// should be divided
function add($child) {
if($child->separate) {
$this->parent->add($child->text);
$this->children[] = $child->value;
}
...
}