} //Создаем армию $main_army = new Army(); //Добавим пару боевых единиц $main_army->addUnit(new Archer()); $main_army->addUnit(new LaserCannonUnit()); //Создаем армию $sub_army = new Army(); $sub_army->addUnit(new Archer()); $sub_army->addUnit(new Archer()); $sub_army->addUnit(new Archer()); $sub_army2 = new Army(); $sub_army2->addUnit(new Sapper()); $sub_army3 = new TroopCarrier(); $sub_army3->addUnit(new Sapper()); $sub_army3->addUnit(new Sapper()); $sub_army3->addUnit(new Archer()); $sub_army2->addUnit(UnitScript::joinExisting(new Sapper(), new TroopCarrier())); //Собираем армии в одну $main_army->addUnit($sub_army); $main_army->addUnit($sub_army2); $main_army->addUnit($sub_army3); $textdump = new TextDumpArmyVisitor(); $main_army->accept($textdump); print $textdump->getText(); print "<br />"; $taxcollector = new TaxCollectionVisitor(); $main_army->accept($taxcollector); print $taxcollector->getReport() . "<br />"; print "Итого: "; print $taxcollector->getTax() . "<br />";
if (in_array($unit, $this->units, true)) { return; } $this->units[] = $unit; } function removeUnit(Unit $unit) { $this->units = array_udiff($this->units, [$unit], function ($a, $b) { return $a === $b ? 0 : 1; }); } function bombardStrength() { $ret = 0; foreach ($this->units as $unit) { $ret += $unit->bombardStrength(); } return $ret; } } $main_army = new Army(); $main_army->addUnit(new Archer()); $main_army->addUnit(new LaserCannonUnit()); $main_army->addUnit(new Cavalry()); $taxCollector = new TaxCollectionVisitor(); $main_army->accept($taxCollector); echo $taxCollector->getReport(); echo $taxCollector->getTax(); $textDump = new TextDumpArmyVisitor(); $main_army->accept($textDump); print $textDump->getText();
<?php require_once "Visitor.php"; require_once "Army.php"; require_once "CompositeUnit.php"; require_once "Unit.php"; class UnitException extends Exception { } $main_army = new Army(); $main_army->addUnit(new Archer()); $main_army->addUnit(new LaserCannonUnit()); $main_army->addUnit(new Cavalry()); $textdump = new TextDumpArmyVisitor(); $main_army->accept($textdump); print $textdump->getText(); $main_army = new Army(); $main_army->addUnit(new Archer()); $main_army->addUnit(new LaserCannonUnit()); $main_army->addUnit(new Cavalry()); $taxcollector = new TaxCollectionVisitor(); $main_army->accept($taxcollector); print $taxcollector->getReport(); print "TOTAL: "; print $taxcollector->getTax() . "\n"; // mz: end add client code