以前出过李维老师的一本书,<<面向对象实践指南c#版>>,我先后使用c++,java语言改写了一遍,这里列出c++改写的主要的类:
======================================================================
TPFMTask:
class TPFMTask{
protected:
TPFMSchedule *pFSchedule;
TPFMConfig *pFConfig;
vector<void *> FHandlers;
virtual vector<void *> CreateHandlersFromConfig(TPFMConfig aConfig);
void ApplyHandlersToTargets(TPFMConfig aConfig, vector<void *> _FHandlers);
void BroadCastHandlers(TCandidate aCandidate,vector<void *> _FHandlers);
public:
TPFMTask();
virtual bool Execute(TPFMSchedule aSchedule, TPFMConfig aConfig);
};
#include "TPFMTask.h"
TPFMTask::TPFMTask()
{
pFSchedule=NULL;
pFConfig=NULL;
FHandlers.clear();
}
/*虚函数:动态绑定*/
vector<void *> TPFMTask::CreateHandlersFromConfig(TPFMConfig aConfig)
{
vector<void *> handlerList;
handlerList.clear();
/*这是最简单的一种,可能会有多个<handler>的子标签*/
TPFMHandler *pTPFMHandler=TPFMHandlerFactory::GetTPFMHandler(aConfig.GetHandler());
handlerList.push_back(pTPFMHandler);
pTPFMHandler=TPFMHandlerFactory::GetTPFMHandler(aConfig.GetDestination());
handlerList.push_back(pTPFMHandler);
return handlerList;
}
void TPFMTask::BroadCastHandlers(TCandidate aCandidate,vector<void *> _FHandlers)
{
vector<byte> baTarget;
baTarget.clear();
vector<void *>::iterator iter=_FHandlers.begin();
while(iter!=_FHandlers.end()){
TPFMHandler *pTPFMHandler=(TPFMHandler*)(*iter);
pTPFMHandler->Perform(aCandidate,baTarget);
}
}
void TPFMTask::ApplyHandlersToTargets(TPFMConfig aConfig, vector<void *> _FHandlers)
{
//应该使用TFinder,但这里做了简化
TFileFinder *pFileFinder=NULL;
pFileFinder=new TFileFinder(aConfig);
TFileFinderEnumerator *pFileFinderEnumerator=pFileFinder->GetEnumerator();
while(pFileFinderEnumerator->MoveNext()){
TCandidate candidateTmp=pFileFinderEnumerator->Current();
BroadCastHandlers(candidateTmp,_FHandlers);
}
delete pFileFinderEnumerator;
delete pFileFinder;
}
/*虚函数:动态绑定*/
bool TPFMTask::Execute(TPFMSchedule aSchedule, TPFMConfig aConfig)
{
if (FHandlers.size()>0){
FHandlers.clear();
}
if (aSchedule.GetExt()!=""){
pFSchedule=&aSchedule;
}
pFConfig=&aConfig;
return true;
}
=======================================================================
TPFMManager:
class TPFMManager
{
private:
TPFMConfigManager *pTPFMConfigManager;
TPFMScheduleManager *pTPFMScheduleManager;
void CreateProcessors();
int GetConfigCount();
int GetScheduleCount();
protected:
XMLNode rootNode;
string configurationFile;
string scheduleFile;
virtual bool ProcessConfiguration();
void CreateXmlNode(string sXMLFile);
void LoadPFMFile(string sFileName);
public:
vector<TPFMManager *> processorList;
int configNodeSeq;
int scheduleNodeSeq;
void RegisterProcessor(TPFMManager *pTPFMManager,TPFMManager *pTPFMManagerOther);
bool ProcessConfigurations();
int ConfigCount();
int ScheduleCount();
TPFMConfig getConfig(int index);
TPFMSchedule getSchedule(int index);
virtual void * GetNextInstance(){return NULL;};
bool Perform(TPFMManager *pCaller, string sFileName, vector<void *> &collector);
TPFMManager(){};
TPFMManager(string sConfigFile, string sScheduleFile);
TPFMConfig GetConfigFromSchedule(TPFMSchedule aSchedule);
vector<void *> FindScheduledObjects(string dt);
};
class TPFMConfigManager: public TPFMManager
{
private:
vector<void *> configs;
TPFMConfig GetItem(int index);
int GetConfigCount();
TPFMConfig *GetNextConfig();
TPFMManager *pBaseTPFMManager;
protected:
bool ProcessConfiguration();
public:
TPFMConfigManager(string _configFile,TPFMManager *pTPFMManager);
TPFMConfig operator[](int index);
int Count();
void * GetNextInstance();
vector<void *> GetConfigs();
};
class TPFMScheduleManager: TPFMManager
{
private:
vector<void *> schedules;
TPFMSchedule GetItem(int index);
int GetScheduleCount();
TPFMSchedule *GetNextSchedule();
TPFMManager *pBaseTPFMManager;
protected:
bool ProcessConfiguration();
public:
TPFMScheduleManager(string _scheduleFile,TPFMManager *pTPFMManager);
TPFMSchedule operator[](int index);
int Count();
void * GetNextInstance();
vector<void *> GetSchedules();
};
#include "TPFMManager.h"
TPFMManager::TPFMManager(string sConfigFile, string sScheduleFile)
{
if (sConfigFile != "")
configurationFile= sConfigFile;
if (sScheduleFile != "")
scheduleFile= sScheduleFile;
}
void TPFMManager::CreateProcessors()
{
if (pTPFMConfigManager==NULL){
pTPFMConfigManager= new TPFMConfigManager(configurationFile,this);
}
if (pTPFMScheduleManager==NULL){
pTPFMScheduleManager= new TPFMScheduleManager(scheduleFile,this);
}
}
bool TPFMManager::ProcessConfigurations()
{
pTPFMConfigManager=NULL;
pTPFMScheduleManager=NULL;
CreateProcessors();
return ProcessConfiguration();
}
int TPFMManager::GetConfigCount()
{
return pTPFMConfigManager->Count();
}
int TPFMManager::GetScheduleCount()
{
return pTPFMScheduleManager->Count();
}
int TPFMManager::ScheduleCount()
{
return GetScheduleCount();
}
bool TPFMManager::ProcessConfiguration()
{
bool bResult = false;
vector<TPFMManager *>::iterator iter=processorList.begin();
while(iter!=processorList.end()){
TPFMManager *pManager=*iter;
bResult = (pManager->ProcessConfiguration())|| bResult;
iter++;
}
return bResult;
}
void TPFMManager::RegisterProcessor(TPFMManager *pTPFMManager,TPFMManager *pTPFMManagerOther)
{
if (count(pTPFMManagerOther->processorList.begin(),pTPFMManagerOther->processorList.end(),pTPFMManager)==0){
pTPFMManagerOther->processorList.push_back(pTPFMManager);
}
}
void TPFMManager::CreateXmlNode(string sXMLFile)
{
string lineBuf="",xmlBuf="";
ifstream initFile(sXMLFile.c_str(),ios::in );
bool fitstLine=true;
while (getline(initFile,lineBuf,'/n')) {
if (fitstLine==true){
fitstLine=false;
continue;
}
xmlBuf.append(trim(lineBuf));
}
rootNode = XMLNode::parseString((const char*)xmlBuf.c_str(),"Targets");
if (rootNode.isEmpty()){
rootNode = XMLNode::parseString((const char*)xmlBuf.c_str(),"Schedules");
}
}
void TPFMManager::LoadPFMFile(string sFileName)
{
CreateXmlNode(sFileName);
}
int TPFMManager::ConfigCount()
{
return GetConfigCount();
}
TPFMConfig TPFMManager::getConfig(int index)
{
return (*pTPFMConfigManager)[index];
}
TPFMSchedule TPFMManager::getSchedule(int index)
{
return (*pTPFMScheduleManager)[index];
}
bool TPFMManager::Perform(TPFMManager *pCaller, string sFileName, vector<void *> &collector)
{
LoadPFMFile(sFileName);
collector.clear();
while (true){
void *p=pCaller->GetNextInstance();
if (p!=NULL){
collector.push_back(p);
}else{
break;
}
}
return true;
}
TPFMConfig TPFMManager::GetConfigFromSchedule(TPFMSchedule aSchedule)
{
vector<void *> vecConfigs=pTPFMConfigManager->GetConfigs();
vector<void *>::iterator iter=vecConfigs.begin();
while(iter!=vecConfigs.end()){
TPFMConfig *pTPFMConfig=(TPFMConfig *)*iter;
if (pTPFMConfig->GetExt()==aSchedule.GetExt()){
return *pTPFMConfig;
}
iter++;
}
}
TPFMConfig TPFMConfigManager::GetItem(int index)
{
if (index<GetConfigCount()){
return *((TPFMConfig *)configs[index]);
}
}
vector<void *> TPFMConfigManager::GetConfigs()
{
return configs;
}
int TPFMConfigManager::GetConfigCount()
{
return configs.size();
}
TPFMConfig * TPFMConfigManager::GetNextConfig()
{
string ext = "";
string location = "";
string subDirectory = "";
string unit = "";
string remove = "";
string handler = "";
string destination = "";
string dir = "";
string connectionSting = "";
XMLNode typeXMLNode=rootNode.getChildNode(pBaseTPFMManager->configNodeSeq);
if (!typeXMLNode.isEmpty()){
XMLNode tempNode=typeXMLNode.getChildNode("EXT");
if (tempNode.getText()!=NULL){
ext=string(tempNode.getText());
}
tempNode=typeXMLNode.getChildNode("LOCATION");
if (tempNode.getText()!=NULL){
location=string(tempNode.getText());
}
tempNode=typeXMLNode.getChildNode("SUBDIRECTORY");
if (tempNode.getText()!=NULL){
subDirectory=string(tempNode.getText());
}
tempNode=typeXMLNode.getChildNode("UNIT");
if (tempNode.getText()!=NULL){
unit=string(tempNode.getText());
}
tempNode=typeXMLNode.getChildNode("REMOVE");
if (tempNode.getText()!=NULL){
remove=string(tempNode.getText());
}
tempNode=typeXMLNode.getChildNode("HANDLER");
if (tempNode.getText()!=NULL){
handler=string(tempNode.getText());
}
tempNode=typeXMLNode.getChildNode("DESTINATION");
if (tempNode.getText()!=NULL){
destination=string(tempNode.getText());
}
tempNode=typeXMLNode.getChildNode("DIR");
if (tempNode.getText()!=NULL){
dir=string(tempNode.getText());
}
tempNode=typeXMLNode.getChildNode("CONNECTIONSTRING");
if (tempNode.getText()!=NULL){
connectionSting=string(tempNode.getText());
}
TPFMConfig *pTPFMConfig=new TPFMConfig(ext,location,subDirectory,unit,remove,
handler,destination,dir,connectionSting);
pBaseTPFMManager->configNodeSeq++;
return pTPFMConfig;
}else{
return NULL;
}
}
bool TPFMConfigManager::ProcessConfiguration()
{
return Perform(this, configurationFile, configs);
}
TPFMConfig TPFMConfigManager::operator[](int index)
{
return GetItem(index);
}
TPFMConfigManager::TPFMConfigManager(string _configFile,TPFMManager *pTPFMManager)
{
if (_configFile!= ""){
configurationFile =_configFile;
}
configs.clear();
pBaseTPFMManager=pTPFMManager;
pBaseTPFMManager->configNodeSeq=0;
RegisterProcessor(this,pBaseTPFMManager);
}
int TPFMConfigManager::Count()
{
return GetConfigCount();
}
void * TPFMConfigManager::GetNextInstance()
{
return GetNextConfig();
}
TPFMSchedule TPFMScheduleManager::GetItem(int index)
{
if (index<GetScheduleCount()){
return *((TPFMSchedule *)schedules[index]);
}
}
vector<void *> TPFMScheduleManager::GetSchedules()
{
return schedules;
}
int TPFMScheduleManager::GetScheduleCount()
{
return schedules.size();
}
TPFMSchedule * TPFMScheduleManager::GetNextSchedule()
{
string ext = "";
string time="";
string interval="";
XMLNode schedulesXMLNode=rootNode.getChildNode(pBaseTPFMManager->scheduleNodeSeq);
if (!schedulesXMLNode.isEmpty()){
XMLNode tempNode=schedulesXMLNode.getChildNode("Ext");
if (tempNode.getText()!=NULL){
ext=string(tempNode.getText());
}
tempNode=schedulesXMLNode.getChildNode("Time");
if (tempNode.getText()!=NULL){
time=string(tempNode.getText());
}
tempNode=schedulesXMLNode.getChildNode("Interval");
if (tempNode.getText()!=NULL){
interval=string(tempNode.getText());
}
TPFMSchedule *pTPFMSchedule=new TPFMSchedule(ext,time,interval);
pBaseTPFMManager->scheduleNodeSeq++;
return pTPFMSchedule;
}else{
return NULL;
}
}
bool TPFMScheduleManager::ProcessConfiguration()
{
return Perform(this, scheduleFile,schedules);
}
TPFMSchedule TPFMScheduleManager::operator[](int index)
{
return GetItem(index);
}
TPFMScheduleManager::TPFMScheduleManager(string _scheduleFile,TPFMManager *pTPFMManager)
{
if (_scheduleFile!= ""){
scheduleFile =_scheduleFile;
}
schedules.clear();
pBaseTPFMManager=pTPFMManager;
pBaseTPFMManager->scheduleNodeSeq=0;
RegisterProcessor(this,pBaseTPFMManager);
}
int TPFMScheduleManager::Count()
{
return GetScheduleCount();
}
void * TPFMScheduleManager::GetNextInstance()
{
return GetNextSchedule();
}
vector<void *> TPFMManager::FindScheduledObjects(string dt)
{
vector<void *> scheduleList;
scheduleList.clear();
for (int i= 0;i<ScheduleCount();i++){
TPFMSchedule schedule=getSchedule(i);
/*没有判断时间间隔,简化为直接处理*/
scheduleList.push_back((TPFMSchedule*)(&schedule));
}
return scheduleList;
}
=======================================================================
TPFMHandlerFactory:
class TPFMHandlerFactory{
private:
static TPFMHandlerMap *handlerMap;
static TPFMHandler* RegisterHandler(string handlerName);
static Mutex handlerMapMutex;
public:
TPFMHandlerFactory(){};
static TPFMHandler * GetTPFMHandler(string handlerName);
static void DestoryHandler();
};
#include "TPFMHandlerFactory.h"
TPFMHandlerMap* TPFMHandlerFactory::handlerMap = NULL; //map初始化
Mutex TPFMHandlerFactory::handlerMapMutex;
TPFMHandler* TPFMHandlerFactory::RegisterHandler(string handlerName)
{
TPFMHandler *pHandler=NULL;
if (handlerName=="TPFMFileHandler"){
pHandler=new TPFMFileHandler();
}else if (handlerName=="TPFMZipHandler"){
pHandler=new TPFMZipHandler();
}else if (handlerName=="TPFMUnZipHandler"){
pHandler=new TPFMUnZipHandler();
}
(*handlerMap)[handlerName]=pHandler;
return pHandler;
}
TPFMHandler * TPFMHandlerFactory::GetTPFMHandler(string handlerName)
{
TPFMHandler *pHandler=NULL;
ScopedLock lock(handlerMapMutex);
if (handlerMap==NULL){
handlerMap=new map<string,TPFMHandler*>;
}
TPFMHandlerMap::iterator it=handlerMap->find(handlerName);
if (it==handlerMap->end()){
pHandler=RegisterHandler(handlerName);
return pHandler;
}else{
return (*handlerMap)[handlerName];
}
}
void TPFMHandlerFactory::DestoryHandler()
{
TPFMHandlerMap::iterator itDel=handlerMap->begin();
while(itDel!=handlerMap->end()){
delete itDel->second;
itDel++;
}
delete handlerMap;
}
==================================================================
TPFMCoordinator:
class TPFMCoordinator{
private:
TPFMManager *pFpfmManager;
TPFMTaskDispatcher *pTPFMTaskDispatcher;
void DoSetups();
public:
TPFMCoordinator(string dirOne,string dirTwo);
void DoSpecifiedTask(TPFMConfig aConfig);
void DoScheduledTask(string dt);
TPFMManager * GetTPFMManager();
};
#include "TPFMCoordinator.h"
TPFMCoordinator::TPFMCoordinator(string dirOne,string dirTwo)
{
pFpfmManager=NULL;
pFpfmManager=new TPFMManager(dirOne,dirTwo);
pTPFMTaskDispatcher=NULL;
pTPFMTaskDispatcher=new TPFMTaskDispatcher();
DoSetups();
}
void TPFMCoordinator::DoSetups()
{
pFpfmManager->ProcessConfigurations();
}
TPFMManager* TPFMCoordinator::GetTPFMManager()
{
return pFpfmManager;
}
void TPFMCoordinator::DoSpecifiedTask(TPFMConfig aConfig)
{
pTPFMTaskDispatcher->DoSpecifiedTask(*pFpfmManager,aConfig);
}
void TPFMCoordinator::DoScheduledTask(string dt)
{
vector<void *> scheduledList;
scheduledList.clear();
scheduledList=pFpfmManager->FindScheduledObjects(dt);
pTPFMTaskDispatcher->DoScheduledTask(*pFpfmManager,scheduledList);
}
==================================================================
TFileFinderEnumerator:
class TFileFinderEnumerator{
private:
int index;
TFileFinder *pFileFinder;
public:
TFileFinderEnumerator(TFileFinder *_pFileFinder);
bool MoveNext();
TCandidate GetCurrent();
TCandidate Current();
void Reset();
};
#include "TFileFinderEnumerator.h"
TFileFinderEnumerator::TFileFinderEnumerator(TFileFinder *_pFileFinder)
{
pFileFinder=_pFileFinder;
index=-1;
}
bool TFileFinderEnumerator::MoveNext()
{
bool bResult=index<pFileFinder->GetCount();
if (bResult==true){
index++;
}
return bResult;
}
TCandidate TFileFinderEnumerator::GetCurrent()
{
return (*pFileFinder)[index];
}
TCandidate TFileFinderEnumerator::Current()
{
return GetCurrent();
}
void TFileFinderEnumerator::Reset()
{
index=-1;
}