实体-属性-值 (EAV) 模式以使用 PHP 实现 EAV 模型。
Entity-attribute-value (EAV) 模型是一种描述实体的数据模型,其中可用于描述它们的属性(属性、参数)的数量可能很大,但实际应用于给定实体的数量是比较适中。
Entity.php
<?php declare(strict_types=1); namespace DesignPatternsMoreEAV; use SplObjectStorage; class Entity implements Stringable { private $values; public function __construct(private string $name, $values) { $this->values = new SplObjectStorage(); foreach ($values as $value) { $this->values->attach($value); } } public function __toString(): string { $text = [$this->name]; foreach ($this->values as $value) { $text[] = (string) $value; } return join(", ", $text); } }
Attribute.php
<?php declare(strict_types=1); namespace DesignPatternsMoreEAV; use SplObjectStorage; class Attribute implements Stringable { private SplObjectStorage $values; public function __construct(private string $name) { $this->values = new SplObjectStorage(); } public function addValue(Value $value) { $this->values->attach($value); } public function getValues(): SplObjectStorage { return $this->values; } public function __toString(): string { return $this->name; } }
Value.php
<?php declare(strict_types=1); namespace DesignPatternsMoreEAV; class Value implements Stringable { public function __construct(private Attribute $attribute, private string $name) { $attribute->addValue($this); } public function __toString(): string { return sprintf("%s: %s", (string) $this->attribute, $this->name); } }
Tests/EAVTest.php
<?php declare(strict_types=1); namespace DesignPatternsMoreEAVTests; use DesignPatternsMoreEAVAttribute; use DesignPatternsMoreEAVEntity; use DesignPatternsMoreEAVValue; use PHPUnitFrameworkTestCase; class EAVTest extends TestCase { public function testCanAddAttributeToEntity() { $colorAttribute = new Attribute("color"); $colorSilver = new Value($colorAttribute, "silver"); $colorBlack = new Value($colorAttribute, "black"); $memoryAttribute = new Attribute("memory"); $memory8Gb = new Value($memoryAttribute, "8GB"); $entity = new Entity("MacBook Pro", [$colorSilver, $colorBlack, $memory8Gb]); $this->assertEquals("MacBook Pro, color: silver, color: black, memory: 8GB", (string) $entity); } }
顾名思义,责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的...
软件项目管理中可以分成两部分:软件创新软件项目管理项目是定义明确的任务,这是为了实现某个目标(例如,软件开发和交付)进行...
OceanBase Connector/J 通过 API 接口调用相关的语句缓存功能,包括启用和禁用,以及可调用语句的数量和设置 SQL 的最大缓存数量...