# Engine object creation strategy.

## Recommended Posts

metsfan    679

Hello all,

I am currently looking to refactor some of the code in my engine to remove some singletons I made out of laziness.  However, I am facing a problem, and I am curious how others would solve this problem.  As it stands, object creation within the engine is done through constructors, and internal engine dependencies are accessed through singletons.  This is the main design I am trying to move away from.  However, there is one distinct advantage to this approach, which I would very much like to maintain:  a user needs to know very little about the internal dependencies to construct a new object, and can easily inherit from certain objects in the engine.

For example, I have in my GUI system a LabelControl class.   Each LabelControl object must have access to the FontManager, which is a subsystem that allows loading, retrieving  and caching of fonts (as it stands, built as a singleton).   I want users of my engine to be able to construct a new LabelControl, and also inherit from LabelControl, without needing to know that the FontManager even exists.  The only way I can see to solve this problem, while still keeping singletons out of my engine is to do the following:

-Take all my "singleton" subsystems and throw them into my Game base class in my engine.

- Require the user to pass their "Game" instance into every single constructor.

I'm not sure if this is the right way to do things, or if there is some other way that makes more sense.  Either way, I'd love some input and opinions on the matter.

Thanks,

##### Share on other sites
EWClay    659
Sounds like LabelControl has a bit too much responsibility. Perhaps it only needs a pointer/handle/ID of a font, which the GUI manager can provide when needed?

It is unfortunately quite hard to get rid of singletons. Sometimes you have to redesign quite heavily. This is one reason why they are a bad thing.

##### Share on other sites
Nercury    812

I would avoid hiding things. If you think FontManager is too complex to assign to your control, maybe control does not need to get full access to it.

For example, I have similar system, and I require to pass Font when creating my Control, and Font needs my FontManager to create :). And it looks nice:

Font * smallFont = new Font("FreeMonoBold.ttf", 10, fontManager);
TextLine t(smallFont);


Of course I am lying, it does not look like this, it is crappier and I wrap my heap objects in shared_ptr.

But maybe it will give you some ideas :)

##### Share on other sites
metsfan    679

Thank you for the responses.  I think you may be right that some of my objects may have too much responsibility.  However, as I think about it, this one example is just one of many problems.  For instance, my GameSound objects rely on my AudioManager class, which maintains a pointer to my FMOD system (another detail users of my engine should not have to deal with).  I think what I'm going to have to do is prefer factory methods for creating certain types of objects, and encourage users of my engine to prefer composition to inheritance for these types, and for types which can be inherited to also be made clear, but also encourage composition all the same.  Thanks guys.

I am of course open to more suggestions from the community, though.

Edited by metsfan

##### Share on other sites
larspensjo    1561

Use the Observer pattern. In principle, you generate events at one place of the source code, and then catch these events elsewhere, as needed.

An example:

• The melee function decides that the condition for an attack on the player from a monster is to be executed. It updates some physical values, and then generates a "MonsterHitPlayerEvent" with some information.
• The Sound system catch this event, and generates an appropriate sound.
• The graphical system catch the event, and splash some blood on the scene.
• The log window catch the event, and prints a message "You are hit %d units by the %s monster".
• The multiplayer module catch the event, and sends a network message to other players, to let them know.

This way, the melee function (usually part of the Model in the Model-View-Controller) does not need to know anything about sound, graphics, etc. Eventually, you no longer need to create a global variable for the sound manager, as it isn't used anywhere. And you don't need to create a global variable for the log window manager, etc.

The generator of the event is the one that declares the event type and what it will contain. The subscribers to this event need to include the header file that declares the event.