The '''marker interface pattern''' is a [[design pattern (computer science)|design pattern]] in [[computer science]], used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata.
ThisTo patternuse allowsthis pattern, a [[Class (computer science)|class]] to implementimplements a '''marker interface''', whichand exposesmethods somethat underlyinginteract semanticwith propertyinstances of thethat class thattest cannotfor bethe determinedexistence solely byof the class' [[Method (computer science)|method]]sinterface. Whereas a typical [[interface (computer science)|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 isfrom the [[Java (programming language)|Java programming language]]. Theis the {{Javadoc:SE|java/langio|CloneableSerializable}} interface. shouldA beclass implementedimplements bythis ainterface classto ifindicate itthat fullyits supportsnon-transient thedata {{Javadoc:SE|java/lang|Object|clone()}}members method.can Everybe classwritten into Java has thean {{Javadoc:SE|java/langio|ObjectObjectOutputStream}}. classThe at<code>ObjectOutputStream</code> theprivate root of its inheritance hierarchy and so every object instantiated from any class ''has'' an associatedmethod <code>clonewriteObject0()</code> method.contains However,a developersseries should only callof <code>clone()instanceof</code> ontests to determine writeability, objectsone of classes which implementlooks for the <code>CloneableSerializable</code> interface,. asIf itnone indicatesof thatthese thetests cloningpass, functionalitythe ismethod actuallythrows supported in a proper manner<code>NotSerializableException</code>.
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 <code>Cloneable</code>, and then do something in your subclass that means it can't be cloned properly, your subclass will still be marked as a <code>Cloneable</code> whether you want it or not. One way out is to throw an Exception (a {{Javadoc:SE|java/lang|CloneNotSupportedException}} is a good idea) in the <code>clone()</code> method, but then you are missing the whole point of the marker interface.
A major problem with marker interfaces is that an interface defines a contract for implementing classes, and that contract is inherited by all subclasses. This means that you cannot "unimplement" a marker. In the example given, if you create a subclass that you do not want to serialize (perhaps because it depends on transient state), you must resort to explicitly throwing <code>NotSerializableException</code> (per <code>ObjectOutputStream</code> docs).
Still, this is generally a good idea, as subclasses usually ''do'' inherit behaviors from their parents and should inherit the marker interfaces as well.
A better solution is for the language to support metadata directly. The [[.NET Framework|.NET framework]] supports attributes that can be used to associate any type of data with a class or with its members. Java, as of Java 5 (1.5), also provides annotations on classes, methods, and member variables, that may be accessed from an instance using reflection.
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 Framework|.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 [[reflection (computer science)|reflective]] [[annotation]]s that can be used to associate attributes with classes, interfaces, methods, [[Constructor (computer science)|constructors]], [[Field (computer science)|fields]] and [[Java package]]s. Annotation allow a rich and fine-grained association of [[metadata]] to program elements.
== See also ==
|