views:

130

answers:

4

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?

A: 

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.

John Saunders
A: 

You have to fully qualify upon use. Use aliases if your fully qualified names are too long.

a.Mesh b.Mesh

tsilb
A: 

In general and in .NET land (specifically C#) I would remap the class if there was absolutely no way around it (namely 3rd party):

using MyMesh = My.Mesh;
using FooMesh = Foo.Mesh;

Barring that and that you're obviously NOT talking C# (the question should be tagged with c++) I would use the full namespace when referencing either. While inconvenient to type it it only takes one mistake to be a hassle.

Colin Burnett
A: 

Remember this golden rule of "best practices" in OO design: naming your classes is easily 50% of the design solution. If you are having problems naming entities, it is indicative of the fact that you do not have a clear conceptual model in your mind.