Check out a free preview of the full A Tour of JavaScript & React Patterns course

The "Factory Pattern" Lesson is part of the full, A Tour of JavaScript & React Patterns course featured in this preview video. Here's what you'd learn in this lesson:

Lydia explains the factory pattern is a function that returns an object. The function may accept parameters and use those parameters to create more properties on the returned object. This pattern is useful when creating multiple objects that share the same set of properties.


Transcript from the "Factory Pattern" Lesson

>> So the factory pattern is a pattern that uses a special function. And this function is called the factory function. And we can use this function which in JavaScript is pretty easy. It's just like an arrow function to create many of the same objects. We know all the properties that is going to have and then based on what we pass to it, maybe we add some more properties, maybe we I change them a little bit, and so on.

So like here we're creating two users like John and Sarah. Now, we only pass the first name and last name. But the object that we create also contains the ID, created ads, and the full name based on the properties that we passed. Yeah, like I said, it's pretty simple in JavaScript to create a factory function.

This pattern is really more for languages that don't really have built in objects. So it's more difficult to create many objects of the same type, but luckily in JavaScript, we can just use an arrow function or a regular function that returns an object. Okay, so the trade offs for the factory pattern is that we don't have to recreate the same object or type the same object out every time.

We will also see this in the exercise later on. So we can just easily create many of the same objects with just a one liner. We just have to say create user, create your user, create user instead of having that user object every time. But yeah, like I said, in JavaScript, it's not really a pattern.

I see there's an overflow here. Now, we will go into the prototype pattern a bit later, which actually shows that this pattern is not very memory efficient, maybe you should try to avoid it if you're actually creating a really large amount of objects. But we will cover that in the prototype pattern.

So yeah, just to recap all it does is we just have a function that returns an object, that's the vector pattern, and that function is called a factory function. So when we look at the exercise here, you see that we have many books. We have Harry Potter, a Great Gatsby and all this stuff.

And in index, I guess, yeah, we're logging all of those books. But in this case, so we create it, explicitly created all of these objects. So the exercise here is to create a factory function instead that returns a book, instead of having to write those books like this.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now