如何在运行时配置访问者模式?

时间:2016-04-17 02:51:34

标签: c++ design-patterns visitor-pattern abstract-factory prototype-pattern

嗯,如您所知,访问者的设计模式有一个类似于抽象工厂问题的“问题”:我访问的类越多,“访问”方法就越具体。

在抽象工厂的情况下,我使用产品原型来“配置”工厂:

factory.h
class ExtensibleFactory
{
public:
    ~ExtensibleFactory();

    void insertProductType(const string &nome, IProductPrototype *product);
    void removeProductType(const string &nome);
    IProductPrototype *createProduct(const string &nome);

private:
    map<string, IProductPrototype *> m_productsHash;
};
factory.cpp
#include "extensiblefactory.h"

#include "iproductprototype.h"

ExtensibleFactory::~ExtensibleFactory()
{
    for(map<string, IProductPrototype *>::iterator iter = this->m_productsHash.begin(); iter != this->m_productsHash.end(); ++iter)
    {
        delete iter->second;
    }

    this->m_productsHash.clear();
}

void ExtensibleFactory::insertProductType(const string &nome, IProductPrototype *product)
{
    this->m_productsHash.insert(make_pair(nome, product));
}

void ExtensibleFactory::removeProductType(const string &nome)
{
    delete this->m_productsHash[nome];
    this->m_productsHash.erase(nome);
}

IProductPrototype *ExtensibleFactory::createProduct(const string &nome)
{
    if ( this->m_productsHash.find(nome) == this->m_productsHash.end() )
    {
      return 0;
    }

    return this->m_productsHash[nome]->clone();
}
的main.cpp
SanduichePrototype *sanduiche = new SanduichePrototype;
CarroPrototype *carro = new CarroPrototype;

ExtensibleFactory *fabrica = new ExtensibleFactory;

fabrica->insertProductType("sanduba", sanduiche);
fabrica->insertProductType("automovel", carro);

IProductPrototype *carro1 = fabrica->createProduct("automovel");
IProductPrototype *carro2 = fabrica->createProduct("automovel");

IProductPrototype *sanduiche1 = fabrica->createProduct("sanduba");
IProductPrototype *sanduiche2 = fabrica->createProduct("sanduba");

现在,请考虑此访问者及其元素:

ivisitor.h
class ElementA;
class ElementB;

class IVisitor
{
public:
    virtual void visit(ElementA *elementA) = 0;
    virtual void visit(ElementB *elementB) = 0;
};
ielement.h
class IVisitor;

class IElement
{
public:
    virtual void accept(IVisitor *visitor) = 0;
};
elementa.h
class ElementA : public IElement
{
public:
    virtual void accept(IVisitor *visitor);
};
elementb.h
class ElementB : public IElement
{
public:
    virtual void accept(IVisitor *visitor);
};

如果我想添加更多元素,我将不得不添加更多方法做IVisitor接口。

我想知道是否可以在运行时“配置”访问者,换句话说,我想知道是否有任何解决方案来模拟通过配置它来添加更多方法到IVisitor接口的行为就像我做了工厂模式,如果是,那将是可能的解决方案。

1 个答案:

答案 0 :(得分:0)

你想要传递给对象的动作(访问者)对象必须至少知道一些可以使用它的功能的公共基类,然后我认为动态没有多大意义注册可访问的类,因为您无论如何都需要动态调度,例如dynamic_cast,并且需要列出公共访问者界面中所有受支持的类,这一点就消失了。

首先考虑对访问者模式代码进行轻微重构 - 除了通用性,命名和访问权限与代码相同:

// Static visitor pattern.

template< class Visitable >
class Visitor_
{
template< class > friend class Visitable_impl_;
private:
    virtual void visit( Visitable& ) {}
};

class A;
class B;

class I_visitor
    : public Visitor_<A>
    , public Visitor_<B>
{};

class I_visitable
{
public:
    virtual void accept( I_visitor& ) = 0;
};

template< class Visitable >
class Visitable_impl_
    : public I_visitable
{
public:
    void accept( I_visitor& v )
        override
    {
        static_cast<Visitor_<Visitable>&>( v )          // Cast for access.
            .visit( static_cast<Visitable&>( *this ) ); // Cast for overload res.
    }
};

class A: public Visitable_impl_<A> {};
class B: public Visitable_impl_<B> {};

#include <iostream>
using namespace std;

auto main()
    -> int
{
    class Action
        : public I_visitor
    {
    private:
        void visit( A& ) override { cout << "Visited an A." << endl; }
    };

    I_visitable&&       a   = A();
    I_visitable&&       b   = B();
    Action              x;

    a.accept( x );  b.accept( x );
}

现在我们只需用static_cast替换第一个dynamic_cast,然后再说:

// Dynamic visitor pattern.

template< class Visitable >
class Visitor_
{
template< class > friend class Visitable_impl_;
private:
    virtual void visit( Visitable& ) {}
};

struct I_visitor { virtual ~I_visitor(){} };    // Note: no mention of A or B.

class I_visitable
{
public:
    virtual void accept( I_visitor& ) = 0;
};

template< class Visitable >
class Visitable_impl_
    : public I_visitable
{
public:
    void accept( I_visitor& v )
        override
    {
        if( auto p_visitor = dynamic_cast<Visitor_<Visitable>*>( &v ) )
        {
            p_visitor->visit( static_cast<Visitable&>( *this ) );   // Cast for overload res.
        }
    }
};

class A: public Visitable_impl_<A> {};
class B: public Visitable_impl_<B> {};

#include <iostream>
using namespace std;

auto main()
    -> int
{
    class Action
        : public I_visitor
        , public Visitor_<A>
    {
    private:
        void visit( A& ) override { cout << "Visited an A." << endl; }
    };

    I_visitable&&       a   = A();
    I_visitable&&       b   = B();
    Action              x;

    a.accept( x );  b.accept( x );
}