// Abstract Factory
interface AbstractFactory {
AbstractProduct createProduct();
}
// Concrete Factory 1
class ConcreteFactory1 implements AbstractFactory {
public AbstractProduct createProduct() {
return new ConcreteProduct1();
}
}
// Concrete Factory 2
class ConcreteFactory2 implements AbstractFactory {
public AbstractProduct createProduct() {
return new ConcreteProduct2();
}
}
// Abstract Product
interface AbstractProduct {
void operation();
}
// Concrete Product 1
class ConcreteProduct1 implements AbstractProduct {
public void operation() {
System.
out.
println("ConcreteProduct1 operation"); }
}
// Concrete Product 2
class ConcreteProduct2 implements AbstractProduct {
public void operation() {
System.
out.
println("ConcreteProduct2 operation"); }
}
// Client
class Client {
private AbstractFactory factory;
public Client(AbstractFactory factory) {
this.factory = factory;
}
public void executeOperation() {
AbstractProduct product = factory.createProduct();
product.operation();
}
}
// Usage
public class Main {
public static void main
(String[] args
) { AbstractFactory factory1 = new ConcreteFactory1();
Client client1 = new Client(factory1);
client1.executeOperation();
AbstractFactory factory2 = new ConcreteFactory2();
Client client2 = new Client(factory2);
client2.executeOperation();
}
}