Voters with friends and without capital

We were the focus of conference season. Millennials -usually graduates- living in big cities are both the most misunderstood and the most important demographic currently shaping British politics…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Strategy Design Pattern

The strategy design pattern is a pivotal object oriented programming (OOP) framework and has several applications. OOP concerns itself with the process of thinking of each class as an object, with its own attributes and methods that specify what the object does. Each object only concerns itself with related tasks and attributes, which are the properties that this object possesses. Furthermore, other objects should not have access to the internal states of other objects. Ensuring the previous conditions are met is known as encapsulation.

Chess is an example where different strategies can be employed

In the strategy design pattern, the objects are algorithms or different strategies of achieving some end goal. Specifically, this design pattern allows the program to flexibly change between algorithms. A simple use case is chess; depending on the current board state, a different strategy or algorithm is employed.

The strategy design pattern is important in object oriented programming (OOP) because it promotes encapsulating the algorithms of a program. This benefits the algorithm designer, as he/she can focus on algorithm whilst ignoring the other details of the program.

To demonstrate, consider the trivial process of evaluating string length. We define the base class for any given strategy as follows

We leave the actual implementation of compare for each individual strategy, as below:

CompareForwards implements a specific algorithm or strategy to compare string lengths

Simple enough, however, we can also evaluate the length of the string by starting at the last letter, and working our way up to the first.

Now, we see that there are two different algorithms or “strategies”, that we can use. In this example, both strategies do the same thing, however in different implementations, the different strategies may differ vastly. This simple example was chosen for ease of demonstration. Given the strategies, we make use of them as such:

It becomes clear that the strategy design allows for easy swapping of algorithms depending on the context. For example, in one class App(CompareForwards) can be called, whilst in another App(CompareBackwards) can be called.

This design requires careful thinking at the start, but later down the project it allows the programmer to swap algorithms with ease. Furthermore, the development process can be compartmentalized such that the algorithm designer does not have to know the implementation for code that is unrelated to his/her task.

Add a comment

Related posts:

My First Colonoscopy

As a member of a family with a history of colon cancer my first colonoscopy was pushed up ten years. Here is the account of my first experience. Warning: if you are offended by potty humor you should…

Install Redis inside Ubuntu VM

I will start a series about Redis, I am planning to talk about a couple of topics about Redis such as Redis as a key-value database and as a cache and pub-sub. Part One: Install Redis inside Ubuntu…

5 Reasons Why Runners HATE Cyclists

All the time. The head, the size, where it’s looking, if it should be ashamed; we try not to judge, but we do. And if your bib looks like you picked it up at a clearance sale (why else would you be…