While reviewing the decorator pattern in chapter six, I notice that the decorating object actually modified the original object data.
So my question is would it be better to have the decorator not modify the original object?
A possible way of doing this is:
PHP Code:
class CDTrackListDecoratorCaps
{
private $_cd;
public function __construct(CD $cd)
{
$this->_cd = $cd;
}
public function __toString()
{
$output = '';
foreach ($this->_cd->trackList as $track) {
$output .= ($num +1) . ") ".strtoupper($track)." ";
}
return $output;
}
}
And then use the code like so...
PHP Code:
$myCD = new CD;
$myCD->addTrack('Song A');
$cdDecorator = new CDTrackListDecoratorCaps($myCD);
echo $cdDecorator;
This allows for a more flexible use of the CD instance because I am not changing the values within the instance.
Am I wrong and completely insane?
