范例一:
#include <iostream>
using namespace std;
class cook
{
public:
void fish()
{
std::cout << "doing fish" << std::endl;
}
void meat()
{
std::cout << "doing meat" << std::endl;
}
};
class command
{
public:
command(cook* p_cook)
{
m_cook = p_cook;
}
~command(){}
public:
virtual void execute() = 0;
protected:
cook* m_cook;
};
class dofish : public command
{
public:
dofish(cook* p_cook) :command(p_cook){}
~dofish(){}
public:
void execute()
{
m_cook->fish();
}
};
class domeat : public command
{
public:
domeat(cook* p_cook) :command(p_cook) {}
~domeat() {}
public:
void execute()
{
m_cook->meat();
}
};
int main()
{
cook mcook;
command* p1 = new dofish(&mcook);
p1->execute();
command* p2 = new domeat(&mcook);
p2->execute();
delete p1;
delete p2;
return 0;
}
范例二:
#include <iostream>
#include <type_traits>
#include <boost/type_index.hpp>
using namespace std;
class cook
{
public:
void fish()
{
std::cout << "doing fish" << std::endl;
}
void meat()
{
std::cout << "doing meat" << std::endl;
}
};
class command
{
public:
command(cook* p_cook)
{
m_cook = p_cook;
}
~command(){}
public:
virtual void execute() = 0;
protected:
cook* m_cook;
};
class dofish : public command
{
public:
dofish(cook* p_cook) :command(p_cook){}
~dofish(){}
public:
void execute()
{
m_cook->fish();
}
};
class domeat : public command
{
public:
domeat(cook* p_cook) :command(p_cook) {}
~domeat() {}
public:
void execute()
{
m_cook->meat();
}
};
class waiter
{
public:
void setcommand(command* pcommand)
{
p_command = pcommand;
}
void notify()
{
p_command->execute();
}
public:
command* p_command;
};
int main()
{
cook mcook;
waiter pwaiter;
command* p1 = new dofish(&mcook);
pwaiter.setcommand(p1);
pwaiter.notify();
command* p2 = new domeat(&mcook);
pwaiter.setcommand(p2);
pwaiter.notify();
delete p1;
delete p2;
return 0;
}
在上面的例程当中,我们增加了一个服务员的类, 通过服务员接收顾客下发的命令,,然后在通知厨师开始做菜,相比于第一种方式有了一点改观,但是,如果顾客要下单一百个菜,服务员就要通知100次???感觉有点不太行,我们要对服务员这个类做下修改。
范例三:
#include <iostream>
#include <list>
#include <type_traits>
#include <boost/type_index.hpp>
using namespace std;
class cook
{
public:
void fish()
{
std::cout << "doing fish" << std::endl;
}
void meat()
{
std::cout << "doing meat" << std::endl;
}
};
class command
{
public:
command(cook* p_cook)
{
m_cook = p_cook;
}
~command(){}
public:
virtual void execute() = 0;
protected:
cook* m_cook;
};
class dofish : public command
{
public:
dofish(cook* p_cook) :command(p_cook){}
~dofish(){}
public:
void execute()
{
m_cook->fish();
}
};
class domeat : public command
{
public:
domeat(cook* p_cook) :command(p_cook) {}
~domeat() {}
public:
void execute()
{
m_cook->meat();
}
};
class waiter
{
public:
void setcommand(command* pcommand)
{
m_commandlist.push_back(pcommand);
}
void Cancelcommand(command* pcommand)
{
m_commandlist.remove(pcommand);
}
void notify()
{
for (auto iter = m_commandlist.begin(); iter != m_commandlist.end(); iter++)
{
(*iter)->execute();
}
}
public:
std::list<command*> m_commandlist;
};
int main()
{
cook mcook;
waiter pwaiter;
command* p1 = new dofish(&mcook);
command* p2 = new domeat(&mcook);
pwaiter.setcommand(p1);
pwaiter.setcommand(p2);
pwaiter.notify();
delete p1;
delete p2;
return 0;
}
终于可以达到我们的目的了。 其实上面就是命令模式的体现,我们接下来仔细谈下这个模式:
|