工厂模式是一种创建对象的设计模式, 旨在将对象创建与使用分离
简单工厂
创建对象逻辑集中在一个工厂类中
#include <iostream>
#include <unordered_map>
#include <functional>
#include <memory>
// 产品基类
class Product {
public:
virtual ~Product() = default;
virtual void DoUse() = 0;
};
class ConcreteProductA : public Product {
public:
ConcreteProductA() { std::cout << "ConcreteProductA" << std::endl; };
void DoUse() override { std::cout << "ConcreteProductA DoUse" << std::endl; }
};
class ConcreteProductB : public Product {
public:
ConcreteProductB() { std::cout << "ConcreteProductB" << std::endl; };
void DoUse() override { std::cout << "ConcreteProductB DoUse" << std::endl; }
};
// 工厂类
class SimpleFactory {
public:
static std::shared_ptr<Product> CreateProduct(const char type) {
std::unordered_map<char, std::function<std::shared_ptr<Product>()> > products = {
{'A', []() -> std::shared_ptr<Product> { return std::make_shared<ConcreteProductA>(); } },
{'B', []() -> std::shared_ptr<Product> { return std::make_shared<ConcreteProductB>(); } },
};
return products[type]();
}
};
int main() {
std::shared_ptr<Product> productA = SimpleFactory::CreateProduct('A');
productA->DoUse();
std::shared_ptr<Product> productB = SimpleFactory::CreateProduct('B');
productB->DoUse();
return 0;
}
工厂方法
定义一个创建对象接口, 让子类决定实例化哪个类
class Product {
public:
virtual ~Product() = default;
virtual void DoUse() = 0;
};
class Creator {
public:
virtual Product* DoFactoryMethod() = 0;
};
class ConcreteCreatorA : public Creator {
public:
Product* DoFactoryMethod() override {
return new ConcreteProductA();
}
};
class ConcreteCreatorB : public Creator {
public:
Product* DoFactoryMethod() override {
return new ConcreteProductB();
}
};
抽象工厂
提供一个创建一系列相关或相互依赖对象接口
class Product {
public:
virtual ~Product() = default;
virtual void DoUse() = 0;
};
class ConcreteProductA : public Product {
public:
ConcreteProductA() { std::cout << "ConcreteProductA" << std::endl; };
void DoUse() override { std::cout << "ConcreteProductA DoUse" << std::endl; }
};
class ConcreteProductB : public Product {
public:
ConcreteProductB() { std::cout << "ConcreteProductB" << std::endl; };
void DoUse() override { std::cout << "ConcreteProductB DoUse" << std::endl; }
};
class AbstractFactory {
public:
virtual ~AbstractFactory() = default;
virtual Product* DoCreateProductA() = 0;
virtual Product* DoCreateProductB() = 0;
};
class ConcreteFactory1 : public AbstractFactory {
public:
static Product* DoCreateProductA() override {
return new ConcreteProductA();
}
static Product* DoCreateProductB() override {
return new ConcreteProductB();
}
};
class ConcreteFactory2 : public AbstractFactory {
public:
static Product* DoCreateProductA() override {
return new ConcreteProductB();
}
static Product* DoCreateProductB() override {
return new ConcreteProductA();
}
};
这些模式区别在于简单工厂集中管理, 工厂方法提供灵活性, 而抽象工厂则处理多个产品族
模板工厂
通过模板参数 Factory 类可以生成任意类型对象, 而无需修改工厂类代码, 当需要添加新产品类型时, 只需要定义新产品类,并传入到 Factory 类中即可
此模式扩展性较好, 添加新产品不会影响现有代码结构
#include <iostream>
#include <memory>
class Product {
public:
virtual void DoUse() const = 0;
virtual ~Product() = default;
};
class ProductA : public Product {
public:
void DoUse() const override { std::cout << "ProductA created!" << std::endl; }
};
class ProductB : public Product {
public:
void DoUse() const override { std::cout << "ProductB created!" << std::endl; }
};
template<class T>
class Factory {
public:
static std::shared_ptr<Product> Create() {
return std::make_shared<T>();
}
};
int main() {
auto productA = Factory<ProductA>::Create();
productA->DoUse();
auto productB = Factory<ProductB>::Create();
productB->DoUse();
return 0;
}