接口模式、工厂模式、模板方法模式的日志文件系统

news/2024/10/18 12:39:00/

日志文件系统

编写一个与具体业务无关的示例代码。这个示例代码主要体现以下几个设计思想和模式:

  1. 接口模式(Interface Pattern):定义接口类,并让具体实现类去实现该接口的功能。
  2. 工厂模式(Factory Pattern):根据不同条件动态生成不同的对象实例。
  3. 模板方法模式(Template Method Pattern):父类定义方法的结构,子类实现具体逻辑。
  4. 多线程处理:创建子类继承自QThread,并实现线程中的具体逻辑。

示例代码设计:

  • 核心逻辑:一个简单的日志系统,根据日志等级(如"info"、“warning”、“error”)动态生成不同的日志处理线程,并执行相应的日志输出。
  • 工厂模式:工厂方法根据日志类型生成不同的处理线程。
  • 模板方法模式:每个日志处理线程继承自基类,基类定义通用处理逻辑,子类实现具体日志输出。

示例代码

1. 日志处理接口定义
#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. 基础日志引擎类
#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 QMap<int, LogEngine*> m_logMap;  // 用于存储不同日志引擎实例int m_logID;LogHelperInterface* m_pHelper;
};#endif // LOGENGINE_H
3. 基础日志处理线程类
#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. 工厂模式实现
#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. 基础日志处理线程类实现
#include "logthreadbase.h"LogThreadBase::LogThreadBase(LogHelperInterface* helper, QObject* parent): QThread(parent), m_logHelper(helper)
{
}
6. InfoLogThread 具体实现
#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
#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 具体实现
#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
#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 具体实现
#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
#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. 日志记录实现类
#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. 主函数示例
#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();
}

总结

  1. 接口模式LogHelperInterface 是接口,SimpleLogHelper 实现了这个接口,用于处理日志输出。
  2. 工厂模式LogThreadBase::createLogHandler 工厂方法根据传入的日志类型动态生成不同的日志处理线程(如InfoLogThreadWarningLogThreadErrorLogThread)。
  3. 模板方法模式LogThreadBase 作为抽象基类,定义了日志处理的通用接口,具体实现由子类完成。

通过这个示例,展示了如何使用这些设计模式来构建一个灵活、可扩展的系统。


http://www.ppmy.cn/news/1540002.html

相关文章

面试官:为什么 MySQL 不推荐使用 join?

1、对于mysql&#xff0c;不推荐使用子查询和join是因为本身join的效率就是硬伤&#xff0c;一旦数据量很大效率就很难保证&#xff0c;强烈推荐分别根据索引单表取数据&#xff0c;然后在程序里面做join&#xff0c;merge数据。 2、子查询就更别用了&#xff0c;效率太差&…

C# 3D开发-环境配置-VS Code + Unity-常见错误汇总

VS Code没有作为Unity的默认开发工具或找不到VS Code 在Unity中&#xff0c;请前往&#xff1a;Window&#xff08;窗口&#xff09;> Package Manager&#xff08;包管理器&#xff09;。然后&#xff0c;确保从左上角的下拉菜单中选择“Packages: Unity Registry”&#…

嵌入式系统---看门狗

在嵌入式系统中&#xff0c;看门狗定时器&#xff08;Watchdog Timer&#xff0c;WDT&#xff09;是一种常用的机制&#xff0c;用于检测和恢复系统在异常情况下的故障。如果系统因为某种原因&#xff08;如软件死循环&#xff09;而变得无响应&#xff0c;看门狗定时器可以自动…

ubuntu 安装kali命令补全功能

输入命令时&#xff0c;之前的命令会以阴影显示&#xff0c;按下右键或 Tab 键可以直接补全 安装zsh-autosuggestions sudo apt install zsh-autosuggestions编辑 ~/.zshrc环境变量 if [ -f /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh ]; then. /usr/share/zs…

SpringBoot智能推荐:健康生活新趋势

摘要 随着信息技术在管理上越来越深入而广泛的应用&#xff0c;管理信息系统的实施在技术上已逐步成熟。本文介绍了基于智能推荐的卫生健康系统的开发全过程。通过分析基于智能推荐的卫生健康系统管理的不足&#xff0c;创建了一个计算机管理基于智能推荐的卫生健康系统的方案。…

关于Git Bash中如何定义alias

一、在一次临时Bash会话中使用alias 在Bash中直接输入alias xxdddd&#xff0c;xx为对应要执行的命令的缩写&#xff0c;dddd为要执行的命令&#xff0c;如alias ddcd /d&#xff0c;输入完成后&#xff0c;在Bash中输入dd&#xff0c;即可切换至D盘。 此种设置方式&#xff…

mysql数据同步ES方案---Canal

引言 之前公司开发社交APP的时候 在开发和初上线阶段&#xff0c;我们一直采用的是 MySQL 来存储用户的各种数据&#xff0c;满足基本的查询需求。当时系统业务量小&#xff0c;数据规模有限&#xff0c;因此 MySQL 能很好地支持查询操作&#xff0c;响应速度快&#xff0c;系…

Graphics2D 打包在Linux运行时中文乱码,展示成方格

项目打jar包在服务器运行时&#xff0c;生成的图片中文全是方格&#xff0c;因为Linux上没有对应的字体文件&#xff0c; 安装字体文件 1、在本地Windows电脑上C:\Windows\Fonts 下面的字体文件全部复制&#xff0c;也可以单独复制需要的字体&#xff0c;复制后要删除所有的不…