厨师类:
#include "Cooker.h" Cooker::Cooker(void) { } Cooker::~Cooker(void) { } void Cooker::MakeDumplings() { cout<<"Dumplings is done!"<<endl; } void Cooker::MakeNoodles() { cout<<"Noodles is done!"<<endl; }
命令类:
#include "Command.h" Command::Command(Cooker* cooker_) { this->cooker= cooker_; } Command::Command() { } Command::~Command(void) { }
面条命令类:
#include "NoodlesCmd.h" NoodlesCmd::NoodlesCmd(Cooker* temp) { this->cooker=temp; } void NoodlesCmd::ExecuteCmd() { cooker->MakeNoodles(); } NoodlesCmd::~NoodlesCmd(void) { }
水饺命令类:
#include "DumplingCmd.h" DumplingCmd::DumplingCmd(Cooker* temp) { this->cooker=temp; } void DumplingCmd::ExecuteCmd() { this->cooker->MakeDumplings(); } DumplingCmd::~DumplingCmd(void) { }
例子:
// CommandModel.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "DumplingCmd.h" #include "NoodlesCmd.h" #include "Cooker.h" int _tmain(int argc, _TCHAR* argv[]) { Cooker *cooker=new Cooker(); NoodlesCmd *cmd1=new NoodlesCmd(cooker); DumplingCmd *cmd2=new DumplingCmd(cooker); cmd1->ExecuteCmd(); cmd2->ExecuteCmd(); getchar(); delete cooker; delete cmd1; delete cmd2; return 0; }
效果: