Ad

Design For A Class Asking Several Classes To Perform The Same Task

I have a Laravel App and I'm creating a class that imports data. The data is being stored in about 15 different models, so I have a Class to handle each as each has its own implementation. Furthermore, more models may be added in the future.

I'd like to have a line of code like $importer->import() in an Importer class, which then takes all 15 of the implementation classes, and calls their import() methods.

However, the generic $importer->import() method will then look like:

public function __construct(ImporterOne $one, ImporterTwo $two, ImporterThree $three, etc..)
{
   $this->importerOne = $importerOne;
   $this->importerTwo = $importerTwo;
   // etc ...
   $this->importerFifteen = $importerFifteen;
}

public function import()
{
    $this->importerOne->import();
    $this->importerTwo->importer();
    // etc...
}

This doesn't seem very good because this class will then have 15+ dependences all essentially doing the same thing. And then to update this, I'll need to go in here and Add / Remove dependencies and import() method calls.

So my solution is to delegate the the 'registration' of importers to each implementation, rather than leaving the Generic Importer class responsible. Effectively, an Observer of sorts. But instead of the client attaching the Observer to the Subject, each implementation attaches itself to the Subject.

use Importer as GenericImporter

public class importerOne {

   public function __construct(SomeModel $model, GenericImporter $importer)
   {
      $this->importer = $importer;
      $this->importer->attach($this);
   }
}

// Do the same for each implementation

// and then the generic importer

public class Importer {

   protected $importables = [];

   public function attach($import_implementation)
   {
      array_push($importables, $import_implementation); 
   }

   public function import()
   {
      foreach($this->importables as $importable)
      {
          $importable->import();
      }
   }

}

This seems nice and SOLID. However, the problem is that each implementation is now using their OWN instance of the GenericImporter. So whats the best way to go about this? Do I implement the Generic Importer as a Singleton? Also, for my research purposes, does this fall into a certain design pattern? It seems similar to an ObservorPattern except that each Observer is registering itself.

Ad

Answer

You say that:

the problem is that each implementation is now using their OWN instance of the GenericImporter

I don't think it would be a problem because GenericImporter is only a dependency you are injecting in the implementations. Furthermore, the GenericImporter you're passing around is always the same object, as it will be passed by 'reference'

EDIT

Regarding the comment below, when you say:

when a dependency is resolved through the IoC Container, it isn't passing by reference. It's instantiating a new instance of that Class

It depends on how you do the binding in the ioC Container: if you use instance(), to bind the Importer in this way:

$importer = new Importer();
$this->app->instance( Importer::class, $importer );

then the same $importer instance will be resolved out of the ioC container when an Importer dependency is requested in your app.

END EDIT

Anyway, i would improve the design by adding an interface; something like this:

//INTERFACE FOR IMPORTABLES
interface Importable
{
    public function __construct( Model $model ); 
    public function import();
}

//IMPLEMENTATION OF IMPORTABLES
class importerOne implements Importable 
{   
   public function __construct( SomeModel $model )
   {
   }

   public function import()
   {
       //logic
   }
}

//THE IMPORTER CLASS
public class Importer 
{    
   protected $importables = [];

   //attach any Importable
   public function attach( Importable $import_implementation )
   {
      array_push( $this->importables, $import_implementation) ; 
   }

   //trigger Importable::import
   public function import()
   {
      foreach($this->importables as $importable)
      {
          $importable->import();
      }
   }   
}

If for some specific reason you don't want to pass the dependency of Importer to your importables, why don't you attach the importables from the client? Something like this:

$importer = new Importer();

$importer->attach( $importable_1 );
$importer->attach( $importable_2 );
//$importer->attach( $importable_n );

$importer->import();

This way the importers don't need that you pass them the Importer dependency

Depending on how you have your importables built, you could also consider building and storing all of them in an array and pass the array to the Importer:

$importer->attachAll( $importables_array );
Ad
source: stackoverflow.com
Ad