The factory design model – Java DZone

0


the factory design model is one of the

creative

patterns. this model will allow the creation of objects without exposing the instantiation logic to the client.


motivation:

in one of the implementations of the project we were trying to read data from multiple sources such as a database, csv and xml. in this case, we have created readers like databasereader, csvreader and xmlreader etc. but, in client code, we were using the concrete implementations to instantiate the class (e.g. xmlreader reader = new xmlreader ()) and resulted in many duplications of the object creation logic in client code. to overcome this problem, we created an interface called reader and all readers implemented the reader interface. defined a factory method that will take the necessary parameters from the client and create the required object.


design elements

:

the design elements below are required to use the factory design template.

  • product – defines the interface of objects created by the factory method.

  • concretproduct – implements the product interface

  • creator – declares the factory method, which returns an object of type product. the creator can also define a default implementation of the factory method that returns a concrete object by default.


Implementation:


Example:

in this example we are going to create reader objects to read database, csv and xml data using the “factory design model“. first, we are going to create a reading interface.


package org.smarttechie;

public interface reader {
 public string read();
}

now we are going to provide the player interface implementation 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 are going to create a 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 model.


package org.smarttechie;
public class factorypatterntest {

/**
 * @param args
 */
 public static void main(string[] args) {
 system.out.println(readerfactory.getreader("xml").read());
 }
}

in the code above we need to ask readerfactory to get the specific reader. thus, client code does not need to know the implementation of concrete reader classes. the code used in this article is available

here

.


Share.

Leave A Reply