mardi 26 mai 2015

Which class should depend on which class based on level of importance

I have a general question relating OOD, OOP, and Modeling and I am not sure how to ask it. The easiest way is with example. I am generaly using PHP but it can be in any other language. Let say I am a bank and I want to make a program that handles withdrawals. So I will be making 2 class Withdrawal and Account. now what is better to have the function that makes the withdrawal. I mean:

$account = getAccountById(1); //account->balance = 200.00
$withdrawal = new Withdrawal(50,'USD');
$withdrawal->setAccount($account); // $withdrawal->account_id=1
$withdrawal->make(); //SQL changes account where id=1 and set balance to 150
                     //Also save a row in withdrawal tables with withdraw details

or

$account = getAccountById(1); //account->balance = 200.00
$withdrawal = new Withdrawal(50,'USD');
$account->processesWithdraw($withdrawal); //SQL changes account where id=1 and set balance to 150
                                          //Also save a row in withdrawal tables with withdraw
                                          //$withdrawal->account_id=1

One thing known an account is more "important" than withdrawal and can "live" without it. There may also be deposits or other actions.

There are probably many other ways to do this action. Which way do you think is the best?

Aucun commentaire:

Enregistrer un commentaire