Last active
August 29, 2015 14:24
-
-
Save mhlavac/465a0efc33ddaae61218 to your computer and use it in GitHub Desktop.
Container vs Composite vs Chain
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 Greeter | |
{ | |
public function greet($name); | |
} | |
class ChainGreeter implements Greeter | |
{ | |
private $greeters = []; | |
public function addGreeter(Greeter $greeter) | |
{ | |
$this->greeters[] = $greeter; | |
} | |
public function greet($name) | |
{ | |
foreach ($this->greeters as $greeter) { | |
$greeter->greet($name); | |
} | |
} | |
} | |
class StringGreeter() implements Greeter | |
{ | |
private $sentence; | |
public function __construct($sentence) | |
{ | |
$this->sentence = $sentence; | |
} | |
public function greet($name) | |
{ | |
spritnf($sentence, $name); | |
} | |
} | |
$chainGreeter = new ChainGreeter(); | |
$chainGreeter->add(new StringGreeter('Hello %s')); | |
$chainGreeter->add(new StringGreeter('Good morning %s')); | |
$chainGreeter->greet('John Doe'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@pavel-dohnal-momentumft I also thought it's a composite, because this is how usuall composites are implemented.
So in the end I came with 2 ideas.
Command
where we can use just __invoke. So it feels almost like namedclosure
.And simple
Greeters
, because it's just multiple Greeters anyway.What do you think? ;-)