Strategy pattern

 0    8 flashcards    tomekmarczak1
mp3 indir Baskı oynamak kendini kontrol et
 
soru cevap
Wzorzec strategii jest również znany jako wzorzec polityki.
öğrenmeye başla
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.
öğrenmeye başla
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
öğrenmeye başla
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.
öğrenmeye başla
we have two payment strategies - using Credit Card or using PayPal.
Na początek stworzymy interfejs dla naszego wzorca strategii
öğrenmeye başla
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.
öğrenmeye başla
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.
öğrenmeye başla
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.
öğrenmeye başla
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.

Yorum yapmak için giriş yapmalısınız.