家庭教師がチャレンジにこの解決策を提示したときに、私はLynda.com PHPデザインパターンのチュートリアルを見ていた。これはデコレータパターン(PHP)ですか?
のindex.php:
include_once 'decorator.php';
$object = new Decorator();
$object->sentence = "This is a sample sentence that we're going to manipulate in the Decorator.";
// This should output: this is a sample sentence that we're going to manipulate in the decorator.
echo $object->lower();
// This should output: THIS IS A SAMPLE SENTENCE THAT WE'RE GOING TO MANIPULATE IN THE DECORATOR.
echo $object->uppercase();
decorator.php:
class Decorator
{
public $sentence = '';
public function lower()
{
return strtolower($this->sentence);
}
public function uppercase()
{
return strtoupper($this->sentence);
}
}
なぜこれはデコレータパターンですか?私が見るのは、オブジェクトのインスタンス化とオブジェクトメソッドの2つにアクセスすることだけです。
クラスのこのレベルでは、実際には無意味ですが、拡張を開始すると、複雑になり、テキスト操作に特化します。 –