'; } } /** * 具体策略角色C */ class ConcreteStrategyC implements Strategy { public function algorithmInterface() { echo 'algorithmInterface C
'; } } /** * 环境角色 */ class Context { /* 引用的策略 */ private $_strategy; public function __construct(Strategy $strategy) { $this->_strategy = $strategy; } public function contextInterface() { $this->_strategy->algorithmInterface(); } } /** * 客户端 */ class Client { /** * Main program. */ public static function main() { $strategyA = new ConcreteStrategyA(); $context = new Context($strategyA); $context->contextInterface(); $strategyB = new ConcreteStrategyB(); $context = new Context($strategyB); $context->contextInterface(); $strategyC = new ConcreteStrategyC(); $context = new Context($strategyC); $context->contextInterface(); } } Client::main(); ?>
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
更多技术文章请搜索千锋PHP,做真实的自己,用良心做教育。
互联网+时代,时刻要保持学习,携手千锋PHP,Dream It Possible。