Design Patterns PHP - Proxy Java

1) What have we at the beginning?

We have object "Subject" which has method "doSomething"

class Subject
{
    public function __construct()
    {
        sleep(10000);
    }

    public function doSomething()
    {
        echo 'doing something' . PHP_EOL;
    }
}



Let's assume that creating this types of objects is heavy. In our case it's time-consuming.
We add sleep for 0.1 of second in the constructor

2) How looks the client ?

/** @var Subject[] $subjects */
$subjects = [];
for ($i = 0; $i < 500; $i++) {
    $subjects[] = new Subject();
}

//some code

for ($i = 0; $i < 10; $i++) {
    $subjects[array_rand($subjects)]->doSomething();
}



Let's imagine that we need a pool of 500 objects.

Later in some other places we use for example 10 objects.

3) What are disadvantages ?

Unfortunately if we want run this code we need to wait for 5 second before we can use this pool of objects.

4) How we can solve this?

Let's apply "Proxy" pattern