当前位置: 移动技术网 > IT编程>软件设计>设计模式 > C++ 深入浅出工厂模式(进阶篇)

C++ 深入浅出工厂模式(进阶篇)

2019年09月16日  | 移动技术网IT编程  | 我要评论

介绍

前文初始篇c++ 深入浅出工厂模式(初始篇),主要阐述了简单工厂模式、工厂方法模式和抽象工厂模式的结构、特点和缺陷等。以上三种方式,在新增产品时,要么修改工厂类,要么需新增具体的工厂类,说明工厂类的封装性还不够好。

本文进阶篇,主要是将工厂类的封装性提高,达到新增产品时,也不需要修改工厂类,不需要新增具体的工厂类。封装性高的工厂类特点是扩展性高、复用性也高。

模板工厂

针对工厂方法模式封装成模板工厂类,那么这样在新增产品时,是不需要新增具体的工厂类,减少了代码的编写量。

uml图:

模板工厂代码:
  • shoesclothe,分别为鞋子和衣服的抽象类(基类)
  • nikeshoesuniqloclothe,分别为耐克鞋子和优衣库衣服具体产品类。
// 基类 鞋子
class shoes
{
public:
    virtual void show() = 0;
    virtual ~shoes() {}
};

// 耐克鞋子
class nikeshoes : public shoes
{
public:
    void show()
    {
        std::cout << "我是耐克球鞋,我的广告语:just do it" << std::endl;
    }
};

// 基类 衣服
class clothe
{
public:
    virtual void show() = 0;
    virtual ~clothe() {}
};

