




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
设计模式访问者模式 访问者模式(Visitor)是一种分离对象数据结构与行为的方法,通过这种分离,可以为一个已存在的类或类群增加新的操作而无需为它们作任何修改。 结构图为:公司的人事评估需要人事部访问每一个员工列表,逐个对员工作出评估,人事部有两个访问者,一个访问者评估员工假期,另一个评估员工薪资。实现代码: /Element.hclassVisitor;classElementpublic:Element();virtualElement();virtualvoidAccept(Visitor*)=0;friendclassVisitor;/Element.cpp#includestdafx.h#includeElement.hElement:Element()Element:Element()/Employee.h#includeElement.hclassEmployee:publicElementpublic:Employee(char*,double,int);virtualEmployee();voidAccept(Visitor*);char*GetName();doubleGetSalary();intGetVacationDays();voidSetSalary(double);voidSetVacationDays(int);private:char*m_pName;doublem_dSalary;intm_nVacationDays;/Employee.cpp#includestdafx.h#includeEmployee.h#includeVisitor.hEmployee:Employee(char*pName,doubledSalary,intnVacationDays)m_pName=pName;m_dSalary=dSalary;m_nVacationDays=nVacationDays;Employee:Employee()voidEmployee:Accept(Visitor*pVisitor)pVisitor-Visit(this);char*Employee:GetName()returnm_pName;doubleEmployee:GetSalary()returnm_dSalary;intEmployee:GetVacationDays()returnm_nVacationDays;voidEmployee:SetSalary(doubledSalary)m_dSalary=dSalary;voidEmployee:SetVacationDays(intnVacationDays)m_nVacationDays=nVacationDays;/Visitor.hclassElement;classVisitorpublic:virtualVisitor()=0;virtualvoidVisit(Element*)=0;protected:Visitor();/Visitor.cpp#includestdafx.h#includeVisitor.hVisitor:Visitor()Visitor:Visitor() /IncomeVisitor.h#includeVisitor.hclassIncomeVisitor:publicVisitorpublic:IncomeVisitor();virtualIncomeVisitor();voidVisit(Element*);/IncomeVisitor.cpp#includestdafx.h#includeIncomeVisitor.h#includeEmployee.h#includeusingnamespacestd;IncomeVisitor:IncomeVisitor() IncomeVisitor:IncomeVisitor() voidIncomeVisitor:Visit(Element*pElement)/访问者提高员工10%的薪资Employee*pEmployee=static_cast(pElement);doubledSalary=pEmployee-GetSalary();pEmployee-SetSalary(dSalary*1.10);cout员工GetName()的新工资是:GetSalary()元endl;/VacationVisitor.h#includeVisitor.hclassVacationVisitor:publicVisitorpublic:VacationVisitor();virtualVacationVisitor();voidVisit(Element*);/VacationVisitor.cpp#includestdafx.h#includeVacationVisitor.h#includeEmployee.h#includeusingnamespacestd;VacationVisitor:VacationVisitor() VacationVisitor:VacationVisitor() voidVacationVisitor:Visit(Element*pElement)/访问者为员工增加3天假期Employee*pEmployee=static_cast(pElement);intnVacationDays=pEmployee-GetVacationDays();pEmployee-SetVacationDays(nVacationDays+3);cout员工GetName()的新假期是:GetVacationDays()天endl;/main.cpp#includestdafx.h#includeEmployee.h#includeIncomeVisitor.h#includeVacationVisitor.hintmain(intargc,char*argv)Element*pEmployeeA=newEmployee(张三,10000.00,10);Element*pEmployeeB=newEmployee(李四,15000.00,20);IncomeVisitorincomeV;VacationVisitorvacationV;incomeV.Visit(pEmployeeA);vacationV.Vi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025中心医院护理管理影响力建设考核
- 2025广西百色市右江民族医学院招聘实名编制高层次人才模拟试卷及答案详解1套
- 秦皇岛市人民医院病案科主管晋升考核
- 张家口市人民医院术后患者超声考核
- 2025年上海数字城市规划研究中心公开招聘考前自测高频考点模拟试题及参考答案详解
- 2025年新乡延津县选调县外在编在岗教师回乡任教的模拟试卷及答案详解(有一套)
- 沧州市中医院宫腔镜取胚术操作资格认证
- 衡水市中医院超声引导穿刺考核
- 秦皇岛市中医院疼痛药物治疗考核
- 北京市中医院安宁疗护技能考核
- 实施指南(2025)《DA-T 59 - 2017 口述史料采集与管理规范》
- 高一物理力学知识点总结与测试题
- 广东省深圳市罗湖区2025-2026学年高三第一学期开学质量检测语文(含答案)
- 2025年高考真题分类汇编专题06 全面依法治国(全国)(解析版)
- 竹围栏施工方案范本
- 液氧安全知识培训课件
- 民间借贷法律知识讲座
- 基于PLC的果园灌溉施肥系统设计
- 2025年武汉市中考英语试卷真题(含答案)
- 无人机清洗玻璃幕墙技术规范
- 基于人工智能的个性化学习路径研究
评论
0/150
提交评论