1. 程式人生 > >How To Use Simple Factory Design Pattern In Java

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.

Employee.java

123456789 publicinterfaceEmployee{publicdoublegetHourlyRate();// add other methods here as you please}

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.

FullTimeEmployee.java

12345678910111213141516171819 publicclassFullTimeEmployeeimplementsEmployee{privatedoublehourlyRate;publicFullTimeEmployee(doublehourlyRate){this.hourlyRate=hourlyRate;}/*Implement the contract methods here*/publicdoublegetHourlyRate(){returnhourlyRate;}}

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

PartTimeEmployee.java

12345678910111213141516171819 publicclassPartTimeEmployeeimplementsEmployee{privatedoublehourlyRate;publicPartTimeEmployee(doublehourlyRate){this.hourlyRate=hourlyRate;}/*Implement the contract methods here*/publicdoublegetHourlyRate(){returnhourlyRate;}}

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.

EmployeeFactory.java

12345678910111213141516171819 publicclassEmployeeFactory{publicstaticEmployee getEmployee(inthoursAllocated,doublehourlyRate){//do error checking hereif(hoursAllocated>=40){returnnewFullTimeEmployee(hourlyRate);}else{returnnewPartTimeEmployee(hourlyRate);}}}

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

SimpleFactoryDesignTester.java

1234567891011121314151617 publicclassSimpleFactoryDesignTester{publicstaticvoidmain(Stringargs[]){Employee employee=EmployeeFactory.getEmployee(30,25.0);System.out.println(employee.getHourlyRate());employee=EmployeeFactory.getEmployee(40,15.45);System.out.println(employee.getHourlyRate());}}

Summary

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.