18Visitor访问者模式Word格式.docx
- 文档编号:21771675
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:8
- 大小:36.97KB
18Visitor访问者模式Word格式.docx
《18Visitor访问者模式Word格式.docx》由会员分享,可在线阅读,更多相关《18Visitor访问者模式Word格式.docx(8页珍藏版)》请在冰豆网上搜索。
{
}
virtual~IVisitor(void)
virtualvoidVisit(CCommonEmployeecommonEmployee)=0;
virtualvoidVisit(CManagermanager)=0;
virtualintGetTotalSalary()=0;
};
//BaseVisitor.h
#include"
ivisitor.h"
CommonEmployee.h"
Manager.h"
#include<
iostream>
usingstd:
:
string;
classCBaseVisitor:
publicIVisitor
CBaseVisitor(void);
~CBaseVisitor(void);
voidVisit(CCommonEmployeecommonEmployee);
voidVisit(CManagermanager);
intGetTotalSalary();
private:
stringGetBasicInfo(CEmployee*pemployee);
stringGetManagerInfo(CManagermanager);
stringGetCommonEmployee(CCommonEmployeeemployee);
staticconstintMANAGER_COEFFICENT=5;
staticconstintCOMMONEMPLOYEE_COEFFICENT=2;
intm_commonTotal;
intm_managerTotal;
voidCalCommonSalary(intsalary);
voidCalManagerSalary(intsalary);
//BaseVisitor.cpp
StdAfx.h"
..\CommonDeclare\Convert.h"
BaseVisitor.h"
cout;
endl;
CBaseVisitor:
CBaseVisitor(void)
m_commonTotal=0;
m_managerTotal=0;
}
~CBaseVisitor(void)
voidCBaseVisitor:
Visit(CCommonEmployeecommonEmployee)
cout<
<
this->
GetCommonEmployee(commonEmployee).c_str()<
endl;
CalCommonSalary(commonEmployee.GetSalary());
Visit(CManagermanager)
GetManagerInfo(manager).c_str()<
CalManagerSalary(manager.GetSalary());
stringCBaseVisitor:
GetBasicInfo(CEmployee*pemployee)
stringinfo="
"
;
info.append("
姓名:
);
info.append(pemployee->
GetName());
\t"
性别:
info.append(CConvert:
ToString(pemployee->
GetSex()));
薪水:
GetSalary()));
returninfo;
GetManagerInfo(CManagermanager)
stringbasicInfo=this->
GetBasicInfo(&
manager);
stringotherInfo="
otherInfo.append("
业绩:
otherInfo.append(manager.GetPerformance());
basicInfo.append(otherInfo);
returnbasicInfo;
GetCommonEmployee(CCommonEmployeeemployee)
employee);
工作:
otherInfo.append(employee.GetJob());
intCBaseVisitor:
GetTotalSalary()
returnthis->
m_commonTotal+this->
m_managerTotal;
CalCommonSalary(intsalary)
m_commonTotal+=salary;
CalManagerSalary(intsalary)
m_managerTotal+=salary;
//Employee.h
IVisitor.h"
classCEmployee
staticintMALE;
staticintFEMALE;
CEmployee(void);
virtual~CEmployee(void);
stringGetName();
voidSetName(stringname);
intGetSalary();
voidSetSalary(intv);
intGetSex();
voidSetSex(intv);
virtualvoidAccept(IVisitor*pVisitor)=0;
stringm_name;
intm_salary;
intm_sex;
//Employee.cpp
Employee.h"
intCEmployee:
MALE=0;
FEMALE=1;
CEmployee:
CEmployee(void)
~CEmployee(void)
stringCEmployee:
GetName()
returnm_name;
voidCEmployee:
SetName(stringname)
m_name=name;
GetSalary()
returnm_salary;
SetSalary(intv)
m_salary=v;
GetSex()
returnm_sex;
SetSex(intv)
m_sex=v;
//Manager.h
employee.h"
classCManager:
publicCEmployee
CManager(void);
~CManager(void);
stringGetPerformance();
voidSetPerformance(stringper);
voidAccept(IVisitor*pVisitor);
protected:
stringGetOtherInfo();
stringm_performance;
//Manager.cpp
CManager:
CManager(void)
this->
m_performance="
~CManager(void)
stringCManager:
GetPerformance()
returnm_performance;
voidCManager:
SetPerformance(stringper)
m_performance=per;
GetOtherInfo()
stringinfo="
info.append("
info.append(this->
m_performance);
returninfo;
Accept(IVisitor*pVisitor)
pVisitor->
Visit(*this);
//Visitor.cpp:
定义控制台应用程序的入口点。
//
stdafx.h"
vector>
vector;
voidMockEmployee(vector<
CEmployee*>
*pvce)
CCommonEmployee*pZhangSan=newCCommonEmployee();
pZhangSan->
SetJob("
编写Java程序,绝对的蓝领、苦工加搬运工"
SetName("
张三"
SetSalary(1800);
SetSex(CEmployee:
MALE);
pvce->
push_back(pZhangSan);
CCommonEmployee*pLiSi=newCCommonEmployee();
pLiSi->
页面美工,审美素质太不流行了!
李四"
SetSalary(1900);
FEMALE);
push_back(pLiSi);
CManager*pWangWu=newCManager();
pWangWu->
SetPerformance("
基本上是负值,但是我会拍马屁呀"
王五"
push_back(pWangWu);
voidDoIt()
vector<
vce;
MockEmployee(&
vce);
const_iteratorreadIt=vce.begin();
CBaseVisitorvisitor;
for(;
readIt!
=vce.end();
readIt++)
(*readIt)->
Accept(&
visitor);
cout<
"
本公司的月工资总额是:
<
CConvert:
ToString(visitor.GetTotalSalary()).c_str()<
reverse_iteratordelIt=vce.rbegin();
delIt!
=vce.rend();
delIt++)
delete(*delIt);
(*delIt)=NULL;
vce.clear();
int_tmain(intargc,_TCHAR*argv[])
DoIt();
return0;
访问者模式属于行为型模式。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 18 Visitor 访问者 模式