网站建设免费维护内容,天津做艺术品的网站,游戏网站设计模板,步骤英文引言#xff1a;
编写一个与具体业务无关的示例代码。这个示例代码主要体现以下几个设计思想和模式#xff1a; 接口模式#xff08;Interface Pattern#xff09;#xff1a;定义接口类#xff0c;并让具体实现类去实现该接口的功能。 工厂模式#xff08;Factory Pa…引言
编写一个与具体业务无关的示例代码。这个示例代码主要体现以下几个设计思想和模式 接口模式Interface Pattern定义接口类并让具体实现类去实现该接口的功能。 工厂模式Factory Pattern根据不同条件动态生成不同的对象实例。 模板方法模式Template Method Pattern父类定义方法的结构子类实现具体逻辑。 多线程处理创建子类继承自QThread并实现线程中的具体逻辑。
示例代码设计 核心逻辑一个简单的日志系统根据日志等级如info、warning、error动态生成不同的日志处理线程并执行相应的日志输出。 工厂模式工厂方法根据日志类型生成不同的处理线程。 模板方法模式每个日志处理线程继承自基类基类定义通用处理逻辑子类实现具体日志输出。
示例代码
1. 日志处理接口定义
cpp复制代码#ifndef LOGHELPERINTERFACE_H
#define LOGHELPERINTERFACE_H
#include QString
#include QVector
class LogHelperInterface
{
public:virtual ~LogHelperInterface() {}
// 记录日志virtual void logMessage(const QString message) 0;
};
#endif // LOGHELPERINTERFACE_H
2. 基础日志引擎类
cpp复制代码#ifndef LOGENGINE_H
#define LOGENGINE_H
#include QMap
#include QThread
#include loghelperinterface.h
class LogEngine : public QObject
{Q_OBJECT
public:LogEngine(int logID, LogHelperInterface* helper);~LogEngine();
void logMessage(const QString message);
static LogEngine* getEngine(const int logID);
private:static QMapint, LogEngine* m_logMap; // 用于存储不同日志引擎实例
int m_logID;LogHelperInterface* m_pHelper;
};
#endif // LOGENGINE_H
3. 基础日志处理线程类
cpp复制代码#ifndef LOGTHREADBASE_H
#define LOGTHREADBASE_H
#include QThread
#include loghelperinterface.h
class LogThreadBase : public QThread
{Q_OBJECT
public:explicit LogThreadBase(LogHelperInterface* helper, QObject* parent nullptr);
static LogThreadBase* createLogHandler(const QString logType, LogHelperInterface* helper);
virtual void handleLog(const QString message) 0;
protected:LogHelperInterface* m_logHelper;
};
#endif // LOGTHREADBASE_H
4. 工厂模式实现
cpp复制代码#include logthreadbase.h
#include infologthread.h
#include warninglogthread.h
#include errorlogthread.h
LogThreadBase* LogThreadBase::createLogHandler(const QString logType, LogHelperInterface* helper)
{if (logType info) {return new InfoLogThread(helper);} else if (logType warning) {return new WarningLogThread(helper);} else if (logType error) {return new ErrorLogThread(helper);}
return nullptr;
}
5. 基础日志处理线程类实现
cpp复制代码#include logthreadbase.h
LogThreadBase::LogThreadBase(LogHelperInterface* helper, QObject* parent): QThread(parent), m_logHelper(helper)
{
}
6. InfoLogThread 具体实现
cpp复制代码#ifndef INFOLOGTHREAD_H
#define INFOLOGTHREAD_H
#include logthreadbase.h
class InfoLogThread : public LogThreadBase
{Q_OBJECT
public:explicit InfoLogThread(LogHelperInterface* helper, QObject* parent nullptr);
void handleLog(const QString message) override;
};
#endif // INFOLOGTHREAD_H
cpp复制代码#include infologthread.h
#include QDebug
InfoLogThread::InfoLogThread(LogHelperInterface* helper, QObject* parent): LogThreadBase(helper, parent)
{
}
void InfoLogThread::handleLog(const QString message)
{qDebug() INFO: message;m_logHelper-logMessage(INFO: message);
}
7. WarningLogThread 具体实现
cpp复制代码#ifndef WARNINGLOGTHREAD_H
#define WARNINGLOGTHREAD_H
#include logthreadbase.h
class WarningLogThread : public LogThreadBase
{Q_OBJECT
public:explicit WarningLogThread(LogHelperInterface* helper, QObject* parent nullptr);
void handleLog(const QString message) override;
};
#endif // WARNINGLOGTHREAD_H
cpp复制代码#include warninglogthread.h
#include QDebug
WarningLogThread::WarningLogThread(LogHelperInterface* helper, QObject* parent): LogThreadBase(helper, parent)
{
}
void WarningLogThread::handleLog(const QString message)
{qDebug() WARNING: message;m_logHelper-logMessage(WARNING: message);
}
8. ErrorLogThread 具体实现
cpp复制代码#ifndef ERRORLOGTHREAD_H
#define ERRORLOGTHREAD_H
#include logthreadbase.h
class ErrorLogThread : public LogThreadBase
{Q_OBJECT
public:explicit ErrorLogThread(LogHelperInterface* helper, QObject* parent nullptr);
void handleLog(const QString message) override;
};
#endif // ERRORLOGTHREAD_H
cpp复制代码#include errorlogthread.h
#include QDebug
ErrorLogThread::ErrorLogThread(LogHelperInterface* helper, QObject* parent): LogThreadBase(helper, parent)
{
}
void ErrorLogThread::handleLog(const QString message)
{qDebug() ERROR: message;m_logHelper-logMessage(ERROR: message);
}
9. 日志记录实现类
cpp复制代码#ifndef SIMPLELOGHELPER_H
#define SIMPLELOGHELPER_H
#include loghelperinterface.h
#include QDebug
class SimpleLogHelper : public LogHelperInterface
{
public:void logMessage(const QString message) override{// 这里我们简单将日志输出到控制台qDebug() Logging message: message;}
};
#endif // SIMPLELOGHELPER_H
10. 主函数示例
cpp复制代码#include QCoreApplication
#include logengine.h
#include simpleloghelper.h
#include logthreadbase.h
int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);
SimpleLogHelper logHelper;
// 创建日志引擎LogEngine* logEngine new LogEngine(1, logHelper);
// 生成不同的日志处理线程LogThreadBase* infoLogThread LogThreadBase::createLogHandler(info, logHelper);LogThreadBase* warningLogThread LogThreadBase::createLogHandler(warning, logHelper);LogThreadBase* errorLogThread LogThreadBase::createLogHandler(error, logHelper);
// 处理日志infoLogThread-handleLog(This is an info message);warningLogThread-handleLog(This is a warning message);errorLogThread-handleLog(This is an error message);
return a.exec();
}
总结 接口模式LogHelperInterface 是接口SimpleLogHelper 实现了这个接口用于处理日志输出。 工厂模式LogThreadBase::createLogHandler 工厂方法根据传入的日志类型动态生成不同的日志处理线程如InfoLogThreadWarningLogThreadErrorLogThread。 模板方法模式LogThreadBase 作为抽象基类定义了日志处理的通用接口具体实现由子类完成。
通过这个示例展示了如何使用这些设计模式来构建一个灵活、可扩展的系统。 文章转载自: http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.pkggl.cn.gov.cn.pkggl.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.twdkt.cn.gov.cn.twdkt.cn http://www.morning.zmyzt.cn.gov.cn.zmyzt.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.xrwsg.cn.gov.cn.xrwsg.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.xnkb.cn.gov.cn.xnkb.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.kfcz.cn.gov.cn.kfcz.cn http://www.morning.ampingdu.com.gov.cn.ampingdu.com http://www.morning.crxdn.cn.gov.cn.crxdn.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.fkgqn.cn.gov.cn.fkgqn.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.dpgdj.cn.gov.cn.dpgdj.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.ypjjh.cn.gov.cn.ypjjh.cn http://www.morning.bzqnp.cn.gov.cn.bzqnp.cn http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.fsqbx.cn.gov.cn.fsqbx.cn http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn http://www.morning.swwpl.cn.gov.cn.swwpl.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.dfhkh.cn.gov.cn.dfhkh.cn http://www.morning.jlrym.cn.gov.cn.jlrym.cn http://www.morning.yymlk.cn.gov.cn.yymlk.cn http://www.morning.qyhcg.cn.gov.cn.qyhcg.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn http://www.morning.kflzy.cn.gov.cn.kflzy.cn http://www.morning.qkrz.cn.gov.cn.qkrz.cn http://www.morning.mnqz.cn.gov.cn.mnqz.cn http://www.morning.xgchm.cn.gov.cn.xgchm.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.hdzty.cn.gov.cn.hdzty.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.mspqw.cn.gov.cn.mspqw.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.pyxtn.cn.gov.cn.pyxtn.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.pxlql.cn.gov.cn.pxlql.cn http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.gpmrj.cn.gov.cn.gpmrj.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.wyjpt.cn.gov.cn.wyjpt.cn http://www.morning.bnygf.cn.gov.cn.bnygf.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.wjplr.cn.gov.cn.wjplr.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.jstggt.cn.gov.cn.jstggt.cn http://www.morning.rksnk.cn.gov.cn.rksnk.cn http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.synlt.cn.gov.cn.synlt.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.nbsfb.cn.gov.cn.nbsfb.cn http://www.morning.lxlfr.cn.gov.cn.lxlfr.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.ljbpk.cn.gov.cn.ljbpk.cn http://www.morning.wglhz.cn.gov.cn.wglhz.cn http://www.morning.sogou66.cn.gov.cn.sogou66.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.qnbgk.cn.gov.cn.qnbgk.cn http://www.morning.kfrhh.cn.gov.cn.kfrhh.cn http://www.morning.yqqxj1.cn.gov.cn.yqqxj1.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.xgjhy.cn.gov.cn.xgjhy.cn