Design Patterns: Strategy Pattern
Tags: oop, designpattern, java, strategy
Published: 2017-09-14 17:04:07

---
title: "Design Patterns: Strategy Pattern"
published: true
description: A quick introduction to the Strategy Design Pattern
tags: oop, design pattern, java, strategy
---

_originally posted on my blog at [henricodesjava.blog](https://henricodesjava.blog/)_

Hello world! I'm currently readingÂ <a href="https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124/ref=sr_1_1?ie=UTF8&qid=1505068308&sr=8-1&keywords=head+first+design+patterns">Head First Design Patterns: A Brain-Friendly Guide</a>, and if it is your first time reading about design patterns in Object Oriented Programming(OOP), its a great start! I wouldn't recommend it as a reference guide, because there are a ton of examples, illustrations, and informal descriptions. So here is the first pattern introduced in the book.

<strong>Strategy Pattern</strong>:Â <em>Defines a family of algorithms, encapsulates each one, and makes them interchangeable. The strategy lets the algorithm vary independently from clients that use it.</em>

Sounds confusing, but it is actually pretty simple. The pattern is based on the following three principles:
<ol>
	<li>Identify the aspect of your application that vary and separate them from what stays the same.</li>
	<li>Program to an interface, not an implementation.</li>
	<li>Favor composition over inheritance.</li>
</ol>
Let me try to clear up what these principles mean. I'll use an example that is different from the one included in the book.

Lets say we are starting a robot company. Like Boston Dynamics. We want to build all types of robots. Check out the class below that will be used for all our robot's behaviors.Â <img class="aligncenter  wp-image-119" src="https://henricodesjava.files.wordpress.com/2017/09/screen-shot-2017-09-13-at-9-09-24-pm.png?w=456" alt="Screen Shot 2017-09-13 at 9.09.24 PM.png" width="217" height="120" />

Now here is where we apply our <strong>first</strong> principle. Our imaginary company needs to be be able to build robots that operate on land, air, or water. All our robots also need to be able to give the greeting, "Hello world!". The greeting is the behavior that stays the same. The other three behaviors are what will change from robot to robot. So we will separate the 'Walk', 'Swim', and 'Fly' behavior from the 'Greeting' behavior. Lets see how.

The <strong>second</strong> principle states it is better to use interfaces rather than direct implementations. So what we will do is create interfaces for each behavior group that has the possibility of changing. This way there can exist many different implementations of each, see below.

<img class="  wp-image-146 aligncenter" src="https://henricodesjava.files.wordpress.com/2017/09/screen-shot-2017-09-13-at-11-43-18-pm.png" alt="Screen Shot 2017-09-13 at 11.43.18 PM.png" width="573" height="312" />

When it comes time to create a specific implementation of a robot, we will extend the Robot class, and in this way have access to the 'greeting' behavior. The greeting behavior can be coded in the Robot class since this will be the same for all robots we ever create. Then when it comes time to add a Walk, Swim, or Fly behavior to our new robot, it will not inherit this behavior from the original Robot class, but it will instead have an implementation of an interface assigned to it, in other words, it will be composed during runtime. This successfully separates these changing, and non-changing behaviors using interfaces. This way we follow our <strong>third</strong> design principle. Take a look at the diagram below for an overhead view of it all, including three example robots, Dog, Shark, and Eagle robots.

<img class="alignnone size-full wp-image-158" src="https://henricodesjava.files.wordpress.com/2017/09/screen-shot-2017-09-14-at-10-40-28-am.png" alt="Screen Shot 2017-09-14 at 10.40.28 AM.png" width="1758" height="1630" />

Â 

So thats it! That is the quick introduction to the Strategy Pattern. Above you can see what a complete class diagram would look like when we implement the Strategy Pattern. Thanks for reading ðŸ‘‹ðŸ½

(originally posted at my personal blog: https://henricodesjava.blog)