3

I've just been reading through some basic design patterns,

Could someone tell me if the term "strategy pattern" only applies if your implementing a completely abstract interface?

What about when your children (concretes?) inherit from a parent class (the strategy?) with some implemented methods and some virtual and/or abstract functions? Otherwise the rest of the implementation, the idea that you can switch between different children at run time, is identical. This is something i'm quite familiar with, i was wondering if you would still call it the Strategy Pattern or if that term only applies to using an interface.

Apologies if this question is not appropriate! Or if this is just nitpicking :) I'm still learning and i'm not really sure if design patterns are quite heavily defined within the industry or just a concept to be implemented as you like.

2 Answers 2

1

Could someone tell me if the term "strategy pattern" only applies if your implementing a completely abstract interface?

Strategy pattern is about choosing the algorithm at runtime; technically the algorithm is ran by calling a function, so only that function needs to be abstract/left unimplemented. If all the similar algorithm call other function, independent of the algorithm, those functions can be pushed in the superclass.

For example you implement a strategy pattern for saving something in a file; depending on something else you could be saving it in XML or binary format. You can implement the logic of saving the data in a file in the superclass (since it's common to all implementation) and the logic of converting the data in XML in a subclass and in binary in a another subclass.

The idea behind patterns is not a rigorous implementation but an idea of design; you can implement it as you find it suitable for your particular problem.

4

A strategy is simply where an object takes in something that defines how it should handle some behavior. It might be an implementation of an abstract base class, it might be a script, it might be a functor, it could be any number of things.

The pattern itself is a design that uses this concept.

1
  • Before design patterns, we used to call this "implementation."
    – Blrfl
    Nov 10, 2012 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.