策略模式

<?php
header("Content-type: text/html; charset=utf-8"); 
class northcook{
    public function fan(){
        return '面条';
    }
    public function cai(){
        return '炒菜';
    }
    public function tang(){
        return '蛋花汤';
    }
}
class southcook{
    public function fan(){
        return '米饭';
    }
    public function cai(){
        return '烧菜+奶油';
    }
    public function tang(){
        return '海鲜汤';
    }
}
class fd{
    protected $fancreator=null;
    protected $caicreator=null;
    protected $tangcreator=null;
    public  function __construct($f,$c,$t){
        $this->fancreator=$f;
        $this->caicreator=$c;
        $this->tangcreator=$t;
    }
public function createfan(){
    return $this->fancreator->fan();
}
public function createcai(){
    return $this->caicreator->cai();
}
public function createtang(){
    return $this->tangcreator->tang();
}
}
$fd=new fd(new northcook,new northcook,new southcook);
echo $fd->createfan();
echo $fd->createtang();//输出“面条海鲜汤”
原文地址:https://www.cnblogs.com/aten/p/8422323.html