php如何实现策略模式?
interface Strategy
{
function show();
}
class MyStrategy implements Strategy
{
function show()
{
$cat = 'cat ';
echo "my name is $cat \n";
}
}
class HerStrategy implements Strategy
{
function show()
{
echo "this is dog \n";
}
}
class Context {
var $strategy ;
function __construct (Strategy $n) // 参数应该要限定类型
{
$this->strategy = $n;
}
function showName()
{
$this->strategy->show();
}
}
$st = new HerStrategy();
$co = new Context($st);
$co->showName();