设计模式读书笔记:Flyweight(享元)

意图:

运用共享技术有效地支持大量细粒度的对象。

结构图:来自 《23种设计模式 - 郗晓勇》


实现:https://github.com/panshiqu/patterns/tree/master/Flyweight

FlyweightFactory

#include "ConcreteFlyweight.h"
#include <map>

namespace NS_FLYWEIGHT {

class FlyweightFactory {
public:
	FlyweightFactory() {}
	virtual ~FlyweightFactory()
	{
		std::map<int, Flyweight *>::iterator itr = _flyweights.begin();
		for (; itr != _flyweights.end(); itr++)
			delete itr->second;

		_flyweights.clear();
	}
	Flyweight *getFlyweight(int key)
	{
		std::map<int, Flyweight *>::iterator itr = _flyweights.find(key);
		if (itr != _flyweights.end()) return itr->second;

		ConcreteFlyweight *flyweight = new ConcreteFlyweight();
		flyweight->setIntrinsicState(key);

		_flyweights.insert(std::make_pair(key, flyweight));
		return flyweight;
	}

private:
	std::map<int, Flyweight *> _flyweights;
};

} /* namespace NS_FLYWEIGHT */
Flyweight

#include <iostream>

namespace NS_FLYWEIGHT {

class Flyweight {
public:
	Flyweight() {}
	virtual ~Flyweight() {}
	virtual void operation(std::string extrinsicState) = 0;
};

} /* namespace NS_FLYWEIGHT */
ConcreteFlyweight

#include "Flyweight.h"

namespace NS_FLYWEIGHT {

class ConcreteFlyweight : public Flyweight
{
public:
	ConcreteFlyweight() : _intrinsicState(0) {}
	virtual ~ConcreteFlyweight() {}
	virtual void operation(std::string extrinsicState)
	{
		std::cout << extrinsicState << " ConcreteFlyweight - " << _intrinsicState << std::endl;
	}

	void setIntrinsicState(int intrinsicState)	{ _intrinsicState = intrinsicState; }

private:
	int _intrinsicState;
};

} /* namespace NS_FLYWEIGHT */
UnsharedConcreteFlyweight

#include "Flyweight.h"
#include <map>

namespace NS_FLYWEIGHT {

class UnsharedConcreteFlyweight : public Flyweight
{
public:
	UnsharedConcreteFlyweight() {}
	virtual ~UnsharedConcreteFlyweight() {}
	virtual void operation(std::string extrinsicState)
	{
		std::multimap<Flyweight *, std::string>::iterator itr = _flyweights.begin();
			for (; itr != _flyweights.end(); itr++)
			{
				std::string str = itr->second;
				if (str == "") str = extrinsicState;

				itr->first->operation(str);
			}
	}
	virtual void add(Flyweight *flyweight, std::string extrinsicState)
	{
		_flyweights.insert(make_pair(flyweight, extrinsicState));
	}
	virtual void remove(Flyweight *flyweight)
	{
		// do remove
	}

private:
	std::multimap<Flyweight *, std::string> _flyweights;
};

} /* namespace NS_FLYWEIGHT */
main

#include "Flyweight/FlyweightFactory.h"
#include "Flyweight/UnsharedConcreteFlyweight.h"
using namespace NS_FLYWEIGHT;
int main(void)
{
	FlyweightFactory ff;
	UnsharedConcreteFlyweight ucf;
	Flyweight *f1 = ff.getFlyweight(1);
	ucf.add(f1, "");
	Flyweight *f2 = ff.getFlyweight(1);
	ucf.add(f2, "black");
	ucf.operation("red");
}
附加:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值