享元模式是一种结构型设计模式,它通过共享对象来最小化内存使用或计算开销。该模式适合用于大量相似对象的情况,通过共享这些对象的公共部分来节省资源。
享元模式的特点
-
共享对象:通过共享相似对象来减少内存占用
-
分离内部状态和外部状态:
-
内部状态(Intrinsic):可以共享的、不变的部分
-
外部状态(Extrinsic):不可共享的、变化的部分
-
-
工厂管理:通常使用工厂来管理和复用享元对象
典型应用场景
-
应用程序使用大量相似对象,导致内存开销过高
-
对象的大部分状态可以外部化
-
需要缓存或对象池的场景
-
图形编辑器中的字符、图形对象
-
游戏开发中的粒子系统、地形图块等
C++ 示例程序
示例1:文字处理器中的字符对象
#include <iostream>
#include <string>
#include <unordered_map>
// 享元类:字符对象
class Character {
private:
char symbol;
int fontSize;
std::string fontFamily;
public:
Character(char s, int fs, const std::string& ff)
: symbol(s), fontSize(fs), fontFamily(ff) {}
void print(int x, int y) {
std::cout << "Character '" << symbol << "' with font " << fontFamily
<< " size " << fontSize << " at position (" << x << ", " << y << ")\n";
}
};
// 享元工厂
class CharacterFactory {
private:
std::unordered_map<char, Character*> characters;
public:
Character* getCharacter(char key, int fontSize, const std::string& fontFamily) {
if (characters.find(key) == characters.end()) {
characters[key] = new Character(key, fontSize, fontFamily);
std::cout << "Creating new character: " << key << "\n";
}
return characters[key];
}
~CharacterFactory() {
for (auto& pair : characters) {
delete pair.second;
}
}
};
// 客户端代码
int main() {
CharacterFactory factory;
// 外部状态:位置
std::vector<std::pair<int, int>> positions = {{10, 20}, {30, 40}, {50, 60}};
// 共享相同字符'A'的实例
Character* a1 = factory.getCharacter('A', 12, "Arial");
a1->print(positions[0].first, positions[0].second);
Character* a2 = factory.getCharacter('A', 12, "Arial");
a2->print(positions[1].first, positions[1].second);
// 不同的字符'B'
Character* b = factory.getCharacter('B', 12, "Arial");
b->print(positions[2].first, positions[2].second);
return 0;
}
示例2:游戏中的树木渲染
#include <iostream>
#include <string>
#include <map>
#include <vector>
// 内部状态:树的类型(享元)
class TreeType {
private:
std::string name;
std::string color;
std::string texture;
public:
TreeType(const std::string& n, const std::string& c, const std::string& t)
: name(n), color(c), texture(t) {}
void draw(int x, int y) {
std::cout << "Drawing " << name << " tree (color: " << color
<< ", texture: " << texture << ") at (" << x << ", " << y << ")\n";
}
};
// 享元工厂
class TreeFactory {
private:
static std::map<std::string, TreeType*> treeTypes;
public:
static TreeType* getTreeType(const std::string& name,
const std::string& color,
const std::string& texture) {
auto it = treeTypes.find(name);
if (it == treeTypes.end()) {
treeTypes[name] = new TreeType(name, color, texture);
std::cout << "Creating new tree type: " << name << "\n";
}
return treeTypes[name];
}
static void cleanUp() {
for (auto& pair : treeTypes) {
delete pair.second;
}
treeTypes.clear();
}
};
std::map<std::string, TreeType*> TreeFactory::treeTypes;
// 外部状态:树的位置
class Tree {
private:
int x;
int y;
TreeType* type;
public:
Tree(int x, int y, TreeType* t) : x(x), y(y), type(t) {}
void draw() {
type->draw(x, y);
}
};
// 森林包含许多树
class Forest {
private:
std::vector<Tree*> trees;
public:
void plantTree(int x, int y, const std::string& name,
const std::string& color, const std::string& texture) {
TreeType* type = TreeFactory::getTreeType(name, color, texture);
trees.push_back(new Tree(x, y, type));
}
void draw() {
for (auto tree : trees) {
tree->draw();
}
}
~Forest() {
for (auto tree : trees) {
delete tree;
}
TreeFactory::cleanUp();
}
};
// 客户端代码
int main() {
Forest forest;
// 种植1000棵橡树和1000棵松树,但只创建2个TreeType对象
for (int i = 0; i < 1000; i++) {
forest.plantTree(rand() % 100, rand() % 100, "Oak", "Green", "OakTexture");
forest.plantTree(rand() % 100, rand() % 100, "Pine", "Dark Green", "PineTexture");
}
// 绘制森林
forest.draw();
return 0;
}
示例3:围棋棋子共享
#include <iostream>
#include <map>
#include <vector>
// 享元类:棋子类型(内部状态)
class PieceType {
private:
std::string color;
std::string texture;
public:
PieceType(const std::string& c, const std::string& t) : color(c), texture(t) {}
void draw(int x, int y) {
std::cout << "Drawing " << color << " piece at (" << x << ", " << y << ")\n";
}
};
// 享元工厂
class PieceFactory {
private:
static std::map<std::string, PieceType*> pieceTypes;
public:
static PieceType* getPieceType(const std::string& color, const std::string& texture) {
std::string key = color + texture;
if (pieceTypes.find(key) == pieceTypes.end()) {
pieceTypes[key] = new PieceType(color, texture);
std::cout << "Creating new piece type: " << color << "\n";
}
return pieceTypes[key];
}
static void cleanUp() {
for (auto& pair : pieceTypes) {
delete pair.second;
}
pieceTypes.clear();
}
};
std::map<std::string, PieceType*> PieceFactory::pieceTypes;
// 外部状态:棋子位置
class Piece {
private:
int x;
int y;
PieceType* type;
public:
Piece(int x, int y, PieceType* t) : x(x), y(y), type(t) {}
void draw() {
type->draw(x, y);
}
};
// 棋盘
class Board {
private:
std::vector<Piece*> pieces;
public:
void placePiece(int x, int y, const std::string& color, const std::string& texture) {
PieceType* type = PieceFactory::getPieceType(color, texture);
pieces.push_back(new Piece(x, y, type));
}
void draw() {
for (auto piece : pieces) {
piece->draw();
}
}
~Board() {
for (auto piece : pieces) {
delete piece;
}
PieceFactory::cleanUp();
}
};
// 客户端代码
int main() {
Board board;
// 放置棋子,相同颜色的棋子共享同一个PieceType对象
board.placePiece(0, 0, "Black", "Stone");
board.placePiece(1, 0, "White", "Stone");
board.placePiece(0, 1, "Black", "Stone");
board.placePiece(1, 1, "White", "Stone");
// 绘制棋盘
board.draw();
return 0;
}
总结
享元模式通过共享相似对象来有效减少内存使用,特别适用于以下情况:
-
应用程序需要创建大量相似对象
-
对象的大部分状态可以外部化
-
内存占用是一个关键考虑因素
实现享元模式的关键在于:
-
分离内部状态(可共享)和外部状态(不可共享)
-
使用工厂管理享元对象的创建和共享
-
客户端负责维护和计算外部状态
在实际应用中,享元模式常与工厂模式、组合模式等结合使用,以达到更好的效果。