如果我在不同的命名空间中有相同的类名该怎么办?
我正在开发一个项目,并遇到了以下命名问题。
我想实现工厂模式,但我不知道要使用的最佳类命名(我正在从一种更改为另一种,这非常耗时:S)。
我通常使用命名空间来分隔类的组,但我的问题与这段特定的代码有关:
class Mesh
{
...
};
namespace Factory
{
class Mesh
{
...
};
}
...
Factory::Mesh meshFactory;
Mesh *mesh = meshFactory.create(...);
我的问题是如果我使用这个结构我可以混合Mesh类(无论是工厂类还是实际的Mesh类)。 实际上,这是一种简化,我的问题涉及更多的命名空间,并且两个命名空间中都使用具有相同名称的类。
我正在考虑是否可以使用后缀来分隔类并将它们放在同一名称空间中,例如:
class Mesh
{
...
};
class MeshFactory
{
...
};
MeshFactory meshFactory;
Mesh *mesh = meshFactory.create(...);
这样每个类的作用就不会混淆。
我不喜欢使用不同的命名空间并发明不同的名称的简单解决方案,因为我最终会使用命名空间名称来区分它们:
class Mesh
{
...
};
namespace Factory
{
class MeshFactory // I can't figure a better different name
{
...
};
}
我更喜欢第二个选项。
是否有充分的理由说明为什么第一个选择更好? 或者还有别的办法吗? 最佳实践对此有何看法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请记住 OO 设计中“最佳实践”的黄金法则:为类命名很容易就占了设计解决方案的 50%。 如果您在命名实体时遇到问题,则表明您头脑中没有清晰的概念模型。
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.
是什么原因导致您将 Mesh 类命名为“Mesh”?
生成 Mesh 实例的工厂类是否有相同的原因?
如果是这样,那么它们应该是同一类,具有相同的名称。
如果不是,那么它们应该是不同的类,具有不同的名称。
由于工厂类的唯一目的很可能是生成 Mesh 类的实例,因此我建议您将其命名为 MeshFactory。
命名空间不相关。
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.
使用时您必须完全符合资格。 如果完全限定名称太长,请使用别名。
a.网格
b.网格
You have to fully qualify upon use. Use aliases if your fully qualified names are too long.
a.Mesh
b.Mesh
一般来说,在 .NET 领域(特别是 C#),如果绝对没有办法绕过它(即第 3 方),我会重新映射该类:
除非这样,而且您显然不是在谈论 C#(问题应该用 c++ 标记)在引用其中一个时,我会使用完整的命名空间。 虽然打字不方便,但只要犯一个错误就会很麻烦。
In general and in .NET land (specifically C#) I would remap the class if there was absolutely no way around it (namely 3rd party):
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.