您的位置:58编程 > PHP 享元模式(Flyweight)

PHP 享元模式(Flyweight)

2023-05-31 15:33 PHP设计模式

 PHP 享元模式(Flyweight)

目的

为了最大限度地减少内存使用,享元与相似对象共享尽可能多的内存。当使用大量状态差异不大的对象时需要它。一种常见的做法是将状态保存在外部数据结构中,并在需要时将它们传递给享元对象。

UML 图

替代享元UML图

代码

Text.php

<?php

declare(strict_types=1);

namespace DesignPatternsStructuralFlyweight;


interface Text
{
    public function render(string $extrinsicState): string;
}

Word.php

<?php

namespace DesignPatternsStructuralFlyweight;

class Word implements Text
{
    public function __construct(private string $name)
    {
    }

    public function render(string $extrinsicState): string
    {
        return sprintf("Word %s with font %s", $this->name, $extrinsicState);
    }
}

Character.php

<?php

declare(strict_types=1);

namespace DesignPatternsStructuralFlyweight;


class Character implements Text
{
    
    public function __construct(private string $name)
    {
    }

    public function render(string $extrinsicState): string
    {
         // Clients supply the context-dependent information that the flyweight needs to draw itself
         // For flyweights representing characters, extrinsic state usually contains e.g. the font.

        return sprintf("Character %s with font %s", $this->name, $extrinsicState);
    }
}

TextFactory.php

<?php

declare(strict_types=1);

namespace DesignPatternsStructuralFlyweight;

use Countable;


class TextFactory implements Countable
{
    
    private array $charPool = [];

    public function get(string $name): Text
    {
        if (!isset($this->charPool[$name])) {
            $this->charPool[$name] = $this->create($name);
        }

        return $this->charPool[$name];
    }

    private function create(string $name): Text
    {
        if (strlen($name) == 1) {
            return new Character($name);
        } else {
            return new Word($name);
        }
    }

    public function count(): int
    {
        return count($this->charPool);
    }
}

测试

Tests/FlyweightTest.php

<?php

declare(strict_types=1);

namespace DesignPatternsStructuralFlyweightTests;

use DesignPatternsStructuralFlyweightTextFactory;
use PHPUnitFrameworkTestCase;

class FlyweightTest extends TestCase
{
    private array $characters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];

    private array $fonts = ["Arial", "Times New Roman", "Verdana", "Helvetica"];

    public function testFlyweight()
    {
        $factory = new TextFactory();

        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);
                }
            }
        }

        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) + count($this->fonts), $factory);
    }
}



阅读全文
以上是58编程为你收集整理的 PHP 享元模式(Flyweight)全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 58编程 58biancheng.com 版权所有 联系我们
桂ICP备12005667号-32 Powered by CMS