Sitemap

Member-only story

Strategy Pattern in Golang

4 min readFeb 20, 2021

--

Usually I find technical books very dry, but Head First Design Patterns was awash with funny anecdotes and eye-grabbing illustrations. It felt a good place to start learning design patterns though it may miss out on more intricate details. The code examples are in Java. I recently started learning Go and chose it to implement the strategy pattern.

Strategy Pattern

According to the book,

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

Okay, that did not make much sense to me at first too. Let’s try to understand through the problem statement.

The problem statement

We are told to design a duck pond simulation where different species of ducks exist. They can have a variety of swimming styles, flying abilities and sounds.

Requirements keep on changing, but finally we have four types of ducks.

--

--