Article View: borland.public.jbuilder.java.language
Article #560Visitor pattern and Command pattern
From: Scott Powell
Date: Fri, 04 Mar 2005 22:09
Date: Fri, 04 Mar 2005 22:09
48 lines
2737 bytes
2737 bytes
Hi,
Just trying out Java and had a few questions about patterns.
1. When designing with the Visitor pattern, is it better to start with an
abstract class at the top of the hierarchy or to declare an interface and
implement where needed.
2. Would visitors typically return a value from their visitObj(Object obj)
method?
3. The app I'm writing has to crawl hierarchal object trees (Borland's
Caliber TreeNodes). So looping over the nodes and calling Visitor seems
appropriate.
But we don't always want to visit every node; we may want to skip some. It
seemed a Strategy pattern that had a method like isObjInSet(Object obj)
might be appropriate.
Combining these things, I thought a Crawler class that took a set of objects
(or the root of an object tree), a strategy object and a visitor object
could be useful: I wouldn't have to worry about the looping code ever
again...just instantiate my crawler with the three elements and it would go
about its business.
Make sense?
4. Strictly speaking if a class implements visitorObjectParent(ObjectParent
objP), should it touch objP's children? In other words, is the following
appropriate?
public void visitObjectParent(ObjectParent objP){
ObjectChildren[] objC = objP.getChildren();
for (int x = 0; x <objC.length; x++){
doSomethingToChild(objC);
}
}
5. If #4 is appropriate but I want to vary what happens in
doSomethingToChild(), is it ok to pass in a Visitor or Command object (e.g.,
ObjectParentVisitor v = new ObjectParentVisitor(ChildVisitor) to specify the
action to take against the child? And if action should not be taken against
EVERY child, could another Strategy pattern be passed in?
Thanks.
(And if these are crazy questions, I hope they're at least amusing.)
Technical Details
Message-ID:
<4228dd80$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed.freenet.de!news-lei1.dfn.de!news-fra1.dfn.de!newscore.univie.ac.at!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: f74b3,a9baad4964a30b83,start X-Google-Attributes: gidf74b3,public X-Google-Language: ENGLISH,ASCII-7-bit Reply-To: "Scott Powell" <spowell@rpsweb.com> From: "Scott Powell" <spowell@rpsweb.com> Newsgroups: borland.public.jbuilder.java.language Subject: Visitor pattern and Command pattern Date: Fri, 4 Mar 2005 14:09:04 -0800 Lines: 50 Organization: RPS X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.224 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.224 NNTP-Posting-Host: 64.80.153.236 Message-ID: <4228dd80$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1109974400 64.80.153.236 (4 Mar 2005 14:13:20 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed.freenet.de!news-lei1.dfn.de!news-fra1.dfn.de!newscore.univie.ac.at!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail