BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Another GoF pattern problem

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
Peter Morris [Droopy eyes
Guest





PostPosted: Mon Apr 24, 2006 4:03 pm    Post subject: Another GoF pattern problem Reply with 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?


Thanks

Pete
Back to top
Angus Miller
Guest





PostPosted: Mon Apr 24, 2006 6:03 pm    Post subject: Re: Another GoF pattern problem Reply with quote



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





PostPosted: Tue Apr 25, 2006 12:03 am    Post subject: Re: Another GoF pattern problem Reply with quote



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





PostPosted: Tue Apr 25, 2006 8:03 am    Post subject: Re: Another GoF pattern problem Reply with quote

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





PostPosted: Tue Apr 25, 2006 9:03 am    Post subject: Re: Another GoF pattern problem Reply with quote

I used it to pretty print XML documents in my patterns paper.

I generated the XML differently though.

http://www.tabdee.ltd.uk/Software/Papers/MoreDesignPatterns/MoreDesignPatterns.html


Cheers,
Jim Cooper

_____________________________________________

Jim Cooper jcooper (AT) tabdee (DOT) ltd.uk
Skype : jim.cooper
Tabdee Ltd http://www.tabdee.ltd.uk

TurboSync - Connecting Delphi to your Palm
_____________________________________________
Back to top
Bryan Crotaz
Guest





PostPosted: Tue Apr 25, 2006 10:03 am    Post subject: Re: Another GoF pattern problem Reply with quote

"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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.