DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

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.

Siva Prasad Rao Janapati user avatar by
Siva Prasad Rao Janapati
·
Mar. 21, 16 · Analysis
Like (21)
Save
Tweet
Share
31.40K Views

Join the DZone community and get the full member experience.

Join For Free

factory 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:

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 doesn't need to know  the implementation of the concrete reader classes. the code used in this article is available here .

Factory (object-oriented programming) Design

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.

Popular on DZone

  • How To Build a Spring Boot GraalVM Image
  • Microservices 101: Transactional Outbox and Inbox
  • Getting a Private SSL Certificate Free of Cost
  • What Is Advertised Kafka Address?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: