Flyweight Pattern
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
Flyweight Pattern的更多相关文章
- 设计模式(十二)享元模式(Flyweight Pattern)
一.引言 在软件开发过程,如果我们需要重复使用某个对象的时候,如果我们重复地使用new创建这个对象的话,这样我们在内存就需要多次地去申请内存空间了,这样可能会出现内存使用越来越多的情况,这样的问题是非 ...
- 深入浅出设计模式——享元模式(Flyweight Pattern)
模式动机 面向对象技术可以很好地解决一些灵活性或可扩展性问题,但在很多情况下需要在系统中增加类和对象的个数.当对象数量太多时,将导致运行代价过高,带来性能下降等问题.享元模式正是为解决这一类问题而诞生 ...
- 二十四种设计模式:享元模式(Flyweight Pattern)
享元模式(Flyweight Pattern) 介绍运用共享技术有效地支持大量细粒度的对象. 示例有一个Message实体类,某些对象对它的操作有Insert()和Get()方法,现在要运用共享技术支 ...
- 乐在其中设计模式(C#) - 享元模式(Flyweight Pattern)
原文:乐在其中设计模式(C#) - 享元模式(Flyweight Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 享元模式(Flyweight Pattern) 作者:weba ...
- 第11章 享元模式(Flyweight Pattern)
原文 第11章 享元模式(Flyweight Pattern) 概述: 面向对象的思想很好地解决了抽象性的问题,一般也不会出现性能上的问题.但是在某些情况下,对象的数量可能会太多,从而导致了运行时 ...
- C#设计模式之十一享元模式(Flyweight Pattern)【结构型】
一.引言 今天我们要讲[结构型]设计模式的第六个模式,该模式是[享元模式],英文名称是:Flyweight Pattern.还是老套路,先从名字上来看看.“享元”是不是可以这样理解,共享“单元”,单元 ...
- NET设计模式 第二部分 结构性模式(12):享元模式(Flyweight Pattern)
享元模式(Flyweight Pattern) ——.NET设计模式系列之十三 Terrylee,2006年3月 摘要:面向对象的思想很好地解决了抽象性的问题,一般也不会出现性能上的问题.但是在某些情 ...
- 享元模式(FlyWeight Pattern)及其在java自动拆箱、自动装箱中的运用
本文主要从三个方面着手,第一:简要介绍享元模式.第二:享元模式在基本类型封装类中的运用以Integer为例进行阐述.第三:根据第一.第二的介绍,进而推出java是如何实现自动拆箱与装箱的. 第一:简要 ...
- 【设计模式】结构型06享元模式(Flyweight Pattern)
享元模式(Flyweight Pattern) 首先吐槽下这个名字,享元?共享元素的话感觉还是蛮好的~但是这个英文... 意图:更好的重用.管理对象,减少内存开销,节约资源. 主要解决:在有大量重复对 ...
- php享元模式(flyweight pattern)
周日一大早,今天要送老婆孩子去火车站, 所以练代码要早点哈. <?php /* The flyweight pattern is about performance and resource r ...
随机推荐
- CSS-点赞爱心小动画
爱心图片: HTML: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- Reveal.js演讲幻灯片框架
摘要 还需学习参考的链接 https://www.tuicool.com/articles/2AFFj2j 无意中看到这个插件,很喜欢,可以作用在演讲ppt,幻灯片,用户指引上等.代码简单,易维护 ...
- html --- rem
// rem (function(doc, win) { var docEle = doc.documentElement, evt = "onorientati ...
- 小程序 之使用HMACSHA1算法加密报文
首先说说我们前端常用的加密技术, 我们常用的加密技术有:如MD5加密,base64加密 今天要说的是HMACSHA1加密技术 先介绍下什么是SHA1算法, 安全哈希算法(Secure Hash Alg ...
- R语言实战读书笔记(十三)广义线性模型
# 婚外情数据集 data(Affairs, package = "AER") summary(Affairs) table(Affairs$affairs) # 用二值变量,是或 ...
- How to Mount a USB Drive in Ubuntu
Read more : http://www.ehow.com/how_6762235_mount-usb-drive-ubuntu.html Most USB drives will automou ...
- cef 下载地址
最新的CEF3源代码在:http://cefbuilds.com/CEF3的论坛:http://www.magpcss.org/ceforum/viewforum.php?f=5CEF3 C++开发环 ...
- Java中泛型T和Class<T>以及Class<?>的理解(转)
注意:class是java的关键字, 在声明Java类时使用; Class类的实例表示Java应用运行时的类(class ans enum)或接口(interface and annotation)( ...
- 【Protocol Buffers】grpc默认使用的Google 开源的一套成熟的结构数据序列化机制
grpc默认使用的Google 开源的一套成熟的结构数据序列化机制 参考地址:https://blog.csdn.net/shensky711/article/details/69696392 参考地 ...
- hdu5384
题意:给你n个母串.m个匹配串,让你求出对于每一个母串 全部匹配串出现的次数和. 思路:完全然全邝斌的模板啊... 凝视掉一行代码就能a... . 代码: #include <algorithm ...