命令模式(Command)把請求封裝成一個對象,使請求能夠存儲更多的信息擁有更多的能力。命令模式同樣能夠把請求的發送者和接收者解耦,但它不關心請求將以何種方式被處理。命令模式經常與職責鏈模式(Chain of Responsibility)和組合模式(Composite)一起使用:職責鏈模式處理命令模式封裝的對象,組合模式可以把簡單的命令對象組合成復雜的命令對象。
本文舉了一個在飯店點菜的例子。(1)烤肉師傅負責在後台(廚房)做飯。(2)Command負責提供提供各種命令,相當於菜單。(3)服務員與客服打交道,負責記錄客戶點了哪些菜(把客戶的命令記下來),然後交給廚師去做。(4)客戶就負責點菜。
代碼:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Command;
class MakeChikenCmd;
class MakeMuttonCmd;
//烤肉師傅
class RoastCook{
friend class MakeChikenCmd;
friend class MakeMuttonCmd;
private:
void MakeMutton()
{ cout << "烤羊肉"; }
void MakeChickenWing()
{ cout << "烤雞翅膀"; }
};
//命令的接口,抽象類
class Command{
public:
Command(RoastCook* cook)
{
cooker = cook;
}
virtual ~Command(){}
virtual void Execute()=0;
protected:
RoastCook *cooker;//包含了烤肉師傅的對象
};
//烤羊肉命令
class MakeMuttonCmd : public Command{
public:
MakeMuttonCmd(RoastCook *cook):Command(cook){}
void Execute()
{
cooker->MakeMutton();
}
};
//烤雞肉命令
class MakeChikenCmd : public Command{
public:
MakeChikenCmd(RoastCook *cook):Command(cook){}
void Execute()
{
cooker->MakeChickenWing();
}
};
//服務員類,負責與客服交互
class Waiter{
public:
Waiter(string _name)
{this->name = _name;}
void AddOder(Command* subOder)//加入客戶點的菜
{
oder.push_back(subOder);
}
void Notify()
{
vector<Command*>::iterator iter;
cout<<name<<" 通知烤肉師傅請做:"<<endl;
for(iter = oder.begin();iter!=oder.end();++iter){
cout<<" ";
(*iter)->Execute();
cout<<endl;
}
}
string getName()const
{return name;}
private:
vector<Command*> oder;//點菜單
string name;//服務員名字
};
class Custmer{
public:
Custmer(string myname)
{
this->name = myname;
this->w = NULL;
}
void CustmerOder(Command *cmd)
{
cout<<"顧客"<<name<<"點菜"<<endl;
w->AddOder(cmd);
}
void CallWaiter(Waiter *_w)
{
cout<<"顧客"<<name<<"呼叫服務員"<<endl;
this->w = _w;
cout<<"服務員"<<w->getName()<<"為您服務"<<endl;
}
private:
string name;
Waiter *w;
};
int main()
{
RoastCook *cooker = new RoastCook();
Command *cmd = new MakeChikenCmd(cooker);
Command *cmd1 = new MakeMuttonCmd(cooker);
Waiter waiter("sps");
Custmer cust("lsj");
cust.CallWaiter(&waiter);//呼叫服務員
cust.CustmerOder(cmd);//點菜
cust.CustmerOder(cmd1);//點菜
waiter.Notify();//服務員通知後台
system("pause");
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Command;
class MakeChikenCmd;
class MakeMuttonCmd;
//烤肉師傅
class RoastCook{
friend class MakeChikenCmd;
friend class MakeMuttonCmd;
private:
void MakeMutton()
{ cout << "烤羊肉"; }
void MakeChickenWing()
{ cout << "烤雞翅膀"; }
};
//命令的接口,抽象類
class Command{
public:
Command(RoastCook* cook)
{
cooker = cook;
}
virtual ~Command(){}
virtual void Execute()=0;
protected:
RoastCook *cooker;//包含了烤肉師傅的對象
};
//烤羊肉命令
class MakeMuttonCmd : public Command{
public:
MakeMuttonCmd(RoastCook *cook):Command(cook){}
void Execute()
{
cooker->MakeMutton();
}
};
//烤雞肉命令
class MakeChikenCmd : public Command{
public:
MakeChikenCmd(RoastCook *cook):Command(cook){}
void Execute()
{
cooker->MakeChickenWing();
}
};
//服務員類,負責與客服交互
class Waiter{
public:
Waiter(string _name)
{this->name = _name;}
void AddOder(Command* subOder)//加入客戶點的菜
{
oder.push_back(subOder);
}
void Notify()
{
vector<Command*>::iterator iter;
cout<<name<<" 通知烤肉師傅請做:"<<endl;
for(iter = oder.begin();iter!=oder.end();++iter){
cout<<" ";
(*iter)->Execute();
cout<<endl;
}
}
string getName()const
{return name;}
private:
vector<Command*> oder;//點菜單
string name;//服務員名字
};
class Custmer{
public:
Custmer(string myname)
{
this->name = myname;
this->w = NULL;
}
void CustmerOder(Command *cmd)
{
cout<<"顧客"<<name<<"點菜"<<endl;
w->AddOder(cmd);
}
void CallWaiter(Waiter *_w)
{
cout<<"顧客"<<name<<"呼叫服務員"<<endl;
this->w = _w;
cout<<"服務員"<<w->getName()<<"為您服務"<<endl;
}
private:
string name;
Waiter *w;
};
int main()
{
RoastCook *cooker = new RoastCook();
Command *cmd = new MakeChikenCmd(cooker);
Command *cmd1 = new MakeMuttonCmd(cooker);
Waiter waiter("sps");
Custmer cust("lsj");
cust.CallWaiter(&waiter);//呼叫服務員
cust.CustmerOder(cmd);//點菜
cust.CustmerOder(cmd1);//點菜
waiter.Notify();//服務員通知後台
system("pause");
return 0;
}