java - How to write an ImmutableMap that follows the Liskov Subsitution and other SOLID principles without code smells? -
i answered question regaurding immutablemap
. suggested using proxy pattern.
the problem map
contains put
method, throw unsupportedoperationexception
. replacing other instances of map
immutablemap
break liskov subsitution principle. not that, need declare put
, putall
[violates interface segregation principle]
technically, there no way replace map
instance immutablemap
, since map
interface. question is:
would creating immutablemap
using map
interface considered breaking lsp, since map
contains put
, putall
method? not implementing map
considered "alternative classes different interfaces" code smell? how 1 create immutablemap
abides lsp yet doesn't contain code smells?
in view, immutablemap
should implement map
. bad idea not implement map
there many methods accept map
argument , use in read-only sense. don't believe violate liskov subsitution principle because contract map
makes clear put
optional operation.
it not ideal classes implementing map
have implement put
, alternative have been have complex hierarchy of interfaces each including subset of possible optional methods. if there n
optional methods, there have 2^n
interfaces cover combinations. don't know value of n
, there non-obvious optional operations, such whether or not iterator
returned map.entryset().iterator()
supports setvalue
operation. if combined hierarchy hierarchy of interfaces , abstract classes exists (including abstractmap
, sortedmap
, navigablemap
, concurrentmap
, concurrentnavigablemap
...) have total mess.
so there no perfect answer this, in view best solution make immutablemap
implement map
, ensure every method write map
argument documents properties map
must have , exceptions thrown if wrong type of map
passed.
Comments
Post a Comment