If your children are identical twins and you introduce them to someone, how do you help that someone distinguish between the 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 to remember the names of classes, the factory pattern returns you the appropriate 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 often, it is extremely tricky to remember the names. Let’s say you have eaten pasta often enough to know the appearance of the pasta you want, but can’t recollect the names. But fear not. The shop attendant of the supermarket can help you to the right type of pasta if you can describe what it looks like.
“Uh, they look like really long noodles!”. The attendant takes you right to the aisle that has stocks of spaghetti.
“Well, they look like small tubes”. The attendant nods but asks another question. “And do those tubes have thin parallel stripes all around?”
“Yes, yes!”, you may say or “No, I don’t remember any stripes. They are plain tubes.”. The attendant is ready for both the answers. He will either guide you to penne, which is striped, or just plain macaroni.
You don’t need to remember names that sound foreign to you. You mention the appearance and get guided to the right option..
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 mention unique things about each twin. So that even if people forget the names, they can simply give you a lead, “I am referring to the one who loves cricket, not the one who loves tennis.”
So, what is the factory pattern?
The 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 design a 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 a parent-child hierarchy in the world of object-oriented software is. 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.
There is a class called a factory, which is used whenever your program wants an object based on specification. In the pasta example, the shop attendant is trained to recognise different pasta types. The factory class is trained to recognise specifications so that it can produce objects of the appropriate class.
Just like you don’t ask for a pasta type by its name, your program’s main code never directly creates an object that the factory is better trained to handle. It always asks the factory to produce the right object based on your specifications.
Factory pattern in the world of computing
Factory pattern is used whenever one program needs to handle several different types of data.
An example is an image viewer, The main program has a screen where images are to be shown. It also has tools to zoom, rotate or flip an image. The program needs to be able to show both 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.
The image viewer pushes the responsibility of decoding images from the respective file formats to each file type’s dedicated decoder. The decoders are served by a factory. The image viewer mentions extension of the image file name to the factory, e.g. png or jpg. Based on the extension, the factory returns the correct type of decoder to the image viewer. The decoder will then return the bitmap image to the viewer, which will then display the bitmap on the screen.
The world has too many names and not everyone wants to know every name by heart. But then someone needs to be able to understand specifications and then lead you to the right names. With the factory pattern, you can forget about names and simply focus on the goal. Leave the names to the factory.
- Factory Method by Refactoring Guru
- Factory Method Design Pattern by Source Making
- Design Pattern – Factory Pattern by Tutorials Point