什么是策略模式
策略模式具体案列代码
<?php abstract class Strategy { abstract public function AlgorithmInterface(); } class ConcreateStratA extends Strategy { public function AlgorithmInterface() { echo "算法A"; } } class ConcreateStratB extends Strategy { public function AlgorithmInterface() { echo "算法B"; } } class ConcreateStratC extends Strategy { public function AlgorithmInterface() { echo "算法C"; } } class Context { private $_StrObj; public function __construct($strobj) { $this->_StrObj = $strobj; } public function ContextInterface() { $this->_StrObj->AlgorithmInterface(); } } $context = new Context(new ConcreateStratA); $context->ContextInterface(); $context = new Context(new ConcreateStratC); $context->ContextInterface(); $context = new Context(new ConcreateStratB); $context->ContextInterface(); ?> |