我应该将所有抽象类命名为 AbstractFoo
确保所有抽象类的名称都以“Abstract”为前缀是个好习惯吗?
Is it good practice to make sure that all abstract classes have names prefixed with "Abstract"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
你可以,但我倾向于不这样做,因为这是一个实现细节。
我不喜欢在类型和标识符的名称中添加实现详细信息,因为此类信息将来可能会发生变化。在我看来,最好按事物的本质来命名,而不是它们是如何实现的。
You can but I tend not to do this since it is an implementation detail.
I don't like adding implementation detail information in the names of types and identifiers as that kind of information may change in the future. In my opinion it is best to name things what they are, not how they happen to be implemented.
我认为这个命名约定只是因为很难想出另一个好名字而被使用。如果您已经有一个名为“List”的接口,那么如何命名“AbstractList”类呢?更重要的是避免名称冲突,然后讲述实现细节。
I think this naming convention is just used because it is hard to come up with another good name. If you already have an interface called "List", how would one name the "AbstractList" class? It's more about avoiding name clashes then telling implementation details.
这取决于您的编码约定。
您也可以将它们称为 FooBase,或者如果您还没有 Foo 接口,则直接将其称为 Foo。
That depends on your coding conventions.
You might also call them FooBase, or just Foo if you don't already have an interface Foo.
如果您考虑一下它在 .NET 框架中的情况,答案是否定的。以抽象 Stream 类为例。类名中没有任何内容表明它实际上是抽象的。
If you consider how it is in the .NET framework, no. Take for example the abstract Stream class. Nothing in the class name indicates that it is in fact abstract.
有点难以解释,但我只是用它来避免在功能类中复制/粘贴相同的代码,而不是在域对象之类的东西中。
您当然应该自己决定,只要在整个项目中遵循相同的约定即可。
Kinda hard to explain but I only use it to avoid copy/pasting the same code in functional classes and not in something like domain objects.
You should ofcourse decide for yourself, as long as the same convention is followed throughout a project.
我不会将抽象类称为 Abstract,原因如下:
每个矩形都是一个形状。凡是可以使用 Shape 的地方,都可以使用 Rectangle。处理矩形(但也可以处理圆形)的代码可能如下所示:
在任何可以使用它的泛化(例如形状)的地方使用对象(例如矩形)是面向对象概念的重要组成部分,并称为里氏替换原则。 (这也很明显:什么样的句子或逻辑会做出关于形状的陈述,但不适用于矩形?)
如果您将泛化命名为AbstractShape,则违反了这一原则。矩形不是抽象形状。如果有的话,那就是“具体形状”!矩形不是抽象的(从“我不知道这是什么类型的形状。可能是矩形,也可能是其他任何形状。”的意义上来说)。使用 AbstractShape 的代码读取错误:
我在博客中写了有关此主题的更多想法 在这里。
I would not call abstract classes Abstract for the following reason:
Every Rectangle is a Shape. Everywhere you can use a Shape, you can use a Rectangle. Code that deals with a Rectangle (but which can also deal with Circles) might look like:
Using an object (e.g. a Rectangle) anywhere you can use a generalization of it (e.g. a Shape) is an important part of the object-oriented concept, and known as the Liskov Substitution Principle. (It’s also obvious: what sort of sentence or logic would make statements about shapes, but then not be applicable to rectangles?)
If you name the generalization an AbstractShape, this principle is violated. A Rectangle isn’t an AbstractShape. If anything it’s a “Concrete Shape”! A rectangle isn’t abstract (in the sense of “I don’t know what type of shape this is. Could be a rectangle, could be anything else.”). Code using AbstractShape then reads wrong:
I blogged with more thoughts on this topic here.
我发现以这种方式命名类很有用。它发出一条消息,表明它们将被细分;未实例化;包含子类等通用的代码。
但这并不总是必要的。大多数程序员可能会将“Shape”识别为抽象类,将“Square”、“Circle”等识别为具体类。但如果不是立即清楚,这是一个有用的提示。
您还应该遵循当地的编程约定和风格指南。
I find it useful to name classes in this fashion. It sends a message that they are intended to be sub-classed; not instantiated; contain code common to subclasses, etc.
It's not always necessary though. Most programmers would probably identify "Shape" as an abstract class and "Square", "Circle", etc. as concrete. But if it's not immediately clear, it's a useful hint.
You should also be guided by local programming conventions and style guides.
我认为这部分取决于您将如何使用该课程。如果它只是供内部使用以强制派生类符合接口,那么在它之前添加 Abstract 可能不是一个坏主意。但是,如果您提供的 Foo 工厂将提供实际上是 SpecializedFoo1 或 SpecializedFoo2 的 Foo 实例,那么返回 AbstractFoo 实例似乎很尴尬。
I think it partly depends on how you will be using the class. If it's only intended for internal use in forcing derived classes to conform to an interface, then adding Abstract before it might not be a bad idea. However, if you are providing a Foo factory that will provide Foo instances that are actually SpecializedFoo1 or SpecializedFoo2, then it seems awkward to return AbstractFoo instances.
到目前为止的答案非常有帮助,并显示了负责任的实践传播。我倾向于同意名称不应该指示实现(
Foo
可能是一个抽象类,后来被移动到一个接口)。然而,在编码时获得需要为派生类提供方法的视觉线索对我来说很有用。作为一个例子,我目前有一个层次结构(不要询问名称的基本原理,但它们在上下文中是有意义的并映射到 XML 元素名称)。我使用的是 Java,但我认为大多数语言都是相似的:
我现在倾向于:
而不是
或
我个人可以记得
Marker
是一个抽象的功能概念,子类是具体的实现。The answers so far are very helpful and show a responsible spread of practice. I tend to agree that names should not indicate implementation (
Foo
could be an abstract class which was later moved to an interface). However it is useful for me when coding to have visual clues that I need to provide methods for derived classes.As an example I currently have a hieararchy (don't ask about the rationale for the names but they are meaningful in the context and map onto XML element names). I'm using Java but I think most languages would be similar:
I am now tending towards:
rather than
or
I personally can remember that
Marker
is an abstract functional concept and that the subclasses are concrete implementations.