Design patterns: Factory pattern

Here’s an interesting question. If your children are identical twins and you are just introducing them to someone, how do you help your guests distinguish between your twins?
You use the factory design pattern of course!

Let’s use this post to learn one of the most commonly used design patterns in the world of object-oriented programming. Instead of compelling you remember the names of classes, the factory pattern gives you a class based on something you know about that class. Let’s dive in to see what I mean.

Factory pattern in real life

There are different types of pasta. Let’s use just three: penne, macaroni and spaghetti. If you are not Italian or if you don’t eat pasta quite often, it is extremely tricky for you to remember those names. Let’s say you have eaten pasta often enough to have a fair idea of what you want, but you can’t recollect the names. But fear not. There is a shop attendant to help you to the right type of pasta if you can describe to him/her what your favoured pasta looks like.

“Uh, they looked like really long noodles!”. The attendant takes you right to the aisle that has stocks of spaghetti.
“Well, they looked like small tubes”. The attendant nods, but then asks another question. “And did those tubes have thin parallel stripes all around?” Yes, yes!”, you may say excitedly or “No, I don’t remember any stripes. They were plain tubes.”. The attendant is ready for both the answers. He will either guide you to striped penne or plain macaroni.
You don’t need to remember names that sound so foreign to you. You mention the specifications and get a suggestion in return.

The solution to the problem in the introductory paragraph is similar. If people have trouble remembering the names of your twins or in distinguishing which is which, you give them the specifications. So that , even if they forget the names, they can simply give you a lead, “You know, the one who plays cricket, not the one who plays tennis.”

So, what is factory pattern?

This approach of using specifications to come up with the right fit is called ‘factory pattern’ in the world of object-oriented software. Using the pasta example, here is how we describe the system around factory pattern.

A group of classes have some properties in common. Keeping that in mind, they are laid out in a parent-child hierarchy. Please check out this post to learn what is a parent-child hierarchy in the world of object-oriented software. In our example, penne, spaghetti and macaroni are all types of pasta. All of them are made of wheat and the cooking method is the same. Only the shapes vary.

pasta-factory-pattern

Next, there is another class called a factory. An object of the factory class is made right when the system starts up. It is ready to be used whenever your program wants an object based on specification. E.g. the attendant is employed along with the store, before you walk in. The factory is trained to recognise certain specifications so that it can create an object of the appropriate class.

Your program never directly creates the object of any of the classes which are supported by the factory. You always ask the factory to produce the right object based on your specifications. Hence the name ‘factory’.

Factory pattern in the world of computing

Factory pattern is used whenever one program needs to handle several different types of specifications. The program then delegates any work that is unique to one type of specification to an object that is made for that type of specification.

Let’s take the example of an image viewer, The main program has a screen where images are to be shown. It also has tools to zoom in and out and to rotate or flip an image. However, the program needs to be able to show JPEG and PNG images. Both types of files store images in different ways. Our image viewer program needs to be able to work past the differences and show both types of images on the screen.

The image viewer pushes the responsibility of decoding images from file formats to each file type’s own decoder. The decoders are then group behind a factory. The image viewer mentions the file name or just the extension to the factory. Based on the extension .png or .jpg, the factory returns the correct type of decoder to the image viewer. The decoder can then return the decoded image to the viewer ready for display on the screen.

Conclusion

The world has too many names and not everyone wants to know every name by heart. However, if that is the case, someone needs to be able to understand specifications and then lead you to the right names. Thankfully, with the factory pattern, you can forget about names and simply focus on the goal that you want to achieve. Please let me know about your own examples of factory pattern in use.

Leave a Reply

Your email address will not be published. Required fields are marked *