设计模式:28 男人和女人_访问者模式

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

 

适用:数据结构相对稳定的系统

数据结构和作用于结构上的操作之间的耦合解脱开,使得操作集合可以相对自由地哑奴啊/

 

目的:把处理从数据结构分离出来/iiao稳定的数据结构和变化的算法,使用访问者模式比较合适。因为访问者模式使得算法操作的增加变得更加容易。

 

优点:增加新的操作很容易,只需要增加一个新的访问者。访问者模式将有关的行为集中到一个访问者对象中。

 

缺点:增加新的数据结构变得困难。

Element:人类:

ConcreteElementA:男人

ObjectStructure:对象结构类

Visitor:状态类

 

main.cpp

#include <iostream>
#include <stdlib.h>
#include "ObjectStructure.h"
#include "Action.h"
#include "Person.h"


using namespace std;

void process()
{
	ObjectStructure object;
	object.attach(std::shared_ptr<Person>(new Man("男人")));
	object.attach(std::shared_ptr<Person>(new Woman("女人")));

	//成功时的反应
	object.display(std::shared_ptr<Action>(new Sucess()));
	//失败时的反应
	object.display(std::shared_ptr<Action>(new Failure()));
	//恋爱时的反应
	object.display(std::shared_ptr<Action>(new Amativeness()));
}

int main(int argc,char* argv[])
{
	process();
	system("pause");
	return 0;
}


Person.h

#ifndef PERSON_H
#define PERSON_H
#include <memory>
#include <string>
class Action;
class Person
{
public:
	Person(const std::string& sName);
	virtual ~Person(void);
	//接受
	virtual void accept(std::shared_ptr<Action> ptrVisitor);
public:
	std::string _sName;
};

//男人
class Man : public Person
{
public:
	Man(const std::string& sName);
	~Man();
	void accept(std::shared_ptr<Action> ptrVisitor);
};

//女人
class Woman : public Person
{
public:
	Woman(const std::string& sName);
	~Woman();
	void accept(std::shared_ptr<Action> ptrVisitor);
};


#endif


Person.cpp

#include "Person.h"
#include "Action.h"


Person::Person(const std::string& sName):_sName(sName)
{
}


Person::~Person(void)
{
}

void Person::accept(std::shared_ptr<Action> ptrVisitor)
{
}

Man::Man(const std::string& sName):Person(sName)
{
}

Man::~Man()
{
}

void Man::accept(std::shared_ptr<Action> ptrVisitor)
{
	ptrVisitor->getManConclusion(this);
}

Woman::Woman(const std::string& sName):Person(sName)
{
}

Woman::~Woman()
{
}

void Woman::accept(std::shared_ptr<Action> ptrVisitor)
{
	ptrVisitor->getWomanConclusion(this);
}



Action.h

#ifndef ACTION_H
#define ACTION_H
#include "Person.h"

//状态
class Action
{
public:
	Action(void);
	virtual ~Action(void);
	virtual void getManConclusion(Man* ptrMan);
	virtual void getWomanConclusion(Woman* ptrWoman);
};

//成功
class Sucess : public Action
{
public:
	void getManConclusion(Man* ptrMan);
	void getWomanConclusion(Woman* ptrWoman);
};

//失败
class Failure : public Action
{
public:
	void getManConclusion(Man* ptrMan);
	void getWomanConclusion(Woman* ptrWoman);
};

//恋爱
class Amativeness : public Action
{
public:
	void getManConclusion(Man* ptrMan);
	void getWomanConclusion(Woman* ptrWoman);
};

#endif


Action.cpp

#include "Action.h"
#include <iostream>
#include "Person.h"
using namespace std;


Action::Action(void)
{
}


Action::~Action(void)
{
}

void Action::getManConclusion(Man* ptrMan)
{
	
}

void Action::getWomanConclusion(Woman* ptrWoman)
{
	
}

void Sucess::getManConclusion(Man* ptrMan)
{
	cout << ptrMan->_sName << "成功时,背后多半有一个伟大的女人" << endl;
}

void Sucess::getWomanConclusion(Woman* ptrWoman)
{
	cout << ptrWoman->_sName << "成功时,背后大多有一个不成功的男人" << endl;
}

void Failure::getManConclusion(Man* ptrMan)
{
	cout << ptrMan->_sName << "失败时,借酒浇愁" << endl;
}

void Failure::getWomanConclusion(Woman* ptrWoman)
{
	cout << ptrWoman->_sName << "失败时,撒娇" << endl;
}

void Amativeness::getManConclusion(Man* ptrMan)
{
	cout << ptrMan->_sName << "恋爱时,言听计从" << endl;
}

void Amativeness::getWomanConclusion(Woman* ptrWoman)
{
	cout << ptrWoman->_sName << "恋爱时,智商为0" << endl;
}


ObjectStructure.h

#ifndef OBJECTSTRUCTURE_H
#define OBJECTSTRUCTURE_H
#include <vector>
#include <memory>

using namespace std;

class Person;
class Action;
class ObjectStructure
{
public:
	ObjectStructure(void);
	~ObjectStructure(void);
	void attach(std::shared_ptr<Person>& ptrPerson);
	void display(std::shared_ptr<Action> ptrVisitor);
public:
	vector< std::shared_ptr<Person> > _vecPtrPerson;
};
#endif


ObjectStructure.cpp

#include "ObjectStructure.h"
#include "Person.h"
#include "Action.h"

ObjectStructure::ObjectStructure()
{
}


ObjectStructure::~ObjectStructure(void)
{
}

void ObjectStructure::attach(std::shared_ptr<Person>& ptrPerson)
{
	_vecPtrPerson.push_back(ptrPerson);
}

void ObjectStructure::display(std::shared_ptr<Action> ptrVisitor)
{
	for(unsigned ui = 0 ; ui < _vecPtrPerson.size() ; ui++)
	{
		_vecPtrPerson[ui]->accept(ptrVisitor);
	}
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值