设计模式 访问者模式

1、main.cpp
/*
作者:jhluroom弹   QQ:454676244  MSN:jhlu0815@hotmail.com
开发IDE:qt creater
开发环境:QT C++
参考网站:神秘果:http://www.shenmiguo.com/

定义:

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

理解:
1.如果一个类有很多内部数据,因此也就有很多访问操作,这样会使它的接口非常庞大,难以变动难以学习。访问者模式可以做到数据
  的存储与使用分离,不同的访问者可以集中不同类别的操作,并且可以随时增加新的访问者或者新方法来增加新的操作。

要点:
1.和观察者的区别是,被观察者要执行一个动作,然后主动发送通知给观察者。访问者模式是由访问者主动发出的动作

应用:
1.适用于数据结构稳定的系统。它把数据结构和作用于数据结构上的操作分离开,使得操作集合。新增加操作很容易,因为增加新操作就
  相当于增加一个访问者,访问者模式将有关的行为集中到一个访问者对象中。
2.一个对象结构包含很多类对象,它们有不同的接口,而你想对这些对象实施一些依赖于其具体类的操作。
3.需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而你想避免让这些操作“污染”这些对象的类。Visitor使得你可以将
  相关的操作集中起来定义在一个类中。当该对象结构被很多应用共享时,用Visitor模式让每个应用仅包含需要用到的操作。
4.定义对象结构的类很少改变,但经常需要在此结构上定义新的操作。改变对象结构类需要重定义对所有访问者的接口,这可能需要很大
  的代价。如果对象结构类经常改变,那么可能还是在这些类中定义这些操作较好。
5.源码如下所示:

以上文字说明,从网上整理而来,有可能部分与其他同仁相同,请谅解,希望我们能够共同交流,谢谢!
*/

#include <QtCore/QCoreApplication>

#include <iostream>
#include <list>
#include <algorithm>
#include <string>
using namespace std;

class Action;
class Man;
class Woman;

class Person //Element,被访问对象的基类
{
protected:
    string name;
public:
    Person(const string& s = "Person") : name(s) {}
    virtual void Accept(Action* visitor) = 0;
    virtual string getName()
    {
        return name;
    }
};

class Action //Visitor,访问对象的基类,
{
protected:
    string name;
public:
    Action(const string& s = "Action") : name(s) {}
    virtual void GetManConclusion(Man* m) = 0;
    virtual void GetWomanConclusion(Woman* w) = 0;
};

class Man : public Person //ConcreteElement,被访问对象的具体类
{
public:
    Man(const string& s = "Man") : Person(s) {}
    virtual void Accept(Action* visitor)
    {
        visitor->GetManConclusion(this);
    }
};

class Woman : public Person //ConcreteElement,被访问对象的具体类
{
public:
    Woman(const string& s = "Woman") : Person(s) {}
    virtual void Accept(Action* visitor)
    {
        visitor->GetWomanConclusion(this);
    }
};

class Success : public Action //ConcreteVisitor,访问对象的具体类,
{
public:
    Success(const string& s = "Success") : Action(s) {}
    virtual void GetManConclusion(Man* m)
    {
        cout << "Action ";
        cout << name<<" ";
        cout << m->getName()<<" ";
        cout << "1" << endl;
    }
    virtual void GetWomanConclusion(Woman* w)
    {
        cout << "Action ";
        cout << name<< " ";
        cout << w->getName()<<" ";
        cout << "2" << endl;
    }
};

class Failing : public Action //ConcreteVisitor,访问对象的具体类,
{
public:
    Failing(const string& s = "Failing") : Action(s) {}
    virtual void GetManConclusion(Man* m)
    {
        cout << "Action ";
        cout << name<<" ";
        cout << m->getName()<<" ";
        cout << "3" << endl;
    }
    virtual void GetWomanConclusion(Woman* w)
    {
        cout << "Action ";
        cout << name<< " ";
        cout << w->getName()<<" ";
        cout << "4" << endl;
    }
};

class Amativeness : public Action //ConcreteVisitor,访问对象的具体类,
{
public:
    Amativeness(const string& s = "Amativeness") : Action(s) {}
    virtual void GetManConclusion(Man* m)
    {
        cout << "Action ";
        cout << name<<" ";
        cout << m->getName()<<" ";
        cout << "5" << endl;
    }
    virtual void GetWomanConclusion(Woman* w)
    {
        cout << "Action ";
        cout << name<< " ";
        cout << w->getName()<<" ";
        cout << "6" << endl;
    }
};

class Marriage : public Action //ConcreteVisitor,访问对象的具体类,
{
public:
    Marriage(const string& s = "Marriage") : Action(s) {}
    virtual void GetManConclusion(Man* m)
    {
        cout << "Action ";
        cout << name<<" ";
        cout << m->getName()<<" ";
        cout << "7" << endl;
    }
    virtual void GetWomanConclusion(Woman* w)
    {
        cout << "Action ";
        cout << name<< " ";
        cout << w->getName()<<" ";
        cout << "8" << endl;
    }
};

class ObjectStructure //ObjectStructure
{
private:
    list<Person*> elements;
public:
    ObjectStructure() {}
    ~ObjectStructure()
    {
        for (list<Person*>::iterator iter = elements.begin(); iter != elements.end(); ++iter)
        {
            delete (*iter);
        }
    }
    void Attach(Person* element)
    {
        elements.push_back(element);
    }
    void Detach(Person* element)
    {
        list<Person*>::iterator iter = find(elements.begin(), elements.end(), element);
        if (iter != elements.end())
        {
            elements.erase(iter);
        }
    }
    void Display(Action* visitor)
    {
        for (list<Person*>::iterator iter = elements.begin(); iter != elements.end(); ++iter)
        {
            (*iter)->Accept(visitor);
        }
    }
    int size()
    {
        return elements.size();
    }
};


int main(int argc, char *argv[])
{
    cout << "=== jhluroom start ========" << endl;

    ObjectStructure objectStructure;
    Man *man = new Man;
    Woman *woman = new Woman;
    Success* v1 = new Success;
    Failing* v2 = new Failing;
    Amativeness* v3 = new Amativeness;
    Marriage* v4 = new Marriage;

    cout <<"Person elements is:"<< objectStructure.size() << endl <<endl;

    cout << "objectStructure.Attach(man)"<<endl;
    cout << "objectStructure.Attach(man)"<<endl;
    objectStructure.Attach(man);
    objectStructure.Attach(woman);
    objectStructure.Display(v1);
    objectStructure.Display(v2);
    objectStructure.Display(v3);
    objectStructure.Display(v4);

    cout << endl;
    cout << "objectStructure.Detach(man)"<<endl;
    objectStructure.Detach(man);
    objectStructure.Display(v1);
    objectStructure.Display(v2);
    objectStructure.Display(v3);
    objectStructure.Display(v4);

    delete man;
    delete woman;
    delete v1;
    delete v2;
    delete v3;
    delete v4;

    cout << "=== jhluroom finish _^_ ===" << endl;
    return 0;
}

运行结果:
=== jhluroom start ========
Person elements is:0
objectStructure.Attach(man)
objectStructure.Attach(man)
Action Success Man 1
Action Success Woman 2
Action Failing Man 3
Action Failing Woman 4
Action Amativeness Man 5
Action Amativeness Woman 6
Action Marriage Man 7
Action Marriage Woman 8
objectStructure.Detach(man)
Action Success Woman 2
Action Failing Woman 4
Action Amativeness Woman 6
Action Marriage Woman 8

=== jhluroom finish _^_ ===


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值