Pergunta

I want to use strategy pattern, but I wander if it is correct to use "this pointer" with the strategy pattern interface.

This is required in order to get internal information which is required for the algorithm, but in all examples I've seen for Strategy pattern, there is no use of this pointer, so I'm not sure if this a correct thing to do.

EDIT: Why do I need this pointer parameter ? For example I have filemanager class which contain several parameters of files and folder state, and the strategy algorithm is used for decision making of files to be deleted and opened and to return current file handler. using this and getters and setters make it easy to handle the algorithm input and output.

Foi útil?

Solução

There is no "correct" in programming. There are good things, there are bad things, but more often than not there's a series of tradeoffs that are some good and some bad. Even if you made code that did the job and mislabeled it the strategy pattern, that doesn't make the code incorrect; it just makes your communication confusing.

Rant aside, using this in the strategy pattern is perfectly fine. These are called functors in C++, and are comparable to function objects and closures in other languages.

Licenciado em: CC-BY-SA com atribuição
scroll top