Note that there are some explanatory texts on larger screens.

plurals
  1. POComplex inheritance scenario
    text
    copied!<p>Let's say you need to build an application that manages cheques. Each cheque contains data about the amount of money, the date, the payee and an additional payment date which may or may not be present. Additionally, each cheque must be related to a current account which belongs to a certain bank. Now, our application should allow cheques printing under these conditions:</p> <ul> <li><p>Each bank managed by the app has a different cheque layout (i.e. each field has a different x,y position).</p></li> <li><p>The cheque layout changes slightly if the payment date is present, even with the same related bank object. But, from bank to bank these changes may not be the same (e.g. bank A may vary position for the date field, while bank B changes position for the payee field)</p></li> </ul> <p>With these restrictions in place, it's difficult to come up with a simple inheritance schema as there is no consistent behavior to factor out accross the different types of cheques there are. One possible solution would be to avoid inheritance and create a class for every cheque - bank combination:</p> <ul> <li>class ChequeNoPaymentDateForBankA</li> <li>class ChequeWithPaymentDateForBankA</li> <li>class ChequeNoPaymentDateForBankB</li> <li>class ChequeWithPaymentDateForBankB, etc.</li> </ul> <p>Each of these classes implement the print() method which takes the fields positions from a Bank object and builds up the cheque layout. So far so good, but this approach leaves me with a strange feeling as there is no room for code reuse. I wonder if I'm misinterpreting the problem and perhaps there is a better way. As this is not a new problem domain at all, I'm sure this is a reinvent-the-wheel effort. Any insights will be kindly appreciated.</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