找回密码
 FreeOZ用户注册
查看: 3229|回复: 5
打印 上一主题 下一主题

[论坛技术] 最新的Boost库(1.38.0)包含了Flyweight设计模式的实现

[复制链接]
跳转到指定楼层
1#
发表于 29-4-2009 16:57:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册

x
著名的GoF经典设计模式中的一种。
文档:
reference:http://www.boost.org/doc/libs/1_ ... eference/index.html
tutorial:http://www.boost.org/doc/libs/1_ ... tutorial/index.html

例子:
  1. #include <boost/flyweight.hpp>

  2. using namespace ::boost;
  3. using namespace ::boost::flyweights;

  4. struct user_entry
  5. {
  6.   flyweight<std::string> first_name;
  7.   flyweight<std::string> last_name;
  8.   int                    age;
  9.   ...
  10. };
  11. //flyweight<std::string> behaves in many ways like std::string;
  12. //... so the first_name and last_name just act as normal std:string.
复制代码
性能测试报告:http://www.boost.org/doc/libs/1_ ... oc/performance.html

无论从memory usage还是执行时间上, Boost.Flyweight都让人印象深刻。

                               
登录/注册后可看大图


                               
登录/注册后可看大图
回复  

使用道具 举报

2#
发表于 29-4-2009 18:37:30 | 只看该作者
唉,我们的代码不让用boost,不然我一定可以提高不少效率。
我去研究一下,我自己实现一个flyweight.
回复  

使用道具 举报

3#
 楼主| 发表于 29-4-2009 20:04:44 | 只看该作者

回复 #2 justinli79 的帖子

boost::flyweight就是泛型版cache,flyweight<std::string>和QT中的QString或者Java中的String原理类似,效果也差不多。
回复  

使用道具 举报

4#
发表于 30-4-2009 16:33:38 | 只看该作者
flyweight也是很实用的pattern啊,对于那些经常使用到的静态数据结构,只有共性没有个性的,做成只读的static class复用,当然效率提升,
回复  

使用道具 举报

5#
发表于 4-5-2009 14:33:21 | 只看该作者
呵呵,可惜我们要支持一个特老的编译器,对泛型支持的不好,我得做一个特殊化版本
回复  

使用道具 举报

6#
发表于 6-5-2009 23:00:58 | 只看该作者
不错,什么时候进入STL就好了。
回复  

使用道具 举报

您需要登录后才可以回帖 登录 | FreeOZ用户注册

本版积分规则

小黑屋|手机版|Archiver|FreeOZ论坛

GMT+11, 23-11-2026 02:46 , Processed in 0.014520 second(s), 22 queries , Gzip On, Redis On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表