I asked a question on StackOverflow on how to properly implement an ImmutableMap
that abides by the SOLID principles.
Due to Java's Map
interface containing put
and putAll
, it violates the interface segregation princple by forcing us to implement the put
and putAll
methods for an ImmutableMap
. If we were to define our own interface, it would fall under the code smell category "Alternative Classes with Different Interfaces".
This got me thinking on how would one properly declare an interface that abides by the Liskov Subsitution principle, while still abiding by the interface segregation principle.
The only way I can think of achieving this would to declare the base interface with only accessors, then supply a subinterface with mutators:
interface Map {
//accessor methods
}
interface MutableMap extends Map {
//mutator methods
}
class HashMap implements MutableMap {
}
class ImmutableMap implements Map {
}
Is this considered "good practice"? It seems to be the only way to implement immutability while still abiding by the SOLID principles, but I've never heard of such a practice.