模块系统分层架构完整方案

第一部分:完整架构设计方案

1. 整体架构概览
应用层 → 服务层 → 组件管理层 → 基础功能层 → 基础设施层
2. 各层详细职责与模块组成

2.1 基础设施层

  • 主要职责:提供最基础的类型定义、错误码管理和通用工具函数
  • 包含模块
    • error_code:错误码定义和错误管理器
    • common:通用工具类和辅助函数
  • 关键特性:完全独立、高度稳定、被所有上层模块依赖

2.2 基础功能层

  • 主要职责:实现各种基础功能组件
  • 包含模块
    • di:依赖注入容器
    • logger:日志系统
    • config:配置管理(原config_manager)
    • message:消息总线
    • task_core:核心任务调度功能
  • 关键特性:相对独立、功能内聚、可被组件管理层统一管理

2.3 组件管理层

  • 主要职责:管理和协调所有基础功能层的组件,提供统一的组件访问接口和生命周期管理
  • 包含模块
    • component:组件注册和访问接口
    • component_manager:组件管理器(从docs/back迁移)
    • dependency_resolver:依赖解析器(从docs/back迁移)
    • plugin:插件管理系统
  • 关键特性:承上启下、统一管理、解耦服务与基础功能

2.4 服务层

  • 主要职责:组装基础功能组件实现具体业务逻辑
  • 包含模块
    • service:系统服务(如systemserver)
    • task_service:业务任务编排服务
    • framework/rpc:RPC通信框架(从原framework模块提取)
  • 关键特性:业务驱动、组合复用、依赖组件管理层

2.5 应用层

  • 主要职责:应用程序入口,负责初始化系统、启动服务
  • 包含模块
    • application/server:应用程序主入口
  • 关键特性:顶层协调、依赖服务层
3. 模块间依赖关系
  • 基础设施层不依赖其他任何模块
  • 基础功能层仅依赖基础设施层
  • 组件管理层依赖基础设施层和基础功能层
  • 服务层仅依赖组件管理层
  • 应用层仅依赖服务层
  • 组件管理层内部:plugin依赖component提供的基础功能
4. 接口与实现分离规范
  • 为所有主要模块定义清晰的接口(如idi_container.h、iplugin.h等)
  • 实现接口与具体实现的完全分离,便于测试和替换
  • 统一接口命名规范:接口名称以"I"开头,如IConfig、ILogger
5. 初始化与生命周期管理
  • 建立统一的初始化顺序:基础设施层 → 基础功能层 → 组件管理层 → 服务层 → 应用层
  • 实现基于依赖关系的组件自动初始化机制
  • 提供优雅的关闭和资源释放流程

第二部分:可执行实施计划

1. 短期任务(1-2周)

1.1 创建common模块

mkdir -p /home/zx/endoscope/modules/common
cd /home/zx/endoscope/modules/common
# 创建基础文件
cat > common.h << 'EOF'
#ifndef COMMON_H
#define COMMON_H

#include <string>
#include <vector>
#include <memory>
#include <sstream>
#include <chrono>

namespace endoscope {
namespace common {

// 字符串工具类
size_t StringLength(const std::string& str);
std::string StringToUpper(const std::string& str);
std::string StringToLower(const std::string& str);
std::vector<std::string> SplitString(const std::string& str, char delimiter);
bool StringStartsWith(const std::string& str, const std::string& prefix);
bool StringEndsWith(const std::string& str, const std::string& suffix);

// 时间工具类
std::string GetCurrentTimeString();
uint64_t GetCurrentTimestamp();

// 文件工具类
bool FileExists(const std::string& path);
std::string ReadFileToString(const std::string& path);
bool WriteStringToFile(const std::string& path, const std::string& content);

// 类型转换工具
template<typename T>
std::string ToString(const T& value) {
    std::ostringstream oss;
    oss << value;
    return oss.str();
}

template<typename T>
bool TryParse(const std::string& str, T& value) {
    std::istringstream iss(str);
    return (iss >> value) && iss.eof();
}

} // namespace common
} // namespace endoscope

#endif // COMMON_H
EOF

cat > common.cpp << 'EOF'
#include "common.h"
#include <fstream>
#include <algorithm>
#include <cctype>

