Marker interface pattern: Difference between revisions

Content deleted Content added
Adding short description: "Design pattern in computer science"
 
(93 intermediate revisions by 65 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 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.
{{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 }}</ref> pattern(also allows a 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 methodsof the interface. 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.
 
== Example ==
One good example of a marker interface comes from the [[Java programming language]]. The ''[http://java.sun.com/j2se/1.4.2/docs/api/ Cloneable]'' interface should be implemented by a class if it fully supports the [http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#clone() Object.clone()] method. Every class in Java has the [http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html 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 ''Clonable'' 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 {
}
 
</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>.
 
==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).
 
Another solution is for the language to support [[metadata]] directly:
* 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]].
* 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==
*[[Design marker]]s for an expansion of this pattern.
 
==References==
{{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)]]