Marker interface pattern

Design pattern in computer science


title: "Marker interface pattern" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["software-design-patterns", "java-(programming-language)"] description: "Design pattern in computer science" topic_path: "technology/software-engineering" source: "https://en.wikipedia.org/wiki/Marker_interface_pattern" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary 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 | last = Bloch | first = Joshua | title = Effective Java | page = 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

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 implementsOnly to declare they do not implement everything from their super classes.

References

References

  1. (2017-03-06). "Marker interface in Java".
  2. Bloch, Joshua. (2018). "Effective Java".

::callout[type=info title="Wikipedia Source"] 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. ::

software-design-patternsjava-(programming-language)