- prototype
- 原型模式
- 如何使用
prototype
prototype 用的是原型模式, 它会被框架启动时会被自动初始化.
原型模式
获取 score为prototype类型的bean每次都是克隆初始化的bean好的。
clone 一个对象 比 重新new一个对象更快, 因为它是拷贝操作。
在 swoft 中 DB 的collection 就是用的prototype类型
如何使用
你可以定义一个 new 方法,替代new关键字
比如Db使用的Collection Prototype :
实体都是
prototype,类型的bean,所有实体都可以使用new方法。
<?php declare(strict_types=1);namespace Swoft\Test;use Swoft\Bean\Annotation\Mapping\Bean;use Swoft\Bean\Concern\PrototypeTrait;/*** Class TestCollection** @Bean(scope=Bean::PROTOTYPE)** @since 2.0*/class TestCollection{use PrototypeTrait;/*** @var array*/private $items;/*** Create a new collection.** @param array $items** @return static*/public static function new(array $items = []): self{$self = self::__instance();$self->items = $items;return $self;}}
需要引入PrototypeTrait,在PrototypeTrait 中实现了 __instance()方法,该返回的就是一个 clone 的自身对象,你只需更新参数 即可获取一个全新的对象。
