Coding Dynamic Behavior with the Strategy Pattern

Coding Dynamic Behavior with the Strategy Pattern hackernoon.com4 years ago in #Dev Love30

Originally published by Severin Perez on October 10th 2018 How to Leverage Polymorphism at Runtime Photo by Linus Nylund on Unsplash One of the benefits of object-oriented design is the ability for objects to share some behaviors while simultaneously differing in others. Typically, this is achieved through inheritance — when many subclasses inherit properties from a parent class but can optionally override certain behaviors as needed. This is a very useful and common design pattern; however, there are situations when polymorphism through inheritance is inappropriate. Consider, for example, when you only need a single behavior to change but otherwise want an object to stay the same. Or, when you want an object to shift its behavior at runtime based on some external (but unpredictable) factor. In such cases, an inheritance scheme is likely to cause unnecessarily bloated code that is difficult to maintain (particularly as the number of subtypes increases). However, there is a better approach: the strategy pattern. Polymorphism through Strategies The strategy pattern, also known as the policy pattern, is a behavioral design pattern that lets an object execute some algorithm (strategy) based on external context provided at runtime. This pattern is particularly useful when you have an object that needs to be able to execute a single behavior in different ways at different times. By using the strategy pattern, you can define a set of algorithms that can be dynamically provided to a particular object if/when they are needed. This pattern has a number of benefits, including: encapsulation of particular algorithms in their own classes; isolation of knowledge about how algorithms are implemented; and, code that is more flexible, mobile, and maintainable. To the last point, you may note that these are the same attributes that result from code that follows the Open/Closed Principle (OCP) and indeed, the strategy pattern is an excellent way to write OCP-adherent code. When implementing the strategy pattern, you need three main elements: A client, which is aware of the existence of some abstract strategy but may not know what that strategy does or how it does it. A set of strategies that the client can use if/when provided with one of them. These may come in the form of first-class functions, objects, classes, or some other data structure. Optional context that the client can provide to its current strategy to use in execution. The classic way to implement strategies is with interfaces. In this case, a client has an internal pointer to some abstract strategy interface, which is then pointed to a concrete strategy implementation via dependency injection (that is, during construction or with a setter at runtime). Thereafter, the client may use the provided strategy to carry out some work, all without knowing (or caring) what the strategy actually does. Although interfaces are the classic way to implement the strategy pattern, a similar effect may be achieved in languages that do not have interfaces. What is important is that the client is aware of some abstract strategy and is able to execute that strategy without knowledge of its inner workings. Polymorphism through Pure Inheritance Before we look at how to use the strategy pattern, let’s look at a few examples that use other approaches to polymorphism. Consider the following snippet, which uses pure inheritance to define different types of runners in a race.  » Read More

Like to keep reading?

This article first appeared on If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply