微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-哪个类应基于重要性级别取决于哪个类

我有一个有关OOD,OOP和建模的一般性问题,我不确定如何提出.最简单的方法是举个例子.我通常使用PHP,但是可以使用任何其他语言.假设我是一家银行,并且想开发一个处理提款的程序.因此,我将进行2类取款和帐户.现在最好有退出功能.
我的意思是:

$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

要么

$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

已知的一件事,比提款更“重要”,没有提款就可以“生存”.可能还会有存款或其他操作.

可能还有许多其他方法可以执行此操作.您认为哪种方式最好?

我将尝试给出一个简单的大学网站示例,该示例需要允许学生报名参加课程.

那么,当用户单击“注册”按钮时,您会选择哪一个?这个:

$student = new Student('John Smith');
$course = new Course('Math');
$student->enrollToCourse($course);

或这个:

$student = new Student('John Smith');
$course = new Course('Math');
$course->addStudent($student);

或第三个选择:

$student = new Student('John Smith');
$course = new Course('Math');
EnrollmentService::enrollStudentToCourse($student,$course);

也许所有选择都同样可行?

解决方法:

更可能是

$withdrawal = $account->withdraw(50, 'USD');
$withdrawal->completeTransaction();

要么

$transfer = $account->transfer(50, 'USD', $transferToAccount);
$transfer->completeTransaction();

帐户操作应导致交易..交易应知道如何保持自身,如果所有更新未成功,则应回滚

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