#include <iostream>
#include <memory>
class Product
{
public:
virtual void show() = 0;
};
class ProductA : public Product
{
public:
void show()
{
std::cout << "Product A" << std::endl;
}
};
class ProductB : public Product
{
public:
void show()
{
std::cout << "Product B" << std::endl;
}
};
class Factory
{
public:
virtual std::shared_ptr<Product> creat_product() = 0;
};
class FactoryA : public Factory
{
public:
std::shared_ptr<Product> creat_product()
{
return std::make_shared<ProductA>();
}
};
class FactoryB : public Factory
{
public:
std::shared_ptr<Product> creat_product()
{
return std::make_shared<ProductB>();
}
};
int main()
{
FactoryA var_1;
FactoryB var_2;
std::shared_ptr<Product> ptr_1 = var_1.creat_product();
ptr_1->show();
std::shared_ptr<Product> ptr_2 = var_2.creat_product();
ptr_2->show();
return 0;
}
工厂方法模式
最新推荐文章于 2023-07-16 13:58:47 发布