Reputation: 488
I was watching a Lynda.com PHP Design Patterns tutorial when the tutor presented this solution to a challenge:
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);
}
}
Why is this a decorator pattern? All I see is the instantiation of an object and accessing two of the objects methods.
Upvotes: 2
Views: 540
Reputation: 465
What you did is not a decorator pattern. You just created Utility class for string if you really want to convert your utility class into decorator then just see below:
<?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();
This are your case examples If you need more real world example see here, I gave here good example from real world.
Upvotes: 1
Reputation: 70701
The example you posted is not a decorator, it simply provides a new interface (lower
and uppercase
) on top of a string. A decorator adds behaviour without changing the underlying interface.
A typical example of decorators is literally decorating graphical elements. For example:
interface Shape {
draw();
}
class Rectangle implements Shape { ... }
class Circle implements Shape { ... }
class BorderDecorator implements Shape {
Shape shape;
draw() {
shape.draw();
drawBorder();
}
}
All of the classes above have the same interface, so if a function needs a Shape
, you can pass in a plain old Rectangle
or you can wrap it in a BorderDecorator
to get a rectangle with a border.
Upvotes: 2