1. 程式人生 > >Design Pattern Visitor 訪問者設計模式

Design Pattern Visitor 訪問者設計模式

string 類對象 action 設計模式 eas nts new cpp att

訪問者設計模式是已經有了一組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 訪問者設計模式