Flyweight模式和C ++模板

时间:2010-09-11 11:16:56

标签: c++ templates flyweight-pattern

我有飞重模式。我有抽象类Glyph。我有来自Glyph的类字母和抽象代码。我有源自Code的YusciiCode,UniCyrCode和UniLatCode。

我的轻量级工厂可以这样做:

    template <class T>
 class CodeFactory : public AbstractCodeFactory
 {
 public:
  CodeFactory();
  virtual ~CodeFactory();
  virtual Glyph* GetFlyweight(unsigned int code);
  virtual Glyph* GetFlyweight(string letter);
 private:
  // pool of flyweights (codes or letters)
  map <unsigned int, Glyph*> my_code_map;
  map <string, Glyph*> my_letter_map;
 };

可以这样做:

template <class key, class T>
 class CodeFactory : public AbstractCodeFactory
 {
 public:
  CodeFactory();
  virtual ~CodeFactory();
  virtual Glyph* GetFlyweight(key code);
 private:
  // pool of flyweights (codes or letters)
  map <key, Glyph*> my_code_map;
 };

在第一个示例中,GCC链接器告诉我没有Letter(unsigned int)和xxxCode(string)构造函数。事实上没有任何和GCC是对的,但有没有比定义那些构造函数更好的方法呢?

在第二个ecample中,GCC编译器告诉我行上有错误

map <key, Glyph*>::iterator it;
GetFlyweight函数。

实施这种飞重模式的方法是什么?

我需要使用它。 这是我目前的实施:

class AbstractCodeFactory
{
public:
    AbstractCodeFactory();
    virtual ~AbstractCodeFactory();
    virtual Glyph* GetFlyweight(unsigned int code) = 0;
    virtual Glyph* GetFlyweight(string letter) = 0;
};


template <class T>
    class CodeFactory : public AbstractCodeFactory
    {
    public:
        CodeFactory();
        virtual ~CodeFactory();
        virtual Glyph* GetFlyweight(unsigned int code);
        virtual Glyph* GetFlyweight(string letter);
    private:
        // pool of flyweights (codes or letters)
        map <unsigned int, Glyph*> my_code_map;
        map <string, Glyph*> my_letter_map;
    };


template <class T>
    CodeFactory<T>::CodeFactory()
    {
        // TODO Auto-generated constructor stub

    }

template <class T>
    CodeFactory<T>::~CodeFactory()
    {
        // TODO Auto-generated destructor stub
        map <unsigned int, Glyph*>::iterator it;
        map <string, Glyph*>::iterator l_it;
        for (it = my_code_map.begin(); it != my_code_map.end(); ++it)
        {
            delete it->second;
            it->second = NULL;
            my_code_map.erase(it);
        }
        for (l_it = my_letter_map.begin(); l_it != my_letter_map.end(); ++l_it)
        {
            delete l_it->second;
            l_it->second = NULL;
            my_letter_map.erase(l_it);
        }
    }

template <class T>
    Glyph* CodeFactory<T>::GetFlyweight(unsigned int code)
    {
        map <unsigned int, Glyph*>::iterator it;
        T *code_class = NULL;
        if ((it = my_code_map.find(code)) == my_code_map.end())
        {
            my_code_map.insert(pair <unsigned int, Glyph*> (code, code_class = new T(code)));
            return code_class;
        }
        else return it->second;
    }

template <class T>
    Glyph* CodeFactory<T>::GetFlyweight(string letter)
    {
        map <string, Glyph*>::iterator it;
        T *letter_class = NULL;
        if ((it = my_letter_map.find(letter)) == my_letter_map.end())
        {
            my_letter_map.insert(pair <string, Glyph*> (letter, letter_class = new T(letter)));
            return letter_class;
        }
        else return it->second;
    }

1 个答案:

答案 0 :(得分:1)

由于您的flyweight工厂只能生成Letter,YusciiCode,UniCyrCode或UniLatCode对象,我会使用第二个选项(第二个模板参数指示键类型。

编译器对声明map <key, Glyph*>::iterator it;的问题在于编译器无法确定map<key, Glyph*>::iterator是否引用类型或其他内容。 这是因为它取决于模板参数key,并且您可能在map<>的某个地方专门化iterator 某个类型。
要帮助编译器,必须指定您希望map<ket, Glyph*>::iterator引用类型名称:

typename map<key, Glyph*>::iterator it;