标记界面的目的是什么? [英] What is the purpose of marker interface?

查看:114
本文介绍了标记界面的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

marker界面

目的是什么一个标记界面?

我知道什么是标记界面 - 一个界面没办法。
示例:Serializable,Remote,Cloneable。

I know what is marker interface - An interface with no methods. Example: Serializable, Remote, Cloneable.

我想知道标记接口的用途是什么。这是我的理解: -

I am wondering what is the purpose of marker inteface. This is my understanding:-

基本上只是从普通对象中识别特殊对象。与序列化的情况一样,需要序列化的对象必须实现可序列化的接口,并且如果它是可序列化的实例,则必须在某处检查writeObject()方法。据我所知,这是使用接口serializable(标记接口)的writeObject的唯一目的。对?或者jvm在可序列化接口的基础上提供了一些额外的功能?

Basically it is just to identify the special objects from normal objects. Like in case of serialization , objects that need to be serialized must implement serializable interface and down the line writeObject() method must be checking somewhere if it is a instance of serializable or not. As far as i think ,Thats the only purpose for which writeObject using interface serializable(marker interface). Right? or jvm provides some extra functionality too on the basis of serializable interface?

类似的逻辑适用于可克隆接口。

Similar kind of logic is true for cloneable interface.

现在让我们看看它是如何有用的。

Now lets see how it is useful.

让我们在应用程序中说我们有1000个值对象。我们只想序列化100个特定对象。现在JDK已经说过程序员角色只是用标记接口将对象标记为特殊的,在这种情况下它是可序列化的接口。现在JDK会处理哪个对象要序列化?

Lets say in a application we have 1000 value objects.We want to serialize only 100 specific objects . Now JDK has said that programmer role is just to mark the object as special with marker interface in this case its serializable interface.Now JDK will take care of that which object to serialize or not?

是的,我们可以通过某种博尔干国旗来实现这种行为。但是
将是一个不好的方法。

Yes we could achieve this kind of behaviour with some kind of bollean flag . but that would be a bad approach.

我们可以说,在用户定义的标记界面的情况下,我们可以借助标记界面区分特殊对象。就像我们可以使用运算符实例来确定它是否是该特定接口的实例。如果是的话,在没有抛出一些例外情况下继续。

Similarily we can say in case of user defined marker interface , we can distinguish special objects with help of marker interface.Like we can use instance of operator to determine if it is a instance of that particular interface . If yes proceed in case of No throw some exception.

如果上述理解是正确的,请告诉我吗?

Please let if above understanding is correct?

推荐答案

您的理解是正确的。标记界面还定义了一种类型。因此,它可以用于方法签名。例如,Hibernate的 Session.get()方法将Serializable作为参数。它避免传递一个不可序列化为主参数的主键。

Your understanding is correct. The marker interface also defines a type. It can thus be used in method signatures. For example, Hibernate's Session.get() method takes a Serializable as argument. It avoids passing a primary key that would not be serializable as argument.

请注意,Cloneable被认为是一个糟糕的设计选择。

Note that Cloneable is, retrospectively, seen as a bad design choice.

如果在实现序列化时存在注释,那么Serializable当然可以使用注释实现。

Serializable could certainly have been implemented with an annotation if those had existed when serialization was implemented.

标记接口大部分时间都是反模式。接口应该定义多态行为。标记界面可以用注释替换。

Marker interfaces are, most of the time, an anti-pattern. An interface should define a polymorphic behaviour. A marker interface can be replaced by an annotation.

这篇关于标记界面的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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