The Factory Design Pattern
Learn all about the factory design pattern, a creational pattern that allows the making of objects with no exposure to instantation logic to the client.
Join the DZone community and get the full member experience.
Join For Freefactory design pattern is one of the creational patterns. this pattern will allow the creation of objects without 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 a 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 a 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.
designelements : 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. the creator may also define a default implementation of the factory method that returns a default concreteproduct object.
implementation:
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 doesn't need to know the implementation of the concrete reader classes. the code used in this article is available here .
Published at DZone with permission of Siva Prasad Rao Janapati, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments