Given a leaf, and its parent TD, do a print:
$parent = new Node('td');
$child = new Leaf('Text:', 'Value');
$parent->add($child);
$parent->print();
print requirement:
sometimes <td>Text: Value</td>
sometimes <td>Text:</td><td>Value</td>
So far i constructed 3 solutions, but none satisfied me, i wonder which one is more OO? And is there a 4th choice?
-- 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;
}
...
}