-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrategy.php
39 lines (38 loc) · 1.12 KB
/
Strategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
interface Strategy { // Abstract policy role, implemented by interface
public function do_method(); // Algorithm interface
}
class ConcreteStrategyA implements Strategy { // Specific strategy role A
public function do_method() {
echo 'do method 1';
}
}
class ConcreteStrategyB implements Strategy { // Specific strategy role B
public function do_method() {
echo 'do method 2';
}
}
class ConcreteStrategyC implements Strategy { // Specific strategy role C
public function do_method() {
echo 'do method 3';
}
}
class Question{ // Environmental role
private $_strategy;
public function __construct(Strategy $strategy) {
$this->_strategy = $strategy;
}
public function handle_question() {
$this->_strategy->do_method();
}
}
// client
$strategyA = new ConcreteStrategyA();
$question = new Question($strategyA);
$question->handle_question();
$strategyB = new ConcreteStrategyB();
$question = new Question($strategyB);
$question->handle_question();
$strategyC = new ConcreteStrategyC();
$question = new Question($strategyC);
$question->handle_question();