Design Pattern Strategy. Simple example implementation in PHP

Pubblicato il: 01 gennaio 2022
sul canale di: Programming Art
28,670
79

The strategy design pattern is a type of behavioural pattern. Behavioural patterns are used to address how an application runs. As a comparison, the factory pattern can change the object type on the fly.

The Strategy design pattern comes in handy when we have multiple chunks of code performing similar operations. It defines an encapsulated and interchangeable family of algorithms(an algorithm just being a process or set of code used to solve a problem).

The strategy design pattern is most useful in situations where you have classes that may be similar, but not related, and differ only in their specific behaviour. For example, say you need a filtering system for strings, Different filters might include

Stripping out HTML
Crossing out swear words
Catching character combinations that can be used to send spam through contacts form and the like.
The only quality that these three approaches have in common is that they are all applied to strings, other than that there are no commonalities that might suggest they would make sense as derived subclasses from a base class (factories).

Furthermore, these filters might be applied differently on the fly. For example, an application switch might indicate whether or not HTML or swear words are allowed in the text. This, then, is a good candidate for the strategy pattern.

#designpatterns
#designpatterntutorial
#designpatternsinphp
#php
#programmingart
#designpatternstrategy
#phpprogramming
#phptutorial
#learnphp
#bestdesignpatterns
#howtousedesignpatterns
#designpatternstutorial


In questa pagina del sito puoi guardare il video online Design Pattern Strategy. Simple example implementation in PHP della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Programming Art 01 gennaio 2022, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 28,670 volte e gli è piaciuto 79 spettatori. Buona visione!