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

在C++中,享元(Flyweight)模式是一种用于减少内存使用的设计模式,特别适用于大量相似对象的场景。它的核心思想是共享尽可能多的相同数据,把不变的部分提取为“内部状态”,而将变化的部分作为“外部状态”传入,避免重复创建对象。
享元模式的核心结构
享元模式通常包含以下几个部分:
- Flyweight(抽象享元类):定义接口,描述操作外部状态的方法。
- ConcreteFlyweight(具体享元类):实现Flyweight接口,并存储内部状态(可共享)。
- UnsharedConcreteFlyweight(非共享具体享元):某些情况下不需要共享的对象,可选。
- FlyweightFactory(享元工厂):负责管理享元对象,通过键值缓存已创建的实例,避免重复创建。
一个实际的C++实现示例
假设我们要绘制森林中的树,每棵树有种类(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 实例被创建并共享。所有树对象共用这个实例,只保存自己的位置信息。这样大大减少了内存开销,尤其适合大规模相似对象的场景,比如图形系统、文本编辑器中的字符格式、游戏中的粒子效果等。注意事项与适用场景


















