Service locator pattern
Often, one or more of our classes will need access to another part of our code base. Usually, it's not a major issue. All you would have to do is pass a pointer or two around, or maybe store them once as data members of the class in need. However, as the amount of code grows, relationships between classes get more and more complex. Dependencies can increase to a point, where a specific class will have more arguments/setters than actual methods. For convenience's sake, sometimes it's better to pass around a single pointer/reference instead of ten. This is where the service locator pattern comes in:
class Window; class EventManager; class TextureManager; class FontManager; ... struct SharedContext{ SharedContext(): m_wind(nullptr), m_eventManager(nullptr), m_textureManager(nullptr), m_fontManager(nullptr), ... {} Window* m_wind; EventManager* m_eventManager; TextureManager* m_textureManager; FontManager* m_fontManager; ... };
As you can see, it's just a struct
with multiple pointers to the core classes of our project. All of those classes are forward-declared in order to avoid unnecessary include
statements, and thus a bloated compilation process.