vczh的日常 2018-04-21
访问者模式是一种将数据操作和数据结构分离的设计模式,可以说是面向数据密集型的一种设计方式,数据的结构相对稳定,有明显的分层和分类,而对数据对象的相关操作进行分组、分析等二次加工,这些操作都是由访问者来执行的,而不是将这些放到被访问的数据对象中,方便了操作方法的扩展。
封装一些作用于某种数据结构中的各元素的操作,它可以在不改变这个数据结构的前提下定义作用于这些元素的新的操作。
访问者模式的关键实现是在Element中的accept函数,该函数传入visitor,并在函数内调用visitor的针对该对象的方法,访问者调用元素类中的方法,又回到访问类中进行调用的方式叫做双重分派。
class Person // 游乐设施 class Facility { public: Facility():m_ncost_money(0), m_ncost_time(0){} virtual void accept( Person*) =0; virtual int SpendMoney()=0; virtual int SpendTime()=0; protected: int m_ncost_money; int m_ncost_time; }; //摩天轮 class FerrisWheel : public Facility { public: FerrisWheel():m_ncost_money(10), m_ncost_time(15){} void accept( Person* persion) { persion->play(this); } int SpendMoney(){ return m_ncost_time;} int SpendTime(){return m_ncost_money;} }; //碰碰车 class BumperCar : public Facility { public: BumperCar():m_ncost_money(30), m_ncost_time(10){} void accept( Person* persion) { persion->play(this); } int SpendMoney(){ return m_ncost_time;} int SpendTime(){return m_ncost_money;} }; //过山车 class RollerCoaster : public Facility { public: RollerCoaster():m_ncost_money(100), m_ncost_time(20){} void accept( Person* persion) { persion->play(this); } int SpendMoney(){ return m_ncost_time;} int SpendTime(){return m_ncost_money;} }; class Person { public: Person() : m_nToTalMoney(0),m_nToTalTime(0){} void Play(FerrisWheel*); void Play(BumperCar*); void Play(FerrisWheel*); public: int GetTotalMoney() { return m_nToTalMoney; } int GetTotalTime() { return m_nToTalTime; } protected: int m_nToTalMoney; int m_nToTalTime; } class Adult : public Person { public: void Play(FerrisWheel* ferriswheel) { int nMoney = ferriswheel->SpendMoney(); m_ncost_money+=nMoney; int nTime = ferriswheel->SpendTime(); m_nToTalTime += nTime; } void Play(BumperCar* bumpercar) { int nMoney = BumperCar->SpendMoney(); m_ncost_money+=nMoney; int nTime = BumperCar->SpendTime(); m_nToTalTime += nTime; } void Play(RollerCoaster* rollercoaster) { int nMoney = RollerCoaster->SpendMoney(); m_ncost_money+=nMoney; int nTime = RollerCoaster->SpendTime(); m_nToTalTime += nTime; } }; //儿童所有花费半价 class Childen : public Person { public: void Play(FerrisWheel* ferriswheel) { int nMoney = ferriswheel->SpendMoney()/2; m_ncost_money+=nMoney; int nTime = ferriswheel->SpendTime(); m_nToTalTime += nTime; } void Play(BumperCar* bumpercar) { int nMoney = BumperCar->SpendMoney()/2; m_ncost_money+=nMoney; int nTime = BumperCar->SpendTime(); m_nToTalTime += nTime; } void Play(RollerCoaster* rollercoaster) { int nMoney = RollerCoaster->SpendMoney()/2; m_ncost_money+=nMoney; int nTime = RollerCoaster->SpendTime(); m_nToTalTime += nTime; } }; int main() { FerrisWheel fw; BumperCar bc; RollerCoaster rc; Childen son; Adult mather; //儿子玩了所有项目 son.play(fw); son.play(bc); son.play(rc); //可怜的妈妈只陪儿子玩了一个过山车 mother.play(rc); int nSonCostMoney = son.GetTotalMoney(); int nSonCostTime = son.getTotalTime(); int nMotherCostMoney = mother.GetTotalMoney(); int nMotherCostTime = mother.getTotalTime(); cout<< "Son spend money :" << nSonCostMoney << " spend time: "<< nSonCostTime; << endl; cout<< "Mother spend money :" << nMotherCostMoney << " Mother time: "<< nMotherCostTime; << endl; }
访问者模式的主要问题是因为每个访问者需要有对应的方法来处理每一种可能的具体数据,,那么一旦实现了访问者模式,其具体类的数量和类型就不能轻易被改变。