程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++:通過私有(private)繼承復用類實現工廠函數詳解

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

編輯:關於C++

通過接口類(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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved