Well, as you know, the design pattern Visitor has a "problem" similar to Abstract Factory problem: the more visitable classes I made, the more specific "visit" methods must create.
In the case of an abstract factory I made a solution using prototype of a product to "configure" the factory:
factory.hclass 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");
Now, consider this visitor and its elements:
ivisitor.hclass 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);
};
If I want to add more elements I will have to add more methods do IVisitor interface.
I wish to know if it's possible to "configure" a visitor in runtime, in other words, I want to know if there are any solution to emulate the act of adding more methods to the IVisitor interface by configuring it just like I did to Factory pattern and, if so, which will be the possible solutions.
The action (visitor) object you want to pass around to objects will have to have hardwired at least knowledge of some common base class whose functionality it can use, and then as I see it there's not much point in dynamically registering visitable classes, because you need a dynamic dispatch anyway, e.g. dynamic_cast
, and with that the need to list all supported classes in the common visitor interface, disappears.
Consider first a slight refactoring of your visitor pattern code – except for generality and naming and access it's the same as your code:
// 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 );
}
Now we just replace the first static_cast
with a dynamic_cast
, and voilà:
// 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 );
}