#include “stdafx.h”
#include
#include
#include <afx.h>
using namespace std;
class CommandBase
{
public:
CommandBase(){};
virtual ~CommandBase(){};
virtual void commandver(){};
virtual void commandhelp(){};
virtual void commandex(){};
};
class CommandList:public CommandBase
{
public:
CommandList()
{
cout<<“Create a CommandList\n”;
};
virtual ~CommandList(){};
virtual void commandver()
{
cout<<“version\n”;
};
void commandhelp()
{
cout<<“help\n”;
};
void commandex()
{
cout<<“excute\n”;
};
};
class CommandCd:public CommandBase
{
public:
CommandCd()
{
cout<<“Create a CommandCd\n”;
};
virtual ~CommandCd(){};
virtual void commandver()
{
cout<<“version\n”;
};
void commandhelp()
{
cout<<“help\n”;
};
void commandex()
{
cout<<“excute\n”;
};
};
class OSimpleFactory
{
public:
static CommandBase* Creatcmd(const string &cmdname)
{
if( cmdname==“list”)
{
return new CommandList();
}
else if (cmdname == “cd”)
{
return new CommandCd();
}
else
{
//assert(false);
}
return NULL;
};
};
int main(int argc, char* argv[])
{
CommandBase *cmdobj=NULL;
string cmdname=“list”;
cmdobj=OSimpleFactory::Creatcmd(cmdname);
cmdobj->commandver();
cmdobj->commandhelp();
cmdobj->commandex();
delete cmdobj;
cmdobj=NULL;
return 0;
}
总结:使用了静态的成员函数,可以不用实例对象,使用类即可调用。