3 ms·
This would be overengineering, but the following six types would solve the problem: * ImmutableSquare * ImmutableRectangle * MutableSquare * MutableRectangl
by Viliam1234 9y ago
This would be overengineering, but the following six types would solve the problem:
* ImmutableSquare
* ImmutableRectangle
* MutableSquare
* MutableRectangle
* Square
* Rectangle
The "Mutable..." classes are mutable, the "Immutable..." classes are immutable, and the "Square" and "Rectangle" classes mean that you can read the values, but there is no guarantee about either mutability or immutability.
In this system, "ImmutableSquare" and "MutableSquare" are subtypes of "Square"; "ImmutableRectangle" and "MutableRectangle" are subtypes of "Rectangle"; and also "ImmutableSquare" is a subtype of "ImmutableRectangle" (and therefore "Rectangle").
But if you go this way, don't be surprised when you end up with millions of classes.
- ken 9y agoIn some languages, there are also (non/)threadsafe variants of mutable classes. As you say, this approach blows up quickly. If I saw that, I would ask what 'problem' it's trying to solve. Is mutation actually the goal, or simply the means? In languages which don't support general mutation (I'm writing Clojure right now), I really don't miss it at all.