<?php //策略模式:不一样的部分采用不一样的算法 //好比我要构建一个sphinx索引,这个索引支持多种数据源 interface indexSource{ public function makeSource($data); } class Strategy{ private $data; public function getData(){ echo "获取数据\n"; $this->data = "to add "; } public function makeSource($source_class_type){ $obj = new $source_class_type; $obj->makeSource($this->data); } } class PythonSource implements indexSource{ public function makeSource($data){ echo "构建python源\n"; } } class XmlSource implements indexSource{ public function makeSource($data){ echo "构建xml源\n"; } } $strategy_obj = new Strategy(); $strategy_obj->getData(); $strategy_obj->makeSource("PythonSource"); $strategy_obj->makeSource("XmlSource");