gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:59:54 25 4
gpt4 key购买 nike

我有一个关于 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

众所周知,帐户比取款更“重要”,没有它也可以“生存”。也可能有存款或其他 Action 。

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

我将尝试给出一个更简单的大学网站示例,该网站需要允许学生注册类(class)。

那么当用户点击注册按钮时,你会选择哪个?这:

$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();

账户操作应该导致交易..交易应该知道如何坚持自己,或者如果所有更新都不成功则回滚

关于php - 哪个类应该取决于哪个类基于重要性级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30465355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com