设计模式——访问者模式(C++实现)

访问者模式

            表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新的操作。属于行为型模式

适用场景

           1、一个对象结构包含很多类对象,它们有不同的接口,而你想对这些对象实施一些依赖于其具体类的操作。
           2、需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而你想避免让这些操作“污染”这些对象的类。 Visitor使得你可以将相关的操作集中起来定义在一个类中。当该对象结构被很多应用共享时,用 Visitor模式让每个应用仅包含需要用到的操作。
           3、定义对象结构的类很少改变,但经常需要在此结构上定义新的操作。改变对象结构类需要重定义对所有访问者的接口,这可能需要很大的代价。如果对象结构类经常改变,那么可能还是在这些类中定义这些操作较好。
 

采用菜鸟教程的例子,这里用C++实现

#include<iostream>
#include<vector>

using namespace std;

class Computer;
class Mouse;
class KeyBoard;
class Monitor;

//创建一个访问者的抽象接口ComputerPartVistor类
class ComputerPartVistor
{
public:
	ComputerPartVistor() {};
	virtual ~ComputerPartVistor() {};
	virtual void visit(Computer *tc)=0;
	virtual void visit(Mouse *tc) = 0;
	virtual void visit(KeyBoard *tc) = 0;
	virtual void visit(Monitor *tc) = 0;
	
};

//创建继承抽象接口ComputerPartVistor的ComputerPartDisplayVisit类
class ComputerPartDisplayVisit :ComputerPartVistor
{
public:
	ComputerPartDisplayVisit() {};
	~ComputerPartDisplayVisit() {};
	void visit(Computer *tc);
	void visit(Mouse *tc);
	void visit(KeyBoard *tc);
	void visit(Monitor *tc);
};


//创建一个表示电脑各个部分的抽象接口ComputerPart
class ComputerPart
{
public:
	ComputerPart() {};
	~ComputerPart() {};
	
	virtual void accept(ComputerPartVistor *tComputerPartVistor)=0;

	virtual void play()=0;
};

//创建继承抽象接口ComputerPart的KeyBoard类
class KeyBoard :ComputerPart
{
public:
	KeyBoard() {};
	 ~KeyBoard() {};

	 void accept(ComputerPartVistor *tComputerPartVistor) { tComputerPartVistor->visit(this); };
	 void play() { cout << "KeyBoard play now" << endl; };
};

//创建继承抽象接口ComputerPart的Monitor类
class Monitor :ComputerPart
{
public:
	Monitor() {};
	~Monitor() {};

	void accept(ComputerPartVistor *tComputerPartVistor) { tComputerPartVistor->visit(this); };
	void play() { cout << "Monitor play now" << endl; };
};

//创建继承抽象接口ComputerPart的Mouse类
class Mouse :ComputerPart
{
public:
	Mouse() {};
	~Mouse() {};

	void accept(ComputerPartVistor *tComputerPartVistor) { tComputerPartVistor->visit(this); };
	void play() { cout << "Mouse play now" << endl; };
};

//创建继承抽象接口ComputerPart的Computer类
class Computer :ComputerPart
{
public:
	Computer() { parts.push_back((ComputerPart*)new Mouse());
	             parts.push_back((ComputerPart*)new KeyBoard());
	             parts.push_back((ComputerPart*)new Monitor());
	           };
	~Computer() {};

	void accept(ComputerPartVistor *tComputerPartVistor) { 
				vector<ComputerPart*>::iterator it;
				for (it = parts.begin(); it != parts.end(); ++it)
				{
					(*it)->accept(tComputerPartVistor);
				}
				tComputerPartVistor->visit(this);
	       };

	void play() { cout << "Computer play now" << endl; };

private:
	vector<ComputerPart*> parts;
};

void ComputerPartDisplayVisit::visit(Computer *tc)
{
	tc->play();
}
void ComputerPartDisplayVisit::visit(Mouse *tc)
{
	tc->play();
}
void ComputerPartDisplayVisit::visit(KeyBoard *tc)
{
	tc->play();
}
void ComputerPartDisplayVisit::visit(Monitor *tc)
{
	tc->play();
}


int main()
{
	ComputerPart *tcomput = (ComputerPart*)new  Computer();
	tcomput->accept((ComputerPartVistor*)new ComputerPartDisplayVisit());

	delete tcomput;
	tcomput = NULL;

	return 0;
}

在visual studio 2015上运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值