C++设计技巧(一)之两个类互相拥有对方的对象指针

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Swallow_he/article/details/84349004

1、在实际的运用中我们也会用到如下的类关系:

class B;
class A
{
int i;
B  *lpb;
}

class B
{
int i;
A* lpa;
}

注意:一般来说,两者的定义,至少有一方是使用指针,或两者都使用指针,但是决不能两者都定义实体对象。

这样的实现,通俗地来说是 “你中有我,我中有你 ”,那么在具体运用时,我们可以将两个类所实现的含义联系起来,比如我们说A类的对象为一个一个的请求,B类对象表示会话(会话有相应的操作接口),此时只要A类和B类拥有对方的对象指针,如上代码所示,我们就可以将一个请求和一个会话,一一对应起来,当我们得到一个请求后,我们也就能得到这个请求对应的会话操作。

2、测试用例

class EventDownloadSession;

class OpenModelEventDownload      //该类我们可以提供给外部使用
{
public:
	OpenModelEventDownload(): bSynchronization(true)
       {
           
        }
	virtual ~OpenModelEventDownload()
        {

        }
	virtual void dealloc()
        {
             delete  this;
        }

	static OpenModelEventDownload * alloc()
        {
            OpenModelEventDownload * lpValue = new OpenModelEventDownload();
		lpValue->AddRef();
		return lpValue;
       }

	inline void setFilePath(const char * value)
	{
		filePath = value;
	}
	inline const char * getFilePath()
	{
		return filePath.c_str();
	}

	inline void setFileSize(size_t value)
	{
		fileSize = value;
	}
	inline size_t getFileSize()
	{
		return fileSize;
	}
	inline void setFileSeek(size_t value)
	{
		fileSeek = value;
	}
	inline size_t getFileSeek()
	{
		return fileSeek;
	}

	inline void setDownloadUrl(const char * value)
	{
		downloadUrl = value;
	}
	inline const char * getDownloadUrl()
	{
		return downloadUrl.c_str();
	}
	inline void setDownloadProcess(int32_t value)
	{
		downloadProcess = value;
	}
	inline int32_t getDownloadProcess()
	{
		return downloadProcess;
	}

	inline void setSynchronization(bool value)
	{
		bSynchronization = value;
	}
	inline bool getSynchronization()
	{
		return bSynchronization;
	}

//注意如下的两个接口
	inline OpenCloud::EventDownloadSession * getEventDownloadSession()
	{
		return downloadSession;
	}
	inline void setEventDownloadSession(OpenCloud::EventDownloadSession *lpSession)
	{
		downloadSession = lpSession;
	}
private:
	std::string   filePath;
	size_t        fileSize;
	size_t        fileSeek;

	bool          bSynchronization;
	
	std::string            downloadUrl;
	int32_t                downloadProcess; //[0,100]
	EventDownloadSession * downloadSession;     //下一个对象指针
};

//基类
class  EventDownloadSession       
{
public:
	EventDownloadSession(OpenCloud::OpenModelEventDownload *lpOpenModelEventDownload)
		:kOpenModelEventDownload(lpOpenModelEventDownload)
	{}
	virtual ~EventDownloadSession()
	{}

	virtual int stopSession() = 0;

public:
	OpenCloud::OpenModelEventDownload *kOpenModelEventDownload;    //上一个类的对象指针
};

//派生类
class EventOperationDownloadSession : public EventDownloadSession
{
public:
	EventOperationDownloadSession(OpenCloud::OpenModelEventDownload *lpOpenModelEventDownload):EventDownloadSession(lpOpenModelEventDownload)
	{
		
	}
	virtual ~EventOperationDownloadSession()
	{}
	virtual void dealloc(void * ref)
	{
		delete this;
	}
	static EventOperationDownloadSession * alloc(OpenCloud::OpenModelEventDownload *lpOpenModelEventDownload)
	{
		EventOperationDownloadSession *lpEventOperationDownloadSession = new EventOperationDownloadSession(lpOpenModelEventDownload);
		return lpEventOperationDownloadSession;
	}
	virtual int Init(void * conf)
	{
             //do something
	     return 0;
	}

	virtual int Close()
	{
            //do something
             return 0;
	}

	virtual int stopSession()
	{
              //do something
	     return 0;
	}
public:
	
};

int main()
{
     int iSsuccess  =0;
     OpenModelEventDownload *lpOpenModelEventDownload = OpenModelEventDownload::alloc();
     lpOpenModelEventDownload->setFilePath("/tmp/123456.mp4");
     
     EventOperationDownloadSession *lpEventOperationDownloadSession =  EventOperationDownloadSession::alloc(lpOpenModelEventDownload );
	download->setEventDownloadSession(lpEventOperationDownloadSession);

    iSsuccess = lpEventOperationDownloadSession->Init(nullptr);   
    //do something
  
return 0;
}

猜你喜欢

转载自blog.csdn.net/Swallow_he/article/details/84349004