如果一个应用程序使用了太多的对象, 就会造成很大的存储开销。 特别是对于大量轻量级 (细粒度)的对象,比如在文档编辑器的设计过程中,我们如果为每个字母创建一个对象的话,系统可能会因为大量的对象而造成存储开销的浪费。例如一个字母“a”在文档中出现了100000 次,而实际上我们可以让这一万个字母“a”共享一个对象,当然因为在不同的位置可能字母“a”有不同的显示效果(例如字体和大小等设置不同) ,在这种情况我们可以为将对象的状态分为“外部状态”和“内部状态” ,将可以被共享(不会变化)的状态作为内部状态存储在对象中,而外部对象(例如上面提到的字体、大小等)我们可以在适当的时候将外部对象最为参数传递给对象(例如在显示的时候,将字体、大小等信息传递给对象) 。

Flyweight 模式可以解决上面的问题,其典型的结构图为:

 ///////////////Flyweight.h////////////////////////
#pragma once
#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();
ConcreteFlyweight(string intrinsicState);
void Operation(const string& extrinsicState);
protected:
private:
};
 ///////////////Flyweight.cpp//////////////////////////
#include "Flyweight.h"
#include <iostream>
using namespace std; Flyweight::Flyweight(string intrinsicState)
{
_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;
}
 /////////////////FlyweightFactory.h////////////////////////////
#pragma once
#include "Flyweight.h"
#include <vector>
class FlyweightFactory
{
public:
FlyweightFactory();
~FlyweightFactory();
Flyweight* GetFlyweight(const string& key); protected:
private:
vector<Flyweight*> _fly ;
};
 ///////////////////FlyweightFactory.cpp//////////////////////////////
#include "FlyweightFactory.h"
#include <iostream>
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 ; }
 /////////////////////////main.cpp/////////////////////////////////////////////////
#include "FlyweightFactory.h"
#include "Flyweight.h"
#include <iostream>
using namespace std;
int main()
{
FlyweightFactory* fc = new FlyweightFactory();
Flyweight* fw1 = fc->GetFlyweight("hello");
Flyweight* fw2 = fc->GetFlyweight("world"); cout<<fw1->GetIntrinsicState()<<endl;
fw1->Operation("red"); cout<<fw2->GetIntrinsicState()<<endl;
fw2->Operation("green"); fc->GetFlyweight("hello")->Operation("black");
getchar();
return ;
}

Flyweight 模式的更多相关文章

  1. 常见设计模式解析和实现(C++)FlyWeight模式

    作用:运用共享技术有效地支持大量细粒度的对象 UML结构图: 解析: Flyweight模式在大量使用一些可以被共享的对象的时候使用.比如,在QQ聊天时很多时候你懒得回复又不得不回复,一般会用一些客套 ...

  2. Structual设计--Flyweight模式

    1.意图 运用共享技术有效地支持大量细粒度的对象. 2.别名 无 3.动机 有些应用程序得意于在其整个设计过程中採用对象技术,但简单化的实现代价极大.如我们在使用word的时候.假设设置正文字体为:t ...

  3. Java 实现享元(Flyweight)模式

    /** * 字母 * @author stone * */ public class Letter { private String name; public Letter(String name) ...

  4. Flyweight模式_Java中23种设计模式

    —————————— ASP.Net+Android+IOS开发..Net培训.期待与您交流! —————————— 享元模式: Flyweight模式的有效性很大程度上取决于如何使用它以及在何处使用 ...

  5. Java设计模式(5)共享模式/享元模式(Flyweight模式)

    Flyweight定义:避免大量拥有相同内容的小类的开销(如耗费内存),使大家共享一个类(元类). 为什么使用共享模式/享元模式 面向对象语言的原则就是一切都是对象,但是如果真正使用起来,有时对象数可 ...

  6. 设计模式之——flyweight模式

    flyweight模式,又叫做享元模式. 顾名思义,享元模式就是共享一个元素. 百度百科 解释为: 享元模式(英语:Flyweight Pattern)是一种软件设计模式.它使用共享物件,用来尽可能减 ...

  7. Flyweight模式(亨元模式)

    这应该算是最好理解的一个设计模式了吧·················· 面向对象语言的原则就是一切都是对象,但是如果真正使用起来,有时对象数可能显得很庞大,比如,字处理软件,如果以每个文字都作为一个 ...

  8. flyweight模式

    参考资料 • 维基百科:https://en.wikipedia.org/wiki/Flyweight_pattern • 百度百科:http://baike.baidu.com/link?url=R ...

  9. 设计模式(二十)Flyweight模式

    当使用new关键字生成类的实例时,需要给其分配足够的内存空间.当程序中需要大量对象时,如果都是用new关键字来分配内存,将会消耗大量内存空间.Flyweight模式就是尽量避免new出实例,而是通过尽 ...

随机推荐

  1. ☀【Zepto】touch events

    https://github.com/madrobby/zepto jTouchhttps://github.com/liutian1937/jTouch iphone.ipod Touch.ipad ...

  2. 博弈论(SG函数):HNOI 2007 分裂游戏

    Description 聪聪和睿睿最近迷上了一款叫做分裂的游戏. 该游戏的规则试: 共有 n 个瓶子, 标号为 0,1,2.....n-1, 第 i 个瓶子中装有 p[i]颗巧克力豆,两个人轮流取豆子 ...

  3. HDOJ/HDU 2539 点球大战(String.endsWith()方法的一个应用~)

    Problem Description 在足球比赛中,有不少赛事,例如世界杯淘汰赛和欧洲冠军联赛淘汰赛中,当比赛双方经过正规比赛和加时赛之后仍然不分胜负时,需要进行点球大战来决定谁能够获得最终的胜利. ...

  4. Front-End Engineer 技术栈

    自己根据各种招聘网站上的技术要求做的,希望自己能成为这样的人.

  5. 【原】centos6.5下hadoop cdh4.6 安装

    1.架构准备:      namenode 10.0.0.2      secondnamenode 10.0.0.3      datanode1 10.0.0.4      datanode2 1 ...

  6. [经典] Best Time to Buy and Sell Stock

    这一系列求最优值的问题变种挺多 1. Say you have an array for which the ith element is the price of a given stock on ...

  7. poj 1847 Tram【spfa最短路】

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 Description ...

  8. angularjs post 跨域

    web api搞好了:用Ajax妥妥的:但是前端用的AngulagJS,也懒得再换为Ajax了: 但是问题来了:提示: 已拦截跨源请求:同源策略禁止读取位于 http://x.x.x.x:port/a ...

  9. UVA 10985 - Rings'n'Ropes(floyd)

    Problem D Rings'n'Ropes Time Limit: 3 seconds "Well, that seems to be the situation. But, I don ...

  10. Oracle 同步表权限分配(同义词)

    新建了同义词之后还要 分配权限