基类Employee.h
#ifndef __EMPLOYEE_H__
#define __EMPLOYEE_H__
#include "stdafx.h"
#include <iostream>
using namespace std;
class CEmployee
{
public:
CEmployee();
void setSuccessor(CEmployee* successor){m_Successor = successor;}
virtual void handleRequest(string strDishName) = 0;
protected:
CEmployee* m_Successor;
};
#endif
Employee.cpp
#include "stdafx.h"
#include "employee.h"
CEmployee::CEmployee()
{
//m_Successor = NULL;
}
assistantCook.cpp
#include "stdafx.h"
#include "assistantCook.h"
void CAssistantCook::handleRequest(string strDishName)
{
if(strDishName.compare("XiaoCai") == 0)
{
printf("assistant cook handle this dish[%s]\n", strDishName.c_str());
}
else if(m_Successor)
{
m_Successor->handleRequest(strDishName);
}
else
{
// end of chain
printf("no one can cook this dish[%s]\n", strDishName.c_str());
}
}
assistantCook.h
#ifndef __ASSISTANT_COOK_H__
#define __ASSISTANT_COOK_H__
#include "employee.h"
class CAssistantCook: public CEmployee
{
public:
void CAssistantCook::handleRequest(string strDishName);
private:
};
#endif
ChefCook.h
#ifndef __CHEF_COOK_H__
#define __CHEF_COOK_H__
#include "employee.h"
class CChefCook : public CEmployee
{
public:
void handleRequest(string strDishName);
private:
};
#endif
ChefCook.cpp
#include "stdafx.h"
#include "ChefCook.h"
void CChefCook::handleRequest(string strDishName)
{
if(strDishName.compare("DaPanJi") == 0)
{
printf("chef cook handle this dish[%s]\n", strDishName.c_str());
}
else if(m_Successor)
{
m_Successor->handleRequest(strDishName);
}
else
{
// end of chain
printf("no one can cook this dish[%s]\n", strDishName.c_str());
}
}
#ifndef __WAITER_H__
#define __WAITER_H__
#include "employee.h"
class CWaiter : public CEmployee
{
public:
void handleRequest(string strDishName);
};
#endif
#include "stdafx.h"
#include "waiter.h"
void CWaiter::handleRequest(string strDishName)
{
if(m_Successor)
{
m_Successor->handleRequest(strDishName);
}
else
{
printf("waiter can do nothing but order dish\n");
}
}
Customer.h
#ifndef __CUSTOMER_H__
#define __CUSTOMER_H__
#include "stdafx.h"
#include <iostream>
using namespace std;
class CCustomer
{
public:
CCustomer(string orderDishName = NULL);
string getDishName(){return m_dishName;}
private:
string m_dishName;
};
#endif
Customer.cpp
#include "stdafx.h"
#include "Customer.h"
CCustomer::CCustomer(string orderDishName)
: m_dishName(orderDishName)
{
}
main.cpp
// CorTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "ChefCook.h"
#include "employee.h"
#include "Customer.h"
#include "waiter.h"
#include "assistantCook.h"
int _tmain(int argc, _TCHAR* argv[])
{
CEmployee* chefCook = new CChefCook();
CEmployee* assit = new CAssistantCook();
CEmployee* waiter = new CWaiter();
waiter->setSuccessor(assit);
assit->setSuccessor(chefCook);
CCustomer* customer1 = new CCustomer("DaPanJi");
CCustomer* customer2 = new CCustomer("XiaoCai");
CCustomer* customer3 = new CCustomer("XiongMao");
waiter->handleRequest(customer1->getDishName());
waiter->handleRequest(customer2->getDishName());
waiter->handleRequest(customer3->getDishName());
system("pause");
return 0;
}