mercredi 20 janvier 2021

Design Pattern for Class with many sub classes and sub classes

I'm trying to think of a (better) way to design the following system in JS.

Say I have a Car with Engine, Doors, Lights etc.

The Engine has: Pistons, Crankshaft, CylinderHead etc.

The way I have it today is in a composition such that:

class Car
{
   constructor()   
   {
       this.engine = Engine();
       ...
   }

   activateEngine()
   {
      this.engine.switchOn();
   }
  ...

}

The problem is that every time I add another part to the car, the Car's class becomes huge! with lots of executions of different parts. On the flip side, all communication with the car goes through one class.

Wonder if this is the right way to go? is there a better way?

Aucun commentaire:

Enregistrer un commentaire