OOP Factories
Lesson Objectives
Create a Factory
Bonus - static properties for a class
Create a factory
Sometimes we need to have a factory object that will generate other objects
The purpose of the factory is so it can control the creation process in some way
This is usually a single object that exists throughout the program that performs a set of functions
also called a singleton
Let's start with a car
class Car {
constructor (maker, serialNumber) {
this.serialNumber = serialNumber;
this.maker = maker
}
drive () {
console.log('Vroom Vroom');
}
}
const newCar = new Car('Mazda', 12345);
console.log(newCar);Now let's make a factory class that will make cars for us.
Now we can easily create another new factory that produces its own cars
Extra
Create static properties for a class
Sometimes you want to define properties that pertain to the class as a whole, not the instance. This allows us to limit, somewhat, what the user of class can do.
Adapted from SEI-MAE
Last updated
Was this helpful?