Decorator
■コード
Text.php
interface Text {
public function getText();
public function setText($str);
}
PlainText.php
class PlainText implements Text
{
private $_textString = null;
public function getText()
{
return $this->_textString;
}
public function setText($str)
{
$this->_textString = $str;
}
}
TextDecorator.php
内部にテキストオブジェクトを保持し同様のインターフェースを提供する。
abstract class TextDecorator implements Text
{
private $_text;
public function __construct(Text $target) {
$this->_text = $target;
}
public function getText() {
return $this->_text->getText();
}
public function setText($str) {
$this->_text->setText($str);
}
}
UpperCaseText.php
class UpperCaseText extends TextDecorator {
public function __construct(Text $target) {
parent::__construct($target);
}
public function getText() {
$str = parent::getText();
$str = strtoupper($str);
return $str;
}
}
DoubleByteText.php
class DoubleByteText extends TextDecorator {
public function __construct(Text $target) {
parent::__construct($target);
}
public function getText() {
$str = parent::getText();
$str = mb_convert_kana($str, 'RANSKV');
return $str;
}
}
$text = new PlainText();
$text->setText('This is a pen.');
$dbText = new DoubleByteText($text);
var_dump($dbText->getText());//string(42) "This is a pen."
$ucText = new UpperCaseText($text);
var_dump($ucText->getText());//string(14) "THIS IS A PEN."
TrackBack URL :
Comments (0)
コメントはまだありません»
コメントはまだありません。
この投稿へのコメントの RSS フィード。TrackBack URL
コメントする