您的位置:首页 > 编程语言 > C语言/C++

大话设计模式--访问者模式 Visitor -- C++实现实例

2013-10-23 10:57 567 查看
1. 访问者模式: 表示一个作用于某对象结构中的和元素的操作,它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。



访问者模式把数据结构和作用于结构上的操作之间的耦合脱开,使得操作集合可以相对自由地演化。

访问者模式的目的是把处理从数据结构分离出来,适用于 有比较稳定的数据结构,又有易于变化的算法。

添加新的操作很简单,只需要添加一个新的访问者就可以了。

实例:



action.h action.cpp 扮演访问者的角色,添加新操作,添加其子类实例就可以了

#ifndef ACTION_H
#define ACTION_H

class Man;
class Woman;

class Action
{
public:
Action();
void virtual getManConclusion(Man *man)=0;
void virtual getWomanConclusion(Woman *woman)=0;
};

#endif // ACTION_H

#include "action.h"
#include "man.h"
#include "woman.h"

Action::Action()
{
}


successaction.h successaction.cpp

#ifndef SUCCESSACTION_H
#define SUCCESSACTION_H

#include "action.h"

class SuccessAction : public Action
{
public:
SuccessAction();
void getManConclusion(Man *man);
void getWomanConclusion(Woman *woman);
};

#endif // SUCCESSACTION_H

#include "successaction.h"
#include <stdio.h>

SuccessAction::SuccessAction()
{
}

void SuccessAction::getManConclusion(Man *man)
{
printf("Success man , a woman on back \n");
}

void SuccessAction::getWomanConclusion(Woman *woman)
{
printf("Success woman , lots of unsuccessfull man on back\n");
}


failaction.h failaction.cpp

#ifndef FAILACTION_H
#define FAILACTION_H

#include "action.h"

class FailAction : public Action
{
public:
FailAction();
void getManConclusion(Man *man);
void getWomanConclusion(Woman *woman);
};

#endif // FAILACTION_H

#include "failaction.h"
#include <stdio.h>

FailAction::FailAction()
{
}

void FailAction::getManConclusion(Man *man)
{
printf("Fail man, lots of woman on back\n");
}

void FailAction::getWomanConclusion(Woman *woman)
{
printf("Fail woman, lots of man on back\n");
}


persion.h persion.cpp

#ifndef PERSION_H
#define PERSION_H

#include "action.h"

class Persion
{
public:
Persion();
void virtual accept(Action *visitor);
};

#endif // PERSION_H

#include "persion.h"

Persion::Persion()
{
}

void Persion::accept(Action *visitor)
{
}


man.h man.cpp

#ifndef MAN_H
#define MAN_H

#include "persion.h"

class Man : public Persion
{
public:
Man();
void accept(Action *visitor);
};

#endif // MAN_H

#include "man.h"

Man::Man()
{
}

void Man::accept(Action *visitor)
{
visitor->getManConclusion(this);
}


woman.h woman.cpp

#ifndef WOMAN_H
#define WOMAN_H

#include "persion.h"

class Woman : public Persion
{
public:
Woman();
void accept(Action *visitor);
};
#endif // WOMAN_H

#include "woman.h"

Woman::Woman()
{
}

void Woman::accept(Action *visitor)
{
visitor->getWomanConclusion(this);
}


main.cpp

#include <iostream>
#include "man.h"
#include "woman.h"
#include "failaction.h"
#include "successaction.h"
using namespace std;

int main()
{
cout << "Visitor test!" << endl;

Persion *man = new Man();
Persion *woman = new Woman();
Action *success = new SuccessAction();
Action *fail = new FailAction();

man->accept(success);
man->accept(fail);
woman->accept(success);
woman->accept(fail);

delete man;
delete woman;
delete success;
delete fail;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: