C++设计模式:享元模式

享元模式

享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式。

享元模式尝试重用现有的同类对象,如果未找到匹配的对象,则创建新对象。

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,C++设计模式,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

使用场景

系统中存在大量的相似对象。

细粒度的对象都具备较接近的外部状态,而且内部状态与环境无关,也就是说对象没有特定身份。

需要缓冲池的场景。

优缺点

优点:

大大减少对象的创建,降低系统的内存,使效率提高。

缺点:

提高了系统的复杂度,需要分离出外部状态和内部状态,而且外部状态具有固有化的性质,不应该随着内部状态的变化而变化,否则会造成系统的混乱。

注意事项

1、注意划分外部状态和内部状态,否则可能会引起线程安全问题。

2、这些类必须有一个工厂对象加以控制。

UML结构图

 

代码实现

interface.h
创建抽象类 - 图形; 创建具体类 - 圆形

#include <string>
#include <iostream>
using namespace std;

class Shape //基类-图形
{
public:
    Shape() {}
    virtual ~Shape() {}

    virtual void draw() = 0;
};

class Circle: public Shape  //子类-圆形
{
public:
    Circle(string color) : color(color) {}

    void setX(int x)
    {
        this->x = x;
        cout << "set x success" << endl;
    }

    void setY(int y)
    {
        this->y = y;
        cout << "set y success" << endl;
    }

    void setRadius(int radius)
    {
        this->radius = radius;
        cout << "set radius success" << endl;
    }

    void draw() {
        cout << "draw Circle [color: + " + color +
                ", x: " + std::to_string(x) +
                ", y: " + std::to_string(y) +
                ", radius: " + std::to_string(radius) + "]" << endl << endl;
    }

private:
    int x;
    int y;
    int radius;
    string color;
};

shapefactory.h
创建工厂类用来控制图形,可创建获取图形

#include "interface.h"
#include <map>

class ShapeFactory
{
public:
    std::map<string, Shape *> map;

    auto getCircle(string color) -> Shape * {
        //这种方法很直观,但是效率差很多,因为需要执行两次查找。
//        if (map.count(color) > 0)
//        {
//            return map[color];
//        }

        auto iter = map.find(color);
        if (iter != map.end())
        {
            return iter->second;
        }
        else
        {
            Shape * circle = new Circle(color);
            map.insert(std::make_pair(color, circle));
            cout << "Creating circle of color : " + color << endl;
            return circle;
        }
    }
};

main.cpp
实例应用 - 通过工厂类来获取 Shape 类,可根据条件减少创建对象的数量,以减少内存占用和提高性能

#include "shapefactory.h"

int main()
{
    ShapeFactory shapeFactory;
    string array[] = { "Red", "Blue", "Red", "Yellow", "Blue" };

    for (auto color : array)
    {
        int x = rand() % 100;
        int y = rand() % 100;
        int radius = rand() % 100;

        Circle * circle = static_cast<Circle *>(shapeFactory.getCircle(color));
        circle->setX(x);
        circle->setY(y);
        circle->setRadius(radius);
        circle->draw();
    }

    return 0;
}

运行结果:
Creating circle of color : Red
set x success
set y success
set radius success
draw Circle [color: + Red, x: 41, y: 67, radius: 34]

Creating circle of color : Blue
set x success
set y success
set radius success
draw Circle [color: + Blue, x: 0, y: 69, radius: 24]

set x success
set y success
set radius success
draw Circle [color: + Red, x: 78, y: 58, radius: 62]

Creating circle of color : Yellow
set x success
set y success
set radius success
draw Circle [color: + Yellow, x: 64, y: 5, radius: 45]

set x success
set y success
set radius success
draw Circle [color: + Blue, x: 81, y: 27, radius: 61]

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,C++设计模式,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值