493 lines
13 KiB
Markdown
493 lines
13 KiB
Markdown
# QTradeProgram 架构优化设计方案
|
||
|
||
## 1. 模块解耦设计方案
|
||
|
||
### 1.1 订阅管理器类 (SubscriptionManager)
|
||
|
||
```cpp
|
||
// 新建文件: Sqbase/qsubscriptionmanager.h
|
||
#ifndef QSUBSCRIPTIONMANAGER_H
|
||
#define QSUBSCRIPTIONMANAGER_H
|
||
|
||
#include <QObject>
|
||
#include <QList>
|
||
#include <QMap>
|
||
#include <QSharedPointer>
|
||
#include "BZStruct.h"
|
||
|
||
struct SubscriptionConfig {
|
||
QString stockCode;
|
||
double threshold; // 检测阈值
|
||
bool enabled;
|
||
QDateTime lastUpdated;
|
||
};
|
||
|
||
class QSubscriptionManager : public QObject
|
||
{
|
||
Q_OBJECT
|
||
public:
|
||
static QSubscriptionManager* instance();
|
||
|
||
// 订阅管理
|
||
bool addSubscription(const QString& stockCode, double threshold);
|
||
bool removeSubscription(const QString& stockCode);
|
||
bool updateThreshold(const QString& stockCode, double threshold);
|
||
|
||
// 查询接口
|
||
QList<SubscriptionConfig> getAllSubscriptions() const;
|
||
double getThreshold(const QString& stockCode) const;
|
||
bool isSubscribed(const QString& stockCode) const;
|
||
|
||
// 配置持久化
|
||
void loadFromFile(const QString& filePath = "");
|
||
void saveToFile(const QString& filePath = "");
|
||
|
||
signals:
|
||
void subscriptionAdded(const QString& stockCode, double threshold);
|
||
void subscriptionRemoved(const QString& stockCode);
|
||
void thresholdUpdated(const QString& stockCode, double threshold);
|
||
void subscriptionsChanged();
|
||
|
||
private:
|
||
explicit QSubscriptionManager(QObject *parent = nullptr);
|
||
~QSubscriptionManager();
|
||
|
||
QMap<QString, SubscriptionConfig> m_subscriptions;
|
||
QString m_configPath;
|
||
};
|
||
|
||
#endif // QSUBSCRIPTIONMANAGER_H
|
||
```
|
||
|
||
### 1.2 UI逻辑与业务逻辑分离
|
||
|
||
#### 重构后的QMainwindow职责:
|
||
```cpp
|
||
// QMainwindow 主要负责:
|
||
// 1. UI组件初始化和布局
|
||
// 2. 用户输入处理
|
||
// 3. 视图更新
|
||
// 4. 事件转发到业务层
|
||
```
|
||
|
||
#### 新建业务逻辑层:
|
||
```cpp
|
||
// 新建文件: Sqbase/qtradecore.h
|
||
#ifndef QTRADECORE_H
|
||
#define QTRADECORE_H
|
||
|
||
#include <QObject>
|
||
#include "qsubscriptionmanager.h"
|
||
#include "qorderprocessor.h"
|
||
#include "qbigordermanager.h"
|
||
#include "qlogmanager.h"
|
||
|
||
class QTradeCore : public QObject
|
||
{
|
||
Q_OBJECT
|
||
public:
|
||
static QTradeCore* instance();
|
||
|
||
// 核心服务访问
|
||
QSubscriptionManager* subscriptionManager();
|
||
QOrderProcessor* orderProcessor();
|
||
QBigOrderManager* bigOrderManager();
|
||
QLogManager* logManager();
|
||
|
||
// 系统控制
|
||
void initialize();
|
||
void shutdown();
|
||
|
||
signals:
|
||
void systemInitialized();
|
||
void systemShutdown();
|
||
void errorOccurred(const QString& error);
|
||
|
||
private:
|
||
explicit QTradeCore(QObject *parent = nullptr);
|
||
~QTradeCore();
|
||
|
||
QSubscriptionManager* m_subscriptionManager;
|
||
QOrderProcessor* m_orderProcessor;
|
||
QBigOrderManager* m_bigOrderManager;
|
||
QLogManager* m_logManager;
|
||
};
|
||
|
||
#endif // QTRADECORE_H
|
||
```
|
||
|
||
## 2. 事件总线模式实现方案
|
||
|
||
### 2.1 事件总线核心类
|
||
|
||
```cpp
|
||
// 新建文件: Sqbase/qeventbus.h
|
||
#ifndef QEVENTBUS_H
|
||
#define QEVENTBUS_H
|
||
|
||
#include <QObject>
|
||
#include <QMap>
|
||
#include <QList>
|
||
#include <QMutex>
|
||
#include <QVariant>
|
||
|
||
// 事件类型定义
|
||
namespace EventType {
|
||
const QString ORDER_PROCESSED = "order.processed";
|
||
const QString BIG_ORDER_DETECTED = "bigorder.detected";
|
||
const QString SUBSCRIPTION_CHANGED = "subscription.changed";
|
||
const QString CONNECTION_STATUS_CHANGED = "connection.status.changed";
|
||
const QString SYSTEM_ERROR = "system.error";
|
||
}
|
||
|
||
struct Event {
|
||
QString type;
|
||
QVariant data;
|
||
QDateTime timestamp;
|
||
QString source;
|
||
};
|
||
|
||
class QEventBus : public QObject
|
||
{
|
||
Q_OBJECT
|
||
public:
|
||
static QEventBus* instance();
|
||
|
||
// 事件发布/订阅
|
||
void publish(const QString& eventType, const QVariant& data = QVariant(),
|
||
const QString& source = "");
|
||
void subscribe(const QString& eventType, QObject* receiver, const char* method);
|
||
void unsubscribe(const QString& eventType, QObject* receiver);
|
||
void unsubscribeAll(QObject* receiver);
|
||
|
||
// 事件历史
|
||
QList<Event> getEventHistory(const QString& type = "", int limit = 100) const;
|
||
void clearEventHistory();
|
||
|
||
signals:
|
||
void eventPublished(const Event& event);
|
||
|
||
private:
|
||
explicit QEventBus(QObject *parent = nullptr);
|
||
~QEventBus();
|
||
|
||
struct Subscription {
|
||
QObject* receiver;
|
||
QByteArray method;
|
||
};
|
||
|
||
QMap<QString, QList<Subscription>> m_subscriptions;
|
||
QList<Event> m_eventHistory;
|
||
mutable QMutex m_mutex;
|
||
int m_maxHistorySize = 1000;
|
||
};
|
||
|
||
#endif // QEVENTBUS_H
|
||
```
|
||
|
||
### 2.2 事件总线使用示例
|
||
|
||
```cpp
|
||
// 在QOrderProcessor中发布事件
|
||
void QOrderProcessor::processOrderBook(const Qot_UpdateOrderBook::Response &stRsp)
|
||
{
|
||
// 原有处理逻辑...
|
||
|
||
// 发布处理完成事件
|
||
QEventBus::instance()->publish(EventType::ORDER_PROCESSED,
|
||
QVariant::fromValue(orderData), "QOrderProcessor");
|
||
|
||
// 如果检测到大单,发布大单事件
|
||
if (bigOrderInfo.isBigOrder) {
|
||
QEventBus::instance()->publish(EventType::BIG_ORDER_DETECTED,
|
||
QVariant::fromValue(bigOrderInfo), "QOrderProcessor");
|
||
}
|
||
}
|
||
|
||
// 在QBigOrderViewer中订阅事件
|
||
void QBigOrderViewer::initConnections()
|
||
{
|
||
QEventBus::instance()->subscribe(EventType::BIG_ORDER_DETECTED,
|
||
this, SLOT(onBigOrderDetected(QVariant)));
|
||
|
||
QEventBus::instance()->subscribe(EventType::SUBSCRIPTION_CHANGED,
|
||
this, SLOT(onSubscriptionChanged(QVariant)));
|
||
}
|
||
|
||
void QBigOrderViewer::onBigOrderDetected(const QVariant& data)
|
||
{
|
||
BigOrderInfo order = data.value<BigOrderInfo>();
|
||
// 处理大单显示...
|
||
}
|
||
```
|
||
|
||
## 3. 插件化架构设计方案
|
||
|
||
### 3.1 插件接口定义
|
||
|
||
```cpp
|
||
// 新建文件: Sqbase/qplugininterface.h
|
||
#ifndef QPLUGININTERFACE_H
|
||
#define QPLUGININTERFACE_H
|
||
|
||
#include <QObject>
|
||
#include <QString>
|
||
#include <QVariantMap>
|
||
|
||
// 插件类型
|
||
namespace PluginType {
|
||
const QString DETECTION_STRATEGY = "detection.strategy";
|
||
const QString DATA_SOURCE = "data.source";
|
||
const QString DISPLAY_COMPONENT = "display.component";
|
||
const QString NOTIFICATION_HANDLER = "notification.handler";
|
||
}
|
||
|
||
class QPluginInterface
|
||
{
|
||
public:
|
||
virtual ~QPluginInterface() = default;
|
||
|
||
// 插件基本信息
|
||
virtual QString pluginId() const = 0;
|
||
virtual QString pluginName() const = 0;
|
||
virtual QString pluginVersion() const = 0;
|
||
virtual QString pluginType() const = 0;
|
||
virtual QString pluginDescription() const = 0;
|
||
|
||
// 插件生命周期
|
||
virtual bool initialize(const QVariantMap& config = QVariantMap()) = 0;
|
||
virtual void shutdown() = 0;
|
||
virtual bool isInitialized() const = 0;
|
||
|
||
// 插件配置
|
||
virtual QVariantMap getConfig() const = 0;
|
||
virtual bool setConfig(const QVariantMap& config) = 0;
|
||
};
|
||
|
||
// 检测策略插件接口
|
||
class QDetectionStrategyInterface : public QPluginInterface
|
||
{
|
||
public:
|
||
virtual QVector<BigOrderInfo> detect(const OrderBookData& data) = 0;
|
||
virtual QVariantMap getStrategyConfig() const = 0;
|
||
virtual bool setStrategyConfig(const QVariantMap& config) = 0;
|
||
};
|
||
|
||
// 数据源插件接口
|
||
class QDataSourceInterface : public QPluginInterface
|
||
{
|
||
public:
|
||
virtual bool connectToSource() = 0;
|
||
virtual void disconnectFromSource() = 0;
|
||
virtual bool isConnected() const = 0;
|
||
virtual QVariantMap getSourceInfo() const = 0;
|
||
};
|
||
|
||
Q_DECLARE_INTERFACE(QPluginInterface, "com.qtrade.plugin/1.0")
|
||
Q_DECLARE_INTERFACE(QDetectionStrategyInterface, "com.qtrade.plugin.detection/1.0")
|
||
Q_DECLARE_INTERFACE(QDataSourceInterface, "com.qtrade.plugin.datasource/1.0")
|
||
|
||
#endif // QPLUGININTERFACE_H
|
||
```
|
||
|
||
### 3.2 插件管理器
|
||
|
||
```cpp
|
||
// 新建文件: Sqbase/qpluginmanager.h
|
||
#ifndef QPLUGINMANAGER_H
|
||
#define QPLUGINMANAGER_H
|
||
|
||
#include <QObject>
|
||
#include <QMap>
|
||
#include <QDir>
|
||
#include <QPluginLoader>
|
||
#include "qplugininterface.h"
|
||
|
||
class QPluginManager : public QObject
|
||
{
|
||
Q_OBJECT
|
||
public:
|
||
static QPluginManager* instance();
|
||
|
||
// 插件管理
|
||
bool loadPlugin(const QString& filePath);
|
||
bool unloadPlugin(const QString& pluginId);
|
||
void loadAllPlugins(const QString& pluginsDir = "");
|
||
|
||
// 插件查询
|
||
QList<QString> getLoadedPlugins() const;
|
||
QPluginInterface* getPlugin(const QString& pluginId) const;
|
||
QList<QPluginInterface*> getPluginsByType(const QString& type) const;
|
||
|
||
// 插件配置
|
||
QVariantMap getPluginConfig(const QString& pluginId) const;
|
||
bool setPluginConfig(const QString& pluginId, const QVariantMap& config);
|
||
|
||
// 策略管理
|
||
QDetectionStrategyInterface* getDetectionStrategy(const QString& strategyId);
|
||
void setActiveDetectionStrategy(const QString& strategyId);
|
||
QDetectionStrategyInterface* getActiveDetectionStrategy() const;
|
||
|
||
signals:
|
||
void pluginLoaded(const QString& pluginId);
|
||
void pluginUnloaded(const QString& pluginId);
|
||
void pluginError(const QString& pluginId, const QString& error);
|
||
|
||
private:
|
||
explicit QPluginManager(QObject *parent = nullptr);
|
||
~QPluginManager();
|
||
|
||
struct PluginInfo {
|
||
QPluginLoader* loader;
|
||
QPluginInterface* instance;
|
||
QString filePath;
|
||
bool enabled;
|
||
};
|
||
|
||
QMap<QString, PluginInfo> m_plugins;
|
||
QString m_activeDetectionStrategy;
|
||
QString m_pluginsDirectory;
|
||
};
|
||
|
||
#endif // QPLUGINMANAGER_H
|
||
```
|
||
|
||
### 3.3 具体插件实现示例
|
||
|
||
#### 3.3.1 基于成交量的检测策略插件
|
||
```cpp
|
||
// 新建文件: plugins/volumedetectionplugin.h
|
||
#ifndef VOLUMEDETECTIONPLUGIN_H
|
||
#define VOLUMEDETECTIONPLUGIN_H
|
||
|
||
#include "qplugininterface.h"
|
||
#include "qdetectionstrategyinterface.h"
|
||
|
||
class VolumeDetectionPlugin : public QObject, public QDetectionStrategyInterface
|
||
{
|
||
Q_OBJECT
|
||
Q_INTERFACES(QPluginInterface QDetectionStrategyInterface)
|
||
Q_PLUGIN_METADATA(IID "com.qtrade.plugin.detection.volume" FILE "volumedetection.json")
|
||
|
||
public:
|
||
// QPluginInterface 实现
|
||
QString pluginId() const override { return "volume_detection"; }
|
||
QString pluginName() const override { return "Volume Based Detection"; }
|
||
QString pluginVersion() const override { return "1.0.0"; }
|
||
QString pluginType() const override { return PluginType::DETECTION_STRATEGY; }
|
||
QString pluginDescription() const override { return "Volume based big order detection strategy"; }
|
||
|
||
bool initialize(const QVariantMap& config = QVariantMap()) override;
|
||
void shutdown() override;
|
||
bool isInitialized() const override { return m_initialized; }
|
||
|
||
QVariantMap getConfig() const override;
|
||
bool setConfig(const QVariantMap& config) override;
|
||
|
||
// QDetectionStrategyInterface 实现
|
||
QVector<BigOrderInfo> detect(const OrderBookData& data) override;
|
||
QVariantMap getStrategyConfig() const override;
|
||
bool setStrategyConfig(const QVariantMap& config) override;
|
||
|
||
private:
|
||
bool m_initialized = false;
|
||
double m_volumeThreshold = 1000.0;
|
||
double m_volumeRatio = 0.1;
|
||
};
|
||
|
||
#endif // VOLUMEDETECTIONPLUGIN_H
|
||
```
|
||
|
||
#### 3.3.2 多数据源支持插件
|
||
```cpp
|
||
// 新建文件: plugins/multisourcedataprovider.h
|
||
#ifndef MULTISOURCEDATAPROVIDER_H
|
||
#define MULTISOURCEDATAPROVIDER_H
|
||
|
||
#include "qplugininterface.h"
|
||
#include "qdatasourceinterface.h"
|
||
|
||
class MultiSourceDataProvider : public QObject, public QDataSourceInterface
|
||
{
|
||
Q_OBJECT
|
||
Q_INTERFACES(QPluginInterface QDataSourceInterface)
|
||
Q_PLUGIN_METADATA(IID "com.qtrade.plugin.datasource.multi" FILE "multisource.json")
|
||
|
||
public:
|
||
// QPluginInterface 实现
|
||
QString pluginId() const override { return "multi_source_provider"; }
|
||
QString pluginName() const override { return "Multi-Source Data Provider"; }
|
||
// ... 其他接口实现
|
||
|
||
// QDataSourceInterface 实现
|
||
bool connectToSource() override;
|
||
void disconnectFromSource() override;
|
||
bool isConnected() const override { return m_connected; }
|
||
QVariantMap getSourceInfo() const override;
|
||
|
||
private slots:
|
||
void onFutuDataReceived(const QVariant& data);
|
||
void onOtherSourceDataReceived(const QVariant& data);
|
||
|
||
private:
|
||
bool m_connected = false;
|
||
bool m_useFutu = true;
|
||
bool m_useOtherSource = false;
|
||
};
|
||
|
||
#endif // MULTISOURCEDATAPROVIDER_H
|
||
```
|
||
|
||
## 4. 重构实施步骤
|
||
|
||
### 4.1 第一阶段:引入事件总线
|
||
1. 实现QEventBus类
|
||
2. 逐步将直接信号槽连接替换为事件总线
|
||
3. 测试事件发布/订阅功能
|
||
|
||
### 4.2 第二阶段:模块解耦
|
||
1. 实现QSubscriptionManager和QTradeCore
|
||
2. 重构QMainwindow,分离UI和业务逻辑
|
||
3. 更新相关组件使用新的管理器
|
||
|
||
### 4.3 第三阶段:插件化架构
|
||
1. 实现插件接口和插件管理器
|
||
2. 开发基础插件(成交量检测、多数据源)
|
||
3. 将现有功能逐步迁移到插件架构
|
||
|
||
### 4.4 第四阶段:集成测试
|
||
1. 全面测试新架构的功能
|
||
2. 性能测试和优化
|
||
3. 文档更新和用户培训
|
||
|
||
## 5. 预期收益
|
||
|
||
### 5.1 架构改善
|
||
- **解耦性**: 各模块职责清晰,降低耦合度
|
||
- **扩展性**: 支持动态加载新功能模块
|
||
- **维护性**: 代码结构清晰,易于理解和维护
|
||
|
||
### 5.2 功能增强
|
||
- **策略多样性**: 支持多种检测算法并存
|
||
- **数据源灵活性**: 支持多个数据源同时接入
|
||
- **用户体验**: 支持自定义界面组件
|
||
|
||
### 5.3 开发效率
|
||
- **并行开发**: 不同团队可独立开发插件
|
||
- **快速迭代**: 插件可独立更新和部署
|
||
- **测试便利**: 插件可独立测试验证
|
||
|
||
## 6. 兼容性考虑
|
||
|
||
### 6.1 向后兼容
|
||
- 保持现有API接口不变
|
||
- 提供适配层支持旧代码
|
||
- 分阶段迁移,确保系统稳定
|
||
|
||
### 6.2 配置迁移
|
||
- 自动迁移现有配置到新架构
|
||
- 提供配置转换工具
|
||
- 保留回滚机制
|
||
|
||
此设计方案为架构优化的详细技术方案,实施前建议进行原型验证和详细设计评审。
|