Qt C++ flyweight模式
来源:互联网 发布:屏幕录像软件 知乎 编辑:程序博客网 时间:2024/06/05 00:54
flyweight.h
#pragma once
#include <QDebug>
class CFlyweightBase
{
public:
virtual void operate() = 0;
virtual QString getText() const{return m_text;}
protected:
CFlyweightBase(QString text){m_text = text;}
QString m_text;
};
class CConcreateFly : public CFlyweightBase
{
public:
CConcreateFly(QString text):CFlyweightBase(text){}
void operate()
{
qDebug() << m_text;
}
};
class CFlyWeightFactory
{
public:
CFlyWeightFactory(){}
~CFlyWeightFactory()
{
qDeleteAll(m_lst.begin(),m_lst.end());
}
CFlyweightBase* getFlyweight(const QString& text);
private:
QList<CFlyweightBase*> m_lst;
};
flyweight.cpp
#include "flyweight.h"
CFlyweightBase* CFlyWeightFactory::getFlyweight(const QString& text)
{
QListIterator<CFlyweightBase*> iter(m_lst);
while(iter.hasNext())
{
CFlyweightBase* ptr = iter.next();
if (ptr->getText() == text)
{
qDebug() << text << " already exits";
return ptr;
}
}
CFlyweightBase* new_ptr= new CConcreateFly(text);
m_lst.append(new_ptr);
return new_ptr;
}
main.cpp
#include <QApplication>
#include "flyweight.h"
int main(int argc,char **argv)
{
QApplication app(argc,argv);
CFlyWeightFactory *fac = new CFlyWeightFactory;
fac->getFlyweight("hello");
fac->getFlyweight("data");
fac->getFlyweight("hello");
delete fac;
return app.exec();
}
- Qt C++ flyweight模式
- flyweight模式
- Flyweight模式
- FlyWeight模式
- Flyweight模式
- Flyweight模式
- Flyweight 模式
- Flyweight模式
- FlyWeight模式
- FlyWeight模式
- Flyweight 模式
- FlyWeight模式
- FlyWeight模式
- FlyWeight模式
- Flyweight模式
- Flyweight模式
- Flyweight模式
- Flyweight模式
- flex AdvancedDataGrid使用心得
- Android编码规范
- jquery获取时间差、时间格式的代码
- Flex Alert组件使用方法
- vs2010实用快捷键
- Qt C++ flyweight模式
- [转]Android开发必看知识
- Apache mod proxy + Jboss + virtual hosting
- JavaScript设置IFrame高度自适应(兼容多浏览器)
- linux\ubuntunutlinux下ubuntu 为应用程序创建快捷方式
- Using mod_proxy with http/https:
- 最小生成树之kruskal算法
- O/R映射层
- 百分比的算法