Another important concept is rent payment in object Monopoly. Let's first write a test instance (test bootstrap development). The following code hopes to achieve the established goals.
function TestRent() {
$game = new Monopoly;
$player1 = new Player(‘Madeline');
$player2 = new Player(‘Caleb');
$this->assertEqual(1500, $player1->getBalance());
$this->assertEqual(1500, $player2->getBalance());
$game->payRent($player1, $player2, new Dollar(26));
$this->assertEqual(1474, $player1->getBalance());
$this->assertEqual(1526, $player2->getBalance());
}
According to this test code, we need to add a payRent() method function to the Monopoly object to implement a Player object to pay rent to another Player object.
Class Monopoly {
// ...
/**
* pay rent from one player to another
* @param Player $from the player paying rent
* @param Player $to the player collecting rent
* @param Dollar $rent the amount of the rent
* @return void
*/
public function payRent($from, $to, $rent) {
$to->collect($from->pay($rent));
}
}
The payRent() method function implements the rent payment between two player objects ($from and $to). The method function Player::collect() has been defined, but Player::pay() must be added in order for instance $from to pay a certain Dollar amount through the pay() method. First we define Player::pay() as:
class Player {
// ...
public function pay($amount) {
$this->savings = $this->savings->add(-1 * $amount);
}
}
However, we found that in PHP you cannot multiply a number by an object (unlike other languages, PHP does not allow overloading operators so that the constructor performs operations). Therefore, we implement the deduction operation of Dollar object by adding a debit() method function.
class Dollar {
protected $amount;
public function __construct($amount=0) {
$this->amount = (float)$amount;
}
public function getAmount() {
return $this->amount;
}
public function add($dollar) {
return new Dollar($this->amount + $dollar->getAmount());
}
public function debit($dollar) {
return new Dollar($this->amount - $dollar->getAmount());
}
}
After introducing Dollar::debit(), the operation of the Player::pay() function is still very simple.
class Player {
// ...
/**
* make a payment
* @param Dollar $amount the amount to pay
* @return Dollar the amount payed
*/
public function pay($amount) {
$this->savings = $this->savings->debit($amount);
return $amount;
}
}
The Player::pay() method returns the $amount object of the payment amount, so there is no problem in the usage of the statement $to->collect($from->pay($rent)) in Monopoly::payRent(). If you do this, if you add new "business logic" in the future to limit a player's inability to pay more than his existing balance. (In this case, the same value as the account balance of the player will be returned. At the same time, a "bankruptcy exception handling" can also be called to calculate the insufficient amount and perform related processing. Object $to still obtains the amount $from can give from object $from.)
Note: Terms------Business Logic
The "business logic" mentioned in the example of a gaming platform seems incomprehensible. The business here does not mean the business operation of a normal company, but rather the concepts required by special application fields. Please recognize it as "a direct task or goal", rather than "a commercial operation that exists here".
So, since we are currently discussing a Monopoly, the meaning of "business logic" here is based on the rules of a game.
Previous page12345Next pageRead the full text