lundi 11 avril 2016

DI design challange with class which creates instances

I want to use Dependency Injection (DI) for one of my projects. I have written a basic DI lib which basically works as follows

let di = new DI();
di.register('screen', Screen);   // contract name, class ref
di.register('pixels', Pixels);

And you can create instances as follows:

let pixels = di.getInstance('pixels');

or with one addition parameter

let pixeks = di.getInstance('pixels', [arrayOfPixels]);

The problem I have now is with the Screen class:

export default class Screen() {
   getPixels() {
       // Get pixels from a canvas
       return new Pixels(pixels);
   }
}

the Screen class creates instances of Pixels. This is not in line with DI, so the question is how can I solve this in a correct way. If there is a design pattern that might help, please let me know!

Aucun commentaire:

Enregistrer un commentaire