namespace endoscope {
namespace common {

size_t StringLength(const std::string& str) {
    return str.length();
}

std::string StringToUpper(const std::string& str) {
    std::string result = str;
    std::transform(result.begin(), result.end(), result.begin(), ::toupper);
    return result;
}

std::string StringToLower(const std::string& str) {
    std::string result = str;
    std::transform(result.begin(), result.end(), result.begin(), ::tolower);
    return result;
}

std::vector<std::string> SplitString(const std::string& str, char delimiter) {
    std::vector<std::string> tokens;
    std::string token;
    std::istringstream tokenStream(str);
    while (std::getline(tokenStream, token, delimiter)) {
        tokens.push_back(token);
    }
    return tokens;
}

bool StringStartsWith(const std::string& str, const std::string& prefix) {
    return str.compare(0, prefix.length(), prefix) == 0;
}

bool StringEndsWith(const std::string& str, const std::string& suffix) {
    if (suffix.length() > str.length()) {
        return false;
    }
    return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
}

std::string GetCurrentTimeString() {
    auto now = std::chrono::system_clock::now();
    auto time_t_now = std::chrono::system_clock::to_time_t(now);
    std::tm tm_now;
    #ifdef _WIN32
    localtime_s(&tm_now, &time_t_now);
    #else
    localtime_r(&time_t_now, &tm_now);
    #endif
    char buffer[30];
    std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm_now);
    return std::string(buffer);
}

uint64_t GetCurrentTimestamp() {
    auto now = std::chrono::system_clock::now();
    auto duration = now.time_since_epoch();
    return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
}

bool FileExists(const std::string& path) {
    std::ifstream file(path);
    return file.good();
}

std::string ReadFileToString(const std::string& path) {
    std::ifstream file(path);
    if (!file.is_open()) {
        return "";
    }
    return std::string((std::istreambuf_iterator<char>(file)),
                      std::istreambuf_iterator<char>());
}

bool WriteStringToFile(const std::string& path, const std::string& content) {
    std::ofstream file(path);
    if (!file.is_open()) {
        return false;
    }
    file << content;
    file.close();
    return true;
}

} // namespace common
} // namespace endoscope
EOF

# 创建BUILD文件和README.md
cat > BUILD << 'EOF'
cc_library(
    name = "common",
    srcs = ["common.cpp"],
    hdrs = ["common.h"],
    visibility = ["//visibility:public"],
)
EOF

cat > README.md << 'EOF'
# Common 模块

提供通用工具类和辅助函数,包括字符串处理、时间工具、文件操作等。

## 主要功能
- 字符串处理(大小写转换、分割、判断前缀后缀等)
- 时间工具(获取当前时间、时间戳等)
- 文件操作(检查文件是否存在、读写文件等)
- 类型转换工具

## 使用示例
```cpp
#include "modules/common/common.h"

// 字符串操作
auto upperStr = endoscope::common::StringToUpper("hello");

// 获取当前时间字符串
auto now = endoscope::common::GetCurrentTimeString();

// 文件操作
bool exists = endoscope::common::FileExists("config.json");

EOF


**1.2 迁移component_manager和dependency_resolver**
```bash
# 从docs/back迁移component_manager
cp /home/zx/endoscope/docs/back/component_manager.h /home/zx/endoscope/modules/component/
cp /home/zx/endoscope/docs/back/component_manager.cpp /home/zx/endoscope/modules/component/

# 从docs/back迁移dependency_resolver
cp /home/zx/endoscope/docs/back/dependency_resolver.h /home/zx/endoscope/modules/component/
cp /home/zx/endoscope/docs/back/dependency_resolver.cpp /home/zx/endoscope/modules/component/

# 更新component模块的BUILD文件
cat > /home/zx/endoscope/modules/component/BUILD << 'EOF'
cc_library(
    name = "component",
    srcs = [
        "component_manager.cpp",
        "dependency_resolver.cpp",
    ],
    hdrs = [
        "component.h",
        "component_manager.h",
        "dependency_resolver.h",
    ],
    deps = [
        "//modules/common",
        "//modules/di",
        "//modules/error_code",
    ],
    visibility = ["//visibility:public"],
)
EOF

1.3 重命名config_manager为config

# 重命名目录
mv /home/zx/endoscope/modules/config_manager /home/zx/endoscope/modules/config

# 更新BUILD文件中的模块名
sed -i 's/config_manager/config/g' /home/zx/endoscope/modules/config/BUILD
2. 中期任务(2-4周)

