Re: When and where to use Visitor Pattern?



Sam Hwang wrote:

> I am confused about Visiotr Pattern.

> Why make it so complicated? Can't we use this one instead?

> class IntExp1 extends Expression
> {
> int value;
>
> void visit()
> {
> System.out.println(value);
> }
> }
>

Or better,

abstract Expression {
abstract void writeTo(PrintStream ps);
}

class IntExp1 extends Expression
{
int value;

void writeTo(PrintStream ps)
{
ps.println(value);
}
}

Leave it to the caller to provide the concrete stream - System.out, a
string stream, file stream, or whatever.

Or alternatively,

abstract Expression {
abstract Result evaluate();
}

and use a separate formatter class(es) to format the result for display
(similiar in spirit to the Java DateFormat classes)

Regards,
Daniel Parker

.