mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 20:20:15 +02:00
improved Flyweight
This commit is contained in:
@@ -3,7 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace DesignPatterns\Structural\Flyweight\Tests;
|
||||
|
||||
use DesignPatterns\Structural\Flyweight\FlyweightFactory;
|
||||
use DesignPatterns\Structural\Flyweight\TextFactory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FlyweightTest extends TestCase
|
||||
@@ -14,20 +14,29 @@ class FlyweightTest extends TestCase
|
||||
|
||||
public function testFlyweight()
|
||||
{
|
||||
$factory = new FlyweightFactory();
|
||||
$factory = new TextFactory();
|
||||
|
||||
foreach ($this->characters as $char) {
|
||||
foreach ($this->fonts as $font) {
|
||||
$flyweight = $factory->get($char);
|
||||
$rendered = $flyweight->render($font);
|
||||
for ($i = 0; $i <= 10; $i++) {
|
||||
foreach ($this->characters as $char) {
|
||||
foreach ($this->fonts as $font) {
|
||||
$flyweight = $factory->get($char);
|
||||
$rendered = $flyweight->render($font);
|
||||
|
||||
$this->assertSame(sprintf('Character %s with font %s', $char, $font), $rendered);
|
||||
$this->assertSame(sprintf('Character %s with font %s', $char, $font), $rendered);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->fonts as $word) {
|
||||
$flyweight = $factory->get($word);
|
||||
$rendered = $flyweight->render('foobar');
|
||||
|
||||
$this->assertSame(sprintf('Word %s with font foobar', $word), $rendered);
|
||||
}
|
||||
|
||||
// Flyweight pattern ensures that instances are shared
|
||||
// instead of having hundreds of thousands of individual objects
|
||||
// there must be one instance for every char that has been reused for displaying in different fonts
|
||||
$this->assertCount(count($this->characters), $factory);
|
||||
$this->assertCount(count($this->characters) + count($this->fonts), $factory);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user