Abstract Factory Pattern

Category : Design Patterns | Sub Category : Creational design patterns | By Prasad Bonam Last updated: 2023-07-09 07:03:30 Viewed : 341


Abstract Factory Pattern:

The Abstract Factory pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It allows the client code to work with abstract interfaces, making it independent of the specific classes that implement those interfaces. Here is an example of implementing the Abstract Factory pattern in Java:

java
// Abstract Product A
interface ProductA { void operationA(); } // Concrete Product A1 class ConcreteProductA1 implements ProductA { @Override public void operationA() { System.out.println("Concrete Product A1 operation A"); } } // Concrete Product A2 class ConcreteProductA2 implements ProductA { @Override public void operationA() { System.out.println("Concrete Product A2 operation A"); } } // Abstract Product B interface ProductB { void operationB(); } // Concrete Product B1 class ConcreteProductB1 implements ProductB { @Override public void operationB() { System.out.println("Concrete Product B1 operation B"); } } // Concrete Product B2 class ConcreteProductB2 implements ProductB { @Override public void operationB() { System.out.println("Concrete Product B2 operation B"); } } // Abstract Factory interface AbstractFactory { ProductA createProductA(); ProductB createProductB(); } // Concrete Factory 1 class ConcreteFactory1 implements AbstractFactory { @Override public ProductA createProductA() { return new ConcreteProductA1(); } @Override public ProductB createProductB() { return new ConcreteProductB1(); } } // Concrete Factory 2 class ConcreteFactory2 implements AbstractFactory { @Override public ProductA createProductA() { return new ConcreteProductA2(); } @Override public ProductB createProductB() { return new ConcreteProductB2(); } } // Client code public class Client { private ProductA productA; private ProductB productB; public Client(AbstractFactory factory) { productA = factory.createProductA(); productB = factory.createProductB(); } public void executeOperations() { productA.operationA(); productB.operationB(); } public static void main(String[] args) { // Create and use products from Concrete Factory 1 AbstractFactory factory1 = new ConcreteFactory1(); Client client1 = new Client(factory1); client1.executeOperations(); // Create and use products from Concrete Factory 2 AbstractFactory factory2 = new ConcreteFactory2(); Client client2 = new Client(factory2); client2.executeOperations(); } }

In this example, we have two families of products: ProductA and ProductB. Each product family has multiple concrete implementations (ConcreteProductA1, ConcreteProductA2, ConcreteProductB1, ConcreteProductB2).

The AbstractFactory interface declares abstract factory methods for creating ProductA and ProductB objects. The concrete factories (ConcreteFactory1 and ConcreteFactory2) implement this interface and provide concrete implementations for creating the products.

The Client class represents the client code that works with the abstract product interfaces (ProductA and ProductB) through the abstract factory interface (AbstractFactory). The client code is decoupled from the concrete product classes and factories, allowing it to work with different families of products by simply changing the concrete factory used.

In the main() method, we create instances of the concrete factories (ConcreteFactory1 and ConcreteFactory2) and pass them to the Client constructor. The Client then uses the created factories to create and work with the products (ProductA and ProductB) without being aware of their concrete classes.

The Abstract Factory pattern enables the creation of families of related products and provides an easy way to switch between different implementations of those product families.


Search
Related Articles

Leave a Comment: