vendredi 18 septembre 2020

Using factory pattern with classes

After reading about factory pattern in javascript this phrase stood out (https://medium.com/javascript-scene/javascript-factory-functions-with-es6-4d224591a8b1)

A factory function is any function which is not a class or constructor that returns a (presumably new) object. In JavaScript, any function can return an object. When it does so without the new keyword, it’s a factory function.

So a fairly simple implementation would be:

const entityFactory = {
  user: () => ({ name: 'John' })
}

The problem I was initially trying to solve is: I would like to generate fresh instances of objects that each has it's own reference. Now that I have a class every new User() will return a fresh instance of an object. Well, some backend guys came and told me:

You should avoid using the word new in your code. The new word should only be in one place in your code and not scattered all around

Representing what I was doing into code:

Component A

const newUser = new User();

Component B

const anotherNewUser = new User();

So I solved the problem by combining the factory pattern with classes and ended up doing this:

class User {
  name: string = 'John';
}

const entityFactory = {
  user: () => new User(),
}

But what I'm doing contradicts what the first block quote said:

In JavaScript, any function can return an object. When it does so without the new keyword, it’s a factory function.

Am I breaking the factory pattern? Is the factory pattern necessary here or should I go with instantiating the class directly?

Aucun commentaire:

Enregistrer un commentaire