简体   繁体   中英

C++ inheritance design

I am writing a c++ for portfolio management. 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. The question I have is how to have the Portfolio class call the correct DAO class, depending on the exchange.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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