In C++, what would be the best design pattern to build a world of nested entities of different types, each being able to hold one or several dynamic collections of entities, and every entity being able to communicate with each other ? Like : a world contains a region which contains biomes and cities which contain animals and people and objects, etc, etc... I'm browsing design patterns but I'm a noob in this area, so maybe a kind fellow fediverse dweller can point me in the right direction ?

@jylm6d I don’t know what it’s called pattern wise but:

Create an object called “WorldObject” that contains a vector or type WorldObject and any data or methods that every object in the world needs.

Everything else is a subclass of that object.

Your top layer object is the world itself. It contains Countries in it vector, they contain biomes in theirs, that contain flora in theirs, that contain cells in theirs and so on.


@jameschip In facts that's how I made it in the first place, but I thought "mmm, you don't know design patterns, you're averagely dumb, so there must be some clever folks out there who designed a much better implementation of what you want to do, and who already addressed the countless dumb mistakes you're bound to make when you'll start building your thing."

But maybe the right thing to do is to face the music : learn design patterns and make my own thing, dumb mistakes included.

@jylm6d I mean, sometimes the pattern you need won’t reveal itself until further down the development process.

Also sometimes obsessing about formal design patters hinders you as you become too inflexible trying to push your square peg into a round hole.

@jameschip Right. So I will look into design patterns for inspiration and dive into it. Thanks for your insight

So each "layer" only has children of one specific type? And the order/hierarchy is known in advance?

In that case you *could* use a template class to share the "WorldObject" code from the other approach, but store the children with a concrete type:

template<ChildType> class WorldObject {
public std::vector<ChildType> children;


class Country extends WorldObject<Biome> {


@s_ol Thanks, I think I would need something more flexible. In some cases, the hierarchy would not be know in advance : for example, a house containing a chest containing fruits, hats and a pair of trousers with a small box in the pocket containing a glove, containing a spirit that can emit sounds or other interactions. And the fruits can hold worms, maybe.

right, then the other idea is ideal. You can have a big enum of "kinds" (or use strings if you prefer the flexibility of that) and create a "kind" field in the base class:

class WorldObject {
public enum Kind {

public Kind kind;
public std::vector<WorldObject*> children;

public WorldObject(Kind kind) : kind(kind) {}

class Biome extends WorldObject {
Biome() : WorldObject(WorldObject::Kind::Biome) {}


then when accessing children, you can switch/check on the "kind" before casting to a specific subtype.

@s_ol Thanks ! I'll start with something like that and see how it unfolds.

Sign in to participate in the conversation

Revel in the marvels of the universe. We are a collective of forward-thinking individuals who strive to better ourselves and our surroundings through constant creation. We express ourselves through music, art, games, and writing. We also put great value in play. A warm welcome to any like-minded people who feel these ideals resonate with them.