How To Use Simple Factory Design Pattern In Java

Simple Factory Design Pattern is one of the many design patterns – best practices on how to solve common problems. Design Patterns were made popular by the Gang of Four (GoF) – Ralph Johnson, John Vlissides, Richard Helm, and Erich Gamma in their book Design Patterns: Elements of Reusable Object-Oriented Software. In this post, I will show you how to use the Simple Factory Design Pattern.
simple factory design pattern in java

Simple Factory Design Pattern In Java – Example Code

In order to use simple factory design pattern, you need at least the following classes:

  • Either an Inteface or an Abstract Class
  • At least two classes that either implement or extend the interface or base class respectively
  • The factory class itself – it should have a static method. Will explain more.

For our example, we will use an interface.

Now that we have our interface done, we can create a few concrete classes that implement this interface; as you might have guessed it, we will create a class called FullTime and PartTime employee to differentiate the two potential needs.

With the first concrete class complete, we can now start working on our second concrete class.

We can finally create our Factory class that we will use to create new instances of our employees. One main thing to remember about Simple Factory Design Pattern is that you define a method inside your factory class that takes an argument. You then check that argument to see if it matches a given criteria before returning the respective class instance, in this case, employee. Let us look at our example.

The signature of our method inside the EmployeeFactory class indicates that we are returning an Employee. This works because our concrete classes implement the Employee interface. So then to get a new full time employee, we simply pass in the number of hours (in some countries, working at least 40 hours a week implies that you are a full time employee. This might not be true for others.

How To Test Simple Factory Design Pattern


The calling program in simple factory design pattern typically has a way of telling the factory what it wants, and the factory makes the decision which subclass should be returned to the calling program. It then creates an instance of that subclass, and then returns it to the calling program.

As you can see, you simply need either an interface or an abstract class, unlimited concrete classes that either implement or extend the interface or abstract class respectively and finally a factory class with a static method that you will use to create a new instance of a concrete class depending on what you pass in as the argument to the static method. It makes things easier because next time you want to add a new class, you will simply add your class, and a simple else if statement in your factory method.

The point of using design patterns in Java is avoid too much coupling between objects. In other words, objects should really know the least possible about other objects. The simple factory design pattern helps us achieve that by helping us create classes when we want them.

Next time, I will be talking about other design patterns; like the Factory Pattern – I know, it sounds like we just did but they are different. That is why the one we just talked about is called Simple Factory Design Pattern and not just Factory Design Pattern. There are so many design patterns in Java. If you want to read more about them, click here and see you soon.

Please consider sharing this post with your friends using the buttons below. Thank you and take care.

Be The First To Get New Posts From Simple Developer…

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

One Comment on “How To Use Simple Factory Design Pattern In Java

  1. Abner Matheus Reply

    October 18, 2014 at 11:33

    Thanks! That as a pretty good reading. Very easy to understand. Good job! 🙂

Leave a Reply

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