Подробнее на 👉 refactoring.guru
В примере создаем Абстрактный Продукт - автомобили, которые потом будут реализованы как Седаны и Спортивные. Также создаем Абстрактную Фабрику, которая будет реализована в виде производителей Toyota и Ford.
#include <iostream>
// Абстрактный класс для типов автомобилей
class Car {
protected:
std::string manufacturer;
public:
Car(std::string iManufacturer) : manufacturer(iManufacturer) {}
virtual void info() = 0;
};
class Sedan : public Car {
public:
Sedan(std::string iManufacturer) : Car(iManufacturer) {}
void info() override {
std::cout << "This is a Sedan car - " << manufacturer << std::endl;
}
};
class Sportcar : public Car {
public:
Sportcar(std::string iManufacturer) : Car(iManufacturer) {}
void info() override {
std::cout << "This is a Sport car - " << manufacturer << std::endl;
}
};
// Абстрактная фабрика
class CarFactory {
public:
virtual Car* createBudgetCar() = 0;
virtual Car* createPremiumCar() = 0;
};
// Фабрика для создания автомобилей Toyota
class ToyotaFactory : public CarFactory {
public:
Car* createBudgetCar() override { return new Sedan("Toyota"); }
Car* createPremiumCar() override { return new Sportcar("Toyota"); }
};
// Фабрика для создания автомобилей Ford
class FordFactory : public CarFactory {
public:
Car* createBudgetCar() override { return new Sedan("Ford"); }
Car* createPremiumCar() override { return new Sportcar("Ford"); }
};
int main() {
// Создание фабрик
CarFactory* toyotaFactory = new ToyotaFactory();
CarFactory* fordFactory = new FordFactory();
// Создание автомобилей с помощью фабрик
Car* toyotaBudgetCar = toyotaFactory->createBudgetCar();
Car* toyotaPremiumCar = toyotaFactory->createPremiumCar();
Car* fordBudgetCar = fordFactory->createBudgetCar();
Car* fordPremiumCar = fordFactory->createPremiumCar();
// Вывод информации о созданных автомобилях
toyotaBudgetCar->info();
toyotaPremiumCar->info();
fordBudgetCar->info();
fordPremiumCar->info();
// Освобождение памяти
delete toyotaBudgetCar;
delete toyotaPremiumCar;
delete fordBudgetCar;
delete fordPremiumCar;
delete toyotaFactory;
delete fordFactory;
return 0;
}
Будет напечатано:
This is a Sedan car - Toyota This is a Sport car - Toyota This is a Sedan car - Ford This is a Sport car - Ford
// Абстрактный класс для типов автомобилей
abstract class Car {
String manufacturer;
Car(this.manufacturer);
void info();
}
class Sedan extends Car {
Sedan(String manufacturer) : super(manufacturer);
void info() {
print('This is a Sedan car - $manufacturer');
}
}
class Sportcar extends Car {
Sportcar(String manufacturer) : super(manufacturer);
void info() {
print('This is a Sport car - $manufacturer');
}
}
// Абстрактная фабрика
abstract class CarFactory {
Car createBudgetCar();
Car createPremiumCar();
}
// Фабрика для создания автомобилей Toyota
class ToyotaFactory extends CarFactory {
Car createBudgetCar() {
return Sedan('Toyota');
}
Car createPremiumCar() {
return Sportcar('Toyota');
}
}
// Фабрика для создания автомобилей Ford
class FordFactory extends CarFactory {
Car createBudgetCar() {
return Sedan('Ford');
}
Car createPremiumCar() {
return Sportcar('Ford');
}
}
void main() {
CarFactory toyotaFactory = ToyotaFactory();
CarFactory fordFactory = FordFactory();
Car toyotaBudgetCar = toyotaFactory.createBudgetCar();
Car toyotaPremiumCar = toyotaFactory.createPremiumCar();
Car fordBudgetCar = fordFactory.createBudgetCar();
Car fordPremiumCar = fordFactory.createPremiumCar();
toyotaBudgetCar.info();
toyotaPremiumCar.info();
fordBudgetCar.info();
fordPremiumCar.info();
}
Будет напечатано:
This is a Sedan car - Toyota This is a Sport car - Toyota This is a Sedan car - Ford This is a Sport car - Ford