为了最大限度地减少内存使用,享元与相似对象共享尽可能多的内存。当使用大量状态差异不大的对象时需要它。一种常见的做法是将状态保存在外部数据结构中,并在需要时将它们传递给享元对象。
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); } }
Alpha测试是一种软件测试中所使用的释放软件真正的用户或公众之前找到的错误。它在实际场景中提供了性能验证。它是一种验收测试...
作者:MaxsuJava技术QQ群:227270512 / Linux QQ群:479429477软件测试的盒子测试方法包括黑盒测试和白盒测试。在这里讨论白盒测...
MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式。这种模式用于应用程序的分层开发。Model(模型) - 模型代表一...
1.创建React应用要使用TypeScript启动一个新的CreateReactApp项目,您可以运行:npxnpx create-react-app my-app --template typ...
您可以使用Impala的Union子句组合两个查询的结果。 语法以下是Impala中的Union子句的语法。 query1 union query2...
数据存储管理指导开发者基于 HarmonyOS 进行存储设备(包含本地存储、SD 卡、U 盘等)的数据存储管理能力的开发,包括获取存储设...
场景介绍通常在汽车使用过程中,驾驶员需要设置仪表屏亮度、时间单位等参数,将电台、音乐等娱乐数据或导航数据显示在仪表屏上,...
分布式数据服务(Distributed Data Service,DDS) 为应用程序提供不同设备间数据库数据分布式的能力。通过调用分布式数据接口,...
Attrpublic interface Attr获取属性值。Attr 接口包含支持属性值设计需要实现的方法集。 Attr 对象在 AttrSet 中用于在构造函数...