例子:家具购买
想象你打算为你的新家购买一整套家具,包括沙发、茶几和椅子。你有两种风格可选:维多利亚风格和现代风格。每种风格的家具都有特定的设计和特点。
抽象工厂模式如何应用?
-
抽象工厂接口:定义一个
FurnitureFactory
接口,用于创建一系列相关的家具对象(沙发、茶几、椅子)。 -
具体工厂:为每种风格的家具实现具体的工厂类,如
VictorianFurnitureFactory
和ModernFurnitureFactory
。 -
抽象产品接口:定义抽象产品接口
Sofa
、CoffeeTable
和Chair
。 -
具体产品:每种风格的具体产品实现这些接口,如
VictorianSofa
、ModernChair
等。
实际应用示例
- 抽象产品接口:
class Sofa {
public:
virtual void sit() = 0;
};
class CoffeeTable {
public:
virtual void placeItems() = 0;
};
class Chair {
public:
virtual void sit() = 0;
};
- 具体产品实现:
class VictorianSofa : public Sofa {
public:
void sit() override {
// 维多利亚风格的沙发
std::cout << "Sitting on a Victorian Sofa." << std::endl;
}
};
class ModernSofa : public Sofa {
public:
void sit() override {
// 现代风格的沙发
std::cout << "Sitting on a Modern Sofa." << std::endl;
}
};
class VictorianCoffeeTable : public CoffeeTable {
public:
void placeItems() override {
// 维多利亚风格的茶几
std::cout << "Placing items on a Victorian Coffee Table." << std::endl;
}
};
class ModernCoffeeTable : public CoffeeTable {
public:
void placeItems() override {
// 现代风格的茶几
std::cout << "Placing items on a Modern Coffee Table." << std::endl;
}
};
// 类似地,定义 VictorianChair 和 ModernChair
- 抽象工厂接口:
class FurnitureFactory {
public:
virtual Sofa* createSofa() = 0;
virtual CoffeeTable* createCoffeeTable() = 0;
virtual Chair* createChair() = 0;
};
- 具体工厂实现:
class VictorianFurnitureFactory : public FurnitureFactory {
public:
Sofa* createSofa() override {
return new VictorianSofa();
}
CoffeeTable* createCoffeeTable() override {
return new VictorianCoffeeTable();
}
Chair* createChair() override {
return new VictorianChair();
}
};
class ModernFurnitureFactory : public FurnitureFactory {
public:
Sofa* createSofa() override {
return new ModernSofa();
}
CoffeeTable* createCoffeeTable() override {
return new ModernCoffeeTable();
}
Chair* createChair() override {
return new ModernChair();
}
};
- 客户端代码:
void createFurniture(FurnitureFactory* factory) {
Sofa* sofa = factory->createSofa();
CoffeeTable* coffeeTable = factory->createCoffeeTable();
Chair* chair = factory->createChair();
sofa->sit();
coffeeTable->placeItems();
chair->sit();
// 记得释放分配的内存
delete sofa;
delete coffeeTable;
delete chair;
}
int main() {
FurnitureFactory* victorianFactory = new VictorianFurnitureFactory();
createFurniture(victorianFactory);
delete victorianFactory;
FurnitureFactory* modernFactory = new ModernFurnitureFactory();
createFurniture(modernFactory);
delete modernFactory;
return 0;
}
完整代码:
#include <iostream>
// 抽象产品接口
class Sofa {
public:
virtual void sit() = 0;
virtual ~Sofa() = default; // 加入虚析构函数
};
class CoffeeTable {
public:
virtual void placeItems() = 0;
virtual ~CoffeeTable() = default; // 加入虚析构函数
};
class Chair {
public:
virtual void sit() = 0;
virtual ~Chair() = default; // 加入虚析构函数
};
// 具体产品实现
class VictorianSofa : public Sofa {
public:
void sit() override {
std::cout << "Sitting on a Victorian Sofa." << std::endl;
}
};
class ModernSofa : public Sofa {
public:
void sit() override {
std::cout << "Sitting on a Modern Sofa." << std::endl;
}
};
class VictorianCoffeeTable : public CoffeeTable {
public:
void placeItems() override {
std::cout << "Placing items on a Victorian Coffee Table." << std::endl;
}
};
class ModernCoffeeTable : public CoffeeTable {
public:
void placeItems() override {
std::cout << "Placing items on a Modern Coffee Table." << std::endl;
}
};
class VictorianChair : public Chair {
public:
void sit() override {
std::cout << "Sitting on a Victorian Chair." << std::endl;
}
};
class ModernChair : public Chair {
public:
void sit() override {
std::cout << "Sitting on a Modern Chair." << std::endl;
}
};
// 抽象工厂接口
class FurnitureFactory {
public:
virtual Sofa* createSofa() = 0;
virtual CoffeeTable* createCoffeeTable() = 0;
virtual Chair* createChair() = 0;
virtual ~FurnitureFactory() = default; // 加入虚析构函数
};
// 具体工厂实现
class VictorianFurnitureFactory : public FurnitureFactory {
public:
Sofa* createSofa() override {
return new VictorianSofa();
}
CoffeeTable* createCoffeeTable() override {
return new VictorianCoffeeTable();
}
Chair* createChair() override {
return new VictorianChair();
}
};
class ModernFurnitureFactory : public FurnitureFactory {
public:
Sofa* createSofa() override {
return new ModernSofa();
}
CoffeeTable* createCoffeeTable() override {
return new ModernCoffeeTable();
}
Chair* createChair() override {
return new ModernChair();
}
};
// 客户端代码
void createFurniture(FurnitureFactory* factory) {
Sofa* sofa = factory->createSofa();
CoffeeTable* coffeeTable = factory->createCoffeeTable();
Chair* chair = factory->createChair();
sofa->sit();
coffeeTable->placeItems();
chair->sit();
// 记得释放分配的内存
delete sofa;
delete coffeeTable;
delete chair;
}
int main() {
FurnitureFactory* victorianFactory = new VictorianFurnitureFactory();
createFurniture(victorianFactory);
delete victorianFactory;
std::cout << std::endl;
FurnitureFactory* modernFactory = new ModernFurnitureFactory();
createFurniture(modernFactory);
delete modernFactory;
return 0;
}
总结
通过使用抽象工厂模式,客户端代码可以在不修改具体家具实现的情况下,创建一整套以相同风格设计的家具。这样,如果将来你决定换成另一种风格,比如中世纪风格,你只需要新增一个 MedievalFurnitureFactory
,而无需改变客户端代码。这样,代码就变得更加灵活和易扩展。