public function visitBook(Book $book) { $price = $book->getPrice(); // implement $5 discount if ($price > 20) { $price -= 5; } return $price; }
public function testVisitor() { $visitor = new PriceItemVisitor(); $item = new BookItem($name = "Im Westen nichts Neues, Erich Maria Remarque", $price = 22.5); $price = $item->accept($visitor); $this->assertEquals(17.5, $price); $item = new FruitItem($name = "apple", $pricePerKg = 1.5, $weight = 2); $price = $item->accept($visitor); $this->assertEquals(3.0, $price); }