我应该将所有抽象类命名为 AbstractFoo

发布于 2024-08-04 23:33:42 字数 39 浏览 4 评论 0原文

确保所有抽象类的名称都以“Abstract”为前缀是个好习惯吗?

Is it good practice to make sure that all abstract classes have names prefixed with "Abstract"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

横笛休吹塞上声 2024-08-11 23:33:42

你可以,但我倾向于不这样做,因为这是一个实现细节。

我不喜欢在类型和标识符的名称中添加实现详细信息,因为此类信息将来可能会发生变化。在我看来,最好按事物的本质来命名,而不是它们是如何实现的。

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.

迷离° 2024-08-11 23:33:42

我认为这个命名约定只是因为很难想出另一个好名字而被使用。如果您已经有一个名为“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.

清醇 2024-08-11 23:33:42

这取决于您的编码约定。

您也可以将它们称为 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.

南风几经秋 2024-08-11 23:33:42

如果您考虑一下它在 .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.

夏夜暖风 2024-08-11 23:33:42

有点难以解释,但我只是用它来避免在功能类中复制/粘贴相同的代码,而不是在域对象之类的东西中。

  • 抽象服务测试用例 --> Abstract 前缀似乎很有帮助
  • AbstractAnimal -->看起来很奇怪并且没有帮助,

您当然应该自己决定,只要在整个项目中遵循相同的约定即可。

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.

  • AbstractServiceTestCase --> The Abstract prefix seems helpful
  • AbstractAnimal --> Seems weird and unhelpful

You should ofcourse decide for yourself, as long as the same convention is followed throughout a project.

悲喜皆因你 2024-08-11 23:33:42

我不会将抽象类称为 Abstract,原因如下:

每个矩形都是一个形状。凡是可以使用 Shape 的地方,都可以使用 Rectangle。处理矩形(但也可以处理圆形)的代码可能如下所示:

Shape s = .....;
s.drawTo(myGraphicsContext);

在任何可以使用它的泛化(例如形状)的地方使用对象(例如矩形)是面向对象概念的重要组成部分,并称为里氏替换原则。 (这也很明显:什么样的句子或逻辑会做出关于形状的陈述,但不适用于矩形?)

如果您将泛化命名为AbstractShape,则违反了这一原则。矩形不是抽象形状。如果有的话,那就是“具体形状”!矩形不是抽象的(从“我不知道这是什么类型的形状。可能是矩形,也可能是其他任何形状。”的意义上来说)。使用 AbstractShape 的代码读取错误:

AbstractShape s = new Rectangle(...);

我在博客中写了有关此主题的更多想法 在这里

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:

Shape s = .....;
s.drawTo(myGraphicsContext);

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:

AbstractShape s = new Rectangle(...);

I blogged with more thoughts on this topic here.

2024-08-11 23:33:42

我发现以这种方式命名类很有用。它发出一条消息,表明它们将被细分;未实例化;包含子类等通用的代码。

但这并不总是必要的。大多数程序员可能会将“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.

孤君无依 2024-08-11 23:33:42

我认为这部分取决于您将如何使用该课程。如果它只是供内部使用以强制派生类符合接口,那么在它之前添加 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.

吻风 2024-08-11 23:33:42

到目前为止的答案非常有帮助,并显示了负责任的实践传播。我倾向于同意名称不应该指示实现(Foo 可能是一个抽象类,后来被移动到一个接口)。然而,在编码时获得需要为派生类提供方法的视觉线索对我来说很有用。

作为一个例子,我目前有一个层次结构(不要询问名称的基本原理,但它们在上下文中是有意义的并映射到 XML 元素名称)。我使用的是 Java,但我认为大多数语言都是相似的:

public abstract class Marker {...}
public class Template extends Marker {...}
public class Regex extends Marker {...}

我现在倾向于:

public abstract class Marker {...}
public class TemplateMarker extends Marker {...}
public class RegexMarker extends Marker {...}

而不是

public abstract class AbstractMarker {...}
public class Template extends AbstractMarker {...}
public class Regex extends AbstractMarker {...}

public abstract class AbstractMarker {...}
public class TemplateMarker extends AbstractMarker {...}
public class RegexMarker extends AbstractMarker {...}

我个人可以记得 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:

public abstract class Marker {...}
public class Template extends Marker {...}
public class Regex extends Marker {...}

I am now tending towards:

public abstract class Marker {...}
public class TemplateMarker extends Marker {...}
public class RegexMarker extends Marker {...}

rather than

public abstract class AbstractMarker {...}
public class Template extends AbstractMarker {...}
public class Regex extends AbstractMarker {...}

or

public abstract class AbstractMarker {...}
public class TemplateMarker extends AbstractMarker {...}
public class RegexMarker extends AbstractMarker {...}

I personally can remember that Marker is an abstract functional concept and that the subclasses are concrete implementations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文