lundi 30 octobre 2017

JavaScript parameter dependent class inheritance

I have the following classes hierarchy:

  Renderer (can render some data on screen)
     |
 DbRenderer (fetch data from the database)
     |
JsonRenderer (my class. converts specific data into needed format supported by Renderer)

It works well but requires to pass DB connection parameters into JsonRenderer constructor in order to call constructor() of DbRenderer via super() since this parameter is obligatory there.

So, it looks like:

const renderer = new JsonRenderer(dbParams);

, where

class JsonRenderer extends DbRenderer {}

But, I want to make dbParams optional and change classes hierarchy to the following:

   Renderer
      |
  JsonRenderer

I can do it by creating separate class:

class JsonRenderer2 extends Renderer {}

Off course separate class will be a source of code duplication and this is not a good idea.

Is there some solution for my situation without changing of Renderer and DbRenderer parent classes?

Aucun commentaire:

Enregistrer un commentaire