Factory Pattern

It is a Creational Pattern as this pattern deals with the best ways to create an object.

We are developing an application which has to get data from third party and display it in our website.

Third party data could be in the form of Excel,text file or anything which can change later.

Let’s create a class for each data type representation

Create DataFromThirdPartyExcel.java

  1. package com.kb.factory.problem;
  2.  
  3. public class DataFromThirdPartyExcel {
  4.    
  5.     void getData(){
  6.         System.out.println("logic to get excel data from third party");
  7.        
  8.     }
  9.    
  10.     void display(){
  11.         System.out.println("logic to display excel data");
  12.     }
  13.  
  14. }
package com.kb.factory.problem;

public class DataFromThirdPartyExcel {
	
	void getData(){
		System.out.println("logic to get excel data from third party");
		
	}
	
	void display(){
		System.out.println("logic to display excel data");
	}

}


Create DataFromThirdPartyTextFile.java

  1. package com.kb.factory.problem;
  2.  
  3. public class DataFromThirdPartyTextFile {
  4.     void getData(){
  5.         System.out.println("logic to get text file data from third party");
  6.        
  7.     }
  8.    
  9.     void display(){
  10.         System.out.println("logic to display text file data");
  11.     }
  12.  
  13. }
package com.kb.factory.problem;

public class DataFromThirdPartyTextFile {
	void getData(){
		System.out.println("logic to get text file data from third party");
		
	}
	
	void display(){
		System.out.println("logic to display text file data");
	}

}


Create Client.java

  1. package com.kb.factory.problem;
  2.  
  3. public class Client {
  4.  
  5.     public static void main(String[] args) {
  6.         //To get Text data
  7.         getDataFormThirdPartyAndDisplay("text");
  8.        
  9.         //To get Excel data
  10.         getDataFormThirdPartyAndDisplay("excel");
  11.  
  12.     }
  13.    
  14.    
  15.     static void getDataFormThirdPartyAndDisplay(String dataType){
  16.         if(dataType.equalsIgnoreCase("text")){
  17.             DataFromThirdPartyTextFile textFile = new DataFromThirdPartyTextFile();
  18.             textFile.getData();
  19.             textFile.display();
  20.         }
  21.         else if(dataType.equalsIgnoreCase("excel")){
  22.             DataFromThirdPartyExcel thirdPartyExcel = new DataFromThirdPartyExcel();   
  23.             thirdPartyExcel.getData();
  24.             thirdPartyExcel.display();
  25.         }
  26.         else
  27.             System.out.println("invalid data type");
  28.            
  29.     }
  30.  
  31. }
package com.kb.factory.problem;

public class Client {

	public static void main(String[] args) {
		//To get Text data
		getDataFormThirdPartyAndDisplay("text");
		
		//To get Excel data
		getDataFormThirdPartyAndDisplay("excel");

	}
	
	
	static void getDataFormThirdPartyAndDisplay(String dataType){
		if(dataType.equalsIgnoreCase("text")){
			DataFromThirdPartyTextFile textFile = new DataFromThirdPartyTextFile();
			textFile.getData();
			textFile.display();
		}
		else if(dataType.equalsIgnoreCase("excel")){
			DataFromThirdPartyExcel thirdPartyExcel = new DataFromThirdPartyExcel();	
			thirdPartyExcel.getData();
			thirdPartyExcel.display();
		}
		else
			System.out.println("invalid data type");
			
	}

}


Now look at the client code which has tight coupling with the excel class and text file class.

In future if we want to add one more data representation say PDF then again we have to go to the client code and change the behavior by adding one more else if condition.

  1. else if(dataType.equalsIgnoreCase("pdf")){
  2.             DataFromThirdPartyPDF thirdPartypPdf = new DataFromThirdPartyPdf();
  3.             thirdPartypPdf.getData();
  4.             thirdPartypPdf.display();
  5.         }
else if(dataType.equalsIgnoreCase("pdf")){
			DataFromThirdPartyPDF thirdPartypPdf = new DataFromThirdPartyPdf();	
			thirdPartypPdf.getData();
			thirdPartypPdf.display();
		}


So we are tending to change client code for every business needs changes.

So why can’t we separate this changing behavior and keep in some separate place.

Yes that separate place we call it as a Factory.

Factory pattern separates the object creation code (in fact frequently changing code) and put it in a place apart from client code.

Let’s see how can we achieve this


Create Interface DataFromThirdParty.java

  1. package com.kb.factory.solution;
  2.  
  3. public interface DataFromThirdParty {
  4.     void getData();
  5.     void display();
  6. }
