1.Flyweight 模式以共享的方式高效的支持大量的细粒度对象,对象分为内部状态、外部状态。将可以被共享的状态作为内部状态存储在对象中,而外部状态在适当的时候作为参数传递给对象。

当以下所有的条件都满足时,可以考虑使用享元模式:

  • 一个系统有大量的对象。
  • 这些对象耗费大量的内存。
  • 这些对象的状态中的大部分都可以外部化。
  • 这些对象可以按照内蕴状态分成很多的组,当把外蕴对象从对象中剔除时,每一个组都可以仅用一个对象代替。
  • 软件系统不依赖于这些对象的身份,换言之,这些对象可以是不可分辨的。

2.Flyweight模式结构图(不想被共享的对象UnshaerConcreteFlyweight,暂不讨论)

3.实现

 #ifndef _FLYWEIGHT_H_
#define _FLYWEIGHT_H_ #include <string>
using namespace std; class Flyweight
{
public:
virtual ~Flyweight();
virtual void Operation(const string& extrinsicState);
string GetIntrinsicState();
protected:
Flyweight(string intrinsicState);
private:
string _intrinsicState;
}; class ConcreteFlyweight:public Flyweight
{
public:
ConcreteFlyweight(string intrinsicState);
~ConcreteFlyweight();
void Operation(const string& extrinsicState);
protected:
private:
}; #endif

Flyweight.h

 #include "Flyweight.h"
#include <iostream>
using namespace std; Flyweight::Flyweight(string intrinsicState)
{
this->_intrinsicState = intrinsicState;
}
Flyweight::~Flyweight()
{ }
void Flyweight::Operation(const string& extrinsicState)
{ }
string Flyweight::GetIntrinsicState()
{
return this->_intrinsicState;
}
ConcreteFlyweight::ConcreteFlyweight(string intrinsicState):Flyweight(intrinsicState)
{
cout<<"ConcreteFlyweight Build....."<<intrinsicState<<endl;
}
ConcreteFlyweight::~ConcreteFlyweight()
{ }
void ConcreteFlyweight::Operation(const string& extrinsicState)
{
cout<<"ConcreteFlyweight:内蕴["<<this->GetIntrinsicState()<<"] 外蕴["<<extrinsicState<<"]"<<endl;
}

Flyweight.cpp

 #ifndef _FLYWEIGHTFACTORY_H_
#define _FLYWEIGHTFACTORY_H_ #include "Flyweight.h"
#include <string>
#include <vector>
using namespace std; class FlyweightFactory
{
public:
FlyweightFactory();
~FlyweightFactory();
Flyweight* GetFlyweight(const string& key);
protected:
private:
vector<Flyweight*> _fly;
}; #endif

FlyweightFactory.h

 #include "FlyweightFactory.h"
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
using namespace std; FlyweightFactory::FlyweightFactory()
{ }
FlyweightFactory::~FlyweightFactory()
{ }
Flyweight* FlyweightFactory::GetFlyweight(const string& key)
{
vector<Flyweight*>::iterator it = _fly.begin();
for (; it != _fly.end();it++)
{ //找到了,就一起用,^_^
if ((*it)->GetIntrinsicState() == key)
{
cout<<"already created by users...."<<endl;
return *it;
}
}
Flyweight* fn = new ConcreteFlyweight(key);
_fly.push_back(fn);
return fn;
}

FlyweightFactory.cpp

 #include "Flyweight.h"
#include "FlyweightFactory.h"
#include <iostream>
using namespace std; int main(int argc,char* argv[])
{
FlyweightFactory* fc = new FlyweightFactory();
Flyweight* fw1 = fc->GetFlyweight("hello");
Flyweight* fw2 = fc->GetFlyweight("world!");
Flyweight* fw3 = fc->GetFlyweight("hello");
return ;
}

main.cpp

最新文章

  1. get_locked_objects_rpt.sql
  2. IE关闭兼容性视图
  3. c++unsigned char的输出问题
  4. Ubuntu 安装tftp服务器
  5. Lucene系列-分析器
  6. freeCodeCamp:Find the Longest Word in a String
  7. [转] Paxos算法2-算法过程(实现)
  8. struts调用的几种方法
  9. OC:Block语法、Block使用、Block实现数组排序
  10. Qt 学习之路 :文本文件读写
  11. asp.net验证码及怎么获取里面的数值(整合)
  12. hdu4679(树形dp)
  13. error C2248: “CObject::operator =”: 不可访问 private 员(于“CObject”类声明)
  14. python中的collections.namedtuple
  15. js 从一个函数中传递值到另一个函数
  16. [图解Java]ReentrantLock重入锁
  17. 成熟的 Git 分支模型
  18. Pandas 学习记录(一)
  19. this引用逃逸
  20. $().click()和$(document).on(&#39;click&#39;,&#39;要选择的元素&#39;,function(){})的不同

热门文章

  1. DB2数据库 CASE WHEN的使用
  2. Springboot 集成 Thymeleaf 及常见错误
  3. DTrace Oracle Database
  4. Concurrency and Application Design (三)
  5. hdu 4300 Clairewd’s message(具体解释,扩展KMP)
  6. iOS开发 使用Cocoapods管理第三方类库
  7. 转: MySQL索引原理及慢查询优化 (from 美团技术博客)
  8. C# Http方式下载文件到本地
  9. mysql insert into 时报1062错误
  10. java性能监控工具jstat-windows