Strategy pattern

 0    8 Datenblatt    tomekmarczak1
mp3 downloaden Drucken spielen überprüfen
 
Frage Antworten
Wzorzec strategii jest również znany jako wzorzec polityki.
Lernen beginnen
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.
Lernen beginnen
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
Lernen beginnen
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.
Lernen beginnen
we have two payment strategies - using Credit Card or using PayPal.
Na początek stworzymy interfejs dla naszego wzorca strategii
Lernen beginnen
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.
Lernen beginnen
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.
Lernen beginnen
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.
Lernen beginnen
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.

Sie müssen eingeloggt sein, um einen Kommentar zu schreiben.