Java Design Pattern Examples - Factory Design Pattern



Factory pattern is one of most commonly used design patterns in Java. This type of design pattern is categorized under creational patterns since it is one of the best ways to create an object.

In Factory pattern, the objects are created without their creation logic being exposed to the client.


The Idea Behind The Factory Pattern

In order to properly explain the idea behind the factory pattern, we are going to use the following example scenario:

"You have recently moved to a new country and you don't have a phone yet but you need to call your mom and say that you have safely arrived (assuming no internet/letter/any communication means except calling are unavailable). Then you decide to buy a Cell Phone and asked the phone company to send you one. After receiving it, you realize that it is very expensive for you, so you decide to get a Home Phone instead and you finally make the call."



In this scenario, the client (You) have no idea how any of these phones are produced or anything. You just know that you can ask the company to give them to you and you can call someone with it. That simple!

First, let's draw the UML diagram of the problem and get an idea about our Java classes.



As you see in the diagram, there are 4 classes and 1 interface in our applciation. CellPhone and HomePhone classes implement the Phone interface and the PhoneFactory and Main are used to create the objects and display the results. After analyzing the UML diagram, let's take a look at this example in Java and apply the factory pattern:

1- Phone.java (The interface of a Phone)


public interface Phone {
 void call(long number); 
}

2- Creation of CellPhone and HomePhone Classes

CellPhone.java

public class CellPhone implements Phone {
 
 private long userID;
 
 public CellPhone(long userID){
  this.userID = userID;
 }
 
 @Override
 public void call(long number){
  System.out.println("Cell -> Calling "+ number+"..");
  System.out.println("Too expensive for you!");
 }
}


HomePhone.java

public class HomePhone implements Phone {
 
 private long userID;
 
 public HomePhone(long userID){
  this.userID = userID;
 }
 
 @Override
 public void call(long number){
  System.out.println("Home -> Calling "+ number+"..");
  System.out.println("Costs 10 cents per minute");
 }
}

3- PhoneFactory Class is our factory


public class PhoneFactory {
 public Phone getPhone(String phoneType){
  if(phoneType == null) 
   return null;
  if(phoneType.equalsIgnoreCase("Cell")) 
   return new CellPhone(124352465L);
  else if(phoneType.equalsIgnoreCase("Home")) 
   return new HomePhone(4135611135L);
  else 
   return null; 
 }
}

4- Let's Order The Phones - Main.java


public class Main {
 public static void main(String[] args) {
  // Create a factory object
  PhoneFactory factory = new PhoneFactory();
  
  // Ask factory object to get a Cell Phone
  Phone myNewCell = factory.getPhone("CELL");
  
  // After getting your cell phone, call mom
  long mom = 6661112233L;  
  myNewCell.call(mom);
  
  // You figured that it was too expensive
  //to use your cell so you got a home phone too
  Phone myNewHome = factory.getPhone("home");
    
  // After getting your home phone, call mom
  myNewHome.call(mom);
  
 }
}

As you see from the main method, the user creates a factory object and uses that object to create 2 other objects without needing to know what's going on in between the factory and the products. Here is the results:

Cell -> Calling 6661112233..
Too expensive for you!

Home -> Calling 6661112233..
Costs 10 cents per minute


Alright, that's all for this post. Please check out the upcoming Design Patterns Tutorial with Examples Using Java post for all the design patterns. Happy coding!
Author:

Software Developer, Codemio Admin

Disqus Comments Loading..