Low grip and tight grip

Of course, it depends on the situation. But when an object or system of a lower level interacts with a system of a higher level, should callbacks or events be preferred to maintain a pointer to a higher level object?

For example, if we are working on a game, we have world classone that has a member variable vector<monster> monsters. When monster classwill it interact with world class, should I use the callback function, or should I have a pointer to the world class inside the monster class?

+3
source share
1 answer

It is generally recommended that you use callbacks to communicate with higher-level classes for the reasons you mentioned, and to avoid mutual / circular dependencies.

In your case, you still need to determine what constitutes a lower-level module. Should the world really know what a monster is? Are monsters simply creatures or opponents? Doesn’t the monster need some kind of environment? Only you can answer this question to come to a workable solution.

+6
source

Source: https://habr.com/ru/post/1793840/


All Articles