abstract class Implementor {
public abstract function operation();
}
class ConcreteImplementorA extends Implementor {
public function operation() {
echo '具体实现A的方法执行<br/>';
}
}
class ConcreteImplementorB extends Implementor {
public function operation() {
echo '具体实现B的方法执行<br/>';
}
}
class Abstraction {
protected $implementor;
public function setImplementor(Implementor $implementor) {
$this->implementor = $implementor;
}
public function operation() {
$this->implementor->operation();
}
}
class RefinedAbstraction extends Abstraction {
public function operation() {
$this->implementor->operation();
}
}
$ab = new RefinedAbstraction();
$ab->setImplementor(new ConcreteImplementorA());
$ab->operation();
$ab->setImplementor(new ConcreteImplementorB());
$ab->operation();