时间:2021-07-01 10:21:17 帮助过:21人阅读
/**
* Colleague.
*/
class NullFilter implements Filter
{
public function filter($value)
{
return $value ? $value : '';
}
}
/**
* Colleague.
*/
class HtmlEntitiesFilter implements Filter
{
public function filter($value)
{
return htmlentities($value);
}
}
/**
* The Mediator. We avoid referencing it from ConcreteColleagues
* and so the need for an interface. We leave the implementation
* of a bidirectional channel for the Observer pattern's example.
* This class responsibility is to store the value and coordinate
* filters computation when they have to be applied to the value.
* Filtering responsibilities are obviously a concern of
* the Colleagues, which are Filter implementations.
*/
class InputElement
{
protected $_filters;
protected $_value;
public function addFilter(Filter $filter)
{
$this->_filters[] = $filter;
return $this;
}
public function setValue($value)
{
$this->_value = $this->_filter($value);
}
protected function _filter($value)
{
foreach ($this->_filters as $filter) {
$value = $filter->filter($value);
}
return $value;
}
public function getValue()
{
return $this->_value;
}
}
$input = new InputElement();
$input->addFilter(new NullFilter())
->addFilter(new TrimFilter())
->addFilter(new HtmlEntitiesFilter());
$input->setValue(' You should use the-
tags for your headings.');
echo $input->getValue(), "\n";
http://www.bkjia.com/PHPjc/327553.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327553.htmlTechArticle调解者模式,这个模式的目的是封装一组对象之间的相互作用,防止对象之间相互干扰,调解者(Mediator)在同事对象(Colleague)之间充当...