page124.html

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

HTML
87
字号
<HTML><HEAD><TITLE>The isDone Property</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="tex2html2640" HREF="page125.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2638" HREF="page123.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2632" HREF="page123.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2642" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION005251000000000000000">The <tt>isDone</tt> Property</A></H3><P>As shown in Program&nbsp;<A HREF="page123.html#progvisitora"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,the <tt>Visitor</tt> class also includes the property <tt>isDone</tt>.The <tt>isDone</tt> propertycalls the <tt>getIsDone</tt> accessorto determine whether a visitor has finished its work.That is, the <tt>getIsDone</tt> method returns the <tt>bool</tt> value<tt>True</tt> if the visitor ``is done.''<P>The idea is this:Sometimes a visitor does not need to visit all the objects in a container.That is, in some cases, the visitor may be finished its taskafter having visited only a some of the objects.The <tt>isDone</tt> property can be used by thecontainer to terminate the <tt>accept</tt> method early like this:<P><PRE>class SomeContainer(Container):    def accept(self, visitor):	for i in self:            if visitor.isDone:                return            visitor.visit(i)    # ...</PRE><P>To illustrate the usefulness of <tt>isDone</tt>,consider a visitor which visits the objects in a containerwith the purpose of finding the first object that matchesa given target object.Having found the first matching object in the container,the visitor is done and does not need to visit any more contained objects.<P>The following code fragment defines a visitorwhich finds the first object in the containerthat matches a given object.<PRE>class MatchingVisitor(Visitor):    def __init__(self, target):	self.target = target	self.found = None    def visit(self, obj):	if not self.isDone and obj == target:            self.found = obj    def isDone(self):        return found not is None:</PRE><P>The <tt>__init__</tt> method of the <tt>MatchingVisitor</tt> visitor takesan <tt>Object</tt> instance that is the target of the search.That is, we wish to find an object in a container that matches the target.For each object the <tt>MatchingVisitor</tt> visitor visits,it compares that object with the targetand makes <tt>found</tt> point at that object if it matches.Clearly, the <tt>MatchingVisitor</tt> visitor is donewhen the <tt>found</tt> pointer is non-zero.<P>Suppose we have a container <tt>c</tt>that is an instance of a concrete container class, <tt>SomeContainer</tt>,derived from the abstract <tt>Container</tt> class;and an object <tt>x</tt>that is an instance of a concrete object class, <tt>SomeObject</tt>,derived from the abstract <tt>Object</tt> class.Then, we can call use the <tt>MatchingVisitor</tt> visitor as follows:<PRE>c = SomeContainer()x = SomeObject()// ...c.accept(MatchingVisitor(x))</PRE><P><HR><A NAME="tex2html2640" HREF="page125.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2638" HREF="page123.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2632" HREF="page123.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2642" 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 + -
显示快捷键?