接口作为标签在java OO中是不好的做法吗? [英] Is Interface as labels a bad practice in java OO?

查看:130
本文介绍了接口作为标签在java OO中是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在解析某些xml文件的过程中,我遇到了一种情况,我必须使用界面作为标签来识别某些标签属于某个类别,例如,我创建了一个标记界面确定这些类用于表示xml标记, ContainableTag 指出某些标记可​​以是某些标记的子标记之一。

During the parsing of certain xml files, I come across a situation that I have to use interface as labels to identify that certain tags belong to certain category, for example, I created a Tag interface to identify that these classes are used to represent xml tags, and ContainableTag to point out that certain tags can be one of the children tags of some tags.

然后我偶然发现了这个页面: http://xahlee.org/java-a- day / interface.html (请查找 Interface as Labels 会话。)。它说:

Then I stumble into this page: http://xahlee.org/java-a-day/interface.html (Please look for the "Interface as Labels" session.). It says:


问题的关键在于它是
这个语言
的数学无关性。作为一种语言的标签机制
,从软件工程
透视可能获得
的利益,那么它不应该是
设计为Class
接口的一部分,因为
标签的概念和编程
接口的概念在语义上是完全不同的。

The gist of the problem is that it is a piece of mathematical irrelevance in the language. As a labeling mechanism in a language, for the possible benefit from the software engineering perspective, then it should not be designed as part of the Class Interface, since the concept of labeling, and concept of programing interface, are semantically disparate.

所以是作为标签的界面一定是不好的做法?作为一名java程序员,我们还有其他一些选择吗?

So is interface as labels necessarily a bad practice? As a java programmer, do we have some other alternatives?

推荐答案

作为标记的接口在很大程度上被Java中的注释机制取代5或以后。它们允许您添加任意元数据。如果您的接口是空的并且仅用作类标记,那么您应该使用注释。

Interfaces as markers have largely been replaces by the annotation mechanism in Java 5 or later. They allow you to add arbitrary meta-data. If your interfaces are empty and are only serving to act as class markers, then you should be using annotations instead.

这篇关于接口作为标签在java OO中是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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