您的位置:首页 > 编程语言 > PHP开发

php设计模式

2016-10-14 15:26 85 查看
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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: