jeudi 1 juin 2017

Design pattern for picking implementation based on whether it can work with the input data

I have a DTO that holds filter settings (on stock, producer, model) and I need to implement SEO module that generates titles and descriptions based on the filter combinations.

  • on stock+Producer1 → "Products from Producer1 that are on stock"
  • on stock → "See what we have on stock"
  • Producer2 → "Just the best things made by Producer2"

Is there a design pattern I could use?

From my research I will definitely need Strategy. I will have OnStockAndProducerStrategy and that will build the title based on on-stock state and producer's name. And then OnStockStrategy that will build the title based on on-stock state only.

Biggest problem right now is how to select the correct strategy. My idea was to implement $strategy->isSuitable(FilterSettings $settings) that will return boolean. Then I will add the strategies to some umbrella class that will iterate over them and use the first that is suitable for current filter settings.

But I don't particularly like the complexity of ordering the strategies so that OnStockStrategy that will match on-stock + producer1 is not called before the more precise OnStockAndProducerStrategy. I thought about having also negative match - like has(on-stock) && !has(producer), but that on the other hand requires me to change all the implementations whenever I add a new parameter.

Is there a way to address the above mentioned problems?

Aucun commentaire:

Enregistrer un commentaire