18.1.解释
概念:表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。
main(),客户
IVisitor,访问者接口
CBaseVisitor,访问者实现类
CEmployee,被访问者抽象类
CCommonEmployee,被访问者实现类之一
CManager,被访问者实现类之二
说明:A接受B的访问,B主动的执行访问动作。
注意:和观察者的区别是,被观察者要执行一个动作,然后主动发送通知给观察者。访问者模式是由访问者主动发出的动作。
看代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
//IVisitor.h #pragma once class CCommonEmployee; class CManager; class IVisitor { public: IVisitor(void) { } virtual ~IVisitor(void) { } virtual void Visit(CCommonEmployee commonEmployee) = 0; virtual void Visit(CManager manager) = 0; virtual int GetTotalSalary() = 0; }; //BaseVisitor.h #pragma once #include "ivisitor.h" #include "CommonEmployee.h" #include "Manager.h" #include <iostream> using std::string; class CBaseVisitor : public IVisitor { public: CBaseVisitor(void); ~CBaseVisitor(void); void Visit(CCommonEmployee commonEmployee); void Visit(CManager manager); int GetTotalSalary(); private: string GetBasicInfo(CEmployee *pemployee); string GetManagerInfo(CManager manager); string GetCommonEmployee(CCommonEmployee employee); static const int MANAGER_COEFFICENT = 5; static const int COMMONEMPLOYEE_COEFFICENT = 2; int m_commonTotal; int m_managerTotal; void CalCommonSalary(int salary); void CalManagerSalary(int salary); }; //BaseVisitor.cpp #include "StdAfx.h" #include "..\CommonDeclare\Convert.h" #include "BaseVisitor.h" #include <iostream> using std::string; using std::cout; using std::endl; CBaseVisitor::CBaseVisitor(void) { m_commonTotal = 0; m_managerTotal = 0; } CBaseVisitor::~CBaseVisitor(void) { } void CBaseVisitor::Visit(CCommonEmployee commonEmployee) { cout << this->GetCommonEmployee(commonEmployee).c_str() << endl; this->CalCommonSalary(commonEmployee.GetSalary()); } void CBaseVisitor::Visit(CManager manager) { cout << this->GetManagerInfo(manager).c_str() << endl; this->CalManagerSalary(manager.GetSalary()); } string CBaseVisitor::GetBasicInfo(CEmployee *pemployee) { string info = ""; info.append("姓名:"); info.append(pemployee->GetName()); info.append("\t"); info.append("性别:"); info.append(CConvert::ToString(pemployee->GetSex())); info.append("\t"); info.append("薪水:"); info.append(CConvert::ToString(pemployee->GetSalary())); info.append("\t"); return info; } string CBaseVisitor::GetManagerInfo(CManager manager) { string basicInfo = this->GetBasicInfo(&manager); string otherInfo = ""; otherInfo.append("业绩:"); otherInfo.append(manager.GetPerformance()); otherInfo.append("\t"); basicInfo.append(otherInfo); return basicInfo; } string CBaseVisitor::GetCommonEmployee(CCommonEmployee employee) { string basicInfo = this->GetBasicInfo(&employee); string otherInfo = ""; otherInfo.append("工作:"); otherInfo.append(employee.GetJob()); otherInfo.append("\t"); basicInfo.append(otherInfo); return basicInfo; } int CBaseVisitor::GetTotalSalary() { return this->m_commonTotal + this->m_managerTotal; } void CBaseVisitor::CalCommonSalary(int salary) { this->m_commonTotal += salary; } void CBaseVisitor::CalManagerSalary(int salary) { this->m_managerTotal += salary; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
//Employee.h #pragma once #include "IVisitor.h" #include <iostream> using std::string; class CEmployee { public: static int MALE; static int FEMALE; CEmployee(void); virtual ~CEmployee(void); string GetName(); void SetName(string name); int GetSalary(); void SetSalary(int v); int GetSex(); void SetSex(int v); virtual void Accept(IVisitor *pVisitor) = 0; private: string m_name; int m_salary; int m_sex; }; //Employee.cpp #include "StdAfx.h" #include "Employee.h" int CEmployee::MALE = 0; int CEmployee::FEMALE = 1; CEmployee::CEmployee(void) { } CEmployee::~CEmployee(void) { } string CEmployee::GetName() { return m_name; } void CEmployee::SetName( string name ) { m_name = name; } int CEmployee::GetSalary() { return m_salary; } void CEmployee::SetSalary( int v ) { m_salary = v; } int CEmployee::GetSex() { return m_sex; } void CEmployee::SetSex( int v ) { m_sex = v; } //Manager.h #pragma once #include "employee.h" #include "IVisitor.h" #include <iostream> using std::string; class CManager : public CEmployee { public: CManager(void); ~CManager(void); string GetPerformance(); void SetPerformance(string per); void Accept(IVisitor *pVisitor); protected: string GetOtherInfo(); private: string m_performance; }; //Manager.cpp #include "StdAfx.h" #include "Manager.h" #include <iostream> using std::string; CManager::CManager(void) { this->m_performance = ""; } CManager::~CManager(void) { } string CManager::GetPerformance() { return m_performance; } void CManager::SetPerformance(string per) { this->m_performance = per; } string CManager::GetOtherInfo() { string info = ""; info.append("业绩:"); info.append(this->m_performance); info.append("\t"); return info; } void CManager::Accept(IVisitor *pVisitor) { pVisitor->Visit(*this); } // Visitor.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "Employee.h" #include "CommonEmployee.h" #include "Manager.h" #include "BaseVisitor.h" #include "..\CommonDeclare\Convert.h" #include <vector> #include <iostream> using std::vector; using std::cout; using std::endl; void MockEmployee(vector<CEmployee*> *pvce) { CCommonEmployee *pZhangSan = new CCommonEmployee(); pZhangSan->SetJob("编写Java程序,绝对的蓝领、苦工加搬运工"); pZhangSan->SetName("张三"); pZhangSan->SetSalary(1800); pZhangSan->SetSex(CEmployee::MALE); pvce->push_back(pZhangSan); CCommonEmployee *pLiSi = new CCommonEmployee(); pLiSi->SetJob("页面美工,审美素质太不流行了!"); pLiSi->SetName("李四"); pLiSi->SetSalary(1900); pLiSi->SetSex(CEmployee::FEMALE); pvce->push_back(pLiSi); CManager *pWangWu = new CManager(); pWangWu->SetPerformance("基本上是负值,但是我会拍马屁呀"); pWangWu->SetName("王五"); pWangWu->SetSalary(1900); pWangWu->SetSex(CEmployee::FEMALE); pvce->push_back(pWangWu); } void DoIt() { vector<CEmployee*> vce; MockEmployee(&vce); vector<CEmployee*>::const_iterator readIt = vce.begin(); CBaseVisitor visitor; for (; readIt != vce.end(); readIt ++) { (*readIt)->Accept(&visitor); } cout << "本公司的月工资总额是:" << CConvert::ToString(visitor.GetTotalSalary()).c_str() << endl; vector<CEmployee*>::reverse_iterator delIt = vce.rbegin(); for (; delIt != vce.rend(); delIt++) { delete (*delIt); (*delIt) = NULL; } vce.clear(); } int _tmain(int argc, _TCHAR* argv[]) { DoIt(); return 0; } |
发表评论
要发表评论,您必须先登录。