C++设计模式-享元(Flyweight)

目录

C++设计模式-享元(Flyweight)

一、意图

二、适用性

三、结构

四、参与者

五、代码


C++设计模式-享元(Flyweight)

一、意图

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

二、适用性

  • 一个应用程序使用了大量的对象。
  • 完全由于使用大量的对象,造成很大的存储开销。
  • 对象的大多数状态都可变为外部状态。
  • 如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象。
  • 应用程序不依赖于对象标识。由于Flyweight对象可以被共享,对于概念上明显有别的对象,标识测试将返回真值。

三、结构

四、参与者

  • Flyweight

描述一个接口,通过这个接口flyweight可以接受并作用于外部状态。

  • ConcreteFlyweight

实现Flyweight接口,并为内部状态(如果有的话)增加存储空间。ConcreteFlyweight对象必须是可共享的。它所存储的状态必须是内部的;即,它必须独立于ConcreteFlyweight对象的场景。

  • UnsharedConcreteFlyweight

并非所有的Flyweight子类都需要被共享。Flyweight接口使共享成为可能,但它并不强制共享。在Flyweight对象结构的某些层次,UnsharedConcreteFlyweight对象通常将ConcreteFlyweight对象作为子节点。

  • FlyweightFactory

创建并管理flyweight对象。

确保合理地共享flyweight。当用户请求一个flyweight时,FlyweightFactory对象提供一个已创建的实例或者创建一个(如果不存在的话)。

  • Client

维持一个对flyweight的引用。

计算或存储一个(多个)flyweight的外部状态。

五、代码

cpp 复制代码
#include<iostream>
#include<vector>
using namespace std;

class Flyweight {
public:
	Flyweight(string tempIntrinsicState) {
		this->intrinsicState = tempIntrinsicState;
	}
	string GetIntrinsicState() {
		return intrinsicState;
	}
	virtual void Operation(const string& tempExtrinsicState) = 0;
private:
	string intrinsicState;
};

class ConcreteFlyweight :public Flyweight {
public:
	ConcreteFlyweight(string tempIntrinsicState) :Flyweight(tempIntrinsicState) {}
	void Operation(const string& tempExtrinsicState) {
		cout << this->GetIntrinsicState() << endl;
		cout << tempExtrinsicState << endl;
	}
};

class UnsharedConcreteFlyweight : public Flyweight {
public:
	UnsharedConcreteFlyweight(string tempIntrinsicState) :Flyweight(tempIntrinsicState) {}
	void UnsharedConcreteOperation(const string& tempExtrinsicState) {
		cout << tempExtrinsicState << endl;
	}
};

class FlyweightFactory {
public:
	Flyweight* GetFlyweight(string state) {
		vector<Flyweight*>::iterator it = this->flyweightVector.begin();
		for (; it != this->flyweightVector.end(); it++) {
			if ((*it)->GetIntrinsicState() == state) {
				return *it;
			}
		}
		Flyweight* flyweight = new ConcreteFlyweight(state);

		this->flyweightVector.push_back(flyweight);

		return flyweight;
	}

	int GetFlyweightCount() {
		return this->flyweightVector.size();
	}
private:
	vector<Flyweight*> flyweightVector;
};

int main() {
	string extrinsicState = "ExtrinsicState";

	FlyweightFactory* flyweightFactory = new FlyweightFactory();

	Flyweight* flyweight1 = flyweightFactory->GetFlyweight("AAA");
	Flyweight* flyweight2 = flyweightFactory->GetFlyweight("AAA");
	Flyweight* flyweight3 = flyweightFactory->GetFlyweight("BBB");

	flyweight1->Operation(extrinsicState);

	cout << flyweightFactory->GetFlyweightCount() << endl;

	cout << (flyweight1 == flyweight2) << endl;

	return 0;
}
相关推荐
weixin_307779136 分钟前
Linux下GCC和C++实现统计Clickhouse数据仓库指定表中各字段的空值、空字符串或零值比例
linux·运维·c++·数据仓库·clickhouse
胡侃有料14 分钟前
【设计模式】1.简单工厂、工厂、抽象工厂模式
设计模式·抽象工厂模式
开发者工具分享36 分钟前
如何应对敏捷转型中的团队阻力
开发语言
gregmankiw42 分钟前
C#调用Rust动态链接库DLL的案例
开发语言·rust·c#
roman_日积跬步-终至千里1 小时前
【Go语言基础【20】】Go的包与工程
开发语言·后端·golang
秦少游在淮海1 小时前
C++ - string 的使用 #auto #范围for #访问及遍历操作 #容量操作 #修改操作 #其他操作 #非成员函数
开发语言·c++·stl·string·范围for·auto·string 的使用
const5441 小时前
cpp自学 day2(—>运算符)
开发语言·c++
心扬1 小时前
python生成器
开发语言·python
阿蒙Amon2 小时前
06. C#入门系列【自定义类型】:从青铜到王者的进阶之路
开发语言·c#
虾球xz2 小时前
CppCon 2015 学习:CLANG/C2 for Windows
开发语言·c++·windows·学习