使用空白界面是一个糟糕的设计吗?

发布于 2024-10-02 08:32:57 字数 125 浏览 2 评论 0原文

我正在考虑创建一个接口并将其应用于某个命名空间中的所有对象。

我将使用它的场景:我想创建这些对象的通用处理程序,并且我想强制通用参数仅接受实现此特定接口的类型。

这是一个糟糕的设计,还是可以接受空白界面?

I'm considering creating an interface and applying it to all objects in a certain namespace.

Scenario in which I would use this: I want to create a generic handler of those objects, and I'd like to enforce the generic parameter to only accept types that implement this particular interface.

Is this a bad design, or are blank interfaces acceptable?

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

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

发布评论

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

评论(6

碍人泪离人颜 2024-10-09 08:32:57

您所说的操作称为标记接口

我过去曾成功地使用过这种技术,但我会质疑您对泛型类施加的人为限制是否实际上是必要的。如果是这样,那么这无疑是一种快速且经过编译检查的实现方法。

What you are talking about doing is known as Marker Interfaces.

I've used this technique successfully in the past, but I would question whether the artificial limitation you are placing on your generic class is actually necessary. If so, then this is certainly a quick and compile checked method of accomplishing it.

拔了角的鹿 2024-10-09 08:32:57

不,这不是一个糟糕的设计。对泛型参数提供合理的约束有助于维护代码的安全性和可读性。此外,您可以非常确定该界面不会永远保持空白。

No, it's not bad design. Providing reasonable constraints on generic arguments helps maintain code safety and readability. Furthermore, you can be quite sure the interface won't remain empty forever.

祁梦 2024-10-09 08:32:57

在我看来他们没有什么问题。它们的很多优势在于与(生成的)部分类的结合。

Nothing wrong with them in my opinion. A lot of their strength lies in combination with (generated) partial classes.

海风掠过北极光 2024-10-09 08:32:57

我个人对接口的理解是,它形成了一个或多个对象之间的公共契约。这个想法很简单,您可以使用接口构建代码库,而不必担心实现。话虽如此,这是一个有趣的问题,因为从技术上讲,您正在尝试执行合同。我会说继续吧。

My personal understanding of an interface is that it forms the public contract between one or more objects. The idea is simple, you can build your code base up using interfaces without worrying about implementation. Having said that it is an interesting question because you are technically trying to inforce a contract. I would say go ahead.

爱*していゐ 2024-10-09 08:32:57

如果它是空白的,为什么要创建这个界面?每节课的内容是什么?它们之间有任何共同点吗?如果这个问题的答案是否定的,那么为什么要为每个人制作一个界面呢?

如果您有充分的理由制作空白界面,那可能没问题。但想一想,看看是否可以将元素之间通用的任何内容放入该界面中。

Why are you creating this interface if it's blank? What are the contents of each class? Is there any commonality between them? If the answer to that question is no, then why are you making an interface for each?

If you have a good reason for making a blank interface, it's probably alright. But think about it and see whether you can put anything into that interface that is common between the elements.

带上头具痛哭 2024-10-09 08:32:57

我想,使用空白界面并没有什么问题。只是,接口的目的是定义一组通用的功能,这些功能的实现方式可能会有所不同。

它不会使编码变得更容易,因为您正在有效地使用黑盒(对象,有些人可能会这样称呼它)。

但老实说,如果我要实现这样一个开放式架构,我只会使用常规的旧对象

There's nothing... Wrong with using a blank interface, I guess. Just, the point of an interface is to define a common set of functionality that may vary in how its implemented.

It won't make coding any easier, since you're effectively working with a black-box (object, as some may call it).

Honestly, though, if I were implementing such an open ended architecture, I'd just go with regular old object.

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