Skip to main content

The Strategy Pattern

Lesson 2 - The Strategy Pattern

The Strategy Pattern is a Behavioural Design Pattern.

Behavioural Design Patterns are design patterns that identify common communication patterns among objects and realise these patterns and in doing so increases flexibility, carrying out this communication.

Definition

The Strategy Pattern defines a family of algorithms, encapsulates each one and makes them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.

When to use ?

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.

When you have a lot of similar classes that only differ in the way they execute some behaviour.

To isolate the business logic of a class from the implementation details of algorithms that may not be as important in the context of that logic.

When your class has a massive conditional operator that switches between different variants of the same algorithm.

Important to Note

One of the dominant strategies of object oriented design is the "Open-Closed principle".

The Strategy pattern suggests that you take a class that does something specific in a lot of different ways and extract all of these algorithms into separate classes called strategies.

Favour composition over inheritance!

Program to an interface and not an implementation.

Identify the parts that vary and separate them from the parts that stay the same.

Benefits

  1. You can swap algorithms used inside an object at runtime.
  2. You can isolate the implementation details of an algorithm from the code that uses it.
  3. You can replace inheritance with composition.
  4. Open/Closed Principle. You can introduce new strategies without having to change the context.

Code Examples

Duck Example
Street Fighter Example
Payment Example
Sort Example

https://www.journaldev.com/1754/strategy-design-pattern-in-java-example-tutorial
https://blog.bitsrc.io/keep-it-simple-with-the-strategy-design-pattern-c36a14c985e9
https://www.geeksforgeeks.org/strategy-pattern-set-1/
https://refactoring.guru/design-patterns/strategy
https://www.youtube.com/watch?v=SicL4fYCz8w
https://www.youtube.com/watch?v=QZIvlny1Onk
https://www.youtube.com/watch?v=Nx8iUv-ZnPw

More Reading

Next Up - The Decorator Pattern

Comments

Popular posts from this blog

The Decorator Pattern

--- --- Lesson 3 - The Decorator Pattern The Decorator Pattern (also known as the wrapper) is a Structural Design Pattern. Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient. It allows us to dynamically add properties to objects at runtime. The goal is to combine a single concrete class with one or more Decorators. Definition Decorator is a structural design pattern that lets you attach new behaviours to objects by placing these objects inside special wrapper objects that contain the behaviours. The Decorator Pattern allows behaviour to be added to an individual object , either statically or dynamically without affecting the behaviour of other objects in the same class. - Wikipedia The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. - Head First Design Patterns Provides ...

The Factory Pattern

Lesson 1 - The Factory Pattern In this series we will be going through the various design patterns that are available. I will be using many different sources to gather information and all these sources will be listed in the links below. I will also be heavily referencing from the Head First Design Patterns Book which is definitely worth a read. At the bottom of each article there will also be links to some code examples for you to go through and play around with. So lets begin.  The Factory Pattern is a Creational Design Pattern. Creational Design Patterns are design patterns that deal with object creation mechanisms. In this tutorial we will look at the following : Simple Factory Factory Method. Abstract Factory. Definition Factory Method “The Factory Method Pattern defines an interface for creating an object but lets the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to sub classes.” - Head First Design Patterns (Page 134...

Design Patterns

Design Patterns The purpose of this blog is to work through a few design patterns and note the important aspects of each. I will also be updating each article with relevant links to videos and articles that have helped me in my learnings.  Some of the patterns we will look at include:   The Factory Method  The Abstract Factory  The Strategy Pattern  The Decorator Pattern  Apart from the important aspects of each of the Design Patterns as well as the useful links to resources I will also include code examples from my bitbucket that will assist in providing a hands-on experience. The code demonstrating these patterns is written in Typescript.