0
点赞
收藏
分享

微信扫一扫

C++【设计模式】 | 【08对象性能】Flyweight

艾晓雪 2022-03-30 阅读 66

文章目录

一、Flyweight

1、优点

2、适用场景

在这里插入图片描述

3、模板示例


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!=footPool.end()){
            return fontPool[key];
        }
        else{
            Font* font = new Font(key);
            fontPool[key]= font;
            return font;
        }
    }
};
举报

相关推荐

0 条评论