2016-05-27 5 views
2

家庭教師がチャレンジにこの解決策を提示したときに、私は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つにアクセスすることだけです。

+0

クラスのこのレベルでは、実際には無意味ですが、拡張を開始すると、複雑になり、テキスト操作に特化します。 –

答えて

1

投稿した例はデコレータではなく、文字列の上に新しいインターフェイス(loweruppercase)を提供するだけです。デコレータは、基本となるインタフェースを変更することなく動作を追加します。

デコレータの典型的な例は、文字通りグラフィック要素を装飾しています。たとえば、次のように

interface Shape { 
    draw(); 
} 

class Rectangle implements Shape { ... } 
class Circle implements Shape { ... } 

class BorderDecorator implements Shape { 
    Shape shape; 

    draw() { 
    shape.draw(); 
    drawBorder(); 
    } 
} 

クラスのすべての機能がShapeを必要とする場合、あなたは昔ながらのRectangleに渡すことができますまたはあなたが持つ矩形を取得するためにBorderDecoratorでそれをラップすることができますので、上記の、同​​じインタフェースを持っています境界。

0

あなたがしたことは、デコレータパターンではありません。あなたがhereを参照してください、より現実世界の例が必要な場合は

<?php 

interface StringInterface 
{ 
    public function string(); 
} 

class StringDecorator implements StringInterface 
{ 
    private $text; 

    public function __construct($text) 
    { 
     $this->text = $text; 
    } 
    public function string() 
    { 
     return $this->text ; 
    } 
} 


class StringLower implements StringInterface 
{ 
    private $text; 
    public function __construct(StringInterface $text) 
    { 
     $this->text = $text; 
    } 
    public function string() 
    { 
     return strtolower($this->text); 
    } 
} 
class StringUpper implements StringInterface 
{ 
    private $text; 
    public function __construct(StringInterface $text) 
    { 
     $this->text = $text; 
    } 
    public function string() 
    { 
     return strtoupper($this->text); 
    } 
} 
class StringTrim implements StringInterface 
{ 
    private $text; 
    public function __construct(StringInterface $text) 
    { 
     $this->text = $text; 
    } 
    public function string() 
    { 
     return trim($this->text); 
    } 
} 

// For Lowercase 
$decorator=new StringLower(new StringDecorator("Text")); 
// Output: Lowercase Text to text 
$text=$decorator->string(); 

// For Uppercase 
$decorator= new StringUpper(new StringDecorator("text")); 
// Output: Uppercase text to Text 
$text=$decorator->string(); 

//TO normalized text from " TeXT " to text. 
$decorator= new StringTrim(new StringLower(new StringDecorator(" TeXT "))); 
// Output: normalized text from " TeXT " to text also extra space removed from left and right. 
$text=$decorator->string(); 

これはあなたのケースの一例であり、私はここで良い例を与えた:あなたが本当にあなたのユーティリティクラスはデコレータに変換したい場合は、ちょうどそのすぐ下を参照文字列のためのユーティリティクラスを作成しました現実世界から。