访问者模式:表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。
适用:数据结构相对稳定的系统
数据结构和作用于结构上的操作之间的耦合解脱开,使得操作集合可以相对自由地哑奴啊/
目的:把处理从数据结构分离出来/iiao稳定的数据结构和变化的算法,使用访问者模式比较合适。因为访问者模式使得算法操作的增加变得更加容易。
优点:增加新的操作很容易,只需要增加一个新的访问者。访问者模式将有关的行为集中到一个访问者对象中。
缺点:增加新的数据结构变得困难。
Element:人类:
ConcreteElementA:男人
ObjectStructure:对象结构类
Visitor:状态类
main.cpp
#include <iostream>
#include <stdlib.h>
#include "ObjectStructure.h"
#include "Action.h"
#include "Person.h"
using namespace std;
void process()
{
ObjectStructure object;
object.attach(std::shared_ptr<Person>(new Man("男人")));
object.attach(std::shared_ptr<Person>(new Woman("女人")));
//成功时的反应
object.display(std::shared_ptr<Action>(new Sucess()));
//失败时的反应
object.display(std::shared_ptr<Action>(new Failure()));
//恋爱时的反应
object.display(std::shared_ptr<Action>(new Amativeness()));
}
int main(int argc,char* argv[])
{
process();
system("pause");
return 0;
}
Person.h
#ifndef PERSON_H
#define PERSON_H
#include <memory>
#include <string>
class Action;
class Person
{
public:
Person(const std::string& sName);
virtual ~Person(void);
//接受
virtual void accept(std::shared_ptr<Action> ptrVisitor);
public:
std::string _sName;
};
//男人
class Man : public Person
{
public:
Man(const std::string& sName);
~Man();
void accept(std::shared_ptr<Action> ptrVisitor);
};
//女人
class Woman : public Person
{
public:
Woman(const std::string& sName);
~Woman();
void accept(std::shared_ptr<Action> ptrVisitor);
};
#endif
Person.cpp
#include "Person.h"
#include "Action.h"
Person::Person(const std::string& sName):_sName(sName)
{
}
Person::~Person(void)
{
}
void Person::accept(std::shared_ptr<Action> ptrVisitor)
{
}
Man::Man(const std::string& sName):Person(sName)
{
}
Man::~Man()
{
}
void Man::accept(std::shared_ptr<Action> ptrVisitor)
{
ptrVisitor->getManConclusion(this);
}
Woman::Woman(const std::string& sName):Person(sName)
{
}
Woman::~Woman()
{
}
void Woman::accept(std::shared_ptr<Action> ptrVisitor)
{
ptrVisitor->getWomanConclusion(this);
}
Action.h
#ifndef ACTION_H
#define ACTION_H
#include "Person.h"
//状态
class Action
{
public:
Action(void);
virtual ~Action(void);
virtual void getManConclusion(Man* ptrMan);
virtual void getWomanConclusion(Woman* ptrWoman);
};
//成功
class Sucess : public Action
{
public:
void getManConclusion(Man* ptrMan);
void getWomanConclusion(Woman* ptrWoman);
};
//失败
class Failure : public Action
{
public:
void getManConclusion(Man* ptrMan);
void getWomanConclusion(Woman* ptrWoman);
};
//恋爱
class Amativeness : public Action
{
public:
void getManConclusion(Man* ptrMan);
void getWomanConclusion(Woman* ptrWoman);
};
#endif
Action.cpp
#include "Action.h"
#include <iostream>
#include "Person.h"
using namespace std;
Action::Action(void)
{
}
Action::~Action(void)
{
}
void Action::getManConclusion(Man* ptrMan)
{
}
void Action::getWomanConclusion(Woman* ptrWoman)
{
}
void Sucess::getManConclusion(Man* ptrMan)
{
cout << ptrMan->_sName << "成功时,背后多半有一个伟大的女人" << endl;
}
void Sucess::getWomanConclusion(Woman* ptrWoman)
{
cout << ptrWoman->_sName << "成功时,背后大多有一个不成功的男人" << endl;
}
void Failure::getManConclusion(Man* ptrMan)
{
cout << ptrMan->_sName << "失败时,借酒浇愁" << endl;
}
void Failure::getWomanConclusion(Woman* ptrWoman)
{
cout << ptrWoman->_sName << "失败时,撒娇" << endl;
}
void Amativeness::getManConclusion(Man* ptrMan)
{
cout << ptrMan->_sName << "恋爱时,言听计从" << endl;
}
void Amativeness::getWomanConclusion(Woman* ptrWoman)
{
cout << ptrWoman->_sName << "恋爱时,智商为0" << endl;
}
ObjectStructure.h
#ifndef OBJECTSTRUCTURE_H
#define OBJECTSTRUCTURE_H
#include <vector>
#include <memory>
using namespace std;
class Person;
class Action;
class ObjectStructure
{
public:
ObjectStructure(void);
~ObjectStructure(void);
void attach(std::shared_ptr<Person>& ptrPerson);
void display(std::shared_ptr<Action> ptrVisitor);
public:
vector< std::shared_ptr<Person> > _vecPtrPerson;
};
#endif
ObjectStructure.cpp
#include "ObjectStructure.h"
#include "Person.h"
#include "Action.h"
ObjectStructure::ObjectStructure()
{
}
ObjectStructure::~ObjectStructure(void)
{
}
void ObjectStructure::attach(std::shared_ptr<Person>& ptrPerson)
{
_vecPtrPerson.push_back(ptrPerson);
}
void ObjectStructure::display(std::shared_ptr<Action> ptrVisitor)
{
for(unsigned ui = 0 ; ui < _vecPtrPerson.size() ; ui++)
{
_vecPtrPerson[ui]->accept(ptrVisitor);
}
}