Re: When and where to use Visitor Pattern?
- From: "Daniel Parker" <danielaparker@xxxxxxxxxxx>
- Date: 29 Apr 2005 09:22:07 -0700
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
.
- References:
- When and where to use Visitor Pattern?
- From: Sam Hwang
- When and where to use Visitor Pattern?
- Prev by Date: Re: Confusion about splitting classes to allow sharing of resources
- Next by Date: Re: E-book?
- Previous by thread: Re: When and where to use Visitor Pattern?
- Next by thread: Re: When and where to use Visitor Pattern?
- Index(es):