Created
May 11, 2015 12:32
-
-
Save ichiriac/e5dd520a0be57ca163f7 to your computer and use it in GitHub Desktop.
decorator pattern
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 Decorable { | |
public function doSomething(); | |
} | |
interface Decorator extends Decorable { | |
public function __construct(Decorable $target); | |
} | |
class Something implements Decorable { | |
public function doSomething() { | |
return true; | |
} | |
} | |
class Somehow implements Decorator { | |
protected $target; | |
public function __construct(Decorable $target) { | |
$this->target = $target; | |
} | |
public function doSomething() { | |
// do some stuff before or after calling the method | |
return $this->target->doSomething(); | |
} | |
} | |
// RUN ... | |
$foo = new Something(); | |
$bar = new Somehow($foo); | |
$bar->doSomething(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment