Design Pattern Visitor 訪问者设计模式
Posted cynchanpin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Design Pattern Visitor 訪问者设计模式相关的知识,希望对你有一定的参考价值。
訪问者设计模式是已经有了一组Person对象了,然后不同的訪问者訪问这组对象。会有不同效果。
这些訪问者实际上就是一个能够让Person对象组运行的动作行为等。
至于这些Person对象是怎样运行这些訪问者的动作的,那是已经在特定的不同的Person对象中设计好的。
比方我们的訪问者或许是一些动作集合的类,如:
class Action { public: string present; string gun; virtual void drinkBeer(Person *p) = 0; virtual void getAGun(Person *p) = 0; };
依据这个訪问者基类定义不同的訪问动作:
class ActionOne:public Action { public: ActionOne() { present = "Alcohol"; gun = "Laiser"; } void drinkBeer(Person *p); void getAGun(Person *p); }; class ActionTwo:public Action { public: ActionTwo() { present = "Beer"; gun = "Machine Gun"; } void getAGun(Person *p); void drinkBeer(Person *p); };
而Person类则运行动作类的不同动作,也是基类动作的一部分,这样呈现出不同Person运行的行为不一样。
class Person { public: string something; virtual void receivedVisitor(Action *visitor) = 0; }; class Bill:public Person { public: Bill() { something = "food"; } void receivedVisitor(Action *visitor) { puts("\nVisitor to Bill bring :"); puts(visitor->present.c_str()); visitor->drinkBeer(this); } }; class Mark:public Person { public: Mark() { something = "Weapon"; } void receivedVisitor(Action *visitor) { puts("\nVisitor to Mark bring :"); puts(visitor->gun.c_str()); visitor->getAGun(this); } };
使用一个类来存放这些对象:
class House { protected: vector<Person *> vps; public: void addPerson(Person *p) { vps.push_back(p); } void receiver(Action *visitor) { for (int i = 0; i < (int)vps.size(); i++) { vps[i]->receivedVisitor(visitor); } } ~House() { for (int i = 0; i < (int)vps.size(); i++) { delete vps[i]; } } };
最后就在这个类对象中存放须要被訪问的对象,然后使用动作类来訪问这些对象。
总的来说也是思想并不太难的一个设计模式,可是要非常好实现还是不太easy的。
所有代码例如以下:
#include <stdio.h> #include <iostream> #include <string> #include <vector> using namespace std; class Person; class Action { public: string present; string gun; virtual void drinkBeer(Person *p) = 0; virtual void getAGun(Person *p) = 0; }; class ActionOne:public Action { public: ActionOne() { present = "Alcohol"; gun = "Laiser"; } void drinkBeer(Person *p); void getAGun(Person *p); }; class ActionTwo:public Action { public: ActionTwo() { present = "Beer"; gun = "Machine Gun"; } void getAGun(Person *p); void drinkBeer(Person *p); }; class Person { public: string something; virtual void receivedVisitor(Action *visitor) = 0; }; class Bill:public Person { public: Bill() { something = "food"; } void receivedVisitor(Action *visitor) { puts("\nVisitor to Bill bring :"); puts(visitor->present.c_str()); visitor->drinkBeer(this); } }; class Mark:public Person { public: Mark() { something = "Weapon"; } void receivedVisitor(Action *visitor) { puts("\nVisitor to Mark bring :"); puts(visitor->gun.c_str()); visitor->getAGun(this); } }; void ActionOne::drinkBeer(Person *p) { puts("Let‘s eat something"); puts(p->something.c_str()); } void ActionOne::getAGun(Person *p) { puts("Let‘s gear up"); puts(p->something.c_str()); } void ActionTwo::getAGun(Person *p) { puts("Let‘s gear up"); puts(p->something.c_str()); } void ActionTwo::drinkBeer(Person *p) { puts("Let‘s eat something"); puts(p->something.c_str()); } class House { protected: vector<Person *> vps; public: void addPerson(Person *p) { vps.push_back(p); } void receiver(Action *visitor) { for (int i = 0; i < (int)vps.size(); i++) { vps[i]->receivedVisitor(visitor); } } ~House() { for (int i = 0; i < (int)vps.size(); i++) { delete vps[i]; } } }; int main() { House house; house.addPerson(new Bill); house.addPerson(new Mark); ActionTwo gun; ActionOne drink; house.receiver(&gun); house.receiver(&drink); return 0; }
运行:
以上是关于Design Pattern Visitor 訪问者设计模式的主要内容,如果未能解决你的问题,请参考以下文章