享元模式,给我的感觉就是对象池,缓存单例对象。
java中的享元模式最经典的例子就是String类了,还有一个最容易理解的就是word文档字符共享的例子,也是享元模式的经典应用。
本文对android中的sql编译类SQLiteCompiledSql说明,展开分析,也是很容易理解的一个例子,其实,android SDK中必然有很多地方需要用到享元模式。
享元模式,Flyweight Pattern,说的严重点,一些程序如果不使用享元模式的话,根本不能使用面向对象的方法实现,对象会多的撑爆你的内存:"用面向对象思想设计的应用常常会面临对象实例过多的问题"。
1.意图
运用共享技术有效地支持大量细粒度的对象。
热门词汇:共享 池 缓存 内部状态 外部状态 对象 单例
2.结构
3.代码
享元对象类SQLiteCompiledSql,主要是内部状态sql语句:
class SQLiteCompiledSql { private String mSqlStmt = null; native_compile(sql); native_finalize();}
享元工厂类:
public class SQLiteDatabase{ Map<String, SQLiteCompiledSql> mCompiledQueries = Maps.newHashMap(); SQLiteCompiledSql getCompiledStatementForSql(String sql) { SQLiteCompiledSql compiledStatement = null; boolean cacheHit; synchronized(mCompiledQueries) { if (mMaxSqlCacheSize == 0) { return null; } cacheHit = (compiledStatement = mCompiledQueries.get(sql)) != null; } if (cacheHit) { mNumCacheHits++; } else { mNumCacheMisses++; } return compiledStatement; } private void deallocCachedSqlStatements() { synchronized (mCompiledQueries) { for (SQLiteCompiledSql compiledSql : mCompiledQueries.values()) { compiledSql.releaseSqlStatement(); } mCompiledQueries.clear(); } } void addToCompiledQueries(String sql, SQLiteCompiledSql compiledStatement) { //省略具体代码 }}
其他类几个相关类是对这个集合的操作相关,和享元模式没有什么实质性的关系,代码省略。
4.效果
(1).结构型模式;
(2).节约存储的方法:用共享减少内部状态的消耗,用计算时间换取对外部状态的存储;
(3).缓冲。
联系客服