c++11设计模式 工厂模式 泛型抽象

当程序中许多的类需要使用共同的接口时,可以考虑工厂模式。传统的抽象工厂实现先定义用于创建不同产品的接口, 但将实际的创建工作留给了派生的具体工厂类。 每个工厂类型都对应派生出的不同产品。本文的工厂模式可以实现不同抽象工厂的异类集合,避免了定义多个工厂类。并且可以传递不同参数来构造生产不同类型的产品。

#include <memory>
#include <map>
#include <unordered_map>
#include <iostream>

template<typename AbsProduct, typename ProductKey=std::string, typename...Args>
class Factory {
 public:
    static Factory &GetInstance() {
        static Factory factory;
        return factory;
    }

    template<typename Product>
    void Register(const ProductKey& key) {
        factory_map_.emplace(key, [](Args...args)->AbsProduct*{return new Product(args...);});
    }

    std::shared_ptr<AbsProduct> MakeProduct(const ProductKey& key, Args&&...args) {
        if (factory_map_.find(key) != factory_map_.end()) {
            return std::shared_ptr<AbsProduct>(factory_map_[key](args...));
        } else {
            return std::shared_ptr<AbsProduct>();
        }
    }

 private:
    using CreatProduct = std::function<AbsProduct*(Args...)>;
    std::unordered_map<ProductKey, CreatProduct> factory_map_;
};

Factory类有三个模板参数,分别是
AbsProduct
抽象产品类,通过传递不同的抽象产品类可以获取对应的抽象工厂类,从而不用像传统的抽象工厂实现中需要定义不同的抽象工厂。

ProductKey
具体产品的键值,用于表示不同的具体产品。默认为std::string。

…Args
可变参数,由于不用的具体产品构造函数的参数可能不相同,可以通过可变参数传递参数来构造。

成员:
CreatProduct
表示具体产品的构造方法。

factory_map_
是一个unordered_map,用于保存具体产品的构造方法,通过ProductKey区别。

GetInstance
用于获取Factory单例。

Register
用于注册具体的产品,它的模板参数Product即为具体产品类型,函数参数ProductKey是具体产品的标识,这里作为factory_map_的key,value是lambda表达式,用来创建具体产品。

MakeProduct
用于创建具体的产品类型对象,为了避免内存泄露问题,这里使用智能指针进行返回。函数的参数ProductKey是具体产品的键值,Args是可变参数,用于传递参数给具体产品的构造函数。先搜索factory_map_中是否已经保存了具体产品的构造方法,存在则直接调用对应lamda表达式构造;没有则直接构造一个抽象类对象,最后保存到智能指针中返回。


测试:

class AbsProductA {
 public:
    AbsProductA() {}
    virtual void Show()=0;

 protected:
    int a_;
    int b_;
};

class PA1 : public AbsProductA {
 public:
    PA1(int a) { a_ = a; };
    void Show() {std::cout << "PA1" << std::endl;};
};

class PA2 : public AbsProductA {
 public:
    PA2(int b) { b_ = b; };
    void Show() {std::cout << "PA2" << std::endl;};
};

class AbsProductB {
 public:
    AbsProductB() {}
    virtual void Show()=0;

 protected:
    std::string str_;
};

class PB1 : public AbsProductB {
 public:
    PB1(std::string str) { str = str_; };
    void Show() {std::cout << "PB1" << std::endl;};
};


int main() {
    std::string pa1_key = "pa1";
    std::string pa2_key = "pa2";
    auto factory_a = Factory<AbsProductA, std::string, int>::GetInstance();
    factory_a.Register<PA1>(pa1_key);
    factory_a.Register<PA2>(pa2_key);
    auto pa1_ptr = factory_a.MakeProduct(pa1_key, 1);
    pa1_ptr->Show();
    auto pa2_ptr = factory_a.MakeProduct(pa2_key, 2);
    pa2_ptr->Show();

    std::string pb1_key = "pb1";
    auto factory_b = Factory<AbsProductB, std::string, std::string>::GetInstance();
    factory_b.Register<PB1>(pb1_key);
    auto pb1_ptr = factory_b.MakeProduct(pb1_key, "pb");
    pb1_ptr->Show();

    return 0;
}

输出

PA1
PA2
PB1
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值