page123.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 106 行

HTML
106
字号
<HTML><HEAD><TITLE>Visitors</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html2627" HREF="page124.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2625" HREF="page113.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2619" HREF="page122.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2629" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION005250000000000000000">Visitors</A></H2><A NAME="secadtsvisitors">&#160;</A><P>In this section we introduce an abstraction calleda <em>visitor</em><A NAME=4633>&#160;</A>.An visitor provides the means to access one-by-one all theobjects in a container and to perform a given operation on those objects.Program&nbsp;<A HREF="page123.html#progvisitora"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the abstract class <tt>Visitor</tt>which is the base class from which all visitors are derived.<P><P><A NAME="4967">&#160;</A><A NAME="progvisitora">&#160;</A> <IMG WIDTH=575 HEIGHT=260 ALIGN=BOTTOM ALT="program4636" SRC="img650.gif"  ><BR><STRONG>Program:</STRONG> <tt>Visitor</tt> interface.<BR><P><P>The <tt>Container</tt> class defines an abstract method called <tt>accept</tt>as shown in Program&nbsp;<A HREF="page123.html#progcontainerc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The idea is that the <tt>accept</tt> method of the <tt>Container</tt>class takes as its argument a any objectthat is an instance of a class derived from the <tt>Visitor</tt> class.<P><P><A NAME="4971">&#160;</A><A NAME="progcontainerc">&#160;</A> <IMG WIDTH=575 HEIGHT=161 ALIGN=BOTTOM ALT="program4653" SRC="img651.gif"  ><BR><STRONG>Program:</STRONG> <tt>Container</tt> class <tt>accept</tt> method.<BR><P><P>But what is a visitor?A shown in Program&nbsp;<A HREF="page123.html#progvisitora"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,a visitor is an object that has a <tt>visit</tt> methodand an <tt>isDone</tt> property.Of these, the <tt>visit</tt> method is the most interesting.The <tt>visit</tt> method takes as its argument an <tt>Object</tt> instance.<P>The interaction between a container and a visitor goes like this:The container is passed a visitor by callingthe container's <tt>accept</tt> method.That is, the container ``accepts'' the visitor.What does a container do with a visitor?It calls the <tt>visit</tt> method of that visitorone-by-one for each object contained in the container.<P>The interaction between a <tt>Container</tt> and its <tt>Visitor</tt>are best understood by considering an example.The following code fragment gives animplementation of the <tt>Accept</tt> methodin some concrete class, say <tt>SomeContainer</tt>,that implements the <tt>Container</tt> interface:<P><PRE>class SomeContainer(Container):    def accept(self, visitor):	for i in self:            visitor.visit(i)    # ...</PRE><P>The <tt>accept</tt> method calls <tt>visit</tt>for each object <tt>i</tt> in the container.Note, the <tt>visit</tt> method in the <tt>Visitor</tt> class is abstract.What a visitor actually does with an objectdepends on the actual class of visitor used.<P>Suppose that we want to print all of the objects in the container.One way to do this is to create a <tt>PrintingVisitor</tt>which prints every object it visits,and then to pass the visitor to the containerby calling the <tt>accept</tt> method.The following code shows how we can declare the <tt>PrintingVisitor</tt> classwhich prints an object on the console.<PRE>class PrintingVisitor(Visitor):    def visit(self, obj):	print obj    # ...</PRE><P>Finally, given an object <tt>c</tt> that is an instance ofa concrete class <tt>SomeContainer</tt>derived from the abstract <tt>Container</tt> class,we can call the <tt>accept</tt> method as follows:<PRE>c = SomeContainer()// ...c.accept(PrintingVisitor())</PRE>The effect of this call is to call the <tt>visit</tt> methodof the visitor for each object in the container.<P><BR> <HR><UL> <LI> <A NAME="tex2html2630" HREF="page124.html#SECTION005251000000000000000">The <tt>isDone</tt> Property</A><LI> <A NAME="tex2html2631" HREF="page125.html#SECTION005252000000000000000">The <tt>Container</tt> Class <tt>__str__</tt> Method</A></UL><HR><A NAME="tex2html2627" HREF="page124.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2625" HREF="page113.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2619" HREF="page122.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2629" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright &#169; 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?