简体   繁体   English

C ++继承设计

[英]C++ inheritance design

I am writing a c++ for portfolio management. 我正在编写用于项目组合管理的C ++。 In this case some of the equities can from a retirement portfolio, and the remaining equities are on the open market. 在这种情况下,某些股票可以来自退休投资组合,而其余股票则在公开市场上。
My general plan is to have the data in the following classes. 我的总体计划是将数据归入以下类别。

class Equity { private:  std::string name, exchange, symbol };
class EquityHistory : public Equity { private std::list<DateRecord> history };
class YahooDAO {};
class RetirementDAO {};
class Portfolio { private: std::list<EquityHistory> equities; }

In the main, or upper level class, the portfolio is populated with a list of equities. 在主要或较高级别的类别中,投资组合填充有股票列表。 The DAO classes provide for obtaining the price history from the web. DAO类提供从Web获取价格历史记录的功能。 The question I have is how to have the Portfolio class call the correct DAO class, depending on the exchange. 我的问题是,如何根据投资交易所将Portfolio类调用正确的DAO类。

You could wrap your two data providers in a third one that will redirect depending on the exchange. 您可以将两个数据提供者包装在第三个数据提供者中,该提供者将根据交换进行重定向。

This way from the point of view of the folio all is abstract, it does not know the data source, and if later you decide to change the way you fetch data you won't have to update the Portfolio class. 从作品集的角度来看,这种方式都是抽象的,它不知道数据源,而且如果以后您决定更改获取数据的方式,则不必更新Portfolio类。

Here is the idea: 这里是想法:

class DAO
{
    private: YahooDAO* yahoo, RetirementDAO* retirement;

    public: DAO()
    {
        yahoo = new YahooDAO();
        retirement = new RetirementDAO();
    }

    public: std::List<Data> getData(EquityHistory* equity)
    {
        std::List<Data> data;

        if (equity->exchange = "some exchange")
        {
            data = yahoo->getData(equity);
        }
        else if (equity->exchange = "another exchange")
        {
            data = retirement->getData(equity);
        }

        return data;
    }
};

class Portfolio
{
    private: std::list<EquityHistory> equities;
    private: DAO* dao;

    public: Portfolio(DAO* dao)
    {
        this->dao = dao;
    }

    public: void loadData()
    {
        ...
        std::List<Data> data = dao->getData(equity);
        ...
    }
};

If you need you can abstract the getData method using an interface IDataSource . 如果需要,可以使用接口IDataSource抽象getData方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM