I've got a paradigm that I'm somewhat fond of and which I use quite a bit in my engine. I came up with on the fly and have been using it for a while but now I'm starting to rethink my code practices and just wanted to run this code by you guys to make sure its kosher. Here's the template:
class Listener
{
public:
Listener(Listener* instance){
mListenerList.push-back();
}
virtual void ProcessMessage(MSG& msg) = 0;
static void _(){
for(auto listener : mListenerList)
mListenerList->ProcessMessage();
}
private:
static std::vector<Listener*> mListenerList;
}
Typo: static void _Listener_UpdateAll()
Then I have an object who uses the interface
class Camera : public Listener
{
public:
Camera() : Listener(this) {}
virtual void ProcessMessage(MSG& msg) override;
}
And then I update somewhere in my main engine loop
void EngineLoop()
{
Application->Update();
//etc....
Listener::_Listener_UpdateAll();
//....
}
My question: is there a name for this patter and is it ok to use it in this fashion with all the static methods and variables and such, mixing it with interfaces etc.
By the way I use this in lots of places, not only for message distribution but if I want to have a class "auto update" itself without the need for me to call ->Update() manually I just define the Update function in the interface and call it from the base pointer in the engine loop.
Or if I want to parent one object to another and have the child follow the parent around in the world I have a similar class that links their position vectors through an interface.
In other words I kind of use this everywhere. So would that be something that is discouraged in general and if so what is the appropriate way to accomplish something similar? Thanks.