Advantages of using strategy pattern in php

前端 未结 6 885
耶瑟儿~
耶瑟儿~ 2021-02-02 13:42

I can\'t seem to get my head around what advantages the strategy pattern offer. See the example below.

//Implementation without the strategy pattern
class Regist         


        
6条回答
  •  时光说笑
    2021-02-02 14:20

    Strategy pattern helps in abstracting out the algorithmic approach for doing some particular work. Suppose you want to sort an array of numbers via different algorithms, In this case it is better to apply a strategy pattern than to tightly couple the algorithm with your code.

    In the class where you are instantiating the class composed of strategy you instantiate the strategy class reference by passing it to the constructor of composed class.

    This way you we are programming to an interface and not implementation and therefore at any point of time we can add more classes to the hierarchy of strategy class and pass the same to the class composed of strategy

    Kindly go through the following link

提交回复
热议问题