• C++中的享元模式


    目录

    享元模式(Flyweight Pattern)

    实际应用

    文字编辑器中的字符

    修仙游戏中的地图瓦片

    图形编辑器中的图形对象

    总结


    享元模式(Flyweight Pattern)

    享元模式是一种结构型设计模式,用于减少对象的内存使用和提高性能。该模式通过共享相似对象之间的公共部分来最小化内存使用。这些公共部分称为享元(Flyweight),而不同的部分称为非共享部分。

    实际应用

    文字编辑器中的字符

    假设正在开发一个文字编辑器,这就可以使用享元模式来共享文本中重复出现的字符,减少内存使用。

    1. #include
    2. #include
    3. #include
    4. // 享元类:字符
    5. class Character {
    6. private:
    7. char symbol;
    8. // 非共享状态可以作为外部状态
    9. int fontSize;
    10. public:
    11. Character(char symbol, int fontSize) : symbol(symbol), fontSize(fontSize) {}
    12. void display(int position) const {
    13. std::cout << "Character '" << symbol << "' displayed at position " << position
    14. << " with font size " << fontSize << "\n";
    15. }
    16. };
    17. // 享元工厂类
    18. class CharacterFactory {
    19. private:
    20. std::unordered_map<char, std::shared_ptr> characters;
    21. public:
    22. std::shared_ptr getCharacter(char symbol, int fontSize) {
    23. if (characters.find(symbol) == characters.end()) {
    24. characters[symbol] = std::make_shared(symbol, fontSize);
    25. }
    26. return characters[symbol];
    27. }
    28. };
    29. int main() {
    30. CharacterFactory characterFactory;
    31. // 使用享元工厂创建并共享字符
    32. auto char1 = characterFactory.getCharacter('A', 12);
    33. auto char2 = characterFactory.getCharacter('B', 12);
    34. auto char3 = characterFactory.getCharacter('A', 14);
    35. auto char4 = characterFactory.getCharacter('B', 12);
    36. char1->display(1);
    37. char2->display(2);
    38. char3->display(3);
    39. char4->display(4);
    40. return 0;
    41. }

    修仙游戏中的地图瓦片

    假设游戏中有许多地图瓦片,这就可以使用享元模式来共享相同类型的地图瓦片。

    1. #include
    2. #include
    3. #include
    4. // 享元类:地图瓦片
    5. class MapTile {
    6. private:
    7. int type;
    8. // 非共享状态可以作为外部状态
    9. int x, y;
    10. public:
    11. MapTile(int type, int x, int y) : type(type), x(x), y(y) {}
    12. void draw() const {
    13. std::cout << "Drawing map tile of type " << type << " at position (" << x << ", " << y << ")\n";
    14. }
    15. };
    16. // 享元工厂类
    17. class MapTileFactory {
    18. private:
    19. std::unordered_map<int, std::shared_ptr> mapTiles;
    20. public:
    21. std::shared_ptr getMapTile(int type, int x, int y) {
    22. int key = type * 10000 + x * 100 + y;
    23. if (mapTiles.find(key) == mapTiles.end()) {
    24. mapTiles[key] = std::make_shared(type, x, y);
    25. }
    26. return mapTiles[key];
    27. }
    28. };
    29. int main() {
    30. MapTileFactory mapTileFactory;
    31. // 使用享元工厂创建并共享地图瓦片
    32. auto tile1 = mapTileFactory.getMapTile(1, 0, 0);
    33. auto tile2 = mapTileFactory.getMapTile(2, 1, 0);
    34. auto tile3 = mapTileFactory.getMapTile(1, 0, 0);
    35. auto tile4 = mapTileFactory.getMapTile(2, 1, 0);
    36. tile1->draw();
    37. tile2->draw();
    38. tile3->draw();
    39. tile4->draw();
    40. return 0;
    41. }

    图形编辑器中的图形对象

    假设正在开发一个图形编辑器,编辑器中有许多图形对象。这时候就可以使用享元模式来共享相同类型的图形对象。

    1. #include
    2. #include
    3. #include
    4. // 享元类:图形对象
    5. class Shape {
    6. protected:
    7. std::string type;
    8. // 非共享状态可以作为外部状态
    9. int x, y;
    10. public:
    11. Shape(const std::string& type, int x, int y) : type(type), x(x), y(y) {}
    12. virtual void draw() const = 0;
    13. };
    14. // 具体享元类:圆形
    15. class Circle : public Shape {
    16. public:
    17. Circle(int x, int y) : Shape("Circle", x, y) {}
    18. void draw() const override {
    19. std::cout << "Drawing " << type << " at position (" << x << ", " << y << ")\n";
    20. }
    21. };
    22. // 享元工厂类
    23. class ShapeFactory {
    24. private:
    25. std::unordered_map> shapes;
    26. public:
    27. std::shared_ptr getShape(const std::string& type, int x, int y) {
    28. if (shapes.find(type) == shapes.end()) {
    29. if (type == "Circle") {
    30. shapes[type] = std::make_shared(x, y);
    31. } // 可以添加更多类型的图形对象
    32. }
    33. return shapes[type];
    34. }
    35. };
    36. int main() {
    37. ShapeFactory shapeFactory;
    38. // 使用享元工厂创建并共享图形对象
    39. auto circle1 = shapeFactory.getShape("Circle", 0, 0);
    40. auto circle2 = shapeFactory.getShape("Circle", 1, 1);
    41. auto circle3 = shapeFactory.getShape("Circle", 0, 0);
    42. auto circle4 = shapeFactory.getShape("Circle", 1, 1);
    43. circle1->draw();
    44. circle2->draw();
    45. circle3->draw();
    46. circle4->draw();
    47. return 0;
    48. }

    总结

    享元模式可以在减少内存使用和提高性能方面发挥作用,因为享元模式可以有效地共享相似对象之间的公共部分,从而减少内存占用。

  • 相关阅读:
    SpringMvc项目部署
    ib中文文学课如何学习重点?
    STM32与陀螺仪、加速度计传感器的数据融合与姿态估计
    【Linux】简易ftp文件传输项目(云盘)
    模仿 mapstruct 实现一个微服务编排框架(上)
    Flask 学习-18.配置管理开发/生产/测试环境
    抖店token的生成和刷新的实际开发笔记
    靠“山寨”发家的名创优品,如今是什么模样
    什么是SpringMVC?它有哪些优点?又是如何执行的?
    【JavaScript】ES6 中class定义类
  • 原文地址:https://blog.csdn.net/GOLOJO/article/details/139596813