C++设计模式-享元模式

37 篇文章 12 订阅

目录

 

 

基本概念

代码与实例


 

基本概念

享元模式(Flyweight):运用共享技术有效的支持大量细粒度的对象。

UML图如下(此图来源于大话设计模式)

享元模式可以避免大量非常相似类的开销。在程序设计中,有时需要生成大量细粒度的类实例来表示数据。如果能发现这些实例除了几个参数外基本上都是相同的,有时就能受大幅度的减少需要实例化的数量。如果能把哪些参数移动到类实例外面,在方法调用是将他们传递进来,就可以通过共享大幅度的减少单个实例的数量。

如果一个应用程序使用了大量的对象,而大量的这些对象造成了很大的存储开销时就可以考虑使用;对象的大多数状态可以外部状态,如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象。

 

 

代码与实例

程序运行截图如下:

源码如下:

Head.h

#ifndef HEAD_H
#define HEAD_H

#include <iostream>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;

class Flyweight;

typedef pair<char, Flyweight*> in_pair;
typedef pair<map<char, Flyweight*>::iterator, bool> in_pair_bool;

//Flyweight类,它是所有具体享元类的超类或接口,通过这个接口,Flyweight可以接收并作用于外部状态
class Flyweight{

public:
	virtual void operation(const int &extrinsicstate);
	virtual ~Flyweight();
};

//ConcreteFlyweight是继承Flyweight超类或实现Flyweight接口,并为内部状态增加存储空间
class ConcreteFlyweight: public Flyweight{

public:
	ConcreteFlyweight();
	void operation(const int &extrinsicstate);
	~ConcreteFlyweight();
};

//UnsharedConcreteFlyweight是指那些不需要共享的Flyweight子类。因为Flyweight接口共享成为可能,但它并不强制共享
class UnsharedConcreteFlyweight: public Flyweight{
	
public:
	void operation(const int &extrinsicstate);
	~UnsharedConcreteFlyweight();
};

//FlyweightFactory是一个享元工厂,用来创建并且管理Flyweight对象。用来确保合理的共享Flyweight
//当用户请求一个Flyweight时,FlyweightFacotry对象提供一个已创建的实例或者创建一个(如果不存在的话)
class FlyweightFacory{

public:
	FlyweightFacory();
	~FlyweightFacory();
	Flyweight* getFlyweight(char c);

protected:
	void insertOk(in_pair_bool pr);
	friend void deleteMapNode(in_pair pr);

private:
	map<char, Flyweight*> m_flyweights;
};


#endif //HEAD_H

Head.cpp

#include "Head.h"

void Flyweight::operation(const int &extrinsicstate)
{

}

Flyweight::~Flyweight()
{

}

ConcreteFlyweight::ConcreteFlyweight()
{

}

void ConcreteFlyweight::operation(const int &extrinsicstate)
{
	cout << "具体Flyweight:" << extrinsicstate << endl;
}

ConcreteFlyweight::~ConcreteFlyweight()
{
	cout << "ConcreteFlyweight::~ConcreteFlyweight()" << endl;
}

void UnsharedConcreteFlyweight::operation(const int &extrinsicstate)
{
	cout << "不共享的具体类Flyweight:" << extrinsicstate << endl;
}

UnsharedConcreteFlyweight::~UnsharedConcreteFlyweight()
{
	cout << "UnsharedConcreteFlyweight::~UnsharedConcreteFlyweight()" << endl;
}

FlyweightFacory::FlyweightFacory()
{
	insertOk(m_flyweights.insert(in_pair('x', new ConcreteFlyweight)));
	insertOk(m_flyweights.insert(in_pair('y', new ConcreteFlyweight)));
	insertOk(m_flyweights.insert(in_pair('z', new ConcreteFlyweight)));
}

FlyweightFacory::~FlyweightFacory()
{
	cout << "FlyweightFacory::~FlyweightFacory()" << endl;
	for_each(m_flyweights.begin(), m_flyweights.end(), deleteMapNode);
	m_flyweights.clear();
}

Flyweight* FlyweightFacory::getFlyweight(char c)
{
	 map<char, Flyweight*>::iterator i = m_flyweights.find(c);
	 return i->second;
}

void FlyweightFacory::insertOk(in_pair_bool pr)
{
	if(pr.second){

		cout << "insert ok!" << endl;
	}
	else{

		cout << "insert failed!" << endl;
	}
}

void deleteMapNode(in_pair pr)
{
	delete pr.second;
}

main.cpp

#include "Head.h"

int main(int *argc, int *argv[]){

	int extrinsicstate = 22;

	FlyweightFacory *f = new FlyweightFacory;
	Flyweight *fx = f->getFlyweight('x');
	fx->operation(--extrinsicstate);

	Flyweight *fy = f->getFlyweight('y');
	fy->operation(--extrinsicstate);

	Flyweight *fz = f->getFlyweight('z');
	fz->operation(--extrinsicstate);

	Flyweight *uf = new UnsharedConcreteFlyweight;
	uf->operation(--extrinsicstate);

	delete f;

	getchar();
	return 0;
}

实际上FlyweightFactory不用在构造函数里面,可以根据需要在进行添加等!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值