Marking interface

from Wikipedia, the free encyclopedia

A marker interface (English marker interface or day Interface ) is a design pattern that a program to maturity provides information about an object. This offers the possibility of adding metadata to a class, although the programming language does not explicitly support this.

In this pattern, an implemented class an interface which usually no functionality in the form of method declarations defined. When interacting with the class, other (non-class) methods test whether the marking interface for the (instantiated) class exists. The presence of such an interface defines a certain behavior for the implementing class. Hybrid interfaces which both act as markers and declare methods are basically possible, but can be confusing if used incorrectly.

An example of the use of marking Serializableinterfaces in the Java programming language is the interface, which signals that the persistent attributes of the implementing class ObjectOutputStreamcan be written into one . The method ObjectOutputStream.writeObject()contains several instanceoftests that check whether the transferred object Serializablecontains the interface . If this is not the case, one is NotSerializableExceptionthrown.

criticism

"Difficult legacy"
In object-oriented programming, the implementation of even the narrowest interface is passed on to inheriting classes. This means that a class that inherits from a serializable class cannot be defined as "non-serializable". For this reason, the runtime is sometimes used in order to reveal logical errors at all: In the Java documentation, for example, it is recommended to explicitly NotSerializableExceptionthrow one.
"Smell"
Another problem is the fact that the operator instanceof ("instance of") must be used to recognize a marking interface, which can be regarded as a smell ("bad smell", i.e. questionable programming technique) and thus also characterizes the pattern as a smell.
Obfuscation
The program logic is moved from the regular source code into frameworks that are based on the analysis of metadata. That makes them difficult for people to understand. Defining marking interfaces yourself is usually completely excessive for everyday programming tasks. However, since tagging seems familiar as a technique, beginners to OO are easily inclined to use this method (ultimately only to postpone the task). The use of marking interfaces deprives the compiler of possibilities for static checking.

literature

Individual evidence

  1. a b Marker Interface. In: Portland Pattern Repository. Ward Cunningham , July 9, 2002, accessed February 19, 2009 .