Strategy pattern

 0    8 schede    tomekmarczak1
Scarica mp3 Stampa Gioca Testa il tuo livello
 
Domanda Risposta
Wzorzec strategii jest również znany jako wzorzec polityki.
inizia ad imparare
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.
inizia ad imparare
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
inizia ad imparare
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.
inizia ad imparare
we have two payment strategies - using Credit Card or using PayPal.
Na początek stworzymy interfejs dla naszego wzorca strategii
inizia ad imparare
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.
inizia ad imparare
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.
inizia ad imparare
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.
inizia ad imparare
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.

Devi essere accedere per pubblicare un commento.