Flyweight模式
在开发时,假设创建非常多对象,就会造成非常大的内存开销。特别是大量轻量级(细粒度)的对象,还会造成内存碎片。
Flyweight模式就是运用共享技术,有效支持大量细粒度对象的设计模式。
其类结构图例如以下:
在FlyweightFactory中有一个管理、存储对象的对象池,当调用GetFlyweight时会首先遍历对象池。假设已存在。则返回,否则创建新对象加入到对象池中。
有些对象可能不想被共享,那么就使用UnshareConcreteFlyweight。
实现:
//Flyweight.h
//Flywight.h#ifndef _FLYWEIGHT_H_
#define _FLYWEIGHT_H_
#include<string>
using std::string;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);
};
#endif
//Flyweight.cpp
//Flyweight.cpp#include"Flyweight.h"
#include<iostream>
using std::cout;Flyweight::Flyweight(string intrinsicState)
{_intrinsicState = intrinsicState;
}
Flyweight::~Flyweight()
{}
void Flyweight::Operation(const string& extrinsicState)
{}
string Flyweight::GetIntrinsicState()
{return _intrinsicState;
}ConcreteFlyweight::ConcreteFlyweight(string intrinsicState) :Flyweight(intrinsicState)
{cout << "ConcreteFlyweight Build..." << std::endl;
}
ConcreteFlyweight::~ConcreteFlyweight()
{}
void ConcreteFlyweight::Operation(const string& extrinsicState)
{cout << "ConcreteFlyweight:内蕴[" << this->GetIntrinsicState() << "]外蕴[" << extrinsicState << "]" << std::endl;
}
//FlyweightFactory.h
//FlyweightFactory.h#ifndef _FLYWEIGHTFACTORY_H_
#define _FLYWEIGHTFACTORY_H_#include"Flyweight.h"
#include<string>
#include<vector>
using std::string;
using std::vector;
class FlyweightFactory
{
public:FlyweightFactory();~FlyweightFactory();Flyweight* GetFlyweight(const string& key);
private:vector<Flyweight*> _fly;};
#endif
//FlyweightFactory.cpp
//FlyweightFactory.cpp#include"FlyweightFactory.h"
#include<iostream>
#include<string>
#include<cassert>
using std::string;
using std::cout;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 create by users..." << std::endl;return *it;}}Flyweight* fn = new ConcreteFlyweight(key);_fly.push_back(fn);return fn;
}
//main.cpp
#include"Flyweight.h"
#include"FlyweightFactory.h"
int main()
{FlyweightFactory* fc = new FlyweightFactory();Flyweight* fw1 = fc->GetFlyweight("hello");Flyweight* fw2 = fc->GetFlyweight("world");Flyweight* fw3 = fc->GetFlyweight("hello");return 0;
}
Flyweight模式相关推荐
- 结构型模式之Flyweight模式
1.意图 运用共享技术有效的支持大量细粒度的对象. 2.适用性 以下情况使用Flyweight模式 (1)一个应用程序使用了大量的对象 (2)完全由于使用大量的对象,造成很大的存储开销 (3)对象的大 ...
- Structual设计--Flyweight模式
1.意图 运用共享技术有效地支持大量细粒度的对象. 2.别名 无 3.动机 有些应用程序得意于在其整个设计过程中採用对象技术,但简单化的实现代价极大.如我们在使用word的时候.假设设置正文字体为:t ...
- 设计模式学习笔记——享元(Flyweight)模式
设计模式学习笔记--享元(Flyweight)模式 @(设计模式)[设计模式, 享元模式, flyweight] 设计模式学习笔记享元Flyweight模式 基本介绍 享元案例 类图 实现代码 Big ...
- 设计模式之Flyweight模式(笔记)
设计模式之Flyweight模式(笔记) 享元模式:运用共享技术有效地支持大量细粒度的对象. 适用场合:假设一个应用程序适用了大量的对象.而大量的这些对象造成了非常大的存储开销时就应该考虑使用. 首先 ...
- Java 实现享元(Flyweight)模式
/*** 字母* @author stone**/ public class Letter {private String name;public Letter(String name) {this. ...
- Flyweight模式——读书笔记
理解:Flyweight模式,相当于建了一个索引表,如果一个对象是由大量子对象组成的,那么直接存储对象的索引,而不需要直接存对象. 参考:http://caterpillar.onlyfun.net/ ...
- 设计模式10——flyweight模式
用例: //flyweight.cpp //享元模式:运用共享技术有效地支持大量细粒度对象. #include "gtest/gtest.h" #include <map&g ...
- 设计模式--享元(Flyweight)模式
模式定义 运用共享技术有效地支持大量细粒度的对象 类图 应用场景 如果系统有大量类似的对象,可以使用享元模式 优点 如果系统有大量类似的对象,可以节省大量的内存及CPU资源 要点总结 要点总结 如果系 ...
- python 享元模式_python 设计模式之享元(Flyweight)模式
#写在前面 这个设计模式理解起来很容易.百度百科上说的有点绕口. #享元模式的定义 运用共享技术来有効地支持大量细粒度对象的复用. 它通过共享已经存在的对橡大幅度减少需要创建的对象数量.避免大量相似类 ...
- 设计模式学习笔记--享元(Flyweight)模式
写在模式学习之前 什么是设计模式:在我们进行程序设计时,逐渐形成了一些典型问题和问题的解决方案,这就是软件模式:每一个模式描述了一个在我们程序设计中经常发生的问题,以及该问题的解决方案:当我们碰到模式 ...
最新文章
- Datawhale组队学习周报(第040周)
- Java的新项目学成在线笔记-day13(九)
- mysql城市联动表怎么建_MVC4.0搭建的省市县三联动,包含数据库
- LeetCode Count and Say
- 驱动和应用层通信列子
- vue路由切换和用location切换url的区别
- PulseAudio 设计和实现浅析
- 深入ASP.NET MVC之七:ActionResult的执行(View的加载和渲染)
- [linux]tcpdump抓包
- html多行合并,Js表格多行合并实现,可对多个列进行处理
- 颜色搭配之BUTTONS 1.0
- git与github从入门到精通
- Windows RGBDS 及 BGB 的安装 及 HelloWorld
- BERT!BERT!BERT!
- 【MySQL】字符集utf8mb4无法存储表情踩坑记录
- 别具一格的沙漠星空跨年,COLMO与百位超级个体揭露未来营养生活图景
- Caffe学习(四)数据层及参数设置
- HCNA期末测试题答案
- 《笨方法学 Python 3》43.基本的面向对象分析和设计
- 家庭NAS服务器安装配置
热门文章
- 【数码管识别】4识别成5或7的问题
- 速读-NFA的GPU加速器
- 红黑树简介与C++应用
- Linux通过Shell进行数学运算
- 从零基础入门Tensorflow2.0 ----一、3.4 实战深度神经网络(dropout)
- ArcGIS学习总结(五)——地形分析-TIN及DEM的生成
- 实习踩坑之路:日期计算错误,Java8API导致Unsupported unit: Seconds,计算当前时间到凌晨00:00的计算方法
- Java设计模式----工厂模式-----简单工厂(静态工厂模式)
- html ui 下拉列表,html - 如何给样式Material-ui选择字段下拉菜单?
- Android Multimedia框架总结(十八)Camera2框架从Java层到C++层类关系