C++设计模式-访问者模式

目录

 

 

基本概念

代码与实例


 

基本概念

访问者模式(Visitor):表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变元素的类的前提下定义作用于这些元素的新操作。

UML图如下(此图来源于大话设计模式)

C++设计模式-访问者模式_第1张图片

访问者模式适用于数据结构相对稳定的系统。

访问者模式的优点就是增加新的操作很容易,因为增加新的操作就意味着增加一个新的访问者。访问者模式将有关行为集中到一个访问者对象中。

 

代码与实例

程序运行截图如下:

C++设计模式-访问者模式_第2张图片

源码如下:

Head.h

#ifndef HEAD_H
#define HEAD_H

#include 
#include 
#include 
#include 
using namespace std;

class ConcreteElementA;
class ConcreteElementB;

//Visitor类,为该对象结构中ConcreteElement的每一个类声明一个Visit操作
class Visitor{

public:
	virtual void visitConcreteElementA(ConcreteElementA *concreteElementA) = 0;
	virtual void visitConcreteElementB(ConcreteElementB *concreteElementB) = 0;
	virtual ~Visitor();
};

//ConcreteVisitor1和ConcreteVisitor2类,具体访问者,实现每一个由Visitor声明的操作。
//每一个操作实现算法的一部分,而该算法片断仍是对应于结构中对象的类。
class ConcreteVisitor1: public Visitor{

public:
	void visitConcreteElementA(ConcreteElementA *concreteElementA);
	void visitConcreteElementB(ConcreteElementB *concreteElementB);
	~ConcreteVisitor1();

	string getName();
	void setName(const string &name);

private:
	string m_name;
};

class ConcreteVisitor2: public Visitor{

public:
	void visitConcreteElementA(ConcreteElementA *concreteElementA);
	void visitConcreteElementB(ConcreteElementB *concreteElementB);
	~ConcreteVisitor2();

	string getName();
	void setName(const string &name);

private:
	string m_name;
};

//Element类,定义一个Accept操作,它以一个访问者为参数
class Element{

public:
	virtual void accept(Visitor *visitor) = 0;
	virtual ~Element();
};

//ConcreteElementA和ConcreteElementB类,具体元素,实现Accept操作
class ConcreteElementA: public Element{

public:
	ConcreteElementA(string name);
	void accept(Visitor *visitor);
	/*
	下面还可能有其他的方法
	*/

	~ConcreteElementA();
	string getName();
	void setName(const string &name);

private:
	string m_name;
};

class ConcreteElementB: public Element{

public:
	ConcreteElementB(string name);
	void accept(Visitor *visitor);
	/*
	下面还可能有其他的方法
	*/

	~ConcreteElementB();
	string getName();
	void setName(const string &name);

private:
	string m_name;
};

class ObjectStructure{

public:
	void attach(Element *element);
	void detach(Element *element);
	void accpet(Visitor *visitor);
	~ObjectStructure();
private:
	list m_list;
};

#endif

Head.cpp

#include "Head.h"

Visitor::~Visitor()
{
	cout << "Visitor::~Visitor()" << endl;
}

void ConcreteVisitor1::visitConcreteElementA(ConcreteElementA *concreteElementA)
{
	cout << concreteElementA->getName() << " 被 " << this->getName() << "访问!" << endl;
}

void ConcreteVisitor1::visitConcreteElementB(ConcreteElementB *concreteElementB)
{
	cout << concreteElementB->getName() << " 被 " << this->getName() << "访问!" << endl;
}

ConcreteVisitor1::~ConcreteVisitor1()
{
	cout << "ConcreteVisitor1::~ConcreteVisitor1()" << endl;
}

std::string ConcreteVisitor1::getName()
{
	return m_name;
}

void ConcreteVisitor1::setName(const string &name)
{
	m_name = name;
}

void ConcreteVisitor2::visitConcreteElementA(ConcreteElementA *concreteElementA)
{
	cout << concreteElementA->getName() << " 被 " << this->getName() << "访问!" << endl;
}

void ConcreteVisitor2::visitConcreteElementB(ConcreteElementB *concreteElementB)
{
	cout << concreteElementB->getName() << " 被 " << this->getName() << "访问!" << endl;
}

ConcreteVisitor2::~ConcreteVisitor2()
{
	cout << "ConcreteVisitor2::~ConcreteVisitor2()" << endl;
}

std::string ConcreteVisitor2::getName()
{
	return m_name;
}

void ConcreteVisitor2::setName(const string &name)
{
	m_name = name;
}

Element::~Element()
{
	cout << "Element::~Element()" << endl;
}

ConcreteElementA::ConcreteElementA(string name)
{
	m_name = name;
}

void ConcreteElementA::accept(Visitor *visitor)
{
	visitor->visitConcreteElementA(this);
}

ConcreteElementA::~ConcreteElementA()
{
	cout << "ConcreteElementA::~ConcreteElementA()" << endl;
}

std::string ConcreteElementA::getName()
{
	return m_name;
}

void ConcreteElementA::setName(const string &name)
{
	m_name = name;
}

ConcreteElementB::ConcreteElementB(string name)
{
	m_name = name;
}

void ConcreteElementB::accept(Visitor *visitor)
{
	visitor->visitConcreteElementB(this);
}

ConcreteElementB::~ConcreteElementB()
{
	cout << "ConcreteElementB::~ConcreteElementB()" << endl;
}

std::string ConcreteElementB::getName()
{
	return m_name;
}

void ConcreteElementB::setName(const string &name)
{
	m_name = name;
}

void ObjectStructure::attach(Element *element)
{
	m_list.push_back(element);
}

void ObjectStructure::detach(Element *element)
{
	m_list.remove(element);
}

void ObjectStructure::accpet(Visitor *visitor)
{
	for(list::iterator it = m_list.begin(); it != m_list.end(); it++){

		(*it)->accept(visitor);
	}
}

ObjectStructure::~ObjectStructure()
{
	cout << "ObjectStructure::~ObjectStructure()" << endl;
	for(list::iterator it = m_list.begin(); it != m_list.end(); it++){

		delete (*it);
	}
	m_list.clear();
}

main.cpp

#include "Head.h"

int main(int *argc, int *argv[]){

	ObjectStructure *o = new ObjectStructure;
	o->attach(new ConcreteElementA("呵呵"));
	o->attach(new ConcreteElementB("哈哈"));

	ConcreteVisitor1 *v1 = new ConcreteVisitor1;
	v1->setName("哦哦");
	ConcreteVisitor2 *v2 = new ConcreteVisitor2;
	v2->setName("嗷嗷");

	o->accpet(v1);
	o->accpet(v2);

	delete o;
	delete v1;
	delete v2;

	getchar();
	return 0;
}

 

你可能感兴趣的:(C/C++,设计模式)