Wzorzec prototype

<?php
$timeStart = microtime(true);

function randomDateTime() {
    $timestamp = mt_rand(1, time());
    $randomDate = date("d M Y", $timestamp);
    return new DateTime($randomDate);
}

abstract class Solder {
    protected $name;
    protected $secondName;
    protected $rank = 'szeregowy';
    protected $uniformColor;
    protected $force;
    protected $flair;
    protected $courage;
    protected $life;
    protected $dateOfBirth;

//    public function __construct(string $name, string $secondName, string $uniformColor, int $force, int $flair, int $courage, int $life, DateTime $dateOfBirth) {
//        $this->name = $name;
//        $this->secondName = $secondName;
//        $this->uniformColor = $uniformColor;
//        $this->force = $force;
//        $this->flair = $flair;
//        $this->courage = $courage;
//        $this->life = $life;
//        $this->dateOfBirth = $dateOfBirth;
//    }

    public function getName() {
        return $this->name;
    }

    public function getSecondName() {
        return $this->secondName;
    }

    public function getRank() {
        return $this->rank;
    }

    public function getUniformColor() {
        return $this->uniformColor;
    }

    public function getForce() {
        return $this->force;
    }

    public function getFlair() {
        return $this->flair;
    }

    public function getCourage() {
        return $this->courage;
    }

    public function getLife() {
        return $this->life;
    }

    public function getDateOfBirth() {
        return $this->dateOfBirth;
    }

    public function setName($name) {
        $this->name = $name;
        return $this;
    }

    public function setSecondName($secondName) {
        $this->secondName = $secondName;
        return $this;
    }

    public function setRank($rank) {
        $this->rank = $rank;
        return $this;
    }

    public function setUniformColor($uniformColor) {
        $this->uniformColor = $uniformColor;
        return $this;
    }

    public function setForce($force) {
        $this->force = $force;
        return $this;
    }

    public function setFlair($flair) {
        $this->flair = $flair;
        return $this;
    }

    public function setCourage($courage) {
        $this->courage = $courage;
        return $this;
    }

    public function setLife($life) {
        $this->life = $life;
        return $this;
    }

    public function setDateOfBirth($dateOfBirth) {
        $this->dateOfBirth = $dateOfBirth;
        return $this;
    }

}

class StrongSolder extends Solder {
    protected $rank = 'general';
}

class CleverSolder extends Solder {
    
}

$strongSolder = (new StrongSolder())->setName('new janusz')->setSecondName('kowalsi')->setUniformColor('blul')->setForce(90)->setFlair(80)->setCourage(70)->setLife(49)->setDateOfBirth(randomDateTime());
$cleverSolder = (new CleverSolder())->setName('new jan')->setSecondName('kowalsi')->setUniformColor('blul')->setForce(90)->setFlair(80)->setCourage(70)->setLife(49)->setDateOfBirth(randomDateTime());

$strongSolders = [];
$cleversSolders = [];

for ($i = 0; $i < 10000; $i++) {
    $strongSolders[] = (new StrongSolder())
            ->setName('new janusz' . $i)
            ->setSecondName('kowalsi' . $i)
            ->setUniformColor('blul')
            ->setForce(90)
            ->setFlair(80)
            ->setCourage(70)
            ->setLife(49)
            ->setDateOfBirth(randomDateTime());

    $cleversSolders[] = (new CleverSolder())
            ->setName('new jan' . $i)
            ->setSecondName('kowalsi' . $i)
            ->setUniformColor('blul')
            ->setForce(90)
            ->setFlair(80)
            ->setCourage(70)
            ->setLife(49)
            ->setDateOfBirth(randomDateTime());
}

$timeEnd = microtime(true);
$executionTime = ($timeEnd - $timeStart);

echo '<h3><b>czas normalne tworzenie obiekto</b> ' . round($executionTime * 1000) . ' milliseconds</h3>';
echo '<h3><b>czas normalne tworzenie obiektow</b> ' . $executionTime . ' mikro</h3>';
echo '<h3><b>czas normalne tworzenie obiektow</b> ' . $executionTime/60 . ' sec</h3>';

$timeStart = microtime(true);

for ($i = 0; $i < 10000; $i++) {
    $strongSolders[] = clone $strongSolder
                    ->setName('new janusz' . $i)
                    ->setSecondName('kowalsi' . $i)
                    ->setUniformColor('blul')
                    ->setForce(90)
                    ->setFlair(80)
                    ->setCourage(70)
                    ->setLife(49)
                    ->setDateOfBirth(randomDateTime())
    ;
    $cleversSolders[] = clone $cleverSolder
                    ->setName('new jan' . $i)
                    ->setSecondName('kowalsi' . $i)
                    ->setUniformColor('blul')
                    ->setForce(90)
                    ->setFlair(80)
                    ->setCourage(70)
                    ->setLife(49)
                    ->setDateOfBirth(randomDateTime())
    ;
}
//printMemory();
//
//function printMemory() {
//    $mem_usage = memory_get_usage();
//    $mem_peak = memory_get_peak_usage();
//    echo 'The script is now using: <strong>' . round($mem_usage / 1024) . 'KB</strong> of memory.<br>';
//    echo 'Peak usage: <strong>' . round($mem_peak / 1024) . 'KB</strong> of memory.<br><br>';
//}
echo '<br><br>';
$timeEnd = microtime(true);
$executionTime = ($timeEnd - $timeStart);

echo '<h3><b>czas klonowanie</b> ' . round($executionTime * 1000) . ' milliseconds</h3>';
echo '<h3><b>czas klonowanie</b> ' . $executionTime . ' mikro</h3>';
echo '<h3><b>czas klonowanie</b> ' . $executionTime/60 . ' sec</h3>';
Komentarze wyłączone