In this article, we will understand what Strategy Pattern is and when we actually need to use it, along with a practical example and real-life use case.
This pattern falls under the category of behavioral pattern and as the name suggests, it allows clients to choose an algorithm from a set of algorithms at run time.
Strategy pattern defines a family of algorithms, encapsulates each one of them and makes them interchangeable at run time within that family.
Now let's understand what each one of them actually means.
Family of Algorithms:
This means this pattern provides a set of algorithms using one of which at run time you can achieve the desired output.
Encapsulate each one of the:
This pattern allows you to place your algorithms in different classes (encapsulate them).
Makes the algorithm interchangeable:
The beauty with strategy pattern is that we can select at run time which algorithm we want to apply to our object and can also replace them with one another.
These are the three main points of Strategy pattern. I hope we are clear with the above- discussed points. So, let's understand one real -world scenario where it can be really helpful to use.
Example
Suppose you want to order food online and apply one of the available coup. In this case you can implement the Strategy Pattern.
Family of Algorithms:
This means this pattern provides a set of algorithms using one of which at run time you can achieve the desired output.
Encapsulate each one of the:
This pattern allows you to place your algorithms in different classes (encapsulate them).
Makes the algorithm interchangeable:
The beauty with strategy pattern is that we can select at run time which algorithm we want to apply to our object and can also replace them with one another.
These are the three main points of Strategy pattern. I hope we are clear with the above- discussed points. So, let's understand one real -world scenario where it can be really helpful to use.
Example
Suppose you want to order food online and apply one of the available coup. In this case you can implement the Strategy Pattern.
- 1. public interface IApplyCoupon
- 2. {
- 3. string applycoupon(string couponType);
- 4. }
We have 2 Concrete coupon classes which implement IApplyCoupon interface and these classes can have their own logic to calculate the time.
- 1. public class FamilyHours :IApplyCoupon
- 2. {
- 3. public string applycoupon(string couponType)
- 4. {
- 5. return "You applied family hours coupon";
- 6. }
- 7. }
- 1. public class FestivalOffer :IApplyCoupon
- 2. {
- 3. public string applycoupon(string couponType)
- 4. {
- 5. return "You applied festival offer coupon";
- 6. }
- 7. }
Lastly, we have one Context class called TravelStrategy using which a client can select any strategy at run-time.
- 1. public class Coupon
- 2. {
- 3. private IApplyCoupon _applycoupon;
- 4. public Coupon(IApplyCoupon applycoupon)
- 5. {
- 6. _applycoupon = applycoupon;
- 7. }
- 8. public void applycoupon(string couponType)
- 9. {
- 10. var result = _applycoupon.applycoupon(couponType);
- 11. Console.WriteLine(result);
- 12. }
- 13.
- 14. }
Our main program looks like:
- 1. class Program
- 2. {
- 3. static void Main(string[] args)
- 4. {
- 5. Console.WriteLine("Hello!, Please select the coupon for Dinining \n1.FamilyHours \n2.FestivalHours");
- 6. var userStrategy = Console.ReadLine().ToLower();
- 7. Console.WriteLine("\nUser has selected " + userStrategy + " as dining coupon\n");
- 8. switch (userStrategy)
- 9. {
- 10. case "FamilyHours":
- 11. new Coupon(new FamilyHours()).applycoupon("FamilyHours");
- 12. break;
- 13. case "FestivalHours":
- 14. new Coupon(new FestivalOffer()).applycoupon("FestivalHours");
- 15. break;
- 16. default:
- 17. Console.WriteLine("You didn't select any coupon");
- 18. break;
- 19. }
- 20. Console.ReadLine();
- 21. }
- 22. }
OUTPUT
When user selects family hours as the preferred dining coupon
Answer
2. When user selects festival hours as the preferred dining coupon
Output
Hope you have learned something new in this article, Stay tune for next one.
No comments:
Post a Comment