I wan't to create Actors
that are tied to a World
. I'm thinking of using one of the two following code fragments. Which one of these is considered the better design? By better I mean, more readable for source code maintainer, more readable for someone just reading through the code, more flexible for future adjustments, etc.
I'm sorry if a similar question has already been asked, but I don't really know what a concept like this is called in OOP.
Option A:
// declaration:
class World {
public:
RegisterActor(Actor* a);
};
class Actor {
public:
Actor(World& w) {
w.RegisterActor(this);
}
foo();
};
// usage:
World myWorld;
Actor myActor(myWorld);
myActor.foo();
Option B:
// delcaration:
class Actor;
class World {
public:
Actor& CreateActor();
}
class Actor {
friend class World;
public:
foo();
private:
Actor();
};
// usage:
World myWorld;
Actor& myActor = myWorld.CreateActor();
myActor.foo();