标识接口

来源:互联网 发布:java全栈框架 编辑:程序博客网 时间:2024/04/29 20:45
  • 《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


  • Design markers for an expansion of this pattern.








原创粉丝点击