adam bien's blog

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

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 was completely refactored. The Marker Interface pattern was used to enhance a class with additional type to change its behavior or introduce some priviliged actions. A prominent example 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 of implementing an interface is actually no more appropriate. The usage of annotations @Serializable or @Remote would be much more elegant. There is an example in JSR-181, where an endpoint has only to be annotated with @WebServce annotation - no additional interface realization 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 has no more to check the type (e.g. with instanceof), but has to check the existence of the annotaition via reflection (class#getAnnotation) instead.

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