<?php
declare(strict_types=1);

/**
 * 单例模式:使用克隆来复制实例化对象,新对象是通过复制原型对象实现的
 * 应用场景:创建某个原型对象的多个实例
 * 优点:通过克隆减少实例化的开销
 */
interface Prototype
{
    public function copy();
}


class ConcretePrototype implements Prototype
{

    private $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name)
    {
        $this->name = $name;
    }


    public function copy()
    {
        return  clone $this;
    }
}


class Client
{
    public function test()
    {
        $pro = new ConcretePrototype('test');
        echo $pro->getName(); // test
        $clonePro = $pro->copy();
        echo $clonePro->getName(); // test
    }
}

$client = new Client();
$client->test();