I consider that the factory keeps raw material (like sf::Image) and produces objects based on that raw material (like sf::Sprite).
Other classes are clients of the factory. Like a Scene class, it stores and uses light objects like sf::Sprite.
That way you have multiple light objects on a higgher class, using the same heavy data that is stored on the factory.
The factory is responsible for creating and provide the light objects, but not to store them. The client stores and manipulates light objects, but don't care on how it was built, and what and where is the low level and hard data of it. Just like in real life!
This is how I see it :wink:
Let's for example consider that factory takes care of sf::Images and Scene takes care of sf::sprites.
To simplify, load all images you need on the factory at start of the application, and free them at application termination.
Example:
Factory::loadEverything();
while(App.IsOpened()){
...
}
Factory::clearEverything();
On your example, your Scene class has a Sprite for each object in the scene. The sprites can be stored in some container.
When left click on screen, the Scene requests the factory for a new sprite of certain type. The factory creates a sprite using a specific image that is defined by the type (case type_tree: sprite->SetImage(tree_image) for example) Finally return it to the Scene, which stores it on the container.
someContainer.push_back( Factory::CreateTree(mouseX, mouseY) );
or
someContainer.push_back( Factory::CreateObject(ObjTypes::tree, mouseX, mouseY) );
When you right click, you search on the container for a sprite that intersects the mouse coordinates (test if it's inside a bounding box for example). Once you find it, just remove it from the container.
It becomes more useful when managing animations and other more complex raw data :wink:
This was a simple example, you may want to specify what happens when you request an object which the raw data needed for it doesn't exist on the factory yet. You can load it or just raise an error. Or you can keep track of how many objects are using a certain raw data, when they are removed on the Scene, notify the factory so that when the last light object using a certain data is removed, the factory can delete the data, etc.