Strategy

Strategy pattern

Strategy pattern
  1. How do you use strategy patterns?
  2. How is the strategy pattern defined?
  3. Why is it called strategy pattern?
  4. What problem does strategy pattern solve?

How do you use strategy patterns?

Use the Strategy pattern when you want to use different variants of an algorithm within an object and be able to switch from one algorithm to another during runtime. Use the Strategy when you have a lot of similar classes that only differ in the way they execute some behavior.

How is the strategy pattern defined?

In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.

Why is it called strategy pattern?

The idea behind the strategy pattern is that "algorithms can be selected at runtime." (Wikipedia, Strategy Pattern) The pattern, in essence, is selecting the right strategy (or behavior) for solving a particular problem at runtime. Hence, it's name.

What problem does strategy pattern solve?

Strategy Pattern: Problems It Solves

Strategy Pattern prevents hard-wiring of all the algorithms into the program. This makes our program complex and much more bogus and hard to refactor/maintain and understand. This, in turn, makes our program to contain algorithms they do not use.

How to design a content moderation system for a time-sensitive use case?
What do you think are the top 3 traits of a successful content moderator? What do you think are the top 3 traits of a successful content moderator?T...
What data visualizations are there for showing n-n relationships / mappings between data input and output names?
Which visualization type measures the relation between multiple variables and shows the strength of relation with colors?Which visualization can be u...
Multiple levels of conditional text
Can you do conditional formatting with 2 conditions?Can you have more than 3 conditional formats in Excel? Can you do conditional formatting with 2 ...