mercredi 18 mai 2022

Is there a design pattern name for this code? [closed]

Can you tell me if this code is a named design pattern ?
I looked to many GOF patterns but I can't find one looking like this one. The closest pattern I found is "strategy" but "strategy" pattern runs only one service at runtime whereas here all services are running at runtime.

<?php

interface stuffable {
  public function doStuff(string $stuff): void;
}

class serviceA implements stuffable
{
  public function doStuff(string $stuff): void
  {
    echo "I am doing some $stuff\n";
  }
}

class serviceB implements stuffable
{
  public function doStuff(string $stuff): void
  {
    echo "I am doing some other $stuff\n";
  }
}

class stuffManager
{
  private $services = [];
  
  public function addStuffableServices(stuffable $service)
  {
    $this->services[] = $service;
  }
  
  public function stuffToDo(string $stuff)
  {
    foreach ($this->services as $service) {
      $service->doStuff($stuff);
    }
  }
}

$serviceA = new serviceA();
$serviceB = new serviceB();
$stuffManager = new stuffManager();
$stuffManager->addStuffableServices($serviceA);
$stuffManager->addStuffableServices($serviceB);
$stuffManager->stuffToDo('stuff');

the expected output is :

I am doing some stuff
I am doing some other stuff

Aucun commentaire:

Enregistrer un commentaire