C++享元模式与内存优化
C享元模式与内存优化享元模式通过共享细粒度对象减少内存使用。它将对象状态分为内部状态共享和外部状态上下文相关适合大量相似对象的场景。享元工厂管理共享对象池。#include#include#include#include#includeenum class FontType { SERIF, SANS_SERIF, MONO };struct CharProperty {char character;FontType font;int size;bool bold;bool italic;};class Glyph {char character_;FontType font_;int size_;bool bold_;bool italic_;public:Glyph(char c, FontType f, int s, bool b, bool i): character_(c), font_(f), size_(s), bold_(b), italic_(i) {}void render(int x, int y) const {std::cout Glyph character_ at ( x , y ) font static_cast(font_) size size_ bold bold_ italic italic_ \n;}bool operator(const Glyph other) const {return character_ other.character_ font_ other.font_ size_ other.size_ bold_ other.bold_ italic_ other.italic_;}};struct GlyphHash {size_t operator()(const Glyph g) const {size_t h1 std::hash{}(g.character_);size_t h2 std::hash{}(static_cast(g.font_));size_t h3 std::hash{}(g.size_);size_t h4 std::hash{}(g.bold_);size_t h5 std::hash{}(g.italic_);return h1 ^ (h2 1) ^ (h3 2) ^ (h4 3) ^ (h5 4);}};class GlyphFactory {std::unordered_map, GlyphHash pool_;public:std::shared_ptr get_glyph(char c, FontType f, int s, bool b, bool i) {Glyph temp(c, f, s, b, i);auto it pool_.find(temp);if (it ! pool_.end()) {std::cout Reusing glyph\n;return it-second;}auto glyph std::make_shared(c, f, s, b, i);pool_[temp] glyph;std::cout Creating new glyph (pool size: pool_.size() )\n;return glyph;}size_t pool_size() const { return pool_.size(); }};void flyweight_demo() {GlyphFactory factory;std::vector, std::pair document;document.push_back({factory.get_glyph(H, FontType::SERIF, 12, true, false), {0, 0}});document.push_back({factory.get_glyph(e, FontType::SERIF, 12, true, false), {10, 0}});document.push_back({factory.get_glyph(l, FontType::SERIF, 12, true, false), {20, 0}});document.push_back({factory.get_glyph(l, FontType::SERIF, 12, true, false), {30, 0}});document.push_back({factory.get_glyph(o, FontType::SERIF, 12, true, false), {40, 0}});std::cout \nRendering document:\n;for (const auto [glyph, pos] : document) {glyph-render(pos.first, pos.second);}std::cout \nPool size: factory.pool_size() (5 chars, 4 unique)\n;}粒子系统使用享元。class ParticleTexture {std::string texture_name_;int width_;int height_;public:ParticleTexture(const std::string name, int w, int h): texture_name_(name), width_(w), height_(h) {}void render(int x, int y, int frame) const {std::cout Render texture_name_ ( width_ x height_ ) at ( x , y ) frame frame \n;}};class ParticleTextureFactory {std::unordered_map textures_;public:std::shared_ptr get_texture(const std::string name, int w, int h) {auto it textures_.find(name);if (it ! textures_.end()) return it-second;auto tex std::make_shared(name, w, h);textures_[name] tex;return tex;}};struct Particle {int x, y, frame;std::shared_ptr texture;void render() const {texture-render(x, y, frame);}};void particle_demo() {ParticleTextureFactory factory;std::vector particles;auto fire factory.get_texture(fire, 32, 32);auto smoke factory.get_texture(smoke, 16, 16);for (int i 0; i 5; i) {particles.push_back({10 * i, 20 * i, i, fire});}for (int i 0; i 5; i) {particles.push_back({5 * i, 15 * i, i, smoke});}std::cout 5 fire 5 smoke particles, 2 textures shared\n;for (const auto p : particles) p.render();}享元模式通过共享减少内存占用适合大量重复对象的场景。

相关新闻

JN516x嵌入式开发:异常处理与MicroMAC低功耗无线通信实战

JN516x嵌入式开发:异常处理与MicroMAC低功耗无线通信实战

1. 项目概述:从“跑飞”到“省电”,嵌入式无线开发的硬核双修在嵌入式无线系统开发,尤其是基于NXP JN516x这类资源受限的微控制器构建物联网节点时,我们开发者每天都在和两个核心矛盾作斗争:系统的稳定性与设备的续航能…

2026/6/17 23:30:19阅读更多 →
如何快速搭建免费音乐库:洛雪音乐开源音源完整配置指南

如何快速搭建免费音乐库:洛雪音乐开源音源完整配置指南

如何快速搭建免费音乐库:洛雪音乐开源音源完整配置指南 【免费下载链接】lxmusic- lxmusic(洛雪音乐)全网最新最全音源 项目地址: https://gitcode.com/gh_mirrors/lx/lxmusic- 还在为音乐平台的会员费用而烦恼吗?想要免费享受高品质音乐却不知道…

2026/6/17 23:30:19阅读更多 →
国产大模型合规应用实战指南:从部署到Prompt工程

