Marker interface pattern: Difference between revisions

Content deleted Content added
Adding short description: "Design pattern in computer science"
 
(72 intermediate revisions by 53 users not shown)
Line 1:
The{{Short '''marker interface pattern''' is a [[design pattern (computer science)description|designDesign pattern]] in [[computer science]].}}
{{Refimprove|date=June 2013}}
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.
 
To use this pattern, a [[Class (computer science)|class]] implements a '''marker interface'''<ref name="EffectiveJava">
This pattern allows a [[Class (computer science)|class]] to implement a '''marker interface''', which exposes some underlying semantic property of the class that cannot be determined solely by the class' [[Method (computer science)|method]]s. 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.
{{cite book
| last = Bloch
| first = Joshua
| title = Effective Java
| page = [https://archive.org/details/effectivejava00bloc_0/page/179 179]
| chapter = Item 37: Use marker interfaces to define types
| year = 2008
| isbn = 978-0-321-35668-0
| publisher = Addison-Wesley
| chapter-url-access = registration
| chapter-url = https://archive.org/details/effectivejava00bloc_0/page/179
| edition = Second
This pattern allows a [[Class }}</ref> (computeralso science)|class]] to implement acalled '''markertagging interface''',) which exposesis somean underlyingempty semanticinterface,<ref>{{Cite propertyweb of|date=2017-03-06 the|title=Marker classinterface in Java |url=https://www.geeksforgeeks.org/marker-interface-java/ |access-date=2022-05-01 |website=GeeksforGeeks |language=en-us}}</ref> and methods that cannotinteract bewith determinedinstances solelyof bythat class test for the class'existence [[Methodof (computerthe 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.
 
== See alsoExample ==
An example of the application of marker interfaces is the [[Java programming language]]. The {{Javadoc:SE|java/lang|Cloneable}} interface should be implemented by a class if it fully supports the {{Javadoc:SE|java/lang|Object|clone()}} method. Every class in Java has the {{Javadoc:SE|java/lang|Object}} class at the root of its inheritance hierarchy and so every object instantiated from any class ''has'' an associated <code>clone()</code> method. However, developers should only call <code>clone()</code> on objects of classes which implement the <code>Cloneable</code> interface, as it indicates that the cloning functionality is actually supported in a proper manner.
An example of the application of marker interfaces from the [[Java (programming language)|Java programming language]] is the {{Javadoc:SE|java/io|Serializable}} interface:<syntaxhighlight lang="java">
package java.io;
 
public interface Serializable {
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.
}
 
</syntaxhighlight>A class implements this interface to indicate that its non-[[Transient (computer programming)|transient]] data members can be written to an {{Javadoc:SE|java/io|ObjectOutputStream}}. The <code>ObjectOutputStream</code> private method <code>writeObject0(Object,boolean)</code> contains a series of <code>instanceof</code> tests to determine writeability, one of which looks for the <code>Serializable</code> interface. If any of these tests fails, the method throws a <code>NotSerializableException</code>.
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 ==
One problem with marker interfaces is that, since an interface defines a contract for implementing classes, and that contract is inherited by all subclasses, a marker cannot be "unimplemented". In the example given, any subclass not intended for serialization (perhaps it depends on transient state), must explicitly throw NotSerializableException exceptions (per <code>ObjectOutputStream</code> docs).
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.
 
Another solution is for the language to support [[metadata]] directly:
== See also ==
* Both the [[.NET Framework]] and [[Java (software platform)|Java]] (as of Java 5 (1.5)) provide support for such metadata. In .NET, they are called ''"custom attributes"'', in Java they are called ''"[[Java annotation|annotations]]"''. Despite the different name, they are conceptually the same thing. They can be defined on classes, member variables, methods, and method parameters and may be accessed using [[Reflection (computer science)|reflection]].
* [[Design marker]]s for an expansion of this pattern.
* In [[Python (programming language)|Python]], the term "marker interface" is common in [[Zope]] and [[Plone (software)|Plone]]. Interfaces are declared as metadata and subclasses can use <code>implementsOnly</code> to declare they do not implement everything from their super classes.
 
==See also==
[[Category:Software design patterns]]
* [[Design marker]]s for an expansion of this pattern.
[[Category:Java programming language]]
[[Category:Java programming language]]
 
==References==
Not able to see how jvm understands the markup interfaces
{{Reflist}}
 
== Further reading ==
''Effective Java''<ref>{{Cite book |last=Bloch |first=Joshua |title=Effective Java |date=2018 |isbn=978-0-13-468599-1 |edition=Third |___location=Boston |oclc=1018432176}}</ref> by [[Joshua Bloch]].
 
{{Design Patterns patterns}}
 
[[Category:Software design patterns]]
[[Category:Java (programming language)]]