dongshi1102 2015-12-18 19:50
浏览 92
已采纳

设计类要求几个类执行相同的任务

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.

  • 写回答

2条回答 默认 最新

  • dsms21398 2015-12-20 11:32
    关注

    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 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序