C++享元模式与内存优化

📅 2026/6/17 23:30:11
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();}享元模式通过共享减少内存占用适合大量重复对象的场景。