Original Post
I will give a very simple example of my situation. Let's say we have a World class, an Actor class, and a Bullet class. The World can contain several Actors and Bullets. Furthermore, the Actor should be able to shoot bullets into the world, but not know anything about the World class, to avoid coupling. The way I've been doing this is that there is an Actor::shootBullets() method that returns a list of bullets that the actor has shot. The World can call this method, get the result and append it to its list of bullets. While this works, yesterday it dawned to me that I could use an alternative method, using callbacks. Consider: In this example, the World calls the Actor::Update method, and passes the function World::addbullet as a callback. The Actor can use this to shoot bullets. I see it like a "pipe" that connects World and Actor, but Actor does not need to know about the World. All the Actor needs to work is to provide it with a callback with the right interface, and nothing more. How does this sound to everyone? I keep thinking it over and I can't seem to find anything wrong with it. But something in my mind nags me that I somehow violate an OO rule here and can't put my finger on it. What do you think?
class Actor:
def update(self,dt,shoot_bullet_callback):
if (self.is_shooting):
shoot_bullet_callback(Bullet())
class World:
def addbullet(self,bullet):
self.bullets.add(bullet)
def update(self,dt):
for actor in self.actors:
actor.update(dt,self.addbullet)