Home  >  Article  >  Java  >  Item Use marker interfaces to define types

Item Use marker interfaces to define types

PHPz
PHPzOriginal
2024-07-17 09:02:20815browse

Definition and Purpose

Marker Interface:

  • Does not contain method declarations.
  • Marks a class as having a specific property.
  • Example: Serializable indicates that a class can be serialized.
  • Advantages of Marker Interfaces

Type Definition:

  • Marker interfaces define a type that instances of a class implement.
  • Allows error detection at compile time.

Example:

public class MyClass implements Serializable {
    // Implementação
}

Compile Time Check:

  • The use of marker interfaces allows errors to be detected during compilation.

Example with Serializable:

ObjectOutputStream.writeObject(myObject); // Garante que myObject seja Serializable

Marking Accuracy:

  • Marker interfaces can be precisely applied to specific subtypes.

Example:

public interface MyMarkerInterface extends MySpecificInterface {
    // Sem métodos adicionais
}

Examples of Usage

Serializable:

  • Indicates that a class can be serialized.

Example:

public class Person implements Serializable {
    private String name;
    private int age;
    // Getters, setters, etc.
}

Set Interface as Restricted Marker:

  • Applies only to subtypes of Collection, but does not add methods other than those defined by Collection.
  • Refines method contracts such as add, equals and hashCode.
  • Comparison with Bullet Notes

Marker Notes:

  • Can be applied to more program elements (classes, interfaces, methods, etc.).

Example:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyMarkerAnnotation {
}

@MyMarkerAnnotation
public class MyClass {
    // Implementação
}

When to use Marker Interface vs. Marker Annotation:

  • Use marker interface if:
  • The markup must define a type.
  • You may want to write methods that only accept objects with that markup.

Use marker annotation if:

  • The tag applies to elements other than classes or interfaces.
  • It is part of a framework that heavily uses annotations.

Advantages of Bullet Notes

  • Consistency in Frameworks:
  • Facilitates consistency in annotation-based frameworks.

Example:

@MyFrameworkAnnotation
public class MyClass {
    // Implementação
}

Usage Decision
To define a type:

  • Use a marker interface.
  • To mark elements that are not classes or interfaces:
  • Use a bullet note.

Part of an annotation-based framework:
Use a bullet note.

Final Example

  • Marker Interface:
public interface MyTypeMarker {
}

public class MyClass implements MyTypeMarker {
    // Implementação
}

public void process(MyTypeMarker obj) {
    // Processa apenas objetos marcados com MyTypeMarker
}

Bookmark Note:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotationMarker {
}

@MyAnnotationMarker
public class MyClass {
    // Implementação
}

Summary

  • Marker interfaces are used to define types without additional methods.
  • Marker annotations are used to broadly mark program elements.
  • The choice depends on the context and objective of the marking.

Complement
Marker interfaces:
Image description

The above is the detailed content of Item Use marker interfaces to define types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:CollectionsNext article:Collections