首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

Command 方式C++ 代码

2012-08-09 
Command 模式C++代码#include iostream#include string#include vectorusing namespace stdclass C

Command 模式C++ 代码

#include <iostream>#include <string>#include <vector>using namespace std;class CommandBase{    public:virtual void run()=0;    private:};class Command1:public CommandBase{    public:Command1(string strName):m_strName(strName){};void run(){        cout<<m_strName<<endl;}private:string m_strName;};class Command2:public CommandBase{    public:Command2(string strName):m_strName(strName){};void run(){        cout<<m_strName<<endl;}private:string m_strName;};class Command3:public CommandBase{    public:Command3(string strName):m_strName(strName){};void run(){        cout<<m_strName<<endl;}private:string m_strName;};class Exclude{   public:    void add(CommandBase& Command)    {    m_vetorCommand.push_back(&Command);}void run(){        vector <CommandBase*>::iterator Itr = m_vetorCommand.begin();while(Itr != m_vetorCommand.end()){            (*Itr++)->run();}}   private:   vector <CommandBase*> m_vetorCommand;   static Exclude m_cExclude; };int main(){    Command1 cCommand1("cmd1");    Command2 cCommand2("cmd2");    Command3 cCommand3("cmd3");   Exclude cExclude;   cExclude.add(cCommand1);   cExclude.add(cCommand2);   cExclude.add(cCommand3);   cExclude.run();   while(1);}


热点排行