When implementing the Visitor Pattern, is it a bad idea to add additional parameters to the Accept and Visit methods? I have never seen any examples that do this but no mention of it being a bad idea either.
I would like to use this pattern in my domain model however additional parameters are required along with the entity itself.
eg -
public interface ISomethingVisitor
{
void Visit(Foo foo, int p1, int p2);
}
public interface ISomethingVisitable
{
void Accept(ISomethingVisitor visitor, int p1, int p2);
}
I'd say it's a bad idea. It might work fine for this visitor, but what happens when another visitor requires more/different parameters? If p1
, p2
don't change, you can give them to the visitor at construction:
public class MyVisitor : ISomethingVisitor
{
private int p1;
private int p2;
public MyVisitor(int p1, int p2)
{
_p1 = p1;
_p2 = p2;
}
public void Visit(Foo foo)
{
//got access to _p1, _p2 here
}
}