Definition
The Simple Factory pattern is a creational design pattern that encapsulates the object creation process by providing a centralized factory method to create instances of different classes based on a given input or condition. It abstracts the instantiation logic, allowing clients to create objects without directly invoking the object's constructor or being aware of the object's concrete class.
Problem that the Pattern Solves
In scenarios where the object creation logic is complex or subject to change, directly instantiating objects using constructors or conditional logic can lead to tight coupling and reduced flexibility. The Simple Factory pattern addresses these challenges by centralizing the object creation process within a factory, promoting loose coupling, and enhancing maintainability.
Key Components
Product Interface: Defines the common interface for all products.
Concrete Products: Implement the product interface, representing different types of products.
Factory: Encapsulates the object creation logic, providing a method to create instances based on input or conditions.
Code
// Step 1: Product Interface
interface Product {
void display();
}
// Step 2: Concrete Products
class Book implements Product {
@Override
public void display() {
System.out.println("Displaying Book");
}
}
class Pencil implements Product {
@Override
public void display() {
System.out.println("Displaying Pencil");
}
}
// Step 3: Factory
class ProductFactory {
public static Product createProduct(String type) {
if ("book".equalsIgnoreCase(type)) {
return new Book();
} else if ("pencil".equalsIgnoreCase(type)) {
return new Pencil();
}
throw new IllegalArgumentException("Invalid product type");
}
}
// Client Code
public class Client {
public static void main(String[] args) {
Product book = ProductFactory.createProduct("book");
book.display();
Product pencil = ProductFactory.createProduct("pencil");
pencil.display();
}
}
Code Explanation
Product Interface: The
Product
interface declares a methoddisplay()
that all concrete products must implement, ensuring a common interface for all products.Concrete Products: The
Book
andPencil
classes implement theProduct
interface, providing specific implementations for different types of products.Factory: The
ProductFactory
class encapsulates the object creation logic within a static methodcreateProduct()
. It determines the type of product to create based on the input and returns the corresponding product instance.
Advantages
Encapsulation: Encapsulates the object creation logic within a centralized factory, promoting loose coupling and encapsulation of object creation details.
Flexibility: Facilitates easy extension and modification of the object creation process, allowing the addition of new product types without modifying client code.
Simplicity: Provides a straightforward and intuitive mechanism for object creation, reducing complexity and enhancing code readability.
Centralized Control: Enables centralized control over the object creation process, ensuring consistent and standardized object creation across the application.
Disadvantages
Limited Extensibility: The Simple Factory pattern can become complex and less maintainable when dealing with a large number of product types or variations.
Violation of Open/Closed Principle: Introducing new product types requires modifying the factory class, violating the Open/Closed Principle of software design.
Tight Coupling: While promoting loose coupling between clients and products, the Simple Factory pattern can lead to tight coupling between the factory and concrete product classes.
When to Use
You need to encapsulate the object creation logic and provide a centralized mechanism for object creation.
The object creation process is subject to change or extension, and you want to isolate clients from the object creation details.
You want to promote loose coupling between clients and products by abstracting the object creation process.
Best Practices
Design the factory method to return interfaces or abstract types to ensure flexibility and adherence to the Dependency Inversion Principle.
Consider combining the Simple Factory pattern with other patterns, such as Factory Method or Abstract Factory, to enhance flexibility and extensibility.
Use meaningful names for product types and adhere to consistent naming conventions to enhance code clarity and maintainability.
By understanding the Simple Factory design pattern's principles, advantages, and considerations, developers can effectively utilize it to encapsulate object creation logic, promote loose coupling, and enhance maintainability in object-oriented systems.