享元模式通过共享内部状态减少内存使用,如示例中多棵树共享相同TreeType实例,仅外部位置不同,从而在大量相似对象场景下显著降低内存开销。

在C++中,享元(Flyweight)模式是一种用于减少内存使用的设计模式,特别适用于大量相似对象的场景。它的核心思想是共享尽可能多的相同数据,把不变的部分提取为“内部状态”,而将变化的部分作为“外部状态”传入,避免重复创建对象。
享元模式通常包含以下几个部分:
假设我们要绘制森林中的树,每棵树有种类(type)、颜色、高度等属性。其中“种类”和“颜色”是固定的,可以共享;而“位置(x, y)”是变化的,应作为外部状态传入。
立即学习“C++免费学习笔记(深入)”;
#include <iostream>
#include <string>
#include <map>
#include <memory>
<p>// 抽象享元类
class TreeType {
public:
std::string type;
std::string color;</p><pre class='brush:php;toolbar:false;'>TreeType(const std::string& t, const std::string& c) : type(t), color(c) {}
void draw(int x, int y) const {
std::cout << "Drawing " << color << " " << type
<< " at (" << x << ", " << y << ")\n";
}};
// 享元工厂 class TreeFactory { private: static std::map<:string std::shared_ptr>> pool;
// 生成唯一键
static std::string getKey(const std::string& type, const std::string& color) {
return type + "_" + color;
}public:
static std::shared_ptr // 静态成员初始化
std::map<:string std::shared_ptr>> TreeFactory::pool; // 外层封装类:代表一棵树
class Tree {
private:
int x, y;
std::shared_ptr public:
Tree(int x, int y, const std::shared_ptr }; // 森林类,包含多棵树
class Forest {
private:
std::vector public:
void plantTree(int x, int y, const std::string& type, const std::string& color) {
auto treeType = TreeFactory::getTreeType(type, color);
trees.emplace_back(x, y, treeType);
} }; 使用示例: } 在这个例子中,即使我们创建了上百棵“Oak - Green”类型的树,也只会有一个 基本上就这些。通过共享不变状态,Flyweight模式有效降低了内存占用,是性能优化中的实用技巧之一。不复杂但容易忽略。void draw() const {
type->draw(x, y);
}void draw() const {
for (const auto& tree : trees) {
tree.draw();
}
}
int main() {
Forest forest;
forest.plantTree(1, 2, "Oak", "Green");
forest.plantTree(3, 4, "Oak", "Green"); // 共享同一个 TreeType
forest.plantTree(5, 6, "Pine", "DarkGreen");
<pre class='brush:php;toolbar:false;'>forest.draw();
return 0;
为什么能节约内存?
TreeType 实例被创建并共享。所有树对象共用这个实例,只保存自己的位置信息。这样大大减少了内存开销,尤其适合大规模相似对象的场景,比如图形系统、文本编辑器中的字符格式、游戏中的粒子效果等。注意事项与适用场景
以上就是c++++如何实现一个享元设计模式_c++ Flyweight模式节约内存的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号