Basic Factory Pattern for JavaScript

The factory pattern is a way to create objects without worrying about how that object is created or what is needed, which allows loose coupling. This means we can ask our factory object to provide us a specific object and the factory will deal with constructing it. Of course, we have to setup our factory to do the creation first!

Here’s an example: you want to hire a new developer to work at your company. Now we may know what skills we are looking for, but what about work benefits and background checks? This is where we would ask the company’s hiring manager to sort things out. The hiring manager is our factory, so we basically ask them “find me a developer with X skills”.

So let’s create a basic factory in a Javascript application. Our application is going to be quite basic; all it’s going to do is display some text, set the text colour, and also set the background colour.

Our basic application:

Now let’s create our ConfigService. This will just hold our primary and secondary colour that will be used for our application, as below:

And let’s go ahead and create our FactoryService. This will deal with giving us our ConfigService when we ask for it:

Now using our ConfigService and FactoryService we can update our application.

We’re going to use the primary colour setting to set the h1 colour and use our secondary colour setting to set the body background colour.

Now we could just create our ConfigService and use that directly, but then if we wanted to replace our ConfigService with a different ConfigService we would have to look through our entire code base to find the “new ConfigService()” and replace it.

This is where our FactoryService comes into play, so let’s setup our application to use the FactoryService when we want to use our ConfigService:

Now our full application does the following: we first define our ConfigService (which just holds our settings). Next we define our FactoryService, this holds an array of objects which are made up of a name and source. The GetInstance method attached to the FactoryService is used to generate a new instance we request.

If you look at the RenderApplication function, we are now calling the FactoryService to get our ConfigService and then using the settings array from the ConfigService to set our colours.

Now I understand we could have set the colour for our HTML page using css or any other number of ways, but I just wanted to demonstrate the factory pattern in a Javascript application.

Also just for a bonus round, let’s say we wanted to use a different config service. Thanks to factory pattern we have implemented, we can simply replace that source target in our FactorySevice like so:

If we now refresh our application, our body and h1 tag will have been assigned a random colour. We could even go further and change our ConfigService to read the colours from a central business API, but this is as far as the post goes.

Thank you for reading, I hope this has helped you or has taught you something new. Feel free to provide feedback, positive or negative!

Factory Pattern:
Random hex colour: