2.策略模式和简单工厂结合

    1. // 只需要修改上方的Context类
    2. class Context
    3. {
    4. private $strategy;
    5. function __construct($operation)
    6. {
    7. $this->strategy = new ConcreteStrategyA();
    8. break;
    9. case 'b':
    10. $this->strategy = new ConcreteStrategyB();
    11. break;
    12. case 'c':
    13. $this->strategy = new ConcreteStrategyC();
    14. break;
    15. public function contextInterface()
    16. {
    17. return $this->strategy->AlgorithmInterface();
    18. }
    19. }
    20. //客户端代码

    总结:

    上一章:

    下一章:第三章 拍摄UFO --- 单一职责原则