笔记:Gof设计模式--Flyweight

1、意图

Use sharing to support large numbers of fine-grained objects efficiently.

 

2、适应性

The Flyweight pattern's effectiveness depends heavily on how and where it's used. Apply the Flyweight pattern when all of the following are true:

  •  An application uses a large number of objects.
  •  Storage costs are high because of the sheer quantity of objects.
  •  Most object state can be made extrinsic.
  •  Many groups of objects may be replaced by relatively few shared objects once extrinsic state is removed.
  •  The application doesn't depend on object identity. Since flyweight objects may be shared, identity tests will return true for conceptually distinct objects.

 

3、结构

 

4、示例代码

 class Glyph { 
    public: 
        virtual ~Glyph(); 
     
        virtual void Draw(Window*, GlyphContext&); 
     
        virtual void SetFont(Font*, GlyphContext&); 
        virtual Font* GetFont(GlyphContext&); 
     
        virtual void First(GlyphContext&);
 virtual void Next(GlyphContext&); 
        virtual bool IsDone(GlyphContext&); 
        virtual Glyph* Current(GlyphContext&); 
     
        virtual void Insert(Glyph*, GlyphContext&); 
        virtual void Remove(GlyphContext&); 
    protected: 
        Glyph(); 
    };


The Character subclass just stores a character code:The Character subclass just stores a character code.

  class Character : public Glyph { 
    public: 
        Character(char); 
     
        virtual void Draw(Window*, GlyphContext&); 
    private: 
        char _charcode; 
    }; 


  GlyphContext acts as a repository of extrinsic state. It maintains a compact mapping between a glyph and its font (and any other graphical attributes it might have) in different contexts.

  class GlyphContext { 
    public: 
        GlyphContext(); 
        virtual ~GlyphContext(); 
     
        virtual void Next(int step = 1); 
        virtual void Insert(int quantity = 1); 
     
        virtual Font* GetFont(); 
        virtual void SetFont(Font*, int span = 1); 
    private: 
  int _index; 
        BTree* _fonts; 
    }; 


  The last object we need is a FlyweightFactory that creates glyphs and ensures they're shared properly.

const int NCHARCODES = 128; 
     
    class GlyphFactory { 
    public: 
        GlyphFactory(); 
        virtual ~GlyphFactory(); 
     
        virtual Character* CreateCharacter(char); 
        virtual Row* CreateRow(); 
        virtual Column* CreateColumn(); 
        // ... 
    private: 
        Character* _character[NCHARCODES]; 
};

GlyphFactory::GlyphFactory () { 
        for (int i = 0; i < NCHARCODES; ++i) { 
             _character[i] = 0; 
        } 
    } 

Character* GlyphFactory::CreateCharacter (char c) { 
        if (!_character[c]) { 
            _character[c] = new Character(c); 
        } 
     
        return _character[c]; 
    } 

 Row* GlyphFactory::CreateRow () { 
        return new Row; 
    } 
     
    Column* GlyphFactory::CreateColumn () { 
        return new Column; 
    } 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值