vendredi 3 décembre 2021

Factory pattern without classes

Factory patterns typically use a parent class and then a set of subclasses. In JavaScript, classes present issues. Is it valid to return plain objects in factories instead of classes?

const MyServiceOne = {
   getData: () => 'data'
}

const MyServiceTwo = {
   getData: () => 'other data'
}

const serviceMap = {
   1: MyServiceOne, 
   2: MyServiceTwo, 
}

const ServiceFactory = (type) => {
   return serviceMap[type]; 
}

Aucun commentaire:

Enregistrer un commentaire