当前位置: 首页 > news >正文

c++开发模式,享元模式

享元模式,个人理解,就是应用共享技术来减少类的对象创建,节省计算机资源消耗,而且能够减少维护成本

#include <iostream>
#include <string>
#include <vector>using namespace std;class Flyweight {
public:Flyweight(string state):_state(state) {}virtual void Operation(const string&state) { }string GetState()const { return _state; }virtual ~Flyweight() { }
private:string _state;
};class ConcreteFlyweight :public Flyweight {
public:ConcreteFlyweight(string state):Flyweight(state) {cout << "ConcreteFlyweight Build..." << state << endl;}void Operation(const string& state) {cout << "ConcreteFlyweight " << GetState() << " \\ " << state << endl;}
};class FlyweightFactory {
public:Flyweight *GetFlyweight(std::string key) {for (auto fly : _flys) {if (fly->GetState() == key) {cout << "already created by users..." << endl;return fly;}}Flyweight *fn = new ConcreteFlyweight(key);_flys.push_back(fn);return fn;}
private:std::vector<Flyweight*> _flys;
};int main() {FlyweightFactory *fc = new FlyweightFactory();Flyweight *fw1 = fc->GetFlyweight("hello");Flyweight *fw2 = fc->GetFlyweight("world");Flyweight *fw3 = fc->GetFlyweight("hello");delete fw1;delete fw2;//delete fw3;delete fc;return 0;
}
http://www.lryc.cn/news/107104.html

相关文章:

  • LLM大模型——langchain相关知识总结
  • 【Python】数据可视化利器PyCharts在测试工作中的应用
  • AOP的实战(统一功能处理模块)
  • 时间复杂度为O(n2)的三种简单排序算法
  • LeetCode 热题 100 JavaScript --226. 翻转二叉树
  • hive所有窗口函数详情总结
  • Talk | 新加坡国立大学博士生施宇钧:DragDiffusion-基于扩散模型的关键点拖拽图片编辑
  • 22 | 贝叶斯分类算法
  • java.sql.SQLSyntaxErrorException: ORA-00909: 参数个数无效
  • 数据结构8-哈希表
  • vue3引用Font-Awesome字体图标库
  • Python: Django 服务部署可能遇到的一些问题
  • Python爬虫时遇到连接超时解决方案
  • 这所国字头双一流,根本招不满,学硕都没人报!
  • macos 查询端口占用 命令
  • 无代码开发:打破传统开发模式,引领数字化转型新方向
  • go-zero超强工具goctl的常用命令api,rpc,model及其构建的服务解析
  • 手机python编程软件怎么用,手机python编程软件下载
  • 【使用 DSP 滤波器加速速度和位移】使用信号处理算法过滤加速度数据并将其转换为速度和位移研究(Matlab代码实现)
  • 家居行业解决方案 | 君子签电子签约助力家居企业减负增效
  • Nodejs 第五章(Npm run 原理)
  • 150. 逆波兰表达式求值
  • js中的设计模式
  • PostgreSQL:string_agg 多列值聚合成一列
  • 通向架构师的道路之apache_tomcat_https应用
  • iOS——锁与死锁问题
  • 恒运资本:股票总市值是什么意思?
  • Selenium Chrome Webdriver 如何获取 Youtube 悬停文本
  • 【LeetCode每日一题】——766.托普利茨矩阵
  • 第三方材料检测实验室LIMS系统源码 lims源码