`
dodoflying
  • 浏览: 177034 次
社区版块
存档分类
最新评论
阅读更多
  • 《java与模式》

标识接口是没有任何方法和属性的接口。标识接口不对实现它的类有任何语义上的要求,它仅仅表明它的类属于一个特定的类型。
标识接口在java语言中有一些很著名的应用,比如java.io.Serializable和java.rmi.Remote等接口便是标识接口。
标识接口通常使用在工具类中,很少在其他地方使用。 
                                            

  • Sun 的 javadoc
public interface Remote

The Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine.

Any object that is a remote object must directly or indirectly implement this interface.

Only those methods specified in a "remote interface", an interface that extends java.rmi.Remote are available remotely.

Implementation classes can implement any number of remote interfaces and can extend other remote implementation classes.

RMI provides some convenience classes that remote object implementations can extend which facilitate remote object creation. These classes are java.rmi.server.UnicastRemoteObject and java.rmi.activation.Activatable.

For complete details on RMI, see the RMI Specification which describes the RMI API and system.

  • 看了这些,我还是不太明白所谓的这种“标识接口”,使jvm级别上的呢?还是design级别上的?
    是否在实际的项目设计中也能够采用这种设计?
  • 再看看Serialization的javadoc
public interface Serializable
......The serialization interface has no methods or fields and serves only to identify the
semantics of being serializable.

http://download.java.net/jdk6/docs/platform/rmi/spec/rmi-objmodel5.html

 

The interface java.rmi.Remote is a marker interface that defines no methods: 

public interface Remote {} 


  • jguru上关于Marker interface的定义


http://www.jguru.com/faq/view.jsp?EID=224126

A so-called marker interface is a Java interface which doesn't actually define any fields. It is just used to "mark" Java classes which support a certain capability -- the class marks itself as implementing the interface. For example, the java.lang.Cloneable interface.

-----------------------------------------------------------------------------------


  • 来自sun的java论坛上的重要信息


http://forum.java.sun.com/thread.jspa?threadID=608939&messageID=3333590

A marker or tagging interface is an interphase with no methods. It can be created by ANYBODY.
 the purpose of these interfaces is to create a special type of Object, in such a way that you
are restricting the classes that can be cast to it without placing ANY restrictions on the exported methods that must be provided. It mostly indicates that a class should or should not be considered an acceptable class for a certain operation.

Some examples are:

Serializable: This is used to indicate that an object may be serialized for persistance purposes.
It has no methods. The writeObject and readObject methods are actually not part of the interface,
 but rather part of the serialization process, and help determine how to serialize an object. hence,
 when using a design tool (like jBuilder), when you implement serializable, it automatically
creates writeObject and readObject -- But they are not necessary.

Cloneable: Every Object has a method clone(). But in order to call that method, a class must
be marked as Cloneable. Thus, it is just another marking interface.

EventListener: EventListener is a tagging interface that is an important part of introspection
in Java Beans. It is possible to use the Delegation Event design for events without implementing
EventListener, but in order for the Java beans introspector to recognize the EventListener, it
must be tagged by implementing java.util.EventListener.

And the list goes on...


Here's an example where you could create youre own tagging interface:

let's say you were modeling a department store. You have objects that represent all of the merchendise you can sell. Now, you have an initiative in the store that you will sell at least 70% American Made merchendise. Then you could create the interface

public interface AmericanMade {} and implement that on every object you sell that is made in America. Now, a customer come
and wants to purchase only American Made products, so you show him a revised catalogue,
like this:

for (Product product : allProducts) { if (product instanceof AmericanMade) { newList.add(product); } } See, the whole point is that it creates a new type of merchendise, the AmericanMade, that
can be distinguished from others, whether the product is Toilet Paper, T-Shirts, Motor Oil,
or Golf Clubs. It does not define any properties or methods, though, because there is likely
no difference between an american made golf club and an English golf club, except that buying
one helps support the American economy, and the other hleps support the English economy.



  • marker interface pattern


The marker interface pattern is a design pattern in computer science
.

This pattern allows a class to implement a marker interface, which
exposes some underlying semantic property of the class that cannot be determined solely by the class' methods. Whereas a typical interface
specifies functionality (in the form of method declarations) that an
implementing class must support, a marker interface need not
do so. The mere presence of such an interface indicates specific
behavior on the part of the implementing class. Hybrid
interfaces, which both act as markers and specify required methods, are
possible but may prove confusing if improperly used.




An example of the application of marker interfaces is the Java programming
language
. The Cloneable interface should be implemented by a class if it fully supports the Object.clone() method. Every class in Java has the Object class at
the root of its inheritance hierarchy and so every object instantiated from any class has an associated
clone() method. However, developers should only call clone() on objects of classes which implement the
Cloneable interface, as it indicates that the cloning functionality is actually supported in a proper manner.




Unfortunately there is a problem with this example, namely that in Java you cannot "unimplement" an interface. So if you
subclass a class that implements Cloneable, and then do something in your subclass that means it can't be cloned
properly, your subclass will still be marked as a Cloneable whether you want it or not. One way out is to throw an
Exception (a CloneNotSupportedException is a good idea) in the clone() method, but then
you are missing the whole point of the marker interface.


Still, this is generally a good idea, as subclasses usually do inherit behaviors from their parents and should inherit

the marker interfaces as well.


Critique

The implementation of this pattern presented above is rather specific to Java. Other object models support rich ways of
quickly querying static data. For example, .NET supports attributes that can be used to
associate any type of data with a class or with its members. As of J2SE
5.0, Java supports reflective annotations that can be used to associate attributes with classes, interfaces, methods, constructors, fields and
Java packages. Annotation allow a rich and fine-grained association of metadata to program elements.



See also










分享到:
评论

相关推荐

    三菱 标识接口模块AD35ID1 2,A1SD35ID1 2用户手册(英文).pdf

    三菱 标识接口模块AD35ID1 2,A1SD35ID1 2用户手册(英文)pdf,三菱 标识接口模块AD35ID1 2,A1SD35ID1 2用户手册(英文)

    Java标识接口的使用方法

    在本篇文章中小编给大家分享了关于Java标识接口的使用方法和教程内容,有需要的朋友们学习下。

    软件开发文档模板GF-接口设计说明GJB438B-2009-2785.doc

    该文档是关于软件开发文档模板的接口设计说明,包含了文档标识、版本号、密级、编制日期、文档名称、接口设计说明、系统概述、文档概述、引用文档、接口设计、接口标识、要求的可追踪性、注释等内容。下面是从该文档...

    springboot通过面向接口编程对控制反转IOC的理解.docx

    需要让上层开发人员根据自己的业务去实现它,比较返回一个当前登陆的用户实体,或者返回... * 返回用户ID的标识接口,由程序使用者去实现. */ public interface UserIdAuditorAware extends AuditorAware<String> { }

    六合一综合平台,外挂系统接口使用手册.pdf

    * jkid:接口标识,5 位,由系统类别+一位级别代码+两位接口顺序号组成(参照监管系统),如 01A01 代表机动车部局端提供的接口。 * QueryXmlDoc:查询条件封装查询条件的 XML 格式文档。 写入类接口 写入类接口...

    计算机常见外部接口图解

    计算机常见外部接口图解,教你识别计算机的各类接口,并对各个接口的作用进行了详细说明

    接口设计说明书(软件设计文档范例)

    接口设计说明书 本接口说明书详细介绍了LK9000编程接口函数的功能,用法、声明所在的头文件。用户在开发LK9000软件时必须阅读该说明书。

    上期CTP期权接口API开发服务v6.3.0官方版

    上期CTP期权接口API开发服务是一款上期CTP期权API开发服务期货接口开发包,南华期货面向高端专业机构,提供内外盘统一的行情和交易API接口,已与POBO、风软等主流软件成功对接,标志API接口商业应用正式启动,服务...

    联通H2接口规范

    编码总则一:中国联通客服H接口编码标准规则满足唯一标识客服H接口中的实体及其属性的需求。编码采用十进制编码。 编码总则二:编码主要采用分层的方式,共分为四层,每层对应若干位编码。编码位的具体值参见相应的...

    农业银行 银企直连 接口文档

    1.4.6 关于续查标志 16 1.4.7 关于多域串 16 1.4.8 关于定位串 16 1.4.9 关于文件 16 1.4.10 关于操作员密码 16 1.4.11 关于认证码、数字签名和加密 16 1.4.12 关于返回结果 17 1.4.13 关于批量数据下传方式 17 ...

    GPRS接口开发

    ID、计费标识、本次通话 GGSN 地址、APN 网络标识、PDP 类型、终端的 PDP 地址、上行流量、下行流量、话单开始时间、话单时长、SGSN 改变标志、记录关闭原因、连接释放原因、记录序号、节点 ID、记录号、APN 的运行...

    安卓 USB 设备检测.rar

    接口数量 可编程组种类 散列码 端点数 端点类型 端点地址 端点属性 端点方向 端点号 端点间隙 端点最大包大小 设备通信状态 准备列表 因为只用于测试设备信息,所以为稳定的获得,请先插入USB设备,再启动本软件。 ...

    深入探索Java对象的序列化

    Serializable接口是一个标识接口,没有抽象方法。 Serializable有一个子接口Externalizable,实现Externalizable接口的类可以自行控制对象序列化荷反序列化过程。 一般来说,没有必要自己实现序列化接口,直接...

    easyopen 接口开放平台.rar

    接口开放平台,为访问者提供统一的URL作为唯一访问入口,参数中指定具体接口标识和接口参数。它的优点包括接口规范性、效率、管理能力。接口开放平台可以分为简单的和复杂的两种方式。简单的接口开放平台,由自身...

    泛微Ecology8系统接口文档 .pdf

    泛微Ecology8系统接口文档,该接口主要用来在 e-cology配置和异构系统的数据库链接方式,给有研发能力的客户或有系统集成需求的客户提供泛微各类接口调用的参考例程,面向具有一定web service开发能力或了解web ...

    前后端接口的链接成品

    URL符合业界普遍接受的REST规则,减少在URL中标识操作类型的情况,如使用POST /users代替POST /users/save。 出于技术的限制,对于一些特殊的场景,接口会在REST设计的基础之上进行一些妥协,具体参考各接口规范...

    api接口文档模板

    API 接口文档模板详解 API 接口文档模板是指用于描述 API 接口的文档,旨在提供给开发者使用,以便他们更好地理解和使用 API 接口。该模板主要包括接口说明、使用场景、接口调用说明、输入参数说明、请求示例、返回...

    网络游戏防沉迷认证系统 接口测试 anti-addiction Node.js版本

    ## 项目介绍 网络游戏防沉迷认证系统 接口测试 anti-addiction Node.js版本 ... //业务权限标识 const RAA_SECRET_KEY = '这里填入Secret Key'; //接口签名 TEST_CODE_1 ... TEST_CODE_8 填入对

    CDMA空中接口技术

    5.5.1 标识 81 5.5.2 消息的三层结构和功能 84 5.5.3 指令消息 85 5.6 小结 87 第六章 窄带CDMA空中接口的运作机制 89 6.1 引言 89 6.2 终端的运作机制 89 6.2.1 初始化状态 90 6.2.2 待机状态 ...

    Cache数据库创建Restful接口.pdf

    名称是Web服务的唯一标识,而描述信息是对Web服务的详细描述。同时,我们还需要定义Web服务的安全设置,例如身份验证和授权机制。 三、测试Restful接口 在创建完Restful接口后,我们需要对其进行测试。测试是软件...

Global site tag (gtag.js) - Google Analytics