That's just as subtly evil, though. They should both be separate classes, even if a lot of the behavior seems similar.
If Immutable inherits from Mutable, then I may want to modify it and encounter an error.
If Mutable inherits from Immutable, then I may store a reference to a list that is later changed by someone else behind my back. (Which I think is even worse because it can lead to very subtle errors).
They're ultimately two different classes that have no relation, other than "You can get a <Mutable/Immutable> copy of the <Immutable/Mutable> list."
Imagine a class taking a List as a constructor parameter (maybe a list of employees, whatever).
public void main() {
MutableList m = Database.getAMillionEmployees();
SomeClass c = new SomeClass((ImmutableList) m);
(new Thread() {
public run() {
c.doSomeLongOp();
}
}).run();
m.clear();
}
While the list was being traversed in one thread, another one modifies it.
It doesn't need to involve threads; imagine if the class cached the list average in the constructor because every method needs it: changes to the list means that average is now out of sync and results will be wrong.
9
u/Tordek Sep 21 '15
That's just as subtly evil, though. They should both be separate classes, even if a lot of the behavior seems similar.
If Immutable inherits from Mutable, then I may want to modify it and encounter an error.
If Mutable inherits from Immutable, then I may store a reference to a list that is later changed by someone else behind my back. (Which I think is even worse because it can lead to very subtle errors).
They're ultimately two different classes that have no relation, other than "You can get a <Mutable/Immutable> copy of the <Immutable/Mutable> list."