Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Design a Rectangle Class?
    text
    copied!<p>Consider a rectangle with sides X and Y, and area Z such that Z=X*Y.</p> <p>The rectangle has to be modeled into the cleanest OOD which will supply the following two services:</p> <ol> <li>Given side X and side Y, calculate area Z.</li> <li>Given side X and area Z, calculate side Y.</li> </ol> <p>One possible design I was thinking on is:</p> <p>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.</p> <p>Good practice OOD tell us that Rectangle should have a constructor which initialize X and Y, with some valid values, both at once.</p> <p>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. </p> <p>Is there any clean, one class, OOD solution?</p> <hr> <p>Thanks to you all for the helpful and insightful answers! This is not homework. Actually, this is a simplified representation meant to demonstrate real world financial problems I'm dealing with.</p> <p>For example, Black&amp;Sholts option pricing model derive the price out of a given 5 financial parameters. One of those 5 is volatility. There are times when you know the price and want to extract the implied volatility using the same computational core. Furthermore I have found the need for inverse calculation using the same computational core, to be a repetitive useful pattern. For example, you can extract bond price knowing the interest rate, but at times you need to extract the implied interest rate knowing the bond price. Often, those models run inside of an iterations loop. Insistence of using instance function, and not static function, has a performance virtue – by minimizing the function arguments, applicaion has less precondition to checks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload