| View previous topic :: View next topic |
| Author |
Message |
Peter Morris [Droopy eyes Guest
|
Posted: Mon Apr 24, 2006 4:03 pm Post subject: Another GoF pattern problem |
|
|
If I have two classes SiteVisit 1----* VisitAction, and my visitor interface
looks like this
VisitSiteVisit(SiteVisit)
VisitSiteAction(SiteAction)
My SiteVisit.AcceptVisitor would look something like this
Visitor.VisitSiteVisit(this);
foreach(VisitAction currentAction in VisitActions)
Visitor.VisitSiteAction(this);
But if I wanted to generate XML I would need this
<siteVisit>
<someInfo>blah</someInfo>
<visitActions>
<visitAction>blah</visitAction>
<visitAction>blah</visitAction>
</visitActions>
</siteVisit>
The standard pattern has no way of knowing if this is the first/last visit
action, so I don't know when to open/close the <visitActions> tag!
Should I just add "IsFirstElement/IsLastElement" or is there something I am
missing?
Thanks
Pete |
|
| Back to top |
|
 |
Angus Miller Guest
|
Posted: Mon Apr 24, 2006 6:03 pm Post subject: Re: Another GoF pattern problem |
|
|
Peter Morris [Droopy eyes software] wrote:
| Quote: | If I have two classes SiteVisit 1----* VisitAction, and my visitor
interface looks like this
VisitSiteVisit(SiteVisit)
VisitSiteAction(SiteAction)
My SiteVisit.AcceptVisitor would look something like this
Visitor.VisitSiteVisit(this);
foreach(VisitAction currentAction in VisitActions)
Visitor.VisitSiteAction(this);
But if I wanted to generate XML I would need this
siteVisit
someInfo>blah</someInfo
visitActions
visitAction>blah</visitAction
visitAction>blah</visitAction
/visitActions
/siteVisit
The standard pattern has no way of knowing if this is the first/last
visit action, so I don't know when to open/close the <visitActions
tag!
Should I just add "IsFirstElement/IsLastElement" or is there
something I am missing?
|
Another problem you are going to have with this is that you will also
need to close the <siteVisit> tag after the VisitSiteActions have been
called..
One suggestion would be just to call: Visitor.VisitSiteVisit(this)
which would include the following:
Open siteVisit tag
SomeInfo XMLData Get
Open visitActions tag
foreach(VisitAction currentAction in VisitActions)
VisitSiteAction(currentAction);
Close visitActions tag
Close siteVisit tag
Regards
Angus Miller |
|
| Back to top |
|
 |
Bryan Crotaz Guest
|
Posted: Tue Apr 25, 2006 12:03 am Post subject: Re: Another GoF pattern problem |
|
|
You could extend the pattern...
Visitor.VisitSiteVisit(this);
Visitor.BeforeMultiVisitSiteAction(this)
try
foreach(VisitAction currentAction in VisitActions)
Visitor.VisitSiteAction(currentAction);
finally
Visitor.AfterMultiVisitSiteAction(this);
end;
Bryan |
|
| Back to top |
|
 |
Peter Morris [Droopy eyes Guest
|
Posted: Tue Apr 25, 2006 8:03 am Post subject: Re: Another GoF pattern problem |
|
|
Hi Bryan
So far I have StartVisit....... and EndVisit........., I don't think I need
three methods per type (what a lot of work!).
If I have to export xml like so
<parent>
<list1>
<itemtype1>
<itemtype1>
</list1>
<list2>
<itemtype2>
<itemtype2>
</list2>
<list3>
<itemtype3>
<itemtype3>
</list3>
</parent>
then it seems I need a separate XML stream for list1, list2, list3 and then
when I get EndVisitParent I grab the XML and insert it into my own XML.
I can see some uses for the visitor pattern, but I'm not convinced that it
is best suited for generating formatted documents!
--
Pete
====
Audio compression components, DIB graphics controls, ECO extensions,
FastStrings
http://www.droopyeyes.com
My blog
http://blogs.slcdug.org/petermorris/ |
|
| Back to top |
|
 |
Jim Cooper Guest
|
|
| Back to top |
|
 |
Bryan Crotaz Guest
|
Posted: Tue Apr 25, 2006 10:03 am Post subject: Re: Another GoF pattern problem |
|
|
"Peter Morris [Droopy eyes software]" <pete (AT) NO_droopyeyes_SPAM (DOT) com> wrote in
message news:444ce5e5$1 (AT) newsgroups (DOT) borland.com...
| Quote: | If I have two classes SiteVisit 1----* VisitAction, and my visitor
interface
looks like this
VisitSiteVisit(SiteVisit)
VisitSiteAction(SiteAction)
|
The way I'd do this is with a factory:
Register handler objects for each type
eg XMLOutputHandler, SparseXMLOutputHandler etc
Then iterate the tree and create the appropriate handler for each node. You
can even iterate recursively and have a particular handler create its
children so that an XMLOutputHandler might choose for some reason to output
one branch in Sparse mode. With this method, the factory passes the object
a pointer to itself on construction and then the object can create new
handlers internally. Externally the code is:
RootHandler := Factory.CreateObject(XMLOutputHandler, RootSite.ClassType);
RootHandler.Target := RootSite;
Result := (RootHandler as IimXMLOut).AsXML;
Then for efficiency you can make your factory keep references to the
objects, turning them into singletons if desired.
Bryan |
|
| Back to top |
|
 |
|