I’m working on a project and I came to the following naming problem.
I’d like to implement the factory pattern but I don’t know the best class namings to use (i’m changing from one to the other and it’s quite time-consuming :S).
I usually go for namespaces to separate groups of classes, but my problem is with this specific piece of code:
class Mesh
{
...
};
namespace Factory
{
class Mesh
{
...
};
}
...
Factory::Mesh meshFactory;
Mesh *mesh = meshFactory.create(...);
My problem is that if I use this structure I can mix up the Mesh classes (whether is the factory class or the actual Mesh class). Actually, this is a simplification, my problem involves some more namespaces and the classes that have the same name are used in both namespaces.
I was thinking on maybe using suffixes to separate the classes and put them on the same namespace, for instance:
class Mesh
{
...
};
class MeshFactory
{
...
};
MeshFactory meshFactory;
Mesh *mesh = meshFactory.create(...);
So there’s no confusion on what each class do.
I don’t like the simple solution which is to use different namespaces and invent different names, because I would end up using the namespace name do differentiate them:
class Mesh
{
...
};
namespace Factory
{
class MeshFactory // I can't figure a better different name
{
...
};
}
I prefer the second option.
Is there a solid reason why is better the first option? Or is there another way? What does best-practices say about this?
What are the reasons that caused you to name the Mesh class “Mesh”?
Does the factory class that produces a Mesh instance share all the same reasons?
If it does, then they should be the same class, with the same name.
If it does not, then they should be different classes, with different names.
Since it’s likely that they only purpose of the factory class will be to produce instances of the Mesh class, I suggest you name it MeshFactory.
The namespace is not relevant.