?????Factory Design Pattern?????
Ankesh Mishra
SERVING NOTICE PERIOD Development Engineer - Backend Technologies | Java RESTful APIs, Spring Boot
Factory Design Pattern
Purpose: The Factory Pattern is used to create objects without specifying the exact class of object that will be created. It provides a way to delegate the instantiation logic to subclasses, making it easier to manage and scale object creation.
UML Diagram for Factory Design Pattern.
Components:
// Product interface
public interface Product {
void use();
}
// ConcreteProductA class
public class ConcreteProductA implements Product {
@Override
public void use() {
System.out.println("Using ConcreteProductA");
}
}
// ConcreteProductB class
public class ConcreteProductB implements Product {
@Override
public void use() {
System.out.println("Using ConcreteProductB");
}
}
// Creator (Factory) interface
public abstract class Creator {
public abstract Product factoryMethod();
}
// ConcreteCreatorA class
public class ConcreteCreatorA extends Creator {
@Override
public Product factoryMethod() {
return new ConcreteProductA();
}
}
// ConcreteCreatorB class
public class ConcreteCreatorB extends Creator {
@Override
public Product factoryMethod() {
return new ConcreteProductB();
}
}
// Main class to test the Factory Pattern
public class FactoryPatternDemo {
public static void main(String[] args) {
Creator creatorA = new ConcreteCreatorA();
Product productA = creatorA.factoryMethod();
productA.use();
Creator creatorB = new ConcreteCreatorB();
Product productB = creatorB.factoryMethod();
productB.use();
}
}
When to Use the Factory Design Pattern
The Factory Design Pattern is particularly useful in several scenarios:
Consequences of Not Using the Factory Design Pattern
If you do not use the Factory Design Pattern, you might encounter several issues:
领英推荐
Example in Java
Scenario: Creating Different Types of Documents
Suppose you need to create different types of documents in a system, such as PDF and Word documents. Without using the Factory Design Pattern, you might end up with tightly coupled code:
Without Factory Pattern:
// Document interface
public interface Document {
void open();
}
// PDFDocument class
public class PDFDocument implements Document {
@Override
public void open() {
System.out.println("Opening PDF Document");
}
}
// WordDocument class
public class WordDocument implements Document {
@Override
public void open() {
System.out.println("Opening Word Document");
}
}
// Main class
public class Main {
public static void main(String[] args) {
Document doc1 = new PDFDocument();
doc1.open();
Document doc2 = new WordDocument();
doc2.open();
}
}
Issues:
With Factory Pattern:
// Document interface
public interface Document {
void open();
}
// Concrete Documents
public class PDFDocument implements Document {
@Override
public void open() {
System.out.println("Opening PDF Document");
}
}
public class WordDocument implements Document {
@Override
public void open() {
System.out.println("Opening Word Document");
}
}
// Factory class
public class DocumentFactory {
public static Document createDocument(String type) {
switch (type) {
case "PDF":
return new PDFDocument();
case "Word":
return new WordDocument();
default:
throw new IllegalArgumentException("Unknown document type");
}
}
}
// Main class
public class Main {
public static void main(String[] args) {
Document pdfDoc = DocumentFactory.createDocument("PDF");
pdfDoc.open();
Document wordDoc = DocumentFactory.createDocument("Word");
wordDoc.open();
}
}
Benefits:
Summary
Use the Factory Design Pattern when you want to decouple object creation from the client code, manage complex creation logic, or ensure consistency in how objects are created. Not using the pattern can lead to tightly coupled code, duplication, and reduced flexibility, making it harder to manage and extend the system.