In an attempt to replace the singleton pattern and a general “Resource Manager”, I came with a solution. Making a resource static and protected. That resource is shared between all children from the inherited class. It works, but I wasn’t sure if it’s a good way to proceed.
Here’s a bit of code to express what I’m doing(the resource here is the sf::Texture):
class Foo {
public:
Foo() {
if(m_texture == nullptr) {
//Création et chargement de la texture
m_texture = std::unique_ptr<sf::Texture>(new sf::Texture());
m_texture->loadFromFile("...");
}
}
void draw(sf::RenderWindow& window) = 0;
protected:
static std::unique_ptr<sf::Texture> m_texture = nullptr;
};
class Bar : public Foo {
public:
Bar()
: m_sprite(*m_texture) {}
void draw(sf::RenderWindow& window) {
window.draw(m_sprite);
}
private:
sf::Sprite m_sprite;
};
That way my resources is shared through all children, and is only initialized once.
Is it a good solution to replace a singleton or a Resource Manager that I would carry everywhere through reference.
Thank you!
Fundamentally what you are tying to do is correct, a static member will be shared (ie be exactly the same) among all the inherited class, this way you only need a single instance which can save you a ton of memory but heres a couple issues … Im assuming you are using g++.
You can’t initialize non-const member within a class declaration so this.
static std::unique_ptr<sf::Texture> m_texture = nullptr;would produce this:
error: ISO C++ forbids in-class initialization of non-const static member
you have to initialize it within the source file of your class, but outside the class.
std::unique_ptr<sf::Texture> Foo::m_texture = nullptr;Second it isn’t save to access member fields directly, always use setters and getters, even within the class functions, this makes the code much more maintainable. As such you can have a static function called getTexture
while its true that the if statement and function call do add come overhead, this is much more maintainable and safer, and it load the texture at the last minute when its truly needed.
Back to your question, the Singleton design pattern is quite simple and mostly used to save memory since only a single instance of the object is ever created 🙂 Resource managers are a whole different beast their goal is to centralize all the actions that are required loading and managing resources, combining the two you would initialize a single instance of a resource manager, then access it though a static member field, having all the objects make requests for resources, this could be good or bad, depending on what you are trying to achieve.
Software design is hard. The best advice I can give is this, when designing a system ask yourself this, “how many lines of code would I have to write to introduce another similar component” your goal should be to minimize this as much as possible, ie reuse as much as possible what you have already created.
The best programmers are the laziest 🙂 and no I don’t mean copy/paste, that should be banned.