// 优衣库衣服
class uniqloclothe : public clothe
{
public:
    void show()
    {
        std::cout << "我是优衣库衣服,我的广告语:i am uniqlo" << std::endl;
    }
};
  • abstractfactory为抽象模板工厂类,其中模板参数:abstractproduct_t 产品抽象类,如shoesclothe
  • concretefactory为具体模板工厂类,其中模板参数:abstractproduct_t 产品抽象类(如shoesclothe),concreteproduct_t 产品具体类(如nikeshoesuniqloclothe
// 抽象模板工厂类
// 模板参数:abstractproduct_t 产品抽象类
template <class abstractproduct_t>
class abstractfactory
{
public:
    virtual abstractproduct_t *createproduct() = 0;
    virtual ~abstractfactory() {}
};

// 具体模板工厂类
// 模板参数:abstractproduct_t 产品抽象类,concreteproduct_t 产品具体类
template <class abstractproduct_t, class concreteproduct_t>
class concretefactory : public abstractfactory<abstractproduct_t>
{
public:
    abstractproduct_t *createproduct()
    {
        return new concreteproduct_t();
    }
};
  • main函数,根据不同类型的产品,构造对应的产品的工厂对象,便可通过对应产品的工厂对象创建具体的产品对象。
int main()
{
    // 构造耐克鞋的工厂对象
    concretefactory<shoes, nikeshoes> nikefactory;
    // 创建耐克鞋对象
    shoes *pnikeshoes = nikefactory.createproduct();
    // 打印耐克鞋广告语
    pnikeshoes->show();

    // 构造优衣库衣服的工厂对象
    concretefactory<clothe, uniqloclothe> uniqlofactory;
    // 创建优衣库衣服对象
    clothe *puniqloclothe = uniqlofactory.createproduct();
    // 打印优衣库广告语
    puniqloclothe->show();

    // 释放资源
    delete pnikeshoes;
    pnikeshoes = null;

    delete puniqloclothe;
    puniqloclothe = null;

    return 0;
}
  • 输出结果:
[root@lincoding factory]# ./templatefactory 
我是耐克球鞋,我的广告语:just do it
我是优衣库衣服,我的广告语:i am uniqlo

产品注册模板类+单例工厂模板类

前面的模板工厂虽然在新增产品的时候,不需要新增具体的工厂类,但是缺少一个可以统一随时随地获取指定的产品对象的类。

还有改进的空间,我们可以把产品注册的对象用std::map的方式保存,通过key-valve的方式可以轻松简单的获取对应的产品对象实例。

实现大致思路:

  • 把产品注册的功能封装成产品注册模板类。注册的产品对象保存在工厂模板类的std::map,便于产品对象的获取。

  • 把获取产品对象的功能封装成工厂模板类。为了能随时随地获取指定产品对象,则把工厂设计成单例模式。

uml图:

产品注册模板类+单例工厂模板类:
  • iproductregistrar为产品注册抽象类,模板参数 producttype_t 表示的类是产品抽象类(如shoesclothe)。提供了产品对象创建的纯虚函数createproduct
  • productfactory为工厂模板类,模板参数 producttype_t 表示的类是产品抽象类(如shoesclothe)。用于保存注册产品对象到std::map中和获取对应的产品对象。
  • productregistrar为产品注册模板类,模板参数 producttype_t 表示的类是产品抽象类(如shoesclothe),productimpl_t 表示的类是具体产品(如nikeshoesuniqloclothe)。用于注册产品到工厂类和创建产品实例对象。
// 基类,产品注册模板接口类
// 模板参数 producttype_t 表示的类是产品抽象类
template <class producttype_t>
class iproductregistrar
{
public:
   // 获取产品对象抽象接口
   virtual producttype_t *createproduct() = 0;

protected:
   // 禁止外部构造和虚构, 子类的"内部"的其他函数可以调用
   iproductregistrar() {}
   virtual ~iproductregistrar() {}

private:
   // 禁止外部拷贝和赋值操作
   iproductregistrar(const iproductregistrar &);
   const iproductregistrar &operator=(const iproductregistrar &);
};

// 工厂模板类,用于获取和注册产品对象
// 模板参数 producttype_t 表示的类是产品抽象类
template <class producttype_t>
class productfactory
{
public:
   // 获取工厂单例,工厂的实例是唯一的
   static productfactory<producttype_t> &instance()
   {
      static productfactory<producttype_t> instance;
      return instance;
   }

   // 产品注册
   void registerproduct(iproductregistrar<producttype_t> *registrar, std::string name)
   {
      m_productregistry[name] = registrar;
   }

   // 根据名字name,获取对应具体的产品对象
   producttype_t *getproduct(std::string name)
   {
      // 从map找到已经注册过的产品,并返回产品对象
      if (m_productregistry.find(name) != m_productregistry.end())
      {
         return m_productregistry[name]->createproduct();
      }

      // 未注册的产品,则报错未找到
      std::cout << "no product found for " << name << std::endl;

      return null;
   }

private:
   // 禁止外部构造和虚构
   productfactory() {}
   ~productfactory() {}

   // 禁止外部拷贝和赋值操作
   productfactory(const productfactory &);
   const productfactory &operator=(const productfactory &);

   // 保存注册过的产品,key:产品名字 , value:产品类型
   std::map<std::string, iproductregistrar<producttype_t> *> m_productregistry;
};

// 产品注册模板类,用于创建具体产品和从工厂里注册产品
// 模板参数 producttype_t 表示的类是产品抽象类(基类),productimpl_t 表示的类是具体产品(产品种类的子类)
template <class producttype_t, class productimpl_t>
class productregistrar : public iproductregistrar<producttype_t>
{
public:
   // 构造函数,用于注册产品到工厂,只能显示调用
   explicit productregistrar(std::string name)
   {
      // 通过工厂单例把产品注册到工厂
      productfactory<producttype_t>::instance().registerproduct(this, name);
   }

   // 创建具体产品对象指针
   producttype_t *createproduct()
   {
      return new productimpl_t();
   }
};
  • main函数,通过productregistrar注册各种不同类型产品,在统一由productfactory单例工厂获取指定的产品对象。
int main()
{
   // ========================== 生产耐克球鞋过程 ===========================//
   // 注册产品种类为shoes(基类),产品为nike(子类)到工厂,产品名为nike
   productregistrar<shoes, nikeshoes> nikeshoes("nike");
   // 从工厂获取产品种类为shoes,名称为nike的产品对象
   shoes *pnikeshoes = productfactory<shoes>::instance().getproduct("nike");
   // 显示产品的广告语
   pnikeshoes->show();
   // 释放资源
   if (pnikeshoes)
   {
      delete pnikeshoes;
   }

   // ========================== 生产优衣库衣服过程 ===========================//
   // 注册产品种类为clothe(基类),产品为uniqloclothe(子类)到工厂,产品名为uniqlo
   productregistrar<clothe, uniqloclothe> adidasshoes("uniqlo");
   // 从工厂获取产品种类为shoes,名称为adidas的产品对象
   clothe *puniqloclothe = productfactory<clothe>::instance().getproduct("uniqlo");
   // 显示产品的广告语
   puniqloclothe->show();
   // 释放资源
   if (puniqloclothe)
   {
      delete puniqloclothe;
   }

   return 0;
}
  • 输出结果:
[root@lincoding factory]# ./singlefactory 
我是耐克球鞋,我的广告语:just do it
我是优衣库衣服,我的广告语:i am uniqlo

总结

将工厂方法模式改良成模板工厂,虽然可以解决产品新增时,不需要新增具体工厂类,但是缺少一个可以随时随地获取产品对象的方式,说明还有改进的空间。

将模板工厂改良成产品注册模板类+单例工厂模板类,产品注册模板类用于注册不同类型的产品,单例工厂模板类用于获取指定已注册的产品对象。这种方式,可以把工厂模式中产品的注册和获取的主要功能很好的抽象成两个类,并且使用单例模式使得工厂类可以随时随地获取已注册的产品对象。

所以产品注册模板类+单例工厂模板类的工厂模式,达到了开闭法则,并且扩展性高和封装度高。

ps:想学习更多单例模式,可以参考c++ 线程安全的单例模式总结文章阅读。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网