The Factory Design Pattern

                   Smart Techie

                       Factory design pattern is one of the creational pattern. This pattern will allow the creation of objects with out exposing the instantiation logic to the client.

Motivation:  During one of the project implementation, we were trying to read the data from multiple sources like database, CSV and XML. In this case, we created readers like DatabaseReader, CSVReader and XMLReader etc. But, in the client code we were using the concrete implementations to instantiate the class(For example, XMLReader reader = new XMLReader()) and leading to lot of duplication of object creational logic across the client code. To overcome this issue, we created an interface called Reader and all the readers implemented the Reader interface. Defined a factory method which will take necessary parameters from the client and creates the required object.

Design Elements: The below design elements are required to use factory design pattern.

  • Product<interface> – defines the interface of objects the factory method creates.
  • ConcreteProduct – implements the product interface
  • Creator – declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.

Implementation:

factory design pattern

Example:

              In this example, we will create Reader objects to read Database, CSV and XML data by using “Factory Design Pattern”. As a first step, we will create Reader interface.

package org.smarttechie;

public interface Reader {
 public String read();
}

Now, we will provide the implementation for Reader interface to read the database.


package org.smarttechie;

public class DataBaseReader implements Reader {

@Override
 public String read() {

 return "Reading database";
 }

}

The implementation to read CSV and XML data.


package org.smarttechie;

public class XMLReader implements Reader {

@Override
 public String read() {

 return "XML file reader";
 }

}

 

package org.smarttechie;

public class CSVReader implements Reader {

@Override
 public String read() {
 return "CSV file reading";
 }

}

Now, we will create factory method to create reader objects.


package org.smarttechie;

public class ReaderFactory {

public static Reader getReader(String readerType) {
 Reader reader = null;
 if (readerType.equalsIgnoreCase("XML")) {
     reader = new XMLReader();
 } else if (readerType.equalsIgnoreCase("CSV")) {
     reader = new CSVReader();
 } else if (readerType.equalsIgnoreCase("DB")) {
     reader = new DataBaseReader();
 }
 return reader;
 }
}

With a test class, we will test the factory design pattern.

package org.smarttechie;
public class FactoryPatternTest {

/**
 * @param args
 */
 public static void main(String[] args) {
 System.out.println(ReaderFactory.getReader("XML").read());
 }
}

                   In the above code, we need to ask the ReaderFactory to get the specific reader. So, client code no need to know  the implementation of the concrete reader classes. The code used in this article is available here.

Advertisements

I am Siva Prasad Rao Janapati. Working as a software developer. Has hands on experience on ATG Commerce(DAS/DPS/DCS), Mozu commerce, Broadleaf Commerce, Java, JEE, Spring, Play, JPA, Hibernate, Velocity, JMS, Jboss, Weblogic,Tomcat, Jetty, Apache, Apache Solr, Spring Batch, JQuery, NodeJS, SOAP, REST, MySQL, Oracle, Mongo DB, Memcached, HazelCast, Git, SVN, CVS, Ant, Maven, Gradle, Amazon Web services, Rackspace, Quartz, JMeter, Junit, Open NLP, Facebook Graph,Twitter4J, YouTube Gdata, Bazzarvoice,Yotpo, 4-Tell, Alatest, Shopzilla, Linkshare. I have hands on experience on open sources and commercial technologies.

Tagged with:
Posted in Creational Patterns, Design Patterns
One comment on “The Factory Design Pattern
  1. Anonymous says:

    Good Simple example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

DZone

DZone MVB

Java Code Geeks
Java Code Geeks
%d bloggers like this: