#include <iostream>
// Abstract Product
class AbstractProduct {
public:
virtual ~AbstractProduct() {}
virtual void operation() = 0;
};
// Concrete Product 1
class ConcreteProduct1 : public AbstractProduct {
public:
void operation() {
std::cout << "ConcreteProduct1 operation" << std::endl;
}
};
// Concrete Product 2
class ConcreteProduct2 : public AbstractProduct {
public:
void operation() {
std::cout << "ConcreteProduct2 operation" << std::endl;
}
};
// Abstract Factory
class AbstractFactory {
public:
virtual ~AbstractFactory() {}
virtual AbstractProduct* createProduct() = 0;
};
// Concrete Factory 1
class ConcreteFactory1 : public AbstractFactory {
public:
AbstractProduct* createProduct() {
return new ConcreteProduct1();
}
};
// Concrete Factory 2
class ConcreteFactory2 : public AbstractFactory {
public:
AbstractProduct* createProduct() {
return new ConcreteProduct2();
}
};
// Client
class Client {
private:
AbstractFactory* factory;
public:
Client(AbstractFactory* factory) : factory(factory) {}
void executeOperation() {
AbstractProduct* product = factory->createProduct();
product->operation();
}
};
// Usage
int main() {
AbstractFactory* factory1 = new ConcreteFactory1();
Client* client1 = new Client(factory1);
client1->executeOperation();
AbstractFactory* factory2 = new ConcreteFactory2();
Client* client2 = new Client(factory2);
client2->executeOperation();
delete factory1;
delete client1;
delete factory2;
delete client2;
return 0;
}