Цитата (twin @ 12.07.2019 - 20:05) |
Отдельная просьба Ron'у, можно этот момент поподробнее? |
Цитата (Ron @ 9.07.2019 - 17:39) |
...декоратор, в отличае от твоей стратегии, позволяет бороться с комбинаторным взрывом, который в данной задаче более чем вероятнет. А точнее, он наверняка наступит с ростом функционала форматирования. |
Цитата |
А упала Б пропала. / \ / \ А упала на бок или вниз / \ Если на бок, покатилась ли дальше Если вниз, не зацепилась ли в последний момент / \ / \ |
Цитата (Ron @ 12.07.2019 - 16:57) |
twin, не понял, вот так что ли? (new ViewBold($html))->render((new ViewItalicQuote($html))->render($text)) |
Цитата (Santehnick @ 11.07.2019 - 17:28) |
У тебя слишком конкретизированный интерфейс. Добавление перечеркнутого текста приведет к изменению всего. Это бесмысленный, плохой интерфейс и как следствие весь зависящий от него код. |
///////////////////////////////////
// БИБЛИОТЕКА
interface FormatterInterface { }
interface SimpleInterface extends FormatterInterface
{
public function bold(Text $text);
public function italic(Text $text);
// Some methods
}
// ..............другие интерфейсы
class SimpleHTMLFormatter implements SimpleInterface
{
public function bold(Text $text)
{
return '<b>' . $text . '</b>';
}
public function italic(Text $text)
{
return '<i>' . $text . '</i>';
}
// Some methods
}
class SimpleBBFormatter implements SimpleInterface
{
public function bold(Text $text)
{
return '[В]' . $text . '[/В]';
}
public function italic(Text $text)
{
return '[l]' . $text . '[/l]';
}
// Some methods
}
//................. другие форматтеры
// VO
class Text
{
protected $text;
public function __construct($text)
{
$this->text = $text;
}
public function __toString()
{
return $this->text;
}
}
//////////////////////////////////
// КОНТЕКСТ
abstract class ViewBase
{
protected $formatter;
public function __construct(FormatterInterface $formatter)
{
$this->formatter = $formatter;
}
abstract public function render(Text $text);
}
class ViewHeader extends ViewBase
{
public function render(Text $text)
{
return $this->bold($text);
}
protected function bold(Text $text)
{
return $this->formatter->bold($text);
}
}
class ViewBody extends ViewBase
{
public function render(Text $text)
{
return $this->italic($text);
}
protected function italic(Text $text)
{
return $this->formatter->italic($text);
}
}
$header = new Text("Задача форматирования текста");
$body = new Text("Вот такая вот фигня. Делегирование рулит)))");
$simple = new SimpleHTMLFormatter;
echo (new ViewHeader($simple))->render($header);
echo '<br>';
echo (new ViewBody($simple))->render($body);