From Surf Wiki (app.surf) — the open knowledge base
Marker interface pattern
Design pattern in computer science
Design pattern in computer science
The marker interface pattern is a 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 implements a marker interface | chapter-url-access = registration | chapter-url = https://archive.org/details/effectivejava00bloc_0/page/179
Example
An example of the application of marker interfaces from the Java programming language is the interface: package java.io;
public interface Serializable { }
A class implements this interface to indicate that its non-transient data members can be written to an . The ObjectOutputStream private method writeObject0(Object,boolean) contains a series of instanceof tests to determine writeability, one of which looks for the Serializable interface. If any of these tests fails, the method throws a NotSerializableException.
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 ObjectOutputStream docs).
Another solution is for the language to support metadata directly:
- Both the .NET Framework and Java (as of Java 5 (1.5)) provide support for such metadata. In .NET, they are called "custom attributes", in Java they are called "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. C++26 adds similar support for annotations to C++.
- In Python, the term "marker interface" is common in Zope and Plone. Interfaces are declared as metadata and subclasses can use
implementsOnlyto declare they do not implement everything from their super classes.
References
References
- (2017-03-06). "Marker interface in Java".
- Bloch, Joshua. (2018). "Effective Java".
This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.
Ask Mako anything about Marker interface pattern — get instant answers, deeper analysis, and related topics.
Research with MakoFree with your Surf account
Create a free account to save articles, ask Mako questions, and organize your research.
Sign up freeThis content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.
Report