1. 程式人生 > >C++ 定義和使用介面類的簡單模式

C++ 定義和使用介面類的簡單模式

以一個Person類為介面類,Student類為實現類的程式碼為例:1. Person.h聲明瞭Person類,PersonCreator類,並定義了一個全域性變數用來建立Student物件

#pragma once

class Person
{
public:
    virtual ~Person() {}

    virtual void Eat() = 0;
    virtual void Sleep() = 0;
};

class PersonCeator
{
public:
    ~PersonCeator();
    Person* CreateStudent();
};

extern PersonCeator* g_creator;

2. Student.cpp聲明瞭Student類,並實現了Student類和PersonCreator類:

#include <iostream>
#include "Person.h"
using namespace std;

class Student : Person
{
public:
    Student();
    virtual ~Student();

    virtual void Eat();
    virtual void Sleep();
};

Student::Student()
{
    cout << "建立Student" << endl;
}

Student::~Student()
{
    cout << "析構Student" << endl;
}

void Student::Eat()
{
    cout << "吃飯" << endl;
}

void Student::Sleep()
{
    cout << "睡覺" << endl;
}

Person* PersonCeator::CreateStudent()
{
    Student* student= new Student();
    //Student student;
    return (Person*)student;

}
PersonCeator::~PersonCeator()
{
    cout << "析構PersonCreator" << endl;
}

PersonCeator creator;
extern PersonCeator* g_creator = &creator;//這樣建立會在main函式執行結束後自動析構
//extern PersonCeator* g_creator= new PersonCeator();//這樣建立需要手動析構

3. main.cpp使用PersonCreator建立Student物件:

#include <iostream>
#include "Person.h"

int main()
{
    Person* Tom = g_creator->CreateStudent();
    Tom->Eat();
    Tom->Sleep();

    delete Tom;  //手動析構
    //delete g_creator;

    getchar();
    return 0;
}

實際上PersonCreator中的create()函式的返回值是Person類的指標,這樣能夠徹底地隱藏Student類的存在。同時,也應該注意到,一個模組很有可能是以動態庫被其他模組呼叫,所以當物件是在本模組new時,也應當在本模組delete。所以介面類應該提供Release()方法。修改後的程式碼如下:1. Person.h聲明瞭IPerson類,IPersonCreator類,並定義了一個全域性變數用來建立Student物件

#pragma once

class IPerson
{
public:
    virtual ~IPerson() {}

    virtual void Release() = 0;
    virtual void Eat() = 0;
    virtual void Sleep() = 0;
};

class IPersonCreator
{
public:
    virtual ~IPersonCreator() {}
    virtual IPerson* CreateStudent() = 0;
};

extern IPersonCreator* g_pCreator;

2. Student.cpp宣告並實現了Student類和PersonCreator類:

#include <iostream>
#include "Person.h"
using namespace std;

//////////////////////////////////////////////////////////////////////////
//
//   宣告類Student和PersonCreator
//
//////////////////////////////////////////////////////////////////////////
class Student : public IPerson
{
public:
    Student();
    virtual ~Student();

    virtual void Release();

    virtual void Eat();
    virtual void Sleep();
private:
    char* name;
};

class PersonCreator : public IPersonCreator
{
public:
    PersonCreator();
    virtual ~PersonCreator();
    virtual IPerson* CreateStudent();

    void ReleasePerson(IPerson* pPerson);
};


PersonCreator creator;
IPersonCreator* g_pCreator = &creator;//這樣建立會在main函式執行結束後自動析構
//////////////////////////////////////////////////////////////////////////
//
//   實現類Student
//
//////////////////////////////////////////////////////////////////////////
Student::Student()
{
    name = new char[255]();
    cout << "建立Student" << endl;
}

Student::~Student()
{
    delete[] name;
    cout << "析構Student" << endl;
}

void Student::Release()
{
    creator.ReleasePerson(this);
}

void Student::Eat()
{
    cout << "吃飯" << endl;
}

void Student::Sleep()
{
    cout << "睡覺" << endl;
}

//////////////////////////////////////////////////////////////////////////
//
//   實現類PersonCreator
//
//////////////////////////////////////////////////////////////////////////

PersonCreator::PersonCreator()
{
    cout << "建立PersonCreator" << endl;
}


PersonCreator::~PersonCreator()
{
    cout << "析構PersonCreator" << endl;
}


IPerson* PersonCreator::CreateStudent()
{
    Student* student = new Student();
    return (IPerson*)student;
}

void PersonCreator::ReleasePerson(IPerson* pPerson)
{
    if (pPerson)
    {
        delete pPerson;
    }
}

3. main.cpp使用介面IPersonCreator建立Student物件,使用完之後需要析構Student物件,程式結束後會自動釋放PersonCreator物件。

#include <iostream>
#include "Person.h"

int main()
{
    IPerson* Tom = g_pCreator->CreateStudent();//使用時不需強制轉換了
    Tom->Eat();
    Tom->Sleep();

    Tom->Release();
    Tom = nullptr;

    getchar();
    return 0;
}

---------------------  作者:文如王勃三生慧  來源:CSDN  原文:https://blog.csdn.net/hf19931101/article/details/78802058  版權宣告:本文為博主原創文章,轉載請附上博文連結!