Skip to content

Commit

Permalink
adding new version of dsl pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
shvets committed Aug 16, 2010
1 parent 1783b99 commit 19cccf0
Show file tree
Hide file tree
Showing 8 changed files with 660 additions and 142 deletions.
9 changes: 9 additions & 0 deletions .idea/design_patterns_in_ruby.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

337 changes: 337 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

41 changes: 19 additions & 22 deletions behavioral/visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# 1. type and visitor interfaces

class Visitable
module Visitable
def accept(&visitor_code)
visitor_code.call(self)
end
Expand All @@ -27,15 +27,24 @@ def accept(&visitor_code)

# basic parts

class MyVisitable1 < Visitable; end
class MyVisitable1
include Visitable
end

class MyVisitable2 < Visitable; end
class MyVisitable2
include Visitable
end

class MyVisitable3
include Visitable
end

class MyVisitable3 < Visitable; end

# compound

class MyCompoundVisitable < Visitable
class MyCompoundVisitable
include Visitable

def initialize
@visitable1 = MyVisitable1.new
@visitable2 = MyVisitable2.new
Expand Down Expand Up @@ -63,29 +72,17 @@ def accept(&visitor_code)

visitable = MyCompoundVisitable.new

# creating visitors dynamically

visitable.accept do |visitable|
if(visitable.kind_of? MyVisitable1)
puts "visitor1: visiting my visitable 1"
elsif(visitable.kind_of? MyVisitable2)
puts "visitor1: visiting my visible 2"
elsif(visitable.kind_of? MyVisitable3)
puts "visitor1: visiting my visitable 3"
elsif(visitable.kind_of? MyCompoundVisitable)
puts "visitor1: visiting my compound visitable"
end
end
# creating visitor dynamically

visitable.accept do |visitable|
if(visitable.kind_of? MyVisitable1)
puts "visitor2: visiting my visitable 1"
puts "visitor: visiting my visitable 1"
elsif(visitable.kind_of? MyVisitable2)
puts "visitor2: visiting my visible 2"
puts "visitor: visiting my visible 2"
elsif(visitable.kind_of? MyVisitable3)
puts "visitor2: visiting my visitable 3"
puts "visitor: visiting my visitable 3"
elsif(visitable.kind_of? MyCompoundVisitable)
puts "visitor2: visiting my compound visitable"
puts "visitor: visiting my compound visitable"
end
end

Loading

0 comments on commit 19cccf0

Please sign in to comment.