IT评测·应用市场-qidao123.com技术社区

标题: C++实现计划模式---享元模式 (Flyweight) [打印本页]

作者: 铁佛    时间: 2025-1-21 23:16
标题: C++实现计划模式---享元模式 (Flyweight)
享元模式 (Flyweight)

享元模式 是一种结构型计划模式,它通过共享对象来减少内存利用和对象创建的开销。当系统中有大量相似对象时,享元模式可以制止重复创建相同对象,从而节流内存。

意图



利用场景


参与者角色


示例代码

以下代码展示了怎样利用享元模式来共享图形对象,例如圆形。
  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_map>
  4. #include <memory>
  5. // 享元接口:图形
  6. class Shape {
  7. public:
  8.     virtual ~Shape() = default;
  9.     virtual void draw(const std::string& color) const = 0; // 外部状态为颜色
  10. };
  11. // 具体享元类:圆形
  12. class Circle : public Shape {
  13. private:
  14.     int radius; // 内部状态:半径
  15. public:
  16.     Circle(int r) : radius(r) {}
  17.     void draw(const std::string& color) const override {
  18.         std::cout << "Drawing a circle with radius " << radius << " and color " << color << std::endl;
  19.     }
  20. };
  21. // 享元工厂:管理享元对象
  22. class ShapeFactory {
  23. private:
  24.     std::unordered_map<int, std::shared_ptr<Shape>> shapes; // 缓存共享对象
  25. public:
  26.     std::shared_ptr<Shape> getCircle(int radius) {
  27.         if (shapes.find(radius) == shapes.end()) {
  28.             shapes[radius] = std::make_shared<Circle>(radius);
  29.             std::cout << "Creating a circle with radius " << radius << std::endl;
  30.         }
  31.         return shapes[radius];
  32.     }
  33. };
  34. // 客户端代码
  35. int main() {
  36.     ShapeFactory factory;
  37.     // 获取共享的圆形对象
  38.     auto circle1 = factory.getCircle(5);
  39.     auto circle2 = factory.getCircle(10);
  40.     auto circle3 = factory.getCircle(5); // 共享对象
  41.     // 使用享元对象绘制
  42.     circle1->draw("red");   // 外部状态:红色
  43.     circle2->draw("blue");  // 外部状态:蓝色
  44.     circle3->draw("green"); // 外部状态:绿色
  45.     return 0;
  46. }
复制代码

代码解析

1. 享元接口 (Shape)


  1. class Shape {
  2. public:
  3.     virtual ~Shape() = default;
  4.     virtual void draw(const std::string& color) const = 0;
  5. };
复制代码
2. 具体享元类 (Circle)


  1. class Circle : public Shape {
  2. private:
  3.     int radius; // 内部状态
  4. public:
  5.     Circle(int r) : radius(r) {}
  6.     void draw(const std::string& color) const override {
  7.         std::cout << "Drawing a circle with radius " << radius << " and color " << color << std::endl;
  8.     }
  9. };
复制代码
3. 享元工厂 (ShapeFactory)


  1. class ShapeFactory {
  2. private:
  3.     std::unordered_map<int, std::shared_ptr<Shape>> shapes; // 缓存共享对象
  4. public:
  5.     std::shared_ptr<Shape> getCircle(int radius) {
  6.         if (shapes.find(radius) == shapes.end()) {
  7.             shapes[radius] = std::make_shared<Circle>(radius);
  8.             std::cout << "Creating a circle with radius " << radius << std::endl;
  9.         }
  10.         return shapes[radius];
  11.     }
  12. };
复制代码
4. 客户端


  1. auto circle1 = factory.getCircle(5); // 创建新对象
  2. auto circle3 = factory.getCircle(5); // 复用已有对象
  3. circle1->draw("red");   // 绘制红色圆形
  4. circle3->draw("green"); // 绘制绿色圆形
复制代码

优缺点

优点

缺点


实用场景


总结

享元模式通过共享相似对象来减少内存利用,是一种优化性能的告急模式。它实用于对象数量巨大且状态大部门可以共享的场景。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4