October 2017
Intermediate to advanced
280 pages
6h 50m
English
Let's suppose that we have a Car class that depends on the Engine and Transmission classes. How can we implement this system? Let's take a look:
class Engine {...}
class Transmission {...}
class Car {
engine;
transmission;
constructor() {
this.engine = new Engine();
this.transmission = new Transmission();
}
}
In the preceding example, we create the dependencies of the Car class inside its constructor. Although it looks simple, it is far from being flexible. Each time we create an instance of the Car class, in its constructor, instances of the same Engine and Transmission classes will be created. This may be problematic because of the following reasons:
Read now
Unlock full access