<?php
abstract class strategy{
abstract function use();
}
class StrategyA extends strategy{
public function use(){
echo "这是使用策略的方法
";
}
}
class StrategyB extends strategy{
public function use(){
echo "这是使用策略的方法
";
}
}
class context{
protected $strategy;
public function setStrategy(strategy $strategy){
$this->strategy = $strategy;
}
public function use(){
$this->strategy->use();
}
}
$context = new context();
$StrategyA = new StrategyA();
$StrategyB = new StrategyB();
$context->setStrategy($StrategyA);
$context->use();
$context->setStrategy($StrategyB);
$context->use();
网友评论