简单工厂模式适用于下列场景:
1所有产品都已确定下来,不再添加新产品也不删除旧产品(如果需要频繁添加或删除产品推荐使用工厂方法模式)
2所有产品都属于同一类别(不分系列,这里博主犯了一个错误,如产品分多个系列,推荐使用抽象工厂模式)
3产品种类基数不大,否则工厂类将会很臃肿(个人觉得这一点相比前两点对开发影响较小)
简单工厂模式代码:
#include <iostream>
#include <vector>
#include <utility>
#include <random>
using namespace std;
const string s_dynastic = "dynastic";
const string s_ocean = "ocean";
const string dyna_arr[5] = {"QING","HAN","TANG","SONG","YUAN"};
const string ocean_arr[3] = {"SEAL","DOLPHIN","SEAGULL"};
const string color_arr[7] = {"red","black","yellow","golden","sliver","blue","white"};
typedef enum carTypeDynastic
{
carty_qing,
carty_han,
carty_tang,
carty_song,
carty_yuan
}carTyDy;
typedef enum carTypeOcean
{
carty_seal,
carty_dolplin,
carty_sea_gull
}carTySea;
class bydCar
{
public:
virtual void type() = 0;
};
class bydQing:public bydCar
{
public:
std::string series;
std::string type_name;
std::string color;
bydQing(std::string s, std::string t) : bydCar(),series(s),type_name(t)
{
int index = rand()%7;
color = color_arr[index];
}
void type() override
{
std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
}
};
class bydHan:public bydCar
{
public:
std::string series;
std::string type_name;
std::string color;
bydHan(std::string s, std::string t) : bydCar(),series(s),type_name(t)
{
int index = rand()%7;
color = color_arr[index];
}
void type() override
{
std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
}
};
class bydTang:public bydCar
{
public:
std::string series;
std::string type_name;
std::string color;
bydTang(std::string s, std::string t) : bydCar(),series(s),type_name(t)
{
int index = rand()%7;
color = color_arr[index];
}
void type() override
{
std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
}
};
class bydSong:public bydCar
{
public:
std::string series;
std::string type_name;
std::string color;
bydSong(std::string s, std::string t) : bydCar(),series(s),type_name(t)
{
int index = rand()%7;
color = color_arr[index];
}
void type() override
{
std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
}
};
class bydYuan:public bydCar
{
public:
std::string series;
std::string type_name;
std::string color;
bydYuan(std::string s, std::string t) : bydCar(),series(s),type_name(t)
{
int index = rand()%7;
color = color_arr[index];
}
void type() override
{
std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
}
};
class bydSeal:public bydCar
{
public:
std::string series;
std::string type_name;
std::string color;
bydSeal(std::string s, std::string t) : bydCar(),series(s),type_name(t)
{
int index = rand()%7;
color = color_arr[index];
}
void type() override
{
std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
}
};
class bydDolphin:public bydCar
{
public:
std::string series;
std::string type_name;
std::string color;
bydDolphin(std::string s, std::string t) : bydCar(),series(s),type_name(t)
{
int index = rand()%7;
color = color_arr[index];
}
void type() override
{
std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
}
};
class bydSeaGull:public bydCar
{
public:
std::string series;
std::string type_name;
std::string color;
bydSeaGull(std::string s, std::string t) : bydCar(),series(s),type_name(t)
{
int index = rand()%7;
color = color_arr[index];
}
void type() override
{
std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
}
};
class bydEquipmentFactory
{
public:
//根据产品信息创建具体的产品类实例,返回一个抽象产品类
static bydCar* creatDynastic(carTypeDynastic car_type)
{
switch(car_type)
{
case carTypeDynastic::carty_han:
return new bydHan(s_dynastic,"HAN");
case carTypeDynastic::carty_qing:
return new bydQing(s_dynastic,"QING");
case carTypeDynastic::carty_tang:
return new bydTang(s_dynastic,"TANG");
case carTypeDynastic::carty_song:
return new bydSong(s_dynastic,"SONG");
case carTypeDynastic::carty_yuan:
return new bydYuan(s_dynastic,"YUAN");
default:
return nullptr;
}
}
static bydCar* creatOcean(carTypeOcean car_type)
{
switch(car_type)
{
case carTypeOcean::carty_seal:
return new bydSeal(s_dynastic,"SEAL");
case carTypeOcean::carty_dolplin:
return new bydDolphin(s_dynastic,"DOLPHIN");
case carTypeOcean::carty_sea_gull:
return new bydSeaGull(s_dynastic,"SEAGULL");
default:
return nullptr;
}
}
};
int main()
{
time(0);
std::pair<carTypeOcean, int > ocean_indent[3] = { std::pair<carTypeOcean , int > (carTypeOcean::carty_seal,15) ,
std::pair<carTypeOcean , int > (carTypeOcean::carty_dolplin,10) ,
std::pair<carTypeOcean , int > (carTypeOcean::carty_sea_gull,3)};
std::vector<bydCar*> products;
for(int i = 0 ; i < 3 ; i ++)
{
int cnt = ocean_indent[i].second;
for(int j = 0 ; j < cnt ; j ++)
{
bydCar*car = bydEquipmentFactory::creatOcean(ocean_indent[i].first);
car->type();
products.push_back(car);
}
}
}
总结:
优点 在调用创建产品接口时候,调用者不需要关心创建细节
缺点 1在产品种类较多时候,工厂类将会显得非常臃肿
2 调用者需要添加新产品时候,工厂类的设计着需要修改工厂类的模型