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

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

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

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

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

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

 

实例:

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

 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;
}

 

原文链接: https://www.cnblogs.com/xj626852095/p/3648089.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/109426

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午10:10
下一篇 2023年2月10日 上午10:36

相关推荐