//产品系列1 class Car { public: Car(string name) : _name(name) {} virtual void show() = 0; protected: string _name; }; class BMW : public Car { public: BMW(string name) : Car(name) {} void show() { cout << "获取了一辆宝马汽车:" << _name << endl; } }; class Audi : public Car { public: Audi(string name) : Car(name) {} void show() { cout << "获取了一辆奥迪汽车:" << _name << endl; } }; //产品系列2 class Light { public: virtual void show() = 0; }; class BMWLight : public Light { public: void show() { cout << "BMW Light!" << endl; } }; class AudiLight : public Light { public: void show() { cout << "Audi light!" << endl; } }; //抽象工厂(对一组关联关系的产品簇提供产品对象的统一创建) class AbstractFactory { public: virtual Car *createCar(string name) = 0;//工厂方法 创建汽车 virtual Light *createCarLight() = 0;//工厂方法 创建汽车关联的产品 车灯 }; class BMWFactory : public AbstractFactory { public: Car* createCar(string name) { return new BMW(name); } Light *createCarLight() { return new BMWLight(); } }; class AudiFactory : public AbstractFactory { public: Car* createCar(string name) { return new Audi(name); } Light *createCarLight() { return new AudiLight(); } }; int main() { //现在考虑产品 一类产品(有关联关系的系列产品) unique_ptr<AbstractFactory> bmwfty(new BMWFactory()); unique_ptr<AbstractFactory> audifty(new AudiFactory()); unique_ptr<Car> p1(bmwfty->createCar("X6")); unique_ptr<Car> p2(audifty->createCar("A8")); unique_ptr<Light> l1(bmwfty->createCarLight()); unique_ptr<Light> l2(audifty->createCarLight()); p1->show(); p2->show(); l1->show(); l2->show(); return 0; }