抽象工厂模式——C++实现

使用抽象工厂模式,完成下述产品等级结构:

c++代码:

#include<iostream>
using namespace std;
//抽象产品类——Man类
class Man{
public:
       virtual void show() = 0;
};
//抽象产品类——Woman类
class Woman{
public:
       virtual void show() = 0;
};
//具体产品类——YellowMan类
class YellowMan : public Man{
public:
       virtual void show(){
             cout << "黄皮肤的男性" << endl;
       }
};
//具体产品类——BlackMan类
class BlackMan : public Man{
public:
       virtual void show(){
              cout << "黑皮肤的男性" << endl;
       }
};
//具体产品类——WhiteMan类
class WhiteMan : public Man{
public:
       virtual void show(){
              cout << "白皮肤的男性" << endl;
       }
};
//具体产品类——YellowWoman类
class YellowWoman : public Woman{
public:
       virtual void show(){
              cout << "黄皮肤的女性" << endl;
       }
};
//具体产品类——BlackWoman类
class BlackWoman : public Woman{
public:
       virtual void show(){
              cout << "黑皮肤的女性" << endl;
       }
};
//具体产品类——WhiteWoman类
class WhiteWoman : public Woman{
public:
       virtual void show(){
              cout << "白皮肤的女性" << endl;
       }
};
//抽象工厂类——AbstractFactory类
class AbstractFactory{
public:
       virtual Man* produceMan() = 0;
       virtual Woman* produceWoman() = 0;
};
//具体工厂类——YellowFactory类
class YellowFactory : public AbstractFactory{
public:
       virtual Man* produceMan(){
              return new YellowMan();
       }
       virtual Woman* produceWoman(){
                     return new YellowWoman();
       }
};
//具体工厂类——BlackFactory类
class BlackFactory : public AbstractFactory{
public:
       virtual Man* produceMan(){
              return new BlackMan();
       }
       virtual Woman* produceWoman(){
                     return new BlackWoman();
       }
};
//具体工厂类——WhiteFactory类
class WhiteFactory : public AbstractFactory{
public:
       virtual Man* produceMan(){
              return new WhiteMan();
       }
       virtual Woman* produceWoman(){
                     return new WhiteWoman();
       }
};
//测试类——test类
void test(){
       //黄皮肤工厂
       AbstractFactory* factory = NULL;
       factory = new YellowFactory;
       Man* man = factory->produceMan();
       man->show();
       delete man;
       man = NULL;
        
       Woman* woman = factory->produceWoman();
       woman->show();
       delete woman;
       woman = NULL;
       delete factory;
       factory = NULL;
       cout<<"*************************"<<endl;
 
       //黑皮肤工厂
       factory = new BlackFactory;
       man = factory->produceMan();
       man->show();
       delete man;
       man = NULL;
        
       woman = factory->produceWoman();
       woman->show();
       delete woman;
       woman = NULL;
       delete factory;
       factory = NULL;
       cout<<"*************************"<<endl;
 
       //白皮肤工厂
       factory = new WhiteFactory;
       man = factory->produceMan();
       man->show();
       delete man;
       man = NULL;
        
       woman = factory->produceWoman();
       woman->show();
       delete woman;
       woman = NULL;
       delete factory;
       factory = NULL;
}
int main(){
       test();
       return 0;
}

运行结果:

 

posted @ 2022-09-26 00:04  好(justice)……  阅读(9)  评论(0编辑  收藏  举报