标签:ack water prot sso receiver space using ddp new
訪问者设计模式是已经有了一组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); };
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 訪问者设计模式
标签:ack water prot sso receiver space using ddp new
原文地址:http://www.cnblogs.com/cynchanpin/p/6860711.html