The PEAK Developers' Center   Diff for "VisitorRevisited" UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View
Ignore changes in the amount of whitespace

Differences between version dated 2004-11-16 10:59:39 and 2005-01-08 03:13:09 (spanning 3 versions)

Deletions are marked like this.
Additions are marked like this.

The "Classic" Visitor Pattern
-----------------------------
 
The "Classic" Visitor pattern, from the `Gang of Four Book`__, tries to solve this problem by putting the algorithm into a "Visitor" class, and using a ``visit()`` method in the target classes, that selects the correct behavior from the Visitor class. For example, if we were writing a "pretty printer" Visitor, we might make a ``PrettyPrinter`` class with methods like ``visit_list()``, ``visit_string()``, and so on, that have the code for printing that kind of object. Then, we would add a simple ``accept()`` method like this::
The "Classic" Visitor pattern, from the `Gang of Four Book`__, tries to solve this problem by putting the algorithm into a "Visitor" class, and using an ``accept()`` method in the target classes, that selects the correct behavior from the Visitor class. For example, if we were writing a "pretty printer" Visitor, we might make a ``PrettyPrinter`` class with methods like ``visit_list()``, ``visit_string()``, and so on, that have the code for printing that kind of object. Then, we would add a simple ``accept()`` method like this::
 
     def accept(self, visitor):
         return visitor.visit_list(self)

    def print_list(target,stream):
        # pretty-print a list (or subclass thereof)
        stream.write("[")
        for item in list:
        for item in target:
             pretty_print(item,stream)
             stream.write(",")
        stream.write("]")

PythonPowered
ShowText of this page
EditText of this page
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck