Design pattern "Strategy"
Real world example
In an e-commerce application, customers might want to pay for their purchases using different payment methods. Some customers might want to pay using a credit card, others might prefer PayPal, and some might even want to use Bitcoin. Each of these payment methods has a different process for transferring money from the customer to the merchant.
The Strategy pattern allows you to encapsulate each of these payment processes into its own class (a βstrategyβ) that implements a common interface. This interface might have a method like pay(amount)
, which each payment method class implements in its own way.
In plain words
Strategy pattern allows you to switch the algorithm or strategy based upon the situation.
Wikipedia definition
In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables an algorithmβs behavior to be selected at runtime.
Programmatic example
Diagram
Loading graph...
Some other examples
Here are some real-world other examples where the Strategy design pattern can be useful:
- Sorting Algorithms: In a data processing application, you might want to sort data in different ways based on the context. You can use the Strategy pattern to switch between QuickSort, MergeSort, BubbleSort, etc.
- Compression Algorithms: In a file storage system, you might want to compress files using different algorithms based on the file type or other factors. You can use the Strategy pattern to switch between ZIP, RAR, TAR, etc.
- Payment Methods: In an e-commerce application, you can use the Strategy pattern to switch between different payment methods like Credit Card, PayPal, Bitcoin, etc.
- Routing Algorithms: In a GPS application, you can use the Strategy pattern to switch between different routing algorithms like Shortest Path, Least Traffic, Scenic Route, etc.
- Image Filters: In a photo editing application, you can use the Strategy pattern to switch between different image filters like Sepia, Black and White, Brightness Increase, etc.
- AI Behaviors: In a video game, you can use the Strategy pattern to switch between different AI behaviors like Aggressive, Defensive, Neutral, etc.
- Validation Rules: In a form validation system, you can use the Strategy pattern to switch between different validation rules like Email Validation, Phone Number Validation, SSN Validation, etc.
- Tax Calculation: In a financial system, you can use the Strategy pattern to switch between different tax calculation strategies based on the country or state.
- Shipping Methods: In a logistics system, you can use the Strategy pattern to switch between different shipping methods like Ground Shipping, Air Freight, Sea Freight, etc.
- Database Access Strategies: In a database access layer, you can use the Strategy pattern to switch between different database access strategies like Direct SQL, Stored Procedure, Entity Framework, etc.
Design pattern "Visitor"
Let's learn what is the "Visitor" design pattern π€ππΆπ§³
Design pattern "Iterator"
Let's learn what is the "Iterator" design pattern π
Design pattern "Observer"
Let's learn what is the "Observer" design pattern ππππ
Design pattern "Memento"
Let's learn what is the "Memento" design pattern πΈοΈπΌοΈπΌοΈππ°οΈπ
Design pattern "Facade"
Let's learn what is the "Facade" design pattern πΉοΈποΈπ¨
Design pattern "Decorator"
Let's learn what is the "Decorator" design pattern πͺβββ
Design pattern "Composite"
Let's learn what is the "Composite" design pattern π³πΏπΏ
Design pattern "State"
Let's learn what is the "State" design pattern ππ»ββοΈπΆπ»ππ»