C++工厂模式
作者:互联网
工厂模式
1. 简单工厂模式
#include <iostream>
using namespace std;
class Shoes
{
public:
virtual ~Shoes() {}
virtual void Show() = 0;
};
class LiNingShoes : public Shoes
{
public:
void Show() { cout << "LiNing" << endl; }
};
class AdidasShoes : public Shoes
{
public:
void Show() { cout << "Adidas" << endl; }
};
enum SHOES_TYPE
{
LINING,
ADIDAS
};
class ShoesFactory
{
public:
Shoes* CreateShoes(SHOES_TYPE type)
{
switch (type)
{
case LINING:
return new LiNingShoes();
break;
case ADIDAS:
return new AdidasShoes();
break;
default:
return NULL;
break;
}
}
};
int main()
{
ShoesFactory shoesFactory;
Shoes* pLiNingShoes = shoesFactory.CreateShoes(LINING);
if (pLiNingShoes != NULL) { pLiNingShoes->Show(); delete pLiNingShoes; }
Shoes* pAdidasShoes = shoesFactory.CreateShoes(ADIDAS);
if (pAdidasShoes != NULL) { pAdidasShoes->Show(); delete pAdidasShoes; }
}
2. 工厂方法模式
#include <iostream>
using namespace std;
class Shoes
{
public:
virtual ~Shoes() {}
virtual void Show() = 0;
};
class LiNingShoes : public Shoes
{
public:
void Show() { cout << "LiNing" << endl; }
};
class AdidasShoes : public Shoes
{
public:
void Show() { cout << "Adidas" << endl; }
};
class ShoesFactory
{
public:
virtual Shoes* CreateShoes() = 0;
virtual ~ShoesFactory() {}
};
class LiNingProducer : public ShoesFactory
{
public:
Shoes *CreateShoes() { return new LiNingShoes(); }
};
class AdidasProducer : public ShoesFactory
{
public:
Shoes *CreateShoes() { return new AdidasShoes(); }
};
int main()
{
ShoesFactory *pLiNingProducer = new LiNingProducer();
Shoes* pLiNingShoes = pLiNingProducer->CreateShoes();
pLiNingShoes->Show();
delete pLiNingShoes;
delete pLiNingProducer;
ShoesFactory *pAdidasProducer = new AdidasProducer();
Shoes* pAdidasShoes = pAdidasProducer->CreateShoes();
pAdidasShoes->Show();
delete pAdidasShoes;
delete pAdidasProducer;
}
3. 模板工厂
#include <iostream>
using namespace std;
class Shoes
{
public:
virtual ~Shoes() {}
virtual void Show() = 0;
};
class LiNingShoes : public Shoes
{
public:
void Show() { cout << "LiNing" << endl; }
};
class AdidasShoes : public Shoes
{
public:
void Show() { cout << "Adidas" << endl; }
};
template <class ConcreteProduct_t>
class ConcreteFactory
{
public:
ConcreteProduct_t* CreateShoes()
{
return new ConcreteProduct_t();
}
};
int main()
{
ConcreteFactory<LiNingShoes> LiNingFactory;
Shoes* pLiNingShoes = LiNingFactory.CreateShoes();
pLiNingShoes->Show();
delete pLiNingShoes;
ConcreteFactory<AdidasShoes> AdidasFactory;
Shoes* pAdidasShoes = AdidasFactory.CreateShoes();
pAdidasShoes->Show();
delete pAdidasShoes;
}
4. 自动注册工厂
#include <iostream>
#include <map>
#include <string>
#include <functional>
using namespace std;
class Shoes
{
public:
virtual ~Shoes() {}
virtual void Show() = 0;
};
class LiNingShoes : public Shoes
{
public:
void Show() { cout << "LiNing" << endl; }
};
class AdidasShoes : public Shoes
{
public:
int data;
AdidasShoes(int data) :data(data) {}
void Show() { cout << "Adidas " << data << endl; }
};
struct factory
{
template<typename T>
struct register_t
{
register_t(string key)
{
factory::get().map_.emplace(key, [] { return new T(); });
}
template<typename... Args>
register_t(string key, Args... args)
{
factory::get().map_.emplace(key, [args...] { return new T(args...); });
}
};
static Shoes* produce(string key)
{
if (map_.find(key) == map_.end())
throw std::invalid_argument("the key is not exist!");
return map_[key]();
}
private:
factory() {};
factory(const factory&) = delete;
factory(factory&&) = delete;
static factory& get() // 单例工厂
{
static factory instance;
return instance;
}
static map<string, function<Shoes* ()>> map_;
};
map<string, function<Shoes* ()>> factory::map_; // 不要忘记初始化私有static
// 以下##__VA_ARGS__表示可省略可变参数宏
#define REGISTER(T, key, ...) static factory::register_t<T> reg_msg_##T##_(key, ##__VA_ARGS__)
// 以下为注册
REGISTER(LiNingShoes, "LiNingShoes");
REGISTER(AdidasShoes, "AdidasShoes", 3);
int main()
{
Shoes* pLiNingShoes = factory::produce("LiNingShoes");
pLiNingShoes->Show();
Shoes* AdidasShoes = factory::produce("AdidasShoes");
AdidasShoes->Show();
}
标签:Shoes,Show,factory,模式,工厂,C++,key,pAdidasShoes,public 来源: https://www.cnblogs.com/xytpai/p/13829624.html