3

This is a sample implementation of Strategy Pattern in C++:

ConcreteStrategy.h

class ConcreteStrategy {
public:
    ConcreteStrategy();
    ~ConcreteStrategy();
    const OtherObject* doSomething(const OtherObject &obj);
};

ConcreteStrategy.cpp

#include "ConcreteStrategy.h"

ConcreteStrategy::ConcreteStrategy() { // etc. }
ConcreteStrategy::~ConcreteStrategy() { // etc. }
const OtherObject* ConcreteStrategy::doSomething(const OtherObject &obj) { // etc. }

MyContext.h

template <class Strategy> class MyContext {
    public:
        MyContext();
        ~MyContext();
        const OtherObject* doAlgorithm(const OtherObject &obj);
    private:
        Strategy* _the_strategy;
};

MyContext.cpp

#include "MyContext.h"

template <typename Strategy>
MyContext<Strategy>::MyContext() {
    _the_strategy = new Strategy;
}

template <typename Strategy>
MyContext<Strategy>::~MyContext() {
    delete _the_strategy;
}

template <typename Strategy>
const OtherObject* MyContext<Strategy>::doAlgorithm(const OtherObject &obj) {
    obj = _the_strategy(obj);
    // do other.
    return obj;
}

main.cpp

#include "MyContext.h"
#include "ConcreteStrategy.h"
#include "OtherPrivateLib.h"

int main(int argc,char **argv) {
    OtherObject* obj = new OtherObject;
    MyContext<ConcreteStrategy>* aContext = new MyContext<ConcreteStrategy>;
    obj = aContext.doAlgorithm(obj);

    // etc.

   delete aContext;
   delete obj;

   return 0;
}

Is this implementation right? It's my first approach with template in C++ and I've some doubt, specially about construction and destruction of template object (Strategy) inside the context (MyContext).

UPDATE: I've this error at compile-time:

undefined reference to `MyContext<Strategy>::MyContext()'
  • 3
    Are you asking a code review question, or did you encounter a specific problem? If the former is true, maybe you should ask this question on [Code Review](http://codereview.stackexchange.com). – Eitan T Jun 25 '12 at 14:48
  • I'm not sure about construction and destruction 'cause it's my first time I use template. Furthermore, I didn't know Code Review, thanks for the suggest. –  Jun 25 '12 at 14:52

2 Answers2

6

First, the class template implementations should go in the header file or in a file included by the header, not in a .cpp file to be compiled. The compiler needs to see the template code in order to create the MyContext<ConcreteStrategy> required by main. This is the cause of the compiler error.

Second, and unrelated to templates, you have many uses of dynamically allocated objects for no apparent reason. I would change doSomething and doAlgorithm to return by value, for example

OtherObject doSomething(const OtherObject &obj);

and remove all uses of new from the main, for example:

int main(int argc,char **argv) {
    OtherObject obj;
    MyContext<ConcreteStrategy> aContext;
    obj = aContext.doAlgorithm(obj);
    return 0;
}

If you really must use dynamically allocated objects, then I suggest using smart pointers, particularly C++11's std::unique_ptr.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • +' Probably add more emphasis on the compilation error cause, like 'thus the compilation error' – unkulunkulu Jun 25 '12 at 15:19
  • I need to use dynamic allocation for the memory management in my project. Beyond this, must I write all code-implementation inside header file? Isn't it dirt? –  Jun 25 '12 at 15:24
  • @vdn, there is no way to hide the source code and eliminate those dependencies if you use templates, see [Sutter's article on 'export'](http://www.gotw.ca/publications/mill23-x.htm) – unkulunkulu Jun 25 '12 at 15:27
  • @unkulunkulu thanks, I wrote the original answer before the compiler error was added to the question. I changed the emphasis now. – juanchopanza Jun 25 '12 at 17:16
  • I've moved the function implementation inside header file and now it works fine. Unfortunately, I cannot use smart pointers 'cause they aren't supported by my gcc version. Thank you so much! –  Jun 25 '12 at 21:09
0

Encapsulating an algorithm in a class hierarchy, having clients of that algorithm hold a pointer to the base class of that hierarchy, and delegating all requests for the algorithm to that "anonymous" contained object whih is called strategy pattern