美文网首页
享元模式

享元模式

作者: 钟离惜 | 来源:发表于2020-11-04 00:41 被阅读0次
    #include <string>
    #include <map>
    using namespace std;
    class Font {
    private:
        //unique object key
        string key;
        //object state
        //....
    public:
        Font(const string& key) {
            //...
        }
    };
    class FontFactory 
    {
    private:
        map<string, Font* > fontPool;
    public:
        Font* GetFont(const string& key) {
            map<string, Font*>::iterator item = fontPool.find(key);
            if (item != fontPool.end()) {
                return fontPool[key];
            }
            else {
                Font* font = new Font(key);
                fontPool[key] = font;
                return font;
            }
        }
        void clear() {
            //...
        }
    };
    

    相关文章

      网友评论

          本文标题:享元模式

          本文链接:https://www.haomeiwen.com/subject/tgrxvktx.html