标签(或“标记")接口是否过时? [英] Are tag (or "marker") interfaces obsolete?

查看:142
本文介绍了标签(或“标记")接口是否过时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试帮助同事与OO达成共识,而且我发现在某些情况下,很难找到可靠的现实示例来说明标签(或 marker )界面. (不包含任何方法的接口;仅用作标签或标记或标签).尽管对于我们的讨论来说确实不重要,但是我们正在使用PHP作为讨论背后的平台(因为这是我们之间的通用语言).我可能不是最适合教授OO的人,因为我的大部分背景都是高度理论化的,大约15岁,但是我就是他所拥有的.

I'm trying to help a coworker come to terms with OO, and I'm finding that for some cases, it's hard to find solid real-world examples for the concept of a tag (or marker) interface. (An interface that contains no methods; it is used as a tag or marker or label only). While it really shouldn't matter for the sakes of our discussions, we're using PHP as the platform behind the discussions (because it's a common language between us). I'm probably not the best person to teach OO since most of my background is highly theoretical and about 15 years old, but I'm what he's got.

无论如何,我发现关于标签接口的讨论不多,使我相信它甚至还不足以用于讨论.我在这里错了吗?

In any case, the dearth of discussions I've found regarding tag interfaces leads me to believe it's not even being used enough to warrant discussion. Am I wrong here?

推荐答案

在Java中使用了标记接口(显而易见的示例是Serializable).尽管C#甚至Java似乎都倾向于使用属性,但它们可以完成相同的任务,但还能做更多的事情.

Tag interfaces are used in Java (Serializable being the obvious example). C# and even Java seem to be moving away from this though in favor of attributes, which can accomplish the same thing but also do much more.

我仍然认为在其他语言中还有一个地方,它们没有.NET和Java具有的属性概念.

I still think there's a place for them in other languages that don't have the attribute concept that .NET and Java have.

ETA:

通常,当您有一个隐含实现的接口时,通常会使用它,但是您不希望实现该接口的类实际上必须提供该实现.

You would typically use this when you have an interface that implies an implementation, but you don't want the class that implements the interface to actually have to provide that implementation.

一些现实世界的例子:

Serializable是一个很好的例子-它表示存在一个可以序列化对象数据的实现(在某个地方),但是由于可以使用通用的实现,因此无需实际使该对象自己实现该功能.

Serializable is a good example - it implies that there is an implementation (somewhere) that can serialize the object data, but since a generic implementation for that is available, there is no need to actually have the object implement that functionality itself.

另一个示例可能是网页缓存系统.假设您有一个"Page"对象和一个"RequestHandler"对象. RequestHandler接收对页面的请求,找到/创建相应的Page对象,在Page对象上调用Render()方法,然后将结果发送到浏览器.

Another example might be a web page caching system. Say you have a "Page" object and a "RequestHandler" object. The RequestHandler takes a request for a page, locates/creates the corresponding Page object, calls a Render() method on the Page object, and sends the results to the browser.

现在,说您想对呈现的页面实施缓存.但是最棘手的是某些页面是动态的,因此无法缓存它们.一种实现方法是使可缓存的Page对象实现ICacheable的标记"接口(反之亦然,您可以具有INotCacheable的接口).然后,RequestHandler将检查Page是否实现了ICacheable,如果这样做了,它将在调用Render()之后缓存结果,并在对该页面的后续请求中提供这些缓存的结果.

Now, say you wanted to implement caching for rendered pages. But the hitch is that some pages are dynamic, so they can't be cached. One way to implement this would be to have the cacheable Page objects implement an ICacheable "tag" interface (Or vice-versa, you could have an INotCacheable interface). The RequestHandler would then check if the Page implemented ICacheable, and if it did, it would cache the results after calling Render() and serve up those cached results on subsequent requests for that page.

这篇关于标签(或“标记")接口是否过时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