Over a million developers have joined DZone.

Silent Death Of The Classic Marker Interface Pattern: Is java.io.Serializable Legacy?

DZone's Guide to

Silent Death Of The Classic Marker Interface Pattern: Is java.io.Serializable Legacy?

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

I was in the process of thinking about @Stateless and JPA and Serializable (needed some examples for my book...) and noticed that actually one of the GoF patterns siltently died. Or at least its realization/implementation has been completely refactored. The Marker Interface pattern was used to enhance a class with an additional type to change its behavior or introduce some priviliged actions. Prominent examples are java.io.Serializable and java.rmi.Remote interfaces. Both were introduced because of security reasons. Only classes which implement those interfaces are able to be flushed to disc or transfered over the network.

With the advent of annotations, the approach whereby you implement an interface is actually no longer appropriate. The usage of annotations @Serializable or @Remote is much more elegant. There is an example of this in JSR-181, where an endpoint has only to be annotated with the @WebServce annotation. No additional interface realization/implementation is necessary.

Using an annotation is not the same as implementing an interface, because the type of the class is not going to be extended. Nonetheless, the same effect can be achieved. So, the infrastructure no longer needs to check the type (e.g., with instanceof), but has to check the existence of the annotation via reflection (class#getAnnotation or isAnnotationPresent) instead.

So java.io.Serializable and java.rmi.Remote are actually legacy now!

From http://www.adam-bien.com/roller/abien/

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}