I have an interface Serializable. This class have methods for each Item subclass in my application. However not all classes that implements Serializable have a reasonable implementation of the serialize method. Some of the serializers should not be able to serialize all the different objects because of security constraints in my application.
How should I solve this? Should I serialize a message that says “Serializer can’t serialize object because of security constraints.” or should I throw an RuntimeException? Or are there other ways of “fixing” this?
You would violate one important principle with your logic: Interface Segregation Principle
So, I would create two distinct base classes to achieve your requirement:
ItemSerializableItemAnd your visitor would only rely on
SerializableItemobjects:Of course, it is possible to gather only common behaviour (unrelated to serialization so) of both classes within a kind of
AbstractItem, as long as the visitor doesn’t deal with it.