jeudi 27 juin 2019

multiple class inheritance vs layered model

Let's say I have class modules A, B and C. Each module has different job. Module A uses B and B uses C (A does not use C and vice versa), but all modules works with same resources (constructor arguments). Also A, B and C use same naming for some basic internal properties. With all these conditions, first that come into my mind is multiple inheritance.

class C {
  constructor(sources) {
    this.sources = sources;
  }
}

class B extends C {
  constructor(sources) {
    super(sources);
  }
}

class A extends B {
  constructor(sources) {
    super(sources);
  }
}

My consideration

Pros:

  • all accessible in one instance (less spaghetti code)
  • less duck typing

Cons:

  • all internal methods exposed to parent
  • possible naming collisions

There is also "default" option to create "property layers":

class C {
  constructor(sources) {
    this.sources = sources;
  }
}

class B {
  constructor(sources) {
    this.sources = sources;
    this.c = new C(sources);
  }
}

class A {
  constructor(sources) {
    this.sources = sources;
    this.b = new B(sources)
  }
}

My consideration: Opossite to above pros/cons

Maybe I miss something. I would like to avoid some coding pitfalls/bad practices so is there "better/proved solution" in terms of flexibility, maintainablity, duck typing etc?

Thanks

Aucun commentaire:

Enregistrer un commentaire