Created
August 1, 2012 13:48
-
-
Save lornajane/3227011 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Interface WeatherInterface { | |
public function getWeather(); | |
} | |
class WeatherBot implements WeatherInterface { | |
public function getWeather() { | |
// imagine something more complicated | |
return 'Sunny'; | |
} | |
} | |
class WordyDecorator implements WeatherInterface | |
{ | |
protected $weatherInterface; | |
public function __construct (WeatherInterface $weather) { | |
$this->weatherInterface = $weather; | |
} | |
public function getWeather () { | |
$string = 'Today it is: ' . $this->weatherInterface->getWeather(); | |
return ($string); | |
} | |
} | |
class BorderDecorator implements WeatherInterface | |
{ | |
protected $weatherInterface; | |
public function __construct (WeatherInterface $weather) { | |
$this->weatherInterface = $weather; | |
} | |
public function getWeather () { | |
$string = '~{' . $this->weatherInterface->getWeather() . '}~'; | |
return ($string); | |
} | |
} | |
class NewLineDecorator implements WeatherInterface { | |
protected $weatherInterface; | |
public function __construct (WeatherInterface $weather) { | |
$this->weatherInterface = $weather; | |
} | |
public function getWeather () { | |
$string = $this->weatherInterface->getWeather() . "\n"; | |
return ($string); | |
} | |
} | |
$weather = new NewLineDecorator(new WordyDecorator(new WeatherBot)); | |
echo $weather->getWeather(); | |
$weather = new NewLineDecorator(new BorderDecorator(new NewLineDecorator(new WordyDecorator(new WeatherBot)))); | |
echo $weather->getWeather(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment