引自:https://blog.csdn.net/weixin_43795921/article/details/127224633
template
class Factory {
public:
bool Register(const IdentifierType &id, ProductCreator creator) {
return producers_.insert(std::make_pair(id, creator)).second;
}
bool Unregister(const IdentifierType &id) {
return producers_.erase(id) == 1;
}
void Clear() { producers_.clear(); }
bool Empty() const { return producers_.empty(); }
template
std::unique_ptr CreateObjectOrNull(const IdentifierType &id,
Args &&... args) {
auto id_iter = producers_.find(id);
if (id_iter != producers_.end()) {
return std::unique_ptr(
(id_iter->second)(std::forward(args)...));
}
return nullptr;
}
template
std::unique_ptr CreateObject(const IdentifierType &id,
Args &&... args) {
auto result = CreateObjectOrNull(id, std::forward(args)...);
PUB_E_IF(!result, "Factory could not create Object of type : " IShape*{return new CircleShape("color")};
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |