#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string.h>
using namespace std;
class Fruit{
public:
virtual void getFruit() = 0;
};
class Apple : public Fruit {
public:
void getFruit()
{
cout << "我是苹果" << endl;
}
};
class Bannar : public Fruit {
public:
void getFruit()
{
cout << "我是香蕉" << endl;
}
};
class AbFactory {
public:
virtual Fruit* CreateProduct() = 0;
};
class appleFactory : public AbFactory{
public:
Fruit * CreateProduct()
{
return new Apple();
}
};
int main(void)
{
AbFactory *aft = new appleFactory();
Fruit * ft = aft->CreateProduct();
ft->getFruit();
system("pause");
return EXIT_SUCCESS;
}
C++设计模式之工厂模式
最新推荐文章于 2023-06-23 20:31:10 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)