国产大模型合规应用实战指南:从部署到Prompt工程

我不能提供任何关于绕过国家网络监管措施的技术方案或建议。根据中国法律法规和网络管理政策,所有互联网服务必须遵守国家关于网络安全、数据安全和内容安全的相关规定。GPT-5目前并非公开发布的模型,截至2024年,OpenAI官方尚未发布名为“GPT…

2026/6/17 23:30:18阅读更多 →
为什么选择d2s-editor:暗黑破坏神2存档编辑的3大核心优势与完整使用指南

为什么选择d2s-editor:暗黑破坏神2存档编辑的3大核心优势与完整使用指南

为什么选择d2s-editor:暗黑破坏神2存档编辑的3大核心优势与完整使用指南 【免费下载链接】d2s-editor 项目地址: https://gitcode.com/gh_mirrors/d2/d2s-editor 你是否曾为暗黑破坏神2中繁琐的角色培养过程感到疲惫?想要快速体验高端内容却受限…

2026/6/18 0:40:28阅读更多 →
嵌入式UART通信进阶:中断与流控机制在JN516x上的实战解析

嵌入式UART通信进阶:中断与流控机制在JN516x上的实战解析

1. 项目概述与核心价值在嵌入式开发领域,UART(通用异步收发传输器)串口通信几乎是每个工程师的“必修课”。它简单、可靠,是连接微控制器与传感器、调试终端、无线模块乃至另一颗MCU的“血管”。然而,很多开发者对UART…

2026/6/18 0:40:28阅读更多 →
终极免费在线图表神器:Mermaid Live Editor完整使用指南

终极免费在线图表神器:Mermaid Live Editor完整使用指南

终极免费在线图表神器:Mermaid Live Editor完整使用指南 【免费下载链接】mermaid-live-editor Edit, preview and share mermaid charts/diagrams. New implementation of the live editor. 项目地址: https://gitcode.com/GitHub_Trending/me/mermaid-live-edit…

2026/6/18 0:40:28阅读更多 →
如何快速定制Office界面:Office Custom UI Editor终极指南

如何快速定制Office界面:Office Custom UI Editor终极指南

如何快速定制Office界面:Office Custom UI Editor终极指南 【免费下载链接】office-custom-ui-editor Standalone tool to edit custom UI part of Office open document file format 项目地址: https://gitcode.com/gh_mirrors/of/office-custom-ui-editor …

2026/6/18 0:40:28阅读更多 →
终极Kodi中文插件库:一站式解决中文用户影音需求

终极Kodi中文插件库:一站式解决中文用户影音需求

终极Kodi中文插件库:一站式解决中文用户影音需求 【免费下载链接】xbmc-addons-chinese Addon scripts, plugins, and skins for XBMC Media Center. Special for chinese laguage. 项目地址: https://gitcode.com/gh_mirrors/xb/xbmc-addons-chinese 对于中…

2026/6/18 0:40:28阅读更多 →
DeepSeekV4实战指南:中文大模型生产落地的确定性选择

DeepSeekV4实战指南:中文大模型生产落地的确定性选择

1. 项目概述:一场没有硝烟的模型代际对垒“它过江我也过江,DeepSeekV4 硬刚GPT-5.5”——这个标题一出来,我就在技术圈的几个老群里被了七八次。不是因为标题夸张,恰恰相反,它异常精准地戳中了当前大模型落地阶段最真实…

2026/6/18 0:35:27阅读更多 →
ZigBee HA智能家居开发实战:从集群模型到NXP JN516x代码实现

ZigBee HA智能家居开发实战:从集群模型到NXP JN516x代码实现

1. ZigBee HA:智能家居的“通用语言”与开发基石如果你正在或计划踏入智能家居设备开发领域,尤其是基于ZigBee协议,那么“ZigBee Home Automation”这个名词你一定不陌生。它不仅仅是ZigBee联盟定义的一套应用层规范,更是确保不同…

2026/6/18 0:00:24阅读更多 →
Java毕设选题推荐:基于 Spring Boot 的个人随笔博客运维管理系统的设计与实现 基于 Spring Boot 的用户原创博客分享社区【附源码、mysql、文档、调试+代码讲解+全bao等】

Java毕设选题推荐:基于 Spring Boot 的个人随笔博客运维管理系统的设计与实现 基于 Spring Boot 的用户原创博客分享社区【附源码、mysql、文档、调试+代码讲解+全bao等】

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

2026/6/18 0:00:24阅读更多 →
JN517x嵌入式开发实战:看门狗、脉冲计数器与I2C接口的深度解析与避坑指南

JN517x嵌入式开发实战:看门狗、脉冲计数器与I2C接口的深度解析与避坑指南

1. 项目概述在嵌入式开发领域,尤其是基于NXP JN517x这类无线微控制器的项目中,系统稳定性和与外设的可靠交互是两大核心挑战。前者关乎产品能否在无人值守的复杂环境中长期运行,后者则决定了设备能否准确感知世界并与其他芯片“对话”。JN517…

2026/6/18 0:00:24阅读更多 →