美文网首页
PHP策略模式

PHP策略模式

作者: followyounger1 | 来源:发表于2017-06-04 14:33 被阅读10次

<?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();

相关文章

网友评论

      本文标题:PHP策略模式

      本文链接:https://www.haomeiwen.com/subject/hzivfxtx.html