Exemplo n.º 1
0
    {
        parent::__construct("assassin");
    }
    public function fight($target)
    {
        print "* creeps behind " . $target . " and stabs at it *" . PHP_EOL;
    }
}
class Llama
{
    public function fight($target)
    {
        print "* spits at " . $target . " *" . PHP_EOL;
    }
}
$uf = new UnholyFactory();
$uf->absorb(new Footsoldier());
$uf->absorb(new Footsoldier());
$uf->absorb(new Archer());
$uf->absorb(new Assassin());
$uf->absorb(new Llama());
$requested_fighters = array("foot soldier", "llama", "foot soldier", "archer", "foot soldier", "assassin", "foot soldier", "archer");
$actual_fighters = array();
foreach ($requested_fighters as $rf) {
    $f = $uf->fabricate($rf);
    if ($f != null) {
        array_push($actual_fighters, $f);
    }
}
$targets = array("the Hound", "Tyrion", "Podrick");
foreach ($actual_fighters as $f) {
Exemplo n.º 2
0
<?php

include_once 'UnholyFactory.class.php';
include_once 'Fighter.class.php';
class CrippledSoldier extends Fighter
{
    public function __construct()
    {
        parent::__construct("crippled soldier");
    }
}
$uf = new UnholyFactory();
$uf->absorb(new CrippledSoldier());
$requested_fighters = array("crippled soldier", "crippled soldier", "crippled soldier", "crippled soldier");
$actual_fighters = array();
foreach ($requested_fighters as $rf) {
    $f = $uf->fabricate($rf);
    if ($f != null) {
        array_push($actual_fighters, $f);
    }
}
$targets = array("the Hound", "Tyrion", "Podrick");
foreach ($actual_fighters as $f) {
    foreach ($targets as $t) {
        $f->fight($t);
    }
}