我有一个关于OOD,OOP和Modeling的一般性问题,我不知道如何提问。最简单的方法就是举例。我通常使用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);
也许所有选项都同样可行?
答案 0 :(得分:2)
更有可能是
$withdrawal = $account->withdraw(50, 'USD');
$withdrawal->completeTransaction();
或
$transfer = $account->transfer(50, 'USD', $transferToAccount);
$transfer->completeTransaction();
帐户操作应该导致交易..交易应该知道如何保持自己,或者如果所有更新都不成功则回滚
答案 1 :(得分:2)
$account = new Account(1);
$withdrawal = new Withdrawal($account);
$withdrawal ->setAmmount(50);
$withdrawal ->setCurrency('USD');
$withdrawal -> makeTransaction();
或
<select class="dropdown-select" onChange="window.open('https://www.google.com');">
<option value="uno">Uno</option>
<option value="dos">Dos</option>
<option value="tres">Tres</option>
</select>
<input type="button" onClick="window.open('https://www.google.com');" value="click me">
我知道这很长,但这种方法可以帮助你遵循“单一责任Principe”