Design Patterns. Strategy Pattern in Examples

Artem Diashkin
Nov 18, 2020 · 3 min read

Let’s take a look at some Strategy patter examples

Most of the time you will see some pattern definition, explanations, conditions, pitfalls, etc, and only after that — an example. I would like to break this chain and to show you some simplest concrete examples first, and after that — some theory.

UML

📝 If you are not familiar with the term aggregation you can check more here:

… or you can just take o look at the examples, and you will get the main idea behind it 😉

All examples will be in Typescript, but don’t worry, you will get the main idea.

Example 1

Run:

Result:

ConcreteStrategy_A
ConcreteStrategy_B

Example 2

Result:

6
0

Example 3

In this case, you can add as many types with strategies as you want (very nice implementation of “Open Closed Principle”)

Example 4

Some theory

I don’t think that you will need this, but still… 😉

After those examples, let’s see a few definitions of Strategy pattern:

Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.

- Refactoring guru

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.

- Wikipedia

Concepts

  1. Eliminate conditional statements
  2. Behavior encapsulated in a class
  3. Difficult to add new strategies

Geek Culture

Proud to geek out.

Sign up for Geek Culture Hits

By Geek Culture

Subscribe to receive top 10 most read stories of Geek Culture — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store