<?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");