Linux学习之线程封装五:基于接口的封装

本例将执行体从线程类中抽象出来,形成一个接口,用户可根据需要派生出不同的执行体(线程或进程),从而使得不同的执行体(线程或进程)能以自己想要的方式运行。

执行体类(接口)"CLExecutive":

头文件:

View Code
#ifndef CLEXECUTIVE_H
#define CLEXECUTIVE_H

#include "CLStatus.h"

class CLExecutiveFunctionProvider;

/*
CLExecutive类用于创建一个执行体
*/
class CLExecutive
{
public:
explicit CLExecutive(CLExecutiveFunctionProvider *pExecutiveFunctionProvider);
virtual ~CLExecutive();

virtual CLStatus Run(void *pContext = 0) = 0;

virtual CLStatus WaitForDeath() = 0;

protected:
CLExecutiveFunctionProvider *m_pExecutiveFunctionProvider;

private:
CLExecutive(const CLExecutive&);
CLExecutive& operator=(const CLExecutive&);
};

#endif

实现:

View Code
#include "CLExecutive.h"
#include "CLExecutiveFunctionProvider.h"

CLExecutive::CLExecutive(CLExecutiveFunctionProvider *pExecutiveFunctionProvider)
{
m_pExecutiveFunctionProvider = pExecutiveFunctionProvider;
}

CLExecutive::~CLExecutive()
{
}

业务逻辑提供者类"CLExecutiveFunctionProvider":

同前一节
一个执行体线程(例)"CLThread"

头文件:

View Code
#ifndef CLTHREAD_H
#define CLTHREAD_H

#include <pthread.h>
#include "CLExecutive.h"
#include "CLStatus.h"

class CLThread : public CLExecutive
{
public:
explicit CLThread(CLExecutiveFunctionProvider *pExecutiveFunctionProvider);
virtual ~CLThread();

virtual CLStatus Run(void *pContext = 0);

virtual CLStatus WaitForDeath();

private:
static void* StartFunctionOfThread(void *pContext);

private:
void *m_pContext;
pthread_t m_ThreadID;
};

#endif

继承自执行体类"CLExecutive"

View Code
#include "CLThread.h"
#include "CLExecutiveFunctionProvider.h"
#include "CLLog.h"

CLThread::CLThread(CLExecutiveFunctionProvider *pExecutiveFunctionProvider) : CLExecutive(pExecutiveFunctionProvider)
{
m_pContext = 0;
}

CLThread::~CLThread()
{
}

CLStatus CLThread::Run(void *pContext)
{
m_pContext = pContext;

int r = pthread_create(&m_ThreadID, 0, StartFunctionOfThread, this);
if(r != 0)
{
CLLog::WriteLogMsg("In CLThread::Run(), pthread_create error", r);
return CLStatus(-1, 0);
}

return CLStatus(0, 0);
}

void* CLThread::StartFunctionOfThread(void *pThis)
{
CLThread *pThreadThis = (CLThread *)pThis;

pThreadThis->m_pExecutiveFunctionProvider->RunExecutiveFunction(pThreadThis->m_pContext);

return 0;
}

CLStatus CLThread::WaitForDeath()
{
int r = pthread_join(m_ThreadID, 0);
if(r != 0)
{
CLLog::WriteLogMsg("In CLThread::WaitForDeath(), pthread_join error", r);
return CLStatus(-1, 0);
}

return CLStatus(0, 0);
}

调用:

View Code
#include <iostream>
#include <unistd.h>
#include "CLThread.h"
#include "CLExecutiveFunctionProvider.h"

using namespace std;

class CLParaPrinter : public CLExecutiveFunctionProvider
{
public:
CLParaPrinter()
{
}

virtual ~CLParaPrinter()
{
}

virtual CLStatus RunExecutiveFunction(void *pContext)
{
int i = (int)pContext;
cout << i << endl;
}
};

int main()
{
CLExecutiveFunctionProvider *printer = new CLParaPrinter();
CLExecutive *pThread = new CLThread(printer);

pThread->Run((void *)2);
pThread->WaitForDeath();

delete pThread;
delete printer;

return 0;
}

这里用执行体CLExecutive指针取代了实际的线程类CLThread指针(多态)。
设计模式:这是一种耦合与接口的编程模式。继承是一种紧耦合的关系,而基于接口的编程模式,仅耦合于接口。




posted @ 2011-10-19 22:18  lq0729  阅读(467)  评论(0)    收藏  举报