If you enjoy this article you may be interested in the book I am working on called Building and Deploying Crypto Trading Bots. You can find more info about it here.
Let's look at an example of the strategy pattern in Ruby.
A common design pattern in software development is known as the "Strategy" pattern. The pattern proposes that when you have a scenario with multiple execution paths, each path be considered as a "strategy" that is recomposed into individual objects (or functions in functional languages). Each Strategy contains the algorithm implementation to execute that code path.
The benefit of using this technique is two fold. First, it allows the algorithm to vary from the clients that uses it. Second, it liberates instances of logic ("strategies") from individual client implementations allow for higher reusability within the software. If you enjoy UML diagrams you can see a visualization below:
To provide a more concrete example consider if you needed to write a program that forecasted the moving trend of a particular stock. In technical charting terms this can be computed by calculating the "Simple Moving Average" of a stock price. We want to write a program that calculates the twenty four and twelve day moving averages and tells us that direction the stock price is going. Sounds easy enough – if we know the strategy pattern.
We begin by defining an abstract base class called Strategy:
This base class is what all our Strategy objects will inherit from. We then create two concrete strategy objects TwelveDayMovingAverage and TwentyFourDayMovingAverage that inherit from the base Strategy class and override the interface's "exec" method with their own algorithm to return a result.
Finally we have the context under which the strategies will be called:
In the listing above a simple
TradingBot class is instantiated with a trading strategy value. This initialization value is used to select the appropriate algorithm to use at run time when the
trend? method is called by a client class. A full use case may look as follows:
By abstracting the strategy here we gain the flexibility to reuse the same
TradingBot class and can also implement the algorithms elsewhere.