学习php设计模式 php实现策略模式(strategy)
《学习php设计模式 php实现策略模式(strategy)》要点: 一、意图 PHP实例 三、策略模式中主要角色 <?php /** * 抽象策略角色,以接口实现 */ interface Strategy { /** * 算法接口 */ public function algorithmInterface(); } /** * 具体策略角色A */ class ConcreteStrategyA implements Strategy { public function algorithmInterface() { echo 'algorithmInterface A<br />'; } } /** * 具体策略角色B */ class ConcreteStrategyB implements Strategy { public function algorithmInterface() { echo 'algorithmInterface B<br />'; } } /** * 具体策略角色C */ class ConcreteStrategyC implements Strategy { public function algorithmInterface() { echo 'algorithmInterface C<br />'; } } /** * 环境角色 */ 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实例 编程之家培训学院每天发布《学习php设计模式 php实现策略模式(strategy)》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |