Factory Design pattern

Rasika Weragoda
4 min readSep 15, 2020

I hope you guys read my introduction article of design pattern in java. If not then the link is available here Design Pattern in Java. This article does not depend on the previous article (Design Pattern in Java). But it has some basic understanding of the background of design patterns. Just remember the Factory design pattern belongs to the category of creational design pattern

Most developers consider Factory design patter as the opposite of the Singleton pattern. So the hint is that the singleton is not fit for your scenario then consider the using the Factory Design pattern 😁. Just simple is that. Most of the times this design pattern used when creating the frameworks. Actually Factory design pattern gives a nice abstraction for the user. Why I am saying that because of user have no idea what type of object that is instantiated. They only know, there is a common interface that they are exposed to.

As an example of the Factory Design pattern that Java API gives,

  • Calendar
  • ResouseBundle
  • NumberFormat

Sometimes you may think Java Calendar as a singleton design. But the thing is that the Calnder can return a different subclass of the calendar and the Singleton returns singleton instance of that object. So the idea is the factory design pattern is it is like the factory. Because the client knows that there is a factory called ‘A’ and it produced ‘C’ and ‘D’ products. So the clients goto that ‘A’ factory and ask to give me ‘C’ product.

note:- Typically singleton object’s constructor has no parameters, But the constructor of the factory object may contain parameters.

As I told before, Calendar is implemented using Factory Design pattern. Then Calander should be a factory right? What about its products 🤔.

Calendar cal = Calender.getInstance(); //get the calendar instance
System.out.println(cal) // ???

So what happens when we print the cal instance ???. Is that type of calendar or something else. It is a GregorianCaledar instance type(it is the concrete implementation). So this implementation is done underneath of this calendar factory instance.

Scenario

Suppose the Apple Company. They create multiple products right? For the demonstration purposes, we consider only the MacBook models. They are MacBook Air and MacBook Pro. So MacBookFactory creates these two models right…

Below diagram illustrate how to implement Factory design pattern to the above scenario

So the above diagram, we can see that there is a Laptop abstract class and which is extends by MacBook Pro and MacBook Air classes. These two classes are the concrete implementation of the Laptop abstract class. The MacBookFactory is a class that gives either Macbook Air instance or MacBook Pro instance when the client needs it.

Code Example

Let’s write some java code to understand the above scenario. First, we have Laptop abstract class. It looks like this

Let’s clear out above code snippet. There are 3 properties we mainly focused on. You can see there is a static field called assert_tag. In the real world, it is a kind of unique id (serial number) Every laptop manufactured vendors that are assigned to laptops. But in this case, think it as a unique id for each laptop. Finally, we have a brand and price.

In addition to the setter and getter method, we have an abstract method called createLaptop(). This abstract method called a factory method. Because of this method implemented by its subclasses. So the factory method is a method implemented by its subclasses.

Let’s implement the MacBookAir class and MacBookPro classes

they are very simple classes that are extended by Laptop classes and override the factory method.

Let’s create the Enum class that contains MacBook types

Now we have to implement the final piece of code. That is MacBookFactory class. This class acts like just the factory as I told before. So this class is responsible for giving the relevant instance to the Client.

Of course, this is it. Our final piece. So here you can see there is a static method called getMacBook(). This method is known as the Simple Factory method. So as code says when the client asked the MacBookAir it returns the instance of MacBookAir, just simple is that.

Let’s create the Main class to get those instance using MacBookFactory

So the output as follows

output image

Remember this design pattern is the second most design pattern in the wold. It helps to create more robust applications. If you want to maintain the codebase or add some parts later on, then I will say this is the best approach you every had. This design pattern almost preserves the open-close principle and dependency inversion principles of SOILD.

I almost forgot to tell why this design pattern is the opposite singleton design pattern. Think about it. if you are not read the singleton design pattern, I strongly suggest you read here.

  • singleton return the same instance and it has only one constructor with no arguments. But factory design pattern can return a various instance and it has multiple constructors with no-args and args.
  • The single design pattern has no interface or abstract class and it has no subclasses. But factory design pattern interface-driven.

Well, I think you get it. Stay tuned for another article ✌

--

--