Re: When and where to use Visitor Pattern?
- From: Calum Grant <calumg@xxxxxxxxxx>
- Date: Fri, 22 Apr 2005 22:36:10 GMT
Sam Hwang wrote:
<snip>Hello, I am confused about Visiotr Pattern. Observe this example from
Why make it so complicated? Can't we use this one instead? class IntExp1 extends Expression { int value;
void visit() { System.out.println(value); } }
class AddExp1 extends Expression { Expression e1, e2;
void visit() { e1.visit(); System.out.print(" + "); e2.visit(); } } I dont know in what cirumstances the pattern is used sensablely. I hope someone can address my confusion. Thanks!
Okay, suppose you instead wanted to find - the largest integer in your expression. - The smallest integer - count the number of additions - count the number of integers - find the leftmost integer - find the rightmost integer - write to a file, not System.out
You can either write a "visit" method for each of those tasks in the above list, thereby inserting lots of junk into your IntExp and AddExp classes, or you can use a visitor. Visitor moves methods into classes, which can structure the problem better.
-- Fast object persistence in C++ http://lightwave2.com/persist A fast malloc/new replacement http://lightwave2.com Home http://visula.org/calum .
- References:
- When and where to use Visitor Pattern?
- From: Sam Hwang
- When and where to use Visitor Pattern?
- Prev by Date: Re: audit log on normalized data
- Next by Date: Re: When and where to use Visitor Pattern?
- Previous by thread: Re: When and where to use Visitor Pattern?
- Next by thread: Re: When and where to use Visitor Pattern?
- Index(es):
Relevant Pages
|