Page 44      All Pages  All Books
44 The Value Object Pattern
* @return void
*/ public function passGo($player) {
$player->collect($this->go_amount); }
}
So far, the Monopoly class is very minimal. The constructor creates $go_amount, an instance of the Dollar Value Object class, set to $200. $go_amount is used by passGo(), which takes a Player as an argument and tells the Player to collect() $200.
Player should be next. The Monopoly class calls a Player::collect() method with one argu-ment, a Dollar, to add that Dollar amount to the player’s cash balance. In addition to that method, let’s add the method Player::getBalance() to access a player’s cash reserve current to validate that the Player and Monopoly objects are working,
class Player { protected $name; protected $savings; /**
constructor
* set name and initial balance
* @param string $name the players name
@return void */
public function __construct($name) {
$this->name = $name;
$this->savings = new Dollar(1500); } /**
* receive a payment
* @param Dollar $amount the amount received
@return void */
public function collect($amount) {
$this->savings = $this->savings->add($amount); }
* return player balance
* @return float */
public function getBalance() {
return $this->savings->getAmount(); }
}

Page 44      All Pages  All Books