C++:通過私人(private)繼承複用類實現工廠函數詳解

來源:互聯網
上載者:User

通過介面類(interface class), 實現工廠函數, 可以複用(reuse)已有類, 重寫實現介面.

如:

介面類: IPerson; 複用類: PersonInfo; 實作類別: CPerson;

工廠函數: DatabaseID askUserForDatabaseID(); std::shared_ptr<IPerson> makePerson(DatabaseID personIndetifier);

可以通過私人繼承複用類PersonInfo,公有繼承介面類IPerson, 並重寫相關介面, 即界定符(delim),實作類別CPerson的要求.

代碼如下:

/*  * test.cpp  *  *  Created on: 2014.04.14  *      Author: Spike  */      /*eclipse cdt, gcc 4.8.1*/      #include <cstring>  #include <iostream>  #include <string>  #include <memory>        using namespace std;        class IPerson {  public:      virtual ~IPerson() = default;      virtual std::string name() const = 0;      virtual std::string birthDate() const = 0;  };        struct DatabaseID {      char name[256];      char birthDate[256];  };        class PersonInfo {  public:      explicit PersonInfo(DatabaseID pid){          std::strcpy(m_name, pid.name);          std::strcpy(m_birthDate, pid.birthDate);      }      virtual ~PersonInfo() = default;      virtual const char* theName() const;      virtual const char* theBirthDate() const;  protected:      virtual const char* valueDelimOpen() const { return "["; };      virtual const char* valueDelimClose() const { return "]"; };  private:      char m_name[256];      char m_birthDate[256];      static const int Max_Formatted_Filed_Value_Length = 512;  };        const char* PersonInfo::theName() const {      static char value[Max_Formatted_Filed_Value_Length];      std::strcpy(value, valueDelimOpen());      std::strcat(value, m_name);      std::strcat(value, valueDelimClose());      return value;  }        const char* PersonInfo::theBirthDate() const {      static char value[Max_Formatted_Filed_Value_Length];      std::strcpy(value, valueDelimOpen());      std::strcat(value, m_birthDate);      std::strcat(value, valueDelimClose());      return value;  }        class CPerson : public IPerson, private PersonInfo {  public:      explicit CPerson(DatabaseID pid) : PersonInfo(pid) {}      virtual std::string name() const { return PersonInfo::theName(); }      virtual std::string birthDate() const { return PersonInfo::theBirthDate(); }  private:      const char* valueDelimOpen() const { return "---"; }      const char* valueDelimClose() const { return "---"; }  };        std::shared_ptr<IPerson> makePerson(DatabaseID personIndetifier) {      std::shared_ptr<IPerson> pP = std::make_shared<CPerson>(personIndetifier);      return pP;  }  DatabaseID askUserForDatabaseID() {      DatabaseID di;      std::strcpy(di.name, "Liu");      std::strcpy(di.birthDate, "1998.9.25");      return di;  }        int main() {      DatabaseID id(askUserForDatabaseID());      std::shared_ptr<IPerson> pp(makePerson(id));      std::cout << pp->name() << std::endl;      std::cout << pp->birthDate() << std::endl;            return 0;  }

輸出:

---Liu---  ---1998.9.25---

作者:csdn部落格 Spike_King

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。