Consider a rectangle with sides X and Y, and area Z such that Z=X*Y.
The rectangle has to be modeled into the cleanest OOD which will supply the following two services,
- Given side X and side Y, calculate area Z.
- Given side X and area Z, calculate side Y.
One possible design I was thinking on is,
A class Rectangle that have private invariant X and Y, public instance method Z = calcArea(), operating on X and Y, and public instance method Y = calcSide(Z), operating on X and on argument Z.
Good practice OOD tell us that Rectangle should have a constructor which initialize X and Y, with some valid values, both at once.
However, this solution implies that to calculate Y, given X and Z, one need to initialize the class constructor with Null value for Y. Initializing a invariant with Null is a dirty programming.
Is there any clean, one class, OOD solution?