Strategy pattern

 0    8 fiche    tomekmarczak1
Télécharger mP3 Imprimer jouer consultez
 
question réponse
Wzorzec strategii jest również znany jako wzorzec polityki.
commencer à apprendre
Strategy pattern is also known as Policy Pattern.
Definiujemy wiele algorytmów i pozwalamy aplikacji klienckiej przekazać algorytm, który będzie używany jako parametr.
commencer à apprendre
We define multiple algorithms and let client application pass the algorithm to be used as a parameter.
W naszym przykładzie spróbujemy zaimplementować prosty koszyk
commencer à apprendre
For our example, we will try to implement a simple Shopping Chart
mamy dwie strategie płatności - za pomocą karty kredytowej lub za pomocą PayPal.
commencer à apprendre
we have two payment strategies - using Credit Card or using PayPal.
Na początek stworzymy interfejs dla naszego wzorca strategii
commencer à apprendre
First of all we will create the interface for our strategy pattern
Teraz będziemy musieli stworzyć konkretną implementację algorytmów płatności kartą kredytową/debetową lub poprzez PayPal.
commencer à apprendre
Now we will have to create concrete implementation of algorithms for payment using credit/debit card or through paypal.
Zauważ, że metoda płatności koszyka wymaga algorytmu płatności jako argumentu i nie przechowuje go nigdzie jako zmiennej instancji.
commencer à apprendre
Notice that payment method of shopping cart requires payment algorithm as argument and doesn’t store it anywhere as instance variable.
Wzorzec strategii jest przydatny, gdy mamy wiele algorytmów dla konkretnego zadania i chcemy, aby nasza aplikacja elastycznie wybierała dowolny algorytm w czasie wykonywania dla konkretnego zadania.
commencer à apprendre
Strategy pattern is useful when we have multiple algorithms for specific task and we want our application to be flexible to chose any of the algorithm at runtime for specific task.

Vous devez vous connecter pour poster un commentaire.