2009/4/15 lightflowmark <[email protected]>:
>
> I'm having a conceptual difficulty with view helpers & decorators and was
> hoping someone could clarify this for me:
>
> When a form element (e.g. a text input) is rendered, the work of creating
> the form element markup (i.e. <INPUT TYPE=text ...>) is done by the
> 'ViewHelper' decorator, which calls a view helper set by the element itself
> (in this case, the FormText view helper).
>
> Why functionality to create the markup in a view helper, rather than have a
> 'FormText' decorator which returns the form element markup directly? Why is
> the decorator acting as a wrapper for the view helper?
This is the basis of the decorator pattern, wrapping is how it works.
The Decorator pattern allows us to add functionality to an object
dynamically at runtime. This is achieved by wrapping the object to be
decorated with a decorator class; the decorator class will have the
same API as wrapped object. By wrapping the object and replacing calls
to that object with calls to the decorator, the decorator is able to
change the behavior of the object.
Heres an example:
abstract class Math
{
abstract public function execute();
}
class base extends Math
{
public function execute()
{
return 0;
}
}
abstract class MathDecorator extends Math
{
protected $_math;
public function __construct(Math $math = null)
{
$this->_math = $math;
}
}
class AddOneDecorator extends MathDecorator
{
public function execute()
{
return $this->_math->execute()+1;
}
}
class MinusTwoDecorator extends MathDecorator
{
public function execute()
{
return $this->_math->execute()-2;
}
}
$d = new MinusTwoDecorator(
new AddOneDecorator(
new AddOneDecorator(
new AddOneDecorator(
new base()
)
)
)
);
echo $d->execute();
The output here would be 1 or 0 + 1 + 1 + 1 - 2
>
> Any insight appreciated, I'm trying to do something very similar and would
> like to understand the reasoning behind this design.
>
> Cheers,
> Mark
> --
> View this message in context:
> http://www.nabble.com/On-view-helpers---decorators-tp23054740p23054740.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
>
--
----------------------------------------------------------------------
[MuTe]
----------------------------------------------------------------------