package com.kb.factory.solution;

public interface DataFromThirdParty {
	void getData();
	void display();
}


Create DataFromThirdPartyExcel.java

  1. package com.kb.factory.solution;
  2.  
  3. public class DataFromThirdPartyExcel implements DataFromThirdParty {
  4.    
  5.     public void getData(){
  6.         System.out.println("logic to get excel data from third party");
  7.        
  8.     }
  9.    
  10.     public void display(){
  11.         System.out.println("logic to display excel data");
  12.     }
  13.  
  14. }
package com.kb.factory.solution;

public class DataFromThirdPartyExcel implements DataFromThirdParty {
	
	public void getData(){
		System.out.println("logic to get excel data from third party");
		
	}
	
	public void display(){
		System.out.println("logic to display excel data");
	}

}


Create DataFromThirdPartyTextFile.java

  1. package com.kb.factory.solution;
  2.  
  3. public class DataFromThirdPartyTextFile  implements DataFromThirdParty {
  4.     public void getData(){
  5.         System.out.println("logic to get text file data from third party");
  6.        
  7.     }
  8.    
  9.     public void display(){
  10.         System.out.println("logic to display text file data");
  11.     }
  12. }
package com.kb.factory.solution;

public class DataFromThirdPartyTextFile  implements DataFromThirdParty {
	public void getData(){
		System.out.println("logic to get text file data from third party");
		
	}
	
	public void display(){
		System.out.println("logic to display text file data");
	}
}


Create DataFromThirdPartyFactory.java

  1. package com.kb.factory.solution;
  2.  
  3. public class DataFromThirdPartyFactory {
  4.    
  5.     DataFromThirdParty getDataFromThirdParty(String dataType){
  6.         if(dataType.equalsIgnoreCase("text")){
  7.             return new DataFromThirdPartyTextFile();
  8.            
  9.         }
  10.         else if(dataType.equalsIgnoreCase("excel")){
  11.             return new DataFromThirdPartyExcel();  
  12.            
  13.         }
  14.         else
  15.             System.out.println("invalid data type");
  16.         return null;
  17.        
  18.     }
  19. }
package com.kb.factory.solution;

public class DataFromThirdPartyFactory {
	
	DataFromThirdParty getDataFromThirdParty(String dataType){
		if(dataType.equalsIgnoreCase("text")){
			return new DataFromThirdPartyTextFile();
			
		}
		else if(dataType.equalsIgnoreCase("excel")){
			return new DataFromThirdPartyExcel();	
			
		}
		else
			System.out.println("invalid data type");
		return null;
		
	}
}


Create Client.java

  1. package com.kb.factory.solution;
  2.  
  3. public class Client {
  4.  
  5.     public static void main(String[] args) {
  6.         DataFromThirdParty dataFromThirdParty;
  7.        
  8.         DataFromThirdPartyFactory thirdPartyFactory = new DataFromThirdPartyFactory();
  9.        
  10.         //To get Text data
  11.         dataFromThirdParty = thirdPartyFactory.getDataFromThirdParty("text");
  12.         dataFromThirdParty.getData();
  13.         dataFromThirdParty.display();
  14.        
  15.         //To get Excel data
  16.         dataFromThirdParty = thirdPartyFactory.getDataFromThirdParty("excel");
  17.         dataFromThirdParty.getData();
  18.         dataFromThirdParty.display();
  19.        
  20.  
  21.     }
  22. }
package com.kb.factory.solution;

public class Client {

	public static void main(String[] args) {
		DataFromThirdParty dataFromThirdParty;
		
		DataFromThirdPartyFactory thirdPartyFactory = new DataFromThirdPartyFactory();
		
		//To get Text data
		dataFromThirdParty = thirdPartyFactory.getDataFromThirdParty("text");
		dataFromThirdParty.getData();
		dataFromThirdParty.display();
		
		//To get Excel data
		dataFromThirdParty = thirdPartyFactory.getDataFromThirdParty("excel");
		dataFromThirdParty.getData();
		dataFromThirdParty.display();
		

	}
}


Now client using factory object to get the required object.

Now if we want to add new functionality to get data in the PDF form, then it needs no change at client with respect to data representation logic . only client has to include a call to the PDF data type.

Let’s see above change in factory pattern


Obviously Change in factory pattern means change inside factory class only.

Create a new functionality but implement it with the interface DataFromThirdParty

As we should do program to Interface not program to Implementation.

