`
dodoflying
  • 浏览: 179684 次
社区版块
存档分类
最新评论
阅读更多
  • 《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、AD35ID2、A1SD35ID1、A1SD35ID2用户手册中涵盖的知识点非常丰富,主要包括了接口模块的技术说明、安全预防措施和设计注意事项等。由于手册是英文的,以下将对这些内容进行详细阐述。 首先...

    接口模板 接口文档

    1. **接口名称**:清晰地标识接口的功能,如“用户注册”、“商品查询”等。 2. **URL**:接口调用的地址,通常由基础URL和资源路径组成。 3. **HTTP方法**:GET、POST、PUT、DELETE等,表示对资源的操作类型。 4. *...

    后端接口文档例子 word

    * 标签:接口的关键词,用于标识接口的类型和用途。 * 请求参数:接口的请求参数,包括参数名、参数类型、是否必填、说明等信息。 * 返回参数:接口的返回参数,包括参数名、参数类型、说明等信息。 三、请求参数...

    7.10 接口设计说明(IDD).docx

    - **接口标识和接口图**:为每个接口分配唯一的项目标识符,并通过名字、编号等标识接口实体。接口图可以直观地展示各实体间的交互。 - **接口特性**:详细说明接口实体的优先级、接口类型、数据元素的特性、数据...

    东北大学接口实验报告.zip

    8. **版本控制**:随着系统的升级,接口可能需要更改,因此需要版本控制,如使用版本号(如/v1,/v2)来标识接口的不同版本。 9. **安全性**:接口安全涉及认证、授权和加密,例如使用OAuth 2.0进行身份验证,HTTPS...

    软件开发文档模板GF-接口设计说明(GJB-438B-2009)-2785.pdf

    * 接口标识是指接口的唯一标识符,用于标识接口的身份和版本号。 七、需求的可追踪性 * 需求的可追踪性是指软件系统的需求追踪,用于标识软件系统的需求和变化。 八、文档模板的使用和裁剪 * 文档模板的使用是指...

    Java的接口简要介绍

    2. **标识接口**:不包含任何方法的接口,主要用于标识实现类的类型。虽然不推荐过度使用,但在某些场景下可以作为标记。 **常量接口** 虽然不推荐,但有时会看到接口被用作存储常量的容器,这是因为接口的常量默认...

    接口文档的描述

    * 接口名称:一个独特的名称,用于标识接口。 * 接口描述:对接口的简要描述,用于解释接口的作用和使用方法。 * 接口格式:接口的格式,包括输入参数和返回值。 * 参数说明:对输入参数的解释,包括参数名称、参数...

    Java标识接口的使用方法

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

    华为5200接口配置

    2. 设置接口描述:为了便于管理和维护,可以在接口上添加描述性文本,以标识接口的功能或位置。 3. 进一步配置接口:这包括设置接口的工作模式(如全双工或半双工)、开启或关闭接口、配置接口的QoS(服务质量)策略...

    基于tp5的restfulapi风格接口oauth20接口版本管理

    在本项目中,可能采用了URL路径、请求头或查询参数等方式来标识接口的版本,允许旧版本的API与新版本并存,减少对已有应用的影响。版本管理使得开发者可以自由地对API进行迭代,同时保持向后兼容。 文件名"Leslin-...

    新接口中国工商银行网上银行B2C在线支付接口说明

    1. **接口名称**(interfaceName):固定为"ICBC_PERBANK_B2C",用于标识接口。 2. **接口版本号**(interfaceVersion):"1.0.0.0",标识接口版本。 3. **订单号**(orderid):商户生成的唯一订单号,用于确认支付...

    接口文档案例

    `key`用于标识接口方法;`sessionId`用于验证用户身份;`reqData`则用于传递具体的请求数据。 - **响应格式**:响应同样采用JSON格式,包含`httpResponse`字段,其内部又包含了`code`、`msg`和`resData`。`code`表示...

    软件开发文档(模版),接口文档

    1. 接口名称:标识接口的独特名称。 2. 描述:简述接口的主要功能和用途。 3. 请求/响应格式:详细列出请求参数、响应数据的结构,通常使用JSON或XML格式。 4. 示例:提供实际的请求和响应示例,帮助开发者快速理解...

    完整的接口调用实例,包括请求类、返回实体类、接口调用、接口处理文件

    因此,良好的API版本控制策略是必要的,比如通过URL路径、请求头或查询参数来标识接口版本。 9. **安全性考虑**: 接口调用可能涉及到身份验证、授权和数据加密。C#提供了多种安全机制,如OAuth2、JWT令牌以及...

    Delphi接口编程

    ['{GUID}'] // GUID 用于唯一标识接口 function SayHello: string; // 定义接口方法 end; ``` 这里的 `TMyInterface` 就是一个接口类型,它定义了一个名为 `SayHello` 的方法,该方法返回一个字符串。 ##### 3.2...

    eSDK TP V100R005C01 接口参考 01(服务端)

    7. **错误码说明**:eSDK TP会返回错误码来标识接口调用失败的原因。理解这些错误码及其含义,有助于定位和解决开发过程中遇到的问题。 通过阅读《eSDK TP V100R005C01 服务端接口参考》的CHM文件,开发者可以全面...

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

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

Global site tag (gtag.js) - Google Analytics