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