Strategy Pattern

A Must Know Design Pattern

·

3 min read

Strategy Pattern

We have already looked at what design patterns are, why we should know them, the three categories and the types of design patterns that we can use. In this blog post we will look at one of the design patterns we had discussed earlier in-depth.

Let’s jump in and look at the first design pattern….

Table of contents

  • What are Behavioural Patterns?
  • What is a Strategy Pattern?
  • How do I Implement the Strategy Pattern?

What if you wanted to create a software that hides the overall working of your application? What then? What pattern would you use?

Fun enough, you would need to look at various things such as the algorithms you want, how you want to hide your implementation of one object class from the interfaces that implement that particular object class. This is where the behavioural design patterns come into play. These patterns are concerned with the algorithms and assignment of responsibilities between objects – Design Patterns: Elements of Reusable Object-oriented Software.

Behavioral patterns tend to characterise complex control flow that is often very difficult to follow at run-time. This is because they describe not only the patterns of objects or classes but also the patterns of how they communicate between or among each other. Behavioural patterns help you shift your focus away from the control flow(how control of the application will be like) of your program and let you concentrate on how the objects are interconnected (how they communicate or the coupling between/among them).

Basically, what this is talking about is the behaviour of your objects and what their responsibilities will be after they are created. Doesn’t sound so hard right? And it isn’t. Every object that you will create using any Object-oriented principles will have to follow this in order to help you manage and hide information about one object from the others.

What now?

Have you ever had to pick algorithms from a certain family while hiding how you are using them to solve a particular problem and also using them in an interchangeable manner?

If the answer is yes, then congratulations. You have used the Strategy Pattern without even knowing it.

So, what is this Strategy Pattern?

A Strategy Pattern is one of the many behavioural patterns that have been discovered overtime to help us with how we use algorithms and encapsulate objects in our designs.

But here is a more technical definition which is sure to make you sound intelligent among your peers; “A Strategy Pattern defines a family of algorithms, encapsulates each one and makes them interchangeable.

The strategy pattern lets the algorithm chosen vary from all the clients that will eventually use it.

How do I Implement the Strategy Pattern?

You are in luck; you can implement this pattern in any programming language that is Object-oriented by nature. Examples of such languages can be Java, C# or C++, the list is endless and it’s your pick according to which one you are most comfortable with.

Strategy Pattern_1.jpg

It is therefore important to note that, design patterns and OOD principles work hand-in-hand. Knowing only OOD principles alone will not help you write code that is not only easy to maintain and refactor. This breaks the principles of good software development designs and should not be taken lightly.

Conclusion

Choosing which algorithms to use for your software is something that is very vital and careful consideration should be taken. The Strategy Pattern is all about algorithms, encapsulation and how you use the algorithms selected.

Note: Remember to always code for the interface and not an implementation as this will save you a lot of time when refactoring or adding new interfaces to your application.

design principle_2.jpg

Thank You for Reading

f693d6332b5468427075a09c6b7d93f8.gif

Thank you for reading up to this point. Hopefully this article helped both you and I learn what the Strategy Pattern is and how we can use it in designing our software applications.

Check out my twitter