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();
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();
相关文章推荐
- 一种PHP设计模式:DPT
- 架构师常用的五种 PHP 设计模式
- [介绍]PHP设计模式:DAO(数据访问对象模式)
- 五种常见的 PHP 设计模式
- php设计模式介绍之值对象模式第1/5页
- 一种PHP设计模式:DPT
- 介绍php设计模式中的工厂模式
- 一种PHP设计模式:DPT
- 一种PHP设计模式:DPT
- [PHP设计模式]Composite(组合)模式范例
- [转]五种常见的PHP设计模式
- 五种常见的PHP设计模式
- 五种常见的PHP设计模式(择自互联网非本人所写)
- [转]五种常见的PHP设计模式
- 五种常见的 PHP 设计模式
- [介绍]PHP设计模式:DAO(数据访问对象模式)
- 五种常见的PHP设计模式
- php设计模式-中级
- php设计模式介绍之编程惯用法第1/3页
- 介绍php设计模式中的工厂模式