Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lornajane
Created August 1, 2012 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lornajane/3227011 to your computer and use it in GitHub Desktop.
Save lornajane/3227011 to your computer and use it in GitHub Desktop.
<?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