You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多格式文本解析的最优面向对象(OOP)实现方案咨询

文本文件解析应用的面向对象实现方案

核心实现思路(对应你的两个问题)

  • 1. 基类+子类继承结构是最优基础框架
    必须定义myFileConversion作为抽象基类,让FileConversionCompanyA、FileConversionCompanyB等子类继承它。这是面向对象多态特性的典型应用,能让你在运行时根据intFormat实例化对应子类,统一通过基类指针调用方法,后续新增格式时无需大量修改核心调用逻辑。

  • 2. 通用方法下沉到基类,Execute()设为虚方法

    • 将所有可复用的解析逻辑(比如文件读取、通用行预处理、结果存储等)设为基类的protected方法(子类可调用但外部无法访问),private方法留给基类内部自用逻辑。
    • 把Execute()声明为纯虚方法(若基类无需默认实现)或虚方法,让每个子类重写Execute(),在内部组合基类通用方法与自身特有的解析逻辑。比如子类A的Execute()可以先调用基类的ReadFile(),再执行自己的ParseLineForCompanyA(),最后调用基类的SaveResult()。

额外优化建议

  • 修复现有代码的switch bug
    你的switch语句缺少break,会导致case 1执行后继续走到case 2,最终myFC总是被赋值为FileConversionCompanyB的实例,必须补充:

    int intFormat = 1; // Loaded from INI file
    
    void __fastcall TForm1::Button1Click(TObject *Sender) {
        myFileConversion *myFC = nullptr;
        switch(intFormat) {
            case 1:
                myFC = new FileConversionCompanyA(); 
                break;
            case 2:
                myFC = new FileConversionCompanyB();
                break;
            default:
                // 处理未知格式,比如抛出异常或提示错误
                throw std::invalid_argument("Unsupported format type");
        }
        if (myFC) {
            myFC->Execute("fileName");
            delete myFC; // 记得释放内存
        }
    }
    
  • 用智能指针简化内存管理
    手动new/delete容易遗漏导致内存泄漏,建议改用智能指针自动管理:

    #include <memory>
    
    void __fastcall TForm1::Button1Click(TObject *Sender) {
        std::unique_ptr<myFileConversion> myFC;
        switch(intFormat) {
            case 1:
                myFC = std::make_unique<FileConversionCompanyA>();
                break;
            case 2:
                myFC = std::make_unique<FileConversionCompanyB>();
                break;
            default:
                throw std::invalid_argument("Unsupported format");
        }
        myFC->Execute("fileName");
    }
    
  • 用工厂模式封装实例化逻辑
    后续新增更多公司解析类时,switch会越来越冗长,建议把实例化逻辑封装到工厂函数/类中,符合开闭原则:

    std::unique_ptr<myFileConversion> CreateFileConverter(int formatType) {
        switch(formatType) {
            case 1:
                return std::make_unique<FileConversionCompanyA>();
            case 2:
                return std::make_unique<FileConversionCompanyB>();
            // 新增格式时只需要在这里加case
            default:
                throw std::invalid_argument("Unsupported format");
        }
    }
    
    // 调用方简化为:
    void __fastcall TForm1::Button1Click(TObject *Sender) {
        auto myFC = CreateFileConverter(intFormat);
        myFC->Execute("fileName");
    }
    
  • 基类必须声明虚析构函数
    确保通过基类指针删除子类对象时,能正确调用子类的析构函数,避免内存泄漏:

    class myFileConversion {
    public:
        virtual ~myFileConversion() = default; // 虚析构函数
        virtual void Execute(const std::string& fileName) = 0; // 纯虚方法
    protected:
        // 通用方法示例
        bool ReadFile(const std::string& fileName, std::vector<std::string>& lines);
        void SaveResult(const std::vector<ParseResult>& results);
    };
    

内容的提问来源于stack exchange,提问作者JKofsky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 15:00:59