Create DataFromThirdPartyPDF.java

  1. package com.kb.factory.solution;
  2.  
  3. public class DataFromThirdPartyPDF implements DataFromThirdParty{
  4.     public void getData(){
  5.         System.out.println("logic to get pdf file data from third party");
  6.        
  7.     }
  8.    
  9.     public void display(){
  10.         System.out.println("logic to display pdf file data");
  11.     }
  12.  
  13. }
package com.kb.factory.solution;

public class DataFromThirdPartyPDF implements DataFromThirdParty{
	public void getData(){
		System.out.println("logic to get pdf file data from third party");
		
	}
	
	public void display(){
		System.out.println("logic to display pdf file data");
	}

}


Now see how Factory class gets one extra else if statement

Create DataFromThirdPartyFactory.java

  1. package com.kb.factory.solution;
  2.  
  3. public class DataFromThirdPartyFactory {
  4.    
  5.     DataFromThirdParty getDataFromThirdParty(String dataType){
  6.         if(dataType.equalsIgnoreCase("text")){
  7.             return new DataFromThirdPartyTextFile();
  8.            
  9.         }
  10.         else if(dataType.equalsIgnoreCase("excel")){
  11.             return new DataFromThirdPartyExcel();  
  12.            
  13.         }
  14.        
  15.         else if(dataType.equalsIgnoreCase("pdf")){
  16.             return new DataFromThirdPartyPDF();
  17.         }
  18.         else
  19.             System.out.println("invalid data type");
  20.         return null;
  21.        
  22.     }
  23. }
package com.kb.factory.solution;

public class DataFromThirdPartyFactory {
	
	DataFromThirdParty getDataFromThirdParty(String dataType){
		if(dataType.equalsIgnoreCase("text")){
			return new DataFromThirdPartyTextFile();
			
		}
		else if(dataType.equalsIgnoreCase("excel")){
			return new DataFromThirdPartyExcel();	
			
		}
		
		else if(dataType.equalsIgnoreCase("pdf")){
			return new DataFromThirdPartyPDF();
		}
		else
			System.out.println("invalid data type");
		return null;
		
	}
}


That’s it new functionality has been implemented without client knows about it.

Now client can use this new functionality just by calling same factory method but passing different string for pdf as below

  1. dataFromThirdParty = thirdPartyFactory.getDataFromThirdParty("pdf");
  2.         dataFromThirdParty.getData();
  3.         dataFromThirdParty.display();
dataFromThirdParty = thirdPartyFactory.getDataFromThirdParty("pdf");
		dataFromThirdParty.getData();
		dataFromThirdParty.display();


Create Client.java

  1. package com.kb.factory.solution;
  2.  
  3. public class Client {
  4.  
  5.     public static void main(String[] args) {
  6.         DataFromThirdParty dataFromThirdParty;
  7.        
  8.         DataFromThirdPartyFactory thirdPartyFactory = new DataFromThirdPartyFactory();
  9.        
  10.         //To get Text data
  11.         dataFromThirdParty = thirdPartyFactory.getDataFromThirdParty("text");
  12.         dataFromThirdParty.getData();
  13.         dataFromThirdParty.display();
  14.        
  15.         //To get Excel data
  16.         dataFromThirdParty = thirdPartyFactory.getDataFromThirdParty("excel");
  17.         dataFromThirdParty.getData();
  18.         dataFromThirdParty.display();
  19.        
  20.  
  21.         //To get PDF data
  22.         dataFromThirdParty = thirdPartyFactory.getDataFromThirdParty("pdf");
  23.         dataFromThirdParty.getData();
  24.         dataFromThirdParty.display();
  25.  
  26.     }
  27. }
package com.kb.factory.solution;

public class Client {

	public static void main(String[] args) {
		DataFromThirdParty dataFromThirdParty;
		
		DataFromThirdPartyFactory thirdPartyFactory = new DataFromThirdPartyFactory();
		
		//To get Text data
		dataFromThirdParty = thirdPartyFactory.getDataFromThirdParty("text");
		dataFromThirdParty.getData();
		dataFromThirdParty.display();
		
		//To get Excel data
		dataFromThirdParty = thirdPartyFactory.getDataFromThirdParty("excel");
		dataFromThirdParty.getData();
		dataFromThirdParty.display();
		

		//To get PDF data
		dataFromThirdParty = thirdPartyFactory.getDataFromThirdParty("pdf");
		dataFromThirdParty.getData();
		dataFromThirdParty.display();

	}
}


The same Factory pattern has been used in spring container to get the required object using XM.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on