2.1 重构plugin模块,增强与component模块的集成

// 修改iplugin.h,增加对组件管理的支持
// /home/zx/endoscope/modules/plugin/iplugin.h
class IPlugin {
public:
    virtual ~IPlugin() = default;
    
    // 插件元数据
    virtual PluginMeta GetMeta() const = 0;
    
    // 初始化插件,注入组件管理器
    virtual bool Initialize(std::shared_ptr<ComponentManager> componentManager) = 0;
    
    // 启动插件
    virtual bool Start() = 0;
    
    // 停止插件
    virtual bool Stop() = 0;
    
    // 获取插件提供的扩展点
    virtual std::vector<std::string> GetExtensionPoints() const = 0;
    
    // 通过扩展点获取功能接口
    virtual void* GetExtension(const std::string& extensionPoint) = 0;
};

2.2 优化task模块,拆分为task_core和task_service

# 创建task_core目录
mkdir -p /home/zx/endoscope/modules/task_core
cd /home/zx/endoscope/modules/task_core

# 移动核心任务调度代码
sed -n '1,/// 业务任务编排/ p' /home/zx/endoscope/modules/task/task_scheduler.cpp > task_scheduler_core.cpp
# 注意:需要手动调整代码,确保只包含核心调度功能

# 创建task_service目录
mkdir -p /home/zx/endoscope/service/task_service
cd /home/zx/endoscope/service/task_service

# 移动业务任务编排代码
# 从task模块提取业务相关代码,创建task_service实现

2.3 重构service模块,使其通过组件管理层访问基础功能

// 修改systemserver.cpp,使用组件管理器获取基础功能
// /home/zx/endoscope/service/systemserver.cpp
bool SystemServer::Initialize() {
    // 获取组件管理器
    componentManager_ = ComponentManager::GetInstance();
    
    // 通过组件管理器获取日志系统
    logger_ = componentManager_->GetComponent<Logger>("logger");
    if (!logger_) {
        // 处理错误
        return false;
    }
    
    // 通过组件管理器获取配置系统
    config_ = componentManager_->GetComponent<Config>("config");
    if (!config_) {
        // 处理错误
        return false;
    }
    
    // 初始化其他服务
    return true;
}
3. 长期任务(4-8周)

3.1 完善各模块的接口与实现分离

  • 为所有主要模块创建接口定义文件
  • 重构现有实现,使其依赖接口而非具体实现

3.2 优化系统初始化流程

// 修改application/server/main.cpp,实现基于依赖的初始化
int main() {
    // 1. 初始化基础设施层
    ErrorManager::GetInstance()->Initialize();
    
    // 2. 初始化基础功能层
    auto diContainer = DIContainer::GetInstance();
    diContainer->Initialize();
    
    // 3. 初始化组件管理层
    auto componentManager = ComponentManager::GetInstance();
    componentManager->Initialize(diContainer);
    
    // 4. 初始化服务层
    auto systemServer = std::make_shared<SystemServer>();
    if (!systemServer->Initialize(componentManager)) {
        return -1;
    }
    
    // 5. 启动系统
    if (!systemServer->Start()) {
        return -1;
    }
    
    // 6. 等待退出信号
    // ...
    
    // 7. 优雅关闭
    systemServer->Stop();
    componentManager->Shutdown();
    diContainer->Shutdown();
    
    return 0;
}

3.3 完善测试系统

  • 增加单元测试覆盖率
  • 完善集成测试和性能测试
  • 创建自动化测试脚本

3.4 文档完善

  • 为每个模块编写详细的API文档和使用示例
  • 创建架构设计文档,明确各层职责和依赖关系
  • 编写开发指南和最佳实践文档

总结

这套完整方案包括了系统的架构设计和具体的实施计划。架构设计明确了各层的职责和模块组成,实施计划则提供了短期、中期和长期的具体任务,确保架构调整能够有序进行。通过这些优化,系统将变得更加模块化、可维护和可扩展,能够更好地支持业务需求的变化和系统的持续演进。

Logo

智能硬件社区聚焦AI智能硬件技术生态,汇聚嵌入式AI、物联网硬件开发者,打造交流分享平台,同步全国赛事资讯、开展 OPC 核心人才招募,助力技术落地与开发者成长。

更多推荐