Created
January 18, 2012 21:51
-
-
Save anewusername1/1635999 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Each of the following classes visit the objects, doing something different | |
# each time. | |
class CarElementPrintVisitor | |
def visit(subject) | |
puts "Visiting: %s" % subject.class.to_s | |
end | |
end | |
class CarElementDoVisitor | |
def visit(subject) | |
puts "Do some other visitation: %s" % subject.class.to_s | |
end | |
end | |
c = Car.new | |
c.accept(CarElementPrintVisitor.new) # pass an object that will be visited | |
c.accept(CarElementDoVisitor.new) # same here | |
# OUTPUT | |
# Visiting: Wheel | |
# Visiting: Wheel | |
# Visiting: Wheel | |
# Visiting: Wheel | |
# Visiting: Body | |
# Visiting: Engine | |
# Visiting: Car | |
# Do some other visitation: Wheel | |
# Do some other visitation: Wheel | |
# Do some other visitation: Wheel | |
# Do some other visitation: Wheel | |
# Do some other visitation: Body | |
# Do some other visitation: Engine | |
# Do some other visitation: Car |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment