The Factory Pattern is used to create objects without exposing the creation logic to the client. Instead of using new
to instantiate an object, you use a factory method.
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const CarFactory = {
createCar: function(make, model, year) {
return new Car(make, model, year);
}
};
const myCar = CarFactory.createCar('Toyota', 'Camry', 2020);
console.log(myCar.make); // Output: Toyota