创建型模式:②工厂方法模式(Factory Method)
c语言写法
核心思想
定义 “产品创建接口”(工厂),由具体工厂决定实例化哪个产品(解耦产品创建与使用)。
关键要点
1.用 函数指针 模拟 “多态”:不同产品实现不同的 run 方法,工厂通过统一接口创建。
2.解耦:客户端无需知道产品创建细节(如 create_car),只需通过工厂接口 create_product 获取产品。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// -------------------------- 产品接口(抽象产品)--------------------------
typedef struct Product {
void (*run)(struct Product*); // 产品行为(函数指针模拟多态)
char name[32];
void (*destroy)(struct Product*); // 销毁接口
} Product;
// 产品销毁通用实现
void product_destroy(Product* product) {
if (product != NULL) {
printf("销毁产品:%s\n", product->name);
free(product);
}
}
// -------------------------- 具体产品1:汽车 --------------------------
void car_run(Product* product) {
printf("产品[%s] 运行:高速行驶\n", product->name);
}
Product* create_car() {
Product* car = (Product*)malloc(sizeof(Product));
strncpy(car->name, "汽车", sizeof(car->name));
car->run = car_run; // 绑定具体行为
car->destroy = product_destroy;
return car;
}
// -------------------------- 具体产品2:自行车 --------------------------
void bike_run(Product* product) {
printf("产品[%s] 运行:人力踩踏\n", product->name);
}
Product* create_bike() {
Product* bike = (Product*)malloc(sizeof(Product));
strncpy(bike->name, "自行车", sizeof(bike->name));
bike->run = bike_run; // 绑定具体行为
bike->destroy = product_destroy;
return bike;
}
// -------------------------- 工厂接口(抽象工厂)--------------------------
typedef struct Factory {
Product* (*create_product)(void); // 产品创建接口
} Factory;
// -------------------------- 具体工厂 --------------------------
// 汽车工厂
Factory* create_car_factory() {
Factory* factory = (Factory*)malloc(sizeof(Factory));
factory->create_product = create_car; // 绑定汽车创建逻辑
return factory;
}
// 自行车工厂
Factory* create_bike_factory() {
Factory* factory = (Factory*)malloc(sizeof(Factory));
factory->create_product = create_bike; // 绑定自行车创建逻辑
return factory;
}
// 销毁工厂
void destroy_factory(Factory* factory) {
if (factory != NULL) {
free(factory);
}
}
// 测试代码
int main() {
// 1. 创建汽车工厂,生产汽车
Factory* car_factory = create_car_factory();
Product* car = car_factory->create_product();
car->run(car);
// 2. 创建自行车工厂,生产自行车
Factory* bike_factory = create_bike_factory();
Product* bike = bike_factory->create_product();
bike->run(bike);
// 3. 资源释放
car->destroy(car);
bike->destroy(bike);
destroy_factory(car_factory);
destroy_factory(bike_factory);
return 0;
}
c++语言写法
核心思想
用 抽象基类定义产品和工厂接口(纯虚函数),具体产品和具体工厂继承基类并实现接口,通过多态解耦产品创建与使用。
关键要点
1.多态核心:抽象基类(Product/Factory)定义纯虚函数,子类(Car/CarFactory)重写实现,客户端通过基类指针 / 引用调用。
2.智能指针:std::unique_ptr 自动管理内存,避免 C 语言手动 malloc/free 的内存泄漏风险。
3.开闭原则:新增产品(如摩托车)只需新增 Motorcycle 产品类和 MotorcycleFactory 工厂类,无需修改原有代码。
#include <iostream>
#include <string>
#include <memory> // 智能指针(自动管理内存)
// -------------------------- 抽象产品(Product)--------------------------
class Product {
public:
virtual ~Product() = default; // 虚析构:确保子类析构被调用
virtual void Run() const = 0; // 纯虚函数:产品统一接口
std::string GetName() const { return name_; }
protected:
Product(std::string name) : name_(std::move(name)) {} // 保护构造:禁止直接实例化
private:
std::string name_;
};
// -------------------------- 具体产品1:Car --------------------------
class Car : public Product {
public:
Car() : Product("汽车") {}
void Run() const override { // 重写接口
std::cout << "产品[" << GetName() << "] 运行:高速行驶" << std::endl;
}
};
// -------------------------- 具体产品2:Bike --------------------------
class Bike : public Product {
public:
Bike() : Product("自行车") {}
void Run() const override {
std::cout << "产品[" << GetName() << "] 运行:人力踩踏" << std::endl;
}
};
// -------------------------- 抽象工厂(Factory)--------------------------
class Factory {
public:
virtual ~Factory() = default;
virtual std::unique_ptr<Product> CreateProduct() = 0; // 纯虚函数:创建产品接口
};
// -------------------------- 具体工厂1:CarFactory --------------------------
class CarFactory : public Factory {
public:
std::unique_ptr<Product> CreateProduct() override {
return std::make_unique<Car>(); // 返回具体产品
}
};
// -------------------------- 具体工厂2:BikeFactory --------------------------
class BikeFactory : public Factory {
public:
std::unique_ptr<Product> CreateProduct() override {
return std::make_unique<Bike>();
}
};
// 测试代码
int main() {
// 1. 汽车工厂生产汽车
std::unique_ptr<Factory> car_factory = std::make_unique<CarFactory>();
std::unique_ptr<Product> car = car_factory->CreateProduct();
car->Run();
// 2. 自行车工厂生产自行车
std::unique_ptr<Factory> bike_factory = std::make_unique<BikeFactory>();
std::unique_ptr<Product> bike = bike_factory->CreateProduct();
bike->Run();
// 智能指针自动释放内存,无需手动delete
return 0;
}
工厂方法模式详解

被折叠的 条评论
为什么被折